Skip to content

Commit a51f458

Browse files
author
Jan Lin
authored
fix: use hex to encode address to string (blocto#39)
1 parent 6ac35d6 commit a51f458

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

client/token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ func (impl *TokenClientImpl) ListAccountTokens(ctx context.Context, owner models
459459
}
460460
`
461461
variables := map[string]interface{}{
462-
"owner_address": graphql.String(owner.PrefixZeroTrimmedHex()),
462+
"owner_address": graphql.String(owner.ToHex()),
463463
"limit": batchSize,
464464
}
465465

models/account_address.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func (addr AccountAddress) PrefixZeroTrimmedHex() string {
2121
return "0x" + strings.TrimPrefix(hex, "0")
2222
}
2323

24+
func (addr AccountAddress) ToHex() string {
25+
return "0x" + hex.EncodeToString(addr[:])
26+
}
27+
2428
func HexToAccountAddress(addr string) (AccountAddress, error) {
2529
addr = strings.TrimPrefix(addr, "0x")
2630
if len(addr)%2 == 1 {

models/account_address_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package models
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestTodHex(t *testing.T) {
10+
t.Run("address", func(t *testing.T) {
11+
addr := "0x1111111111111111111111111111111111111111111111111111111111111111"
12+
accountAddr, err := HexToAccountAddress(addr)
13+
assert.NoError(t, err)
14+
15+
assert.Equal(t, addr, accountAddr.ToHex())
16+
})
17+
18+
t.Run("addressWithZeroAfter0xPrefix", func(t *testing.T) {
19+
addr := "0x0111111111111111111111111111111111111111111111111111111111111111"
20+
accountAddr, err := HexToAccountAddress(addr)
21+
assert.NoError(t, err)
22+
23+
assert.Equal(t, addr, accountAddr.ToHex())
24+
})
25+
}

0 commit comments

Comments
 (0)