Skip to content

Commit 66f64a2

Browse files
Create utility func to convert HRP addresses (ava-labs#986)
create utility func to convert addresses Co-authored-by: StephenButtolph <[email protected]>
1 parent e360cce commit 66f64a2

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// (c) 2021, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package formatting
5+
6+
// ConvertAddresses converts a list of addresses with arbitrary chains and HRPs
7+
// (e.g. X-local1....) to a list of addresses with the provided format
8+
// (e.g. P-custom1...).
9+
func ConvertAddresses(destChain string, toHRP string, addresses []string) ([]string, error) {
10+
convertedAddrs := make([]string, len(addresses))
11+
for i, addr := range addresses {
12+
_, _, addrBytes, err := ParseAddress(addr)
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
newAddrStr, err := FormatAddress(destChain, toHRP, addrBytes)
18+
if err != nil {
19+
return nil, err
20+
}
21+
convertedAddrs[i] = newAddrStr
22+
}
23+
return convertedAddrs, nil
24+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// (c) 2021, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package formatting
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestAddressConversion(t *testing.T) {
13+
assert := assert.New(t)
14+
15+
tests := []struct {
16+
chain string
17+
hrp string
18+
srcAddrs []string
19+
expectedErr error
20+
expectedAddrs []string
21+
}{
22+
{
23+
chain: "X",
24+
hrp: "custom",
25+
srcAddrs: []string{
26+
"X-local1g65uqn6t77p656w64023nh8nd9updzmxyymev2",
27+
"X-local18jma8ppw3nhx5r4ap8clazz0dps7rv5u00z96u",
28+
},
29+
expectedAddrs: []string{
30+
"X-custom1g65uqn6t77p656w64023nh8nd9updzmxwd59gh",
31+
"X-custom18jma8ppw3nhx5r4ap8clazz0dps7rv5u9xde7p",
32+
},
33+
},
34+
{
35+
chain: "X",
36+
hrp: "local",
37+
srcAddrs: []string{
38+
"X-local1g65uqn6t77p656w64023nh8nd9updzmxyymev2",
39+
"X-local18jma8ppw3nhx5r4ap8clazz0dps7rv5u00z96u",
40+
},
41+
expectedAddrs: []string{
42+
"X-local1g65uqn6t77p656w64023nh8nd9updzmxyymev2",
43+
"X-local18jma8ppw3nhx5r4ap8clazz0dps7rv5u00z96u",
44+
},
45+
},
46+
{
47+
srcAddrs: []string{
48+
"X-local1g65uqn6t77p656w64023nh8nd9updzmxyymev2",
49+
},
50+
expectedAddrs: []string{
51+
"-1g65uqn6t77p656w64023nh8nd9updzmx4x372p",
52+
},
53+
},
54+
{
55+
srcAddrs: []string{
56+
"not a valid address",
57+
},
58+
expectedErr: errNoSeparator,
59+
},
60+
}
61+
for _, test := range tests {
62+
result, err := ConvertAddresses(test.chain, test.hrp, test.srcAddrs)
63+
assert.Equal(test.expectedErr, err)
64+
assert.Len(result, len(test.expectedAddrs))
65+
for i, want := range test.expectedAddrs {
66+
got := result[i]
67+
assert.Equal(want, got)
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)