From c1b1b6fca420290fab8be32cbb94c434141cd640 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 22 Aug 2019 15:35:18 +0300 Subject: [PATCH] uint160: move ReverseString() from _pkg.dev With associated test and drop duplicating Uint160 implementation from _pkg.dev. It doesn't seem to be used in pkg code at the moment, but still it can be useful. Refs #307. --- _pkg.dev/wire/util/uint160.go | 91 ------------------------------ _pkg.dev/wire/util/uint160_test.go | 62 -------------------- pkg/util/uint160.go | 5 ++ pkg/util/uint160_test.go | 11 ++++ 4 files changed, 16 insertions(+), 153 deletions(-) delete mode 100644 _pkg.dev/wire/util/uint160.go delete mode 100644 _pkg.dev/wire/util/uint160_test.go diff --git a/_pkg.dev/wire/util/uint160.go b/_pkg.dev/wire/util/uint160.go deleted file mode 100644 index fa81299bf..000000000 --- a/_pkg.dev/wire/util/uint160.go +++ /dev/null @@ -1,91 +0,0 @@ -package util - -import ( - "crypto/sha256" - "encoding/hex" - "encoding/json" - "fmt" - - "github.com/CityOfZion/neo-go/pkg/wire/util/slice" - "golang.org/x/crypto/ripemd160" -) - -const uint160Size = 20 - -// Uint160 is a 20 byte long unsigned integer. -type Uint160 [uint160Size]uint8 - -// Uint160DecodeString attempts to decode the given string into an Uint160. -func Uint160DecodeString(s string) (u Uint160, err error) { - 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 Uint160DecodeBytes(b) -} - -// Uint160DecodeBytes attempts to decode the given bytes into an Uint160. -func Uint160DecodeBytes(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 := 0; i < uint160Size; i++ { - u[i] = b[i] - } - return -} - -// Uint160FromScript returns a Uint160 type from a raw script. -func Uint160FromScript(script []byte) (u Uint160, err error) { - sha := sha256.New() - sha.Write(script) - b := sha.Sum(nil) - ripemd := ripemd160.New() - ripemd.Write(b) - b = ripemd.Sum(nil) - return Uint160DecodeBytes(b) -} - -// Bytes returns the byte slice representation of u. -func (u Uint160) Bytes() []byte { - b := make([]byte, uint160Size) - for i := 0; i < uint160Size; i++ { - b[i] = byte(u[i]) - } - return b -} - -// BytesReverse return a reversed byte representation of u. -func (u Uint160) BytesReverse() []byte { - return slice.Reverse(u.Bytes()) -} - -// String implements the stringer interface. -func (u Uint160) String() string { - return hex.EncodeToString(u.Bytes()) -} - -// ReverseString implements the stringer interface. -func (u Uint160) ReverseString() string { - return hex.EncodeToString(u.BytesReverse()) -} - -// Equals returns true if both Uint256 values are the same. -func (u Uint160) Equals(other Uint160) bool { - for i := 0; i < uint160Size; i++ { - if u[i] != other[i] { - return false - } - } - return true -} - -// MarshalJSON implements the json marshaller interface. -func (u Uint160) MarshalJSON() ([]byte, error) { - return json.Marshal( - fmt.Sprintf("0x%s", hex.EncodeToString(slice.Reverse(u.Bytes()))), - ) -} diff --git a/_pkg.dev/wire/util/uint160_test.go b/_pkg.dev/wire/util/uint160_test.go deleted file mode 100644 index f92f736c7..000000000 --- a/_pkg.dev/wire/util/uint160_test.go +++ /dev/null @@ -1,62 +0,0 @@ -package util - -import ( - "encoding/hex" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestUInt160DecodeString(t *testing.T) { - hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302" - val, err := Uint160DecodeString(hexStr) - if err != nil { - t.Fatal(err) - } - assert.Equal(t, hexStr, val.String()) -} - -func TestUint160DecodeBytes(t *testing.T) { - hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302" - b, err := hex.DecodeString(hexStr) - if err != nil { - t.Fatal(err) - } - val, err := Uint160DecodeBytes(b) - if err != nil { - t.Fatal(err) - } - assert.Equal(t, hexStr, val.String()) -} - -func TestUInt160Equals(t *testing.T) { - a := "2d3b96ae1bcc5a585e075e3b81920210dec16302" - b := "4d3b96ae1bcc5a585e075e3b81920210dec16302" - - ua, err := Uint160DecodeString(a) - if err != nil { - t.Fatal(err) - } - ub, err := Uint160DecodeString(b) - if err != nil { - t.Fatal(err) - } - if ua.Equals(ub) { - t.Fatalf("%s and %s cannot be equal", ua, ub) - } - if !ua.Equals(ua) { - t.Fatalf("%s and %s must be equal", ua, ua) - } -} - -func TestUInt160String(t *testing.T) { - hexStr := "b28427088a3729b2536d10122960394e8be6721f" - hexRevStr := "1f72e68b4e39602912106d53b229378a082784b2" - - val, err := Uint160DecodeString(hexStr) - assert.Nil(t, err) - - assert.Equal(t, hexStr, val.String()) - assert.Equal(t, hexRevStr, val.ReverseString()) - -} diff --git a/pkg/util/uint160.go b/pkg/util/uint160.go index 9f1341e28..6efb0f546 100644 --- a/pkg/util/uint160.go +++ b/pkg/util/uint160.go @@ -63,6 +63,11 @@ func (u Uint160) String() string { return hex.EncodeToString(u.Bytes()) } +// ReverseString is the same as String, but returnes an inversed representation. +func (u Uint160) ReverseString() string { + return hex.EncodeToString(u.BytesReverse()) +} + // 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 a527974db..0d64a5623 100644 --- a/pkg/util/uint160_test.go +++ b/pkg/util/uint160_test.go @@ -75,3 +75,14 @@ func TestUInt160Equals(t *testing.T) { t.Fatalf("%s and %s must be equal", ua, ua) } } + +func TestUInt160String(t *testing.T) { + hexStr := "b28427088a3729b2536d10122960394e8be6721f" + hexRevStr := "1f72e68b4e39602912106d53b229378a082784b2" + + val, err := Uint160DecodeString(hexStr) + assert.Nil(t, err) + + assert.Equal(t, hexStr, val.String()) + assert.Equal(t, hexRevStr, val.ReverseString()) +}