diff --git a/pkg/util/uint160.go b/pkg/util/uint160.go index 2be0819cf..9e63f4dce 100644 --- a/pkg/util/uint160.go +++ b/pkg/util/uint160.go @@ -35,6 +35,20 @@ func Uint160DecodeBytesBE(b []byte) (u Uint160, err error) { return } +// Uint160DecodeBytesLE attempts to decode the given bytes in little-endian +// into an Uint160. +func Uint160DecodeBytesLE(b []byte) (u Uint160, err error) { + if len(b) != Uint160Size { + return u, fmt.Errorf("expected byte size of %d got %d", Uint160Size, len(b)) + } + + for i := range b { + u[Uint160Size-i-1] = b[i] + } + + return +} + // BytesBE returns a big-endian byte representation of u. func (u Uint160) BytesBE() []byte { return u[:] diff --git a/pkg/util/uint160_test.go b/pkg/util/uint160_test.go index 9519f4f70..96edcca3a 100644 --- a/pkg/util/uint160_test.go +++ b/pkg/util/uint160_test.go @@ -52,6 +52,13 @@ func TestUint160DecodeBytes(t *testing.T) { assert.NoError(t, err) assert.Equal(t, hexStr, val.String()) + valLE, err := Uint160DecodeBytesLE(b) + assert.NoError(t, err) + assert.Equal(t, val, valLE.Reverse()) + + _, err = Uint160DecodeBytesLE(b[1:]) + assert.Error(t, err) + _, err = Uint160DecodeBytesBE(b[1:]) assert.Error(t, err) }