From b9b118d3ea8eb9c772f1bf9f37b52d0f9777f573 Mon Sep 17 00:00:00 2001 From: dauTT <30392990+dauTT@users.noreply.github.com> Date: Mon, 5 Aug 2019 09:34:31 +0200 Subject: [PATCH] Add ReverseString, ToReverseScriptHash method (#281) * Added 1) ReverseString method to the Uint160 type 2) ToReverseScriptHash method to convert a base58 address to a reverse script hash * Simplified ToScriptHash method --- pkg/wire/util/address/address.go | 19 ++++++++++++------- pkg/wire/util/address/address_test.go | 17 +++++++++++++++++ pkg/wire/util/uint160.go | 5 +++++ pkg/wire/util/uint160_test.go | 12 ++++++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 pkg/wire/util/address/address_test.go diff --git a/pkg/wire/util/address/address.go b/pkg/wire/util/address/address.go index 12f09c63a..08b0347af 100644 --- a/pkg/wire/util/address/address.go +++ b/pkg/wire/util/address/address.go @@ -1,22 +1,27 @@ package address import ( - "encoding/hex" - "github.com/CityOfZion/neo-go/pkg/crypto/base58" "github.com/CityOfZion/neo-go/pkg/wire/util" ) // ToScriptHash converts an address to a script hash func ToScriptHash(address string) string { - - decodedAddressAsBytes, err := base58.Decode(address) + a, err := Uint160Decode(address) if err != nil { return "" } - decodedAddressAsHex := hex.EncodeToString(decodedAddressAsBytes) - scriptHash := (decodedAddressAsHex[2:42]) - return scriptHash + return a.String() + +} + +// ToReverseScriptHash converts an address to a reverse script hash +func ToReverseScriptHash(address string) string { + a, err := Uint160Decode(address) + if err != nil { + return "" + } + return a.ReverseString() } // FromUint160 returns the "NEO address" from the given diff --git a/pkg/wire/util/address/address_test.go b/pkg/wire/util/address/address_test.go new file mode 100644 index 000000000..af264a935 --- /dev/null +++ b/pkg/wire/util/address/address_test.go @@ -0,0 +1,17 @@ +package address + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestScriptHash(t *testing.T) { + address := "AJeAEsmeD6t279Dx4n2HWdUvUmmXQ4iJvP" + + hash := ToScriptHash(address) + reverseHash := ToReverseScriptHash(address) + + assert.Equal(t, "b28427088a3729b2536d10122960394e8be6721f", reverseHash) + assert.Equal(t, "1f72e68b4e39602912106d53b229378a082784b2", hash) +} diff --git a/pkg/wire/util/uint160.go b/pkg/wire/util/uint160.go index 429fe5d95..fa81299bf 100644 --- a/pkg/wire/util/uint160.go +++ b/pkg/wire/util/uint160.go @@ -68,6 +68,11 @@ 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++ { diff --git a/pkg/wire/util/uint160_test.go b/pkg/wire/util/uint160_test.go index 96416653c..f92f736c7 100644 --- a/pkg/wire/util/uint160_test.go +++ b/pkg/wire/util/uint160_test.go @@ -48,3 +48,15 @@ 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()) + +}