[#478] morph/client: Try to parse integer as a byte type

Neo node can return integers values where []byte is expected.
To cover such cases, try to parse integers in `BytesFromStackItem`.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-04-15 16:23:12 +03:00 committed by Alex Vanin
parent 13149e794f
commit 434ecb41da
2 changed files with 11 additions and 0 deletions

View file

@ -48,6 +48,13 @@ func BytesFromStackItem(param stackitem.Item) ([]byte, error) {
switch param.Type() { switch param.Type() {
case stackitem.BufferT, stackitem.ByteArrayT: case stackitem.BufferT, stackitem.ByteArrayT:
return param.TryBytes() return param.TryBytes()
case stackitem.IntegerT:
n, err := param.TryInteger()
if err != nil {
return nil, errors.Wrap(err, "can't parse integer bytes")
}
return n.Bytes(), nil
case stackitem.AnyT: case stackitem.AnyT:
if param.Value() == nil { if param.Value() == nil {
return nil, nil return nil, nil

View file

@ -74,6 +74,10 @@ func TestBytesFromStackItem(t *testing.T) {
val, err := BytesFromStackItem(stringByteItem) val, err := BytesFromStackItem(stringByteItem)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, stringByteItem.Value().([]byte), val) require.Equal(t, stringByteItem.Value().([]byte), val)
val, err = BytesFromStackItem(intItem)
require.NoError(t, err)
require.Equal(t, intItem.Value().(*big.Int).Bytes(), val)
}) })
t.Run("incorrect assert", func(t *testing.T) { t.Run("incorrect assert", func(t *testing.T) {