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()) + +}