rpc: use base64 for getstorage in/out

See neo-project/neo#484.
This commit is contained in:
Roman Khimov 2021-02-07 18:27:19 +03:00
parent 66471de9d2
commit 272bb03e3b
4 changed files with 15 additions and 21 deletions

View file

@ -1,7 +1,7 @@
package client package client
import ( import (
"encoding/hex" "encoding/base64"
"errors" "errors"
"fmt" "fmt"
@ -340,24 +340,20 @@ func (c *Client) GetRawTransactionVerbose(hash util.Uint256) (*result.Transactio
// GetStorageByID returns the stored value, according to the contract ID and the stored key. // GetStorageByID returns the stored value, according to the contract ID and the stored key.
func (c *Client) GetStorageByID(id int32, key []byte) ([]byte, error) { func (c *Client) GetStorageByID(id int32, key []byte) ([]byte, error) {
return c.getStorage(request.NewRawParams(id, hex.EncodeToString(key))) return c.getStorage(request.NewRawParams(id, base64.StdEncoding.EncodeToString(key)))
} }
// GetStorageByHash returns the stored value, according to the contract script hash and the stored key. // GetStorageByHash returns the stored value, according to the contract script hash and the stored key.
func (c *Client) GetStorageByHash(hash util.Uint160, key []byte) ([]byte, error) { func (c *Client) GetStorageByHash(hash util.Uint160, key []byte) ([]byte, error) {
return c.getStorage(request.NewRawParams(hash.StringLE(), hex.EncodeToString(key))) return c.getStorage(request.NewRawParams(hash.StringLE(), base64.StdEncoding.EncodeToString(key)))
} }
func (c *Client) getStorage(params request.RawParams) ([]byte, error) { func (c *Client) getStorage(params request.RawParams) ([]byte, error) {
var resp string var resp []byte
if err := c.performRequest("getstorage", params, &resp); err != nil { if err := c.performRequest("getstorage", params, &resp); err != nil {
return nil, err return nil, err
} }
res, err := hex.DecodeString(resp) return resp, nil
if err != nil {
return nil, err
}
return res, nil
} }
// GetTransactionHeight returns the block index in which the transaction is found. // GetTransactionHeight returns the block index in which the transaction is found.

View file

@ -595,7 +595,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
} }
return c.GetStorageByHash(hash, key) return c.GetStorageByHash(hash, key)
}, },
serverResponse: `{"jsonrpc":"2.0","id":1,"result":"4c696e"}`, serverResponse: `{"jsonrpc":"2.0","id":1,"result":"TGlu"}`,
result: func(c *Client) interface{} { result: func(c *Client) interface{} {
value, err := hex.DecodeString("4c696e") value, err := hex.DecodeString("4c696e")
if err != nil { if err != nil {
@ -613,7 +613,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
} }
return c.GetStorageByID(-1, key) return c.GetStorageByID(-1, key)
}, },
serverResponse: `{"jsonrpc":"2.0","id":1,"result":"4c696e"}`, serverResponse: `{"jsonrpc":"2.0","id":1,"result":"TGlu"}`,
result: func(c *Client) interface{} { result: func(c *Client) interface{} {
value, err := hex.DecodeString("4c696e") value, err := hex.DecodeString("4c696e")
if err != nil { if err != nil {

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"crypto/elliptic" "crypto/elliptic"
"encoding/binary" "encoding/binary"
"encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -29,12 +28,12 @@ import (
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/network" "github.com/nspcc-dev/neo-go/pkg/network"
"github.com/nspcc-dev/neo-go/pkg/services/oracle"
"github.com/nspcc-dev/neo-go/pkg/services/oracle/broadcaster"
"github.com/nspcc-dev/neo-go/pkg/rpc" "github.com/nspcc-dev/neo-go/pkg/rpc"
"github.com/nspcc-dev/neo-go/pkg/rpc/request" "github.com/nspcc-dev/neo-go/pkg/rpc/request"
"github.com/nspcc-dev/neo-go/pkg/rpc/response" "github.com/nspcc-dev/neo-go/pkg/rpc/response"
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result" "github.com/nspcc-dev/neo-go/pkg/rpc/response/result"
"github.com/nspcc-dev/neo-go/pkg/services/oracle"
"github.com/nspcc-dev/neo-go/pkg/services/oracle/broadcaster"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
@ -910,7 +909,7 @@ func (s *Server) getStorage(ps request.Params) (interface{}, *response.Error) {
return nil, rErr return nil, rErr
} }
key, err := ps.Value(1).GetBytesHex() key, err := ps.Value(1).GetBytesBase64()
if err != nil { if err != nil {
return nil, response.ErrInvalidParams return nil, response.ErrInvalidParams
} }
@ -920,7 +919,7 @@ func (s *Server) getStorage(ps request.Params) (interface{}, *response.Error) {
return "", nil return "", nil
} }
return hex.EncodeToString(item.Value), nil return item.Value, nil
} }
func (s *Server) getrawtransaction(reqParams request.Params) (interface{}, *response.Error) { func (s *Server) getrawtransaction(reqParams request.Params) (interface{}, *response.Error) {

View file

@ -3,7 +3,6 @@ package server
import ( import (
"bytes" "bytes"
"encoding/base64" "encoding/base64"
"encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -346,15 +345,15 @@ var rpcTestCases = map[string][]rpcTestCase{
"getstorage": { "getstorage": {
{ {
name: "positive", name: "positive",
params: fmt.Sprintf(`["%s", "746573746b6579"]`, testContractHash), params: fmt.Sprintf(`["%s", "dGVzdGtleQ=="]`, testContractHash),
result: func(e *executor) interface{} { result: func(e *executor) interface{} {
v := hex.EncodeToString([]byte("testvalue")) v := base64.StdEncoding.EncodeToString([]byte("testvalue"))
return &v return &v
}, },
}, },
{ {
name: "missing key", name: "missing key",
params: fmt.Sprintf(`["%s", "7465"]`, testContractHash), params: fmt.Sprintf(`["%s", "dGU="]`, testContractHash),
result: func(e *executor) interface{} { result: func(e *executor) interface{} {
v := "" v := ""
return &v return &v
@ -377,7 +376,7 @@ var rpcTestCases = map[string][]rpcTestCase{
}, },
{ {
name: "invalid key", name: "invalid key",
params: fmt.Sprintf(`["%s", "notahex"]`, testContractHash), params: fmt.Sprintf(`["%s", "notabase64$"]`, testContractHash),
fail: true, fail: true,
}, },
}, },