util: add Uint160DecodeBytesLE()

This commit is contained in:
Evgenii Stratonikov 2019-12-03 16:12:26 +03:00
parent 09b295d727
commit 07e832f046
2 changed files with 21 additions and 0 deletions

View file

@ -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[:]

View file

@ -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)
}