private/blockchain: fix Hex to add 0x prefix
Change-Id: I47da9cea160fe9bbdf452956836e3d4b5b44646f
This commit is contained in:
parent
8b70f969b6
commit
5cfd64579e
@ -32,7 +32,10 @@ func (h Hash) Bytes() []byte { return h[:] }
|
||||
|
||||
// Hex gets the hex string representation of the underlying hash.
|
||||
func (h Hash) Hex() string {
|
||||
return hex.EncodeToString(h.Bytes())
|
||||
var buf [len(h)*2 + 2]byte
|
||||
copy(buf[:2], "0x")
|
||||
hex.Encode(buf[2:], h[:])
|
||||
return string(buf[:])
|
||||
}
|
||||
|
||||
// MarshalJSON implements json marshalling interface.
|
||||
@ -55,7 +58,10 @@ func (a Address) Bytes() []byte { return a[:] }
|
||||
|
||||
// Hex gets string representation of the underlying address.
|
||||
func (a Address) Hex() string {
|
||||
return hex.EncodeToString(a.Bytes())
|
||||
var buf [len(a)*2 + 2]byte
|
||||
copy(buf[:2], "0x")
|
||||
hex.Encode(buf[2:], a[:])
|
||||
return string(buf[:])
|
||||
}
|
||||
|
||||
// MarshalJSON implements json marshalling interface.
|
||||
@ -68,6 +74,12 @@ func (a *Address) UnmarshalJSON(bytes []byte) error {
|
||||
return unmarshalHexString(a[:], bytes, reflect.TypeOf(Address{}))
|
||||
}
|
||||
|
||||
// BytesToHash create a new hash from raw bytes.
|
||||
func BytesToHash(bytes []byte) (h Hash, err error) {
|
||||
copy(h[:], bytes)
|
||||
return h, err
|
||||
}
|
||||
|
||||
// unmarshalHexString decodes JSON string containing hex string into bytes.
|
||||
// Copies result into dst byte slice.
|
||||
func unmarshalHexString(dst, src []byte, typ reflect.Type) error {
|
||||
|
@ -1,20 +1,54 @@
|
||||
// Copyright (C) 2021 Storj Labs, Inc.
|
||||
// See LICENSE for copying information.
|
||||
package blockchain
|
||||
package blockchain_test
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"storj.io/storj/private/blockchain"
|
||||
)
|
||||
|
||||
func TestBytesToAddress(t *testing.T) {
|
||||
a := Address{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 10}
|
||||
gotA, err := BytesToAddress(a.Bytes())
|
||||
a := blockchain.Address{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 10}
|
||||
gotA, err := blockchain.BytesToAddress(a.Bytes())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, a, gotA)
|
||||
|
||||
_, err = BytesToAddress([]byte{1, 2, 3})
|
||||
_, err = blockchain.BytesToAddress([]byte{1, 2, 3})
|
||||
require.Error(t, err)
|
||||
|
||||
}
|
||||
|
||||
func TestHex(t *testing.T) {
|
||||
addresses := []string{
|
||||
"0xd15aaacddf4b72ba8fc5c41fc4b03f05aa73385c",
|
||||
"0xbe8f1abb014ec9988b6185ab4d8870992e838231",
|
||||
"0xd24400ae8bfebb18ca49be86258a3c749cf46853",
|
||||
}
|
||||
hashes := []string{
|
||||
"0xdea1082dbea119c822dfe804264f5b880d4208ef51e8c5a8995eff10a5094de8",
|
||||
"0xd1a78f16158b550945dd39182d188c3fb7285b431223c8b0fe38f4181e9ac197",
|
||||
"0x47a5b3fae0bad45631a65324001f75dea311898e26ee10597a66953b57dfe332",
|
||||
}
|
||||
|
||||
for _, address := range addresses {
|
||||
decoded, err := hex.DecodeString(address[2:])
|
||||
require.NoError(t, err)
|
||||
|
||||
a, err := blockchain.BytesToAddress(decoded)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, address, a.Hex())
|
||||
}
|
||||
for _, hash := range hashes {
|
||||
decoded, err := hex.DecodeString(hash[2:])
|
||||
require.NoError(t, err)
|
||||
|
||||
h, err := blockchain.BytesToHash(decoded)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, hash, h.Hex())
|
||||
}
|
||||
}
|
||||
|
@ -927,7 +927,7 @@ func TestWalletJsonMarshall(t *testing.T) {
|
||||
|
||||
out, err := json.Marshal(wi)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(out), "\"address\":\"0102030000000000000000000000000000000000\"")
|
||||
require.Contains(t, string(out), "\"address\":\"0x0102030000000000000000000000000000000000\"")
|
||||
require.Contains(t, string(out), "\"balance\":{\"value\":\"100\",\"currency\":\"USD\"}")
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user