util: add Uint160DecodeStringLE()

This commit is contained in:
Evgenii Stratonikov 2019-12-03 16:14:44 +03:00
parent 07e832f046
commit 72fe884faa
2 changed files with 26 additions and 0 deletions

View file

@ -26,6 +26,22 @@ func Uint160DecodeStringBE(s string) (Uint160, error) {
return Uint160DecodeBytesBE(b)
}
// Uint160DecodeStringLE attempts to decode the given string
// in little-endian hex encoding into an Uint160.
func Uint160DecodeStringLE(s string) (Uint160, error) {
var u Uint160
if len(s) != Uint160Size*2 {
return u, fmt.Errorf("expected string size of %d got %d", Uint160Size*2, len(s))
}
b, err := hex.DecodeString(s)
if err != nil {
return u, err
}
return Uint160DecodeBytesLE(b)
}
// Uint160DecodeBytesBE attempts to decode the given bytes into an Uint160.
func Uint160DecodeBytesBE(b []byte) (u Uint160, err error) {
if len(b) != Uint160Size {

View file

@ -35,12 +35,22 @@ func TestUInt160DecodeString(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, hexStr, val.String())
valLE, err := Uint160DecodeStringLE(hexStr)
assert.NoError(t, err)
assert.Equal(t, val, valLE.Reverse())
_, err = Uint160DecodeStringBE(hexStr[1:])
assert.Error(t, err)
_, err = Uint160DecodeStringLE(hexStr[1:])
assert.Error(t, err)
hexStr = "zz3b96ae1bcc5a585e075e3b81920210dec16302"
_, err = Uint160DecodeStringBE(hexStr)
assert.Error(t, err)
_, err = Uint160DecodeStringLE(hexStr)
assert.Error(t, err)
}
func TestUint160DecodeBytes(t *testing.T) {