From 09b295d7277dd5624c3583b1bda3fff23346e187 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 3 Dec 2019 16:07:15 +0300 Subject: [PATCH] util: add Uint160.Reverse() --- pkg/util/uint160.go | 9 +++++++++ pkg/util/uint160_test.go | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/util/uint160.go b/pkg/util/uint160.go index 01de6a2f5..2be0819cf 100644 --- a/pkg/util/uint160.go +++ b/pkg/util/uint160.go @@ -60,6 +60,15 @@ func (u Uint160) StringLE() string { return hex.EncodeToString(u.BytesLE()) } +// Reverse returns reversed representation of u. +func (u Uint160) Reverse() (r Uint160) { + for i := 0; i < Uint160Size; i++ { + r[i] = u[Uint160Size-i-1] + } + + return +} + // Equals returns true if both Uint256 values are the same. func (u Uint160) Equals(other Uint160) bool { return u == other diff --git a/pkg/util/uint160_test.go b/pkg/util/uint160_test.go index 6a489f1a7..9519f4f70 100644 --- a/pkg/util/uint160_test.go +++ b/pkg/util/uint160_test.go @@ -94,3 +94,12 @@ func TestUInt160String(t *testing.T) { assert.Equal(t, hexStr, val.String()) assert.Equal(t, hexRevStr, val.StringLE()) } + +func TestUint160_Reverse(t *testing.T) { + hexStr := "b28427088a3729b2536d10122960394e8be6721f" + val, err := Uint160DecodeStringBE(hexStr) + + require.NoError(t, err) + assert.Equal(t, hexStr, val.Reverse().StringLE()) + assert.Equal(t, val, val.Reverse().Reverse()) +}