smartcontract: adjust parameter value to type properly

This commit is contained in:
Evgenii Stratonikov 2020-03-04 19:32:35 +03:00
parent 8141d49e5a
commit 7cde58f731
2 changed files with 42 additions and 28 deletions

View file

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

View file

@ -1,8 +1,10 @@
package smartcontract package smartcontract
import ( import (
"encoding/hex"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -151,7 +153,7 @@ func TestAdjustValToType(t *testing.T) {
}{{ }{{
typ: SignatureType, typ: SignatureType,
val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b", val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
out: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b", out: mustHex("602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b"),
}, { }, {
typ: SignatureType, typ: SignatureType,
val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c", val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c",
@ -199,11 +201,17 @@ func TestAdjustValToType(t *testing.T) {
}, { }, {
typ: Hash160Type, typ: Hash160Type,
val: "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y", 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, typ: Hash160Type,
val: "50befd26fdf6e4d957c11e078b24ebce6291456f", 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, typ: Hash160Type,
val: "befd26fdf6e4d957c11e078b24ebce6291456f", val: "befd26fdf6e4d957c11e078b24ebce6291456f",
@ -215,7 +223,10 @@ func TestAdjustValToType(t *testing.T) {
}, { }, {
typ: Hash256Type, typ: Hash256Type,
val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7", 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, typ: Hash256Type,
val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282d", val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282d",
@ -227,15 +238,15 @@ func TestAdjustValToType(t *testing.T) {
}, { }, {
typ: ByteArrayType, typ: ByteArrayType,
val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282d", val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282d",
out: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282d", out: mustHex("602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282d"),
}, { }, {
typ: ByteArrayType, typ: ByteArrayType,
val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7", val: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7",
out: "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7", out: mustHex("602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"),
}, { }, {
typ: ByteArrayType, typ: ByteArrayType,
val: "50befd26fdf6e4d957c11e078b24ebce6291456f", val: "50befd26fdf6e4d957c11e078b24ebce6291456f",
out: "50befd26fdf6e4d957c11e078b24ebce6291456f", out: mustHex("50befd26fdf6e4d957c11e078b24ebce6291456f"),
}, { }, {
typ: ByteArrayType, typ: ByteArrayType,
val: "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y", val: "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y",
@ -247,11 +258,11 @@ func TestAdjustValToType(t *testing.T) {
}, { }, {
typ: ByteArrayType, typ: ByteArrayType,
val: "ab", val: "ab",
out: "ab", out: mustHex("ab"),
}, { }, {
typ: PublicKeyType, typ: PublicKeyType,
val: "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c", val: "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c",
out: "03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c", out: mustHex("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c"),
}, { }, {
typ: PublicKeyType, typ: PublicKeyType,
val: "01b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c", 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
}