From 434ecb41dafd3b1a0614e81ed91065f3c789a77a Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 15 Apr 2021 16:23:12 +0300 Subject: [PATCH] [#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 --- pkg/morph/client/util.go | 7 +++++++ pkg/morph/client/util_test.go | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/pkg/morph/client/util.go b/pkg/morph/client/util.go index 3af9c4c4..da61e276 100644 --- a/pkg/morph/client/util.go +++ b/pkg/morph/client/util.go @@ -48,6 +48,13 @@ func BytesFromStackItem(param stackitem.Item) ([]byte, error) { switch param.Type() { case stackitem.BufferT, stackitem.ByteArrayT: 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: if param.Value() == nil { return nil, nil diff --git a/pkg/morph/client/util_test.go b/pkg/morph/client/util_test.go index a3588b35..897a0233 100644 --- a/pkg/morph/client/util_test.go +++ b/pkg/morph/client/util_test.go @@ -74,6 +74,10 @@ func TestBytesFromStackItem(t *testing.T) { val, err := BytesFromStackItem(stringByteItem) require.NoError(t, err) 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) {