Merge pull request #719 from nspcc-dev/fix/rpc

smartcontract: fix bugs in paramter marshal/unmarshal
This commit is contained in:
Roman Khimov 2020-03-04 19:58:56 +03:00 committed by GitHub
commit 1dd7c8d337
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 29 deletions

View file

@ -66,7 +66,11 @@ func (p *Parameter) MarshalJSON() ([]byte, error) {
case BoolType, IntegerType, StringType, Hash256Type, Hash160Type:
resultRawValue, resultErr = json.Marshal(p.Value)
case PublicKeyType, ByteArrayType, SignatureType:
resultRawValue, resultErr = json.Marshal(hex.EncodeToString(p.Value.([]byte)))
if p.Value == nil {
resultRawValue = []byte("null")
} else {
resultRawValue, resultErr = json.Marshal(hex.EncodeToString(p.Value.([]byte)))
}
case ArrayType:
var value = make([]rawParameter, 0)
for _, parameter := range p.Value.([]Parameter) {

View file

@ -30,6 +30,10 @@ var marshalJSONTestCases = []struct {
input: Parameter{Type: ByteArrayType, Value: []byte{0x01, 0x02, 0x03}},
result: `{"type":"ByteArray","value":"010203"}`,
},
{
input: Parameter{Type: ByteArrayType},
result: `{"type":"ByteArray","value":null}`,
},
{
input: Parameter{
Type: PublicKeyType,

View file

@ -9,6 +9,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/pkg/errors"
)
@ -169,7 +170,7 @@ func adjustValToType(typ ParamType, val string) (interface{}, error) {
if len(b) != 64 {
return nil, errors.New("not a signature")
}
return val, nil
return b, nil
case BoolType:
switch val {
case "true":
@ -184,37 +185,31 @@ func adjustValToType(typ ParamType, val string) (interface{}, error) {
case Hash160Type:
u, err := address.StringToUint160(val)
if err == nil {
return hex.EncodeToString(u.BytesBE()), nil
return u, nil
}
b, err := hex.DecodeString(val)
u, err = util.Uint160DecodeStringLE(val)
if err != nil {
return nil, err
}
if len(b) != 20 {
return nil, errors.New("not a hash160")
}
return val, nil
return u, nil
case Hash256Type:
u, err := util.Uint256DecodeStringLE(val)
if err != nil {
return nil, err
}
return u, nil
case ByteArrayType:
b, err := hex.DecodeString(val)
if err != nil {
return nil, err
}
if len(b) != 32 {
return nil, errors.New("not a hash256")
}
return val, nil
case ByteArrayType:
_, err := hex.DecodeString(val)
if err != nil {
return nil, err
}
return val, nil
return b, nil
case PublicKeyType:
_, err := keys.NewPublicKeyFromString(val)
pub, err := keys.NewPublicKeyFromString(val)
if err != nil {
return nil, err
}
return val, nil
return pub.Bytes(), nil
case StringType:
return val, nil
default:

View file

@ -1,8 +1,10 @@
package smartcontract
import (
"encoding/hex"
"testing"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
)
@ -151,7 +153,7 @@ func TestAdjustValToType(t *testing.T) {
}{{
typ: SignatureType,
val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
out: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
out: mustHex("602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b"),
}, {
typ: SignatureType,
val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c",
@ -199,11 +201,17 @@ func TestAdjustValToType(t *testing.T) {
}, {
typ: Hash160Type,
val: "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y",
out: "23ba2703c53263e8d6e522dc32203339dcd8eee9",
out: util.Uint160{
0x23, 0xba, 0x27, 0x3, 0xc5, 0x32, 0x63, 0xe8, 0xd6, 0xe5,
0x22, 0xdc, 0x32, 0x20, 0x33, 0x39, 0xdc, 0xd8, 0xee, 0xe9,
},
}, {
typ: Hash160Type,
val: "50befd26fdf6e4d957c11e078b24ebce6291456f",
out: "50befd26fdf6e4d957c11e078b24ebce6291456f",
out: util.Uint160{
0x6f, 0x45, 0x91, 0x62, 0xce, 0xeb, 0x24, 0x8b, 0x7, 0x1e,
0xc1, 0x57, 0xd9, 0xe4, 0xf6, 0xfd, 0x26, 0xfd, 0xbe, 0x50,
},
}, {
typ: Hash160Type,
val: "befd26fdf6e4d957c11e078b24ebce6291456f",
@ -215,7 +223,10 @@ func TestAdjustValToType(t *testing.T) {
}, {
typ: Hash256Type,
val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7",
out: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7",
out: util.Uint256{
0xe7, 0x2d, 0x28, 0x69, 0x79, 0xee, 0x6c, 0xb1, 0xb7, 0xe6, 0x5d, 0xfd, 0xdf, 0xb2, 0xe3, 0x84,
0x10, 0xb, 0x8d, 0x14, 0x8e, 0x77, 0x58, 0xde, 0x42, 0xe4, 0x16, 0x8b, 0x71, 0x79, 0x2c, 0x60,
},
}, {
typ: Hash256Type,
val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282d",
@ -227,15 +238,15 @@ func TestAdjustValToType(t *testing.T) {
}, {
typ: ByteArrayType,
val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282d",
out: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282d",
out: mustHex("602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282d"),
}, {
typ: ByteArrayType,
val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7",
out: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7",
out: mustHex("602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"),
}, {
typ: ByteArrayType,
val: "50befd26fdf6e4d957c11e078b24ebce6291456f",
out: "50befd26fdf6e4d957c11e078b24ebce6291456f",
out: mustHex("50befd26fdf6e4d957c11e078b24ebce6291456f"),
}, {
typ: ByteArrayType,
val: "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y",
@ -247,11 +258,11 @@ func TestAdjustValToType(t *testing.T) {
}, {
typ: ByteArrayType,
val: "ab",
out: "ab",
out: mustHex("ab"),
}, {
typ: PublicKeyType,
val: "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c",
out: "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c",
out: mustHex("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"),
}, {
typ: PublicKeyType,
val: "01b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c",
@ -296,3 +307,11 @@ func TestAdjustValToType(t *testing.T) {
}
}
}
func mustHex(s string) []byte {
b, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return b
}