-
Notifications
You must be signed in to change notification settings - Fork 2
/
account_test.go
157 lines (134 loc) · 3.72 KB
/
account_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package caip
import (
"encoding/json"
"errors"
"fmt"
"testing"
)
// See: https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-10.md#test-cases
func TestAccountID(t *testing.T) {
for _, tc := range []struct {
id string
}{{
// Ethereum mainnet
id: "eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb",
}, {
// Bitcoin mainnet
id: "bip122:000000000019d6689c085ae165831e93:128Lkh3S7CkDTBZ8W7BbpsN3YYizJMp8p6",
}, {
// Cosmos Hub
id: "cosmos:cosmoshub-3:cosmos1t2uflqwqe0fsj0shcfkrvpukewcw40yjj6hdc0",
}, {
// Kusama network
id: "polkadot:b0a8d493285c2df73290dfb7e61f870f:5hmuyxw9xdgbpptgypokw4thfyoe3ryenebr381z9iaegmfy",
}, {
// Dummy max length (64+1+8+1+32 = 106 chars/bytes)
id: "chainstd:8c3444cf8970a9e41a706fab93e7a6c4:6d9b0b4b9994e8a6afbd3dc3ed983cd51c755afb27cd1dc7825ef59c134a39f7",
}} {
a := AccountID{}
if err := a.Parse(tc.id); err != nil {
t.Errorf("Failed to parse account id")
}
if a.String() != tc.id {
t.Errorf("Failed to serialize account id to string")
}
if _, err := NewAccountID(a.ChainID, a.Address); err != nil {
t.Errorf("Failed to create account id from address")
}
b, err := json.Marshal(a)
if err != nil {
t.Errorf("Failed to marshal to json")
}
a = AccountID{}
if err := json.Unmarshal(b, &a); err != nil {
t.Errorf("Failed to unmarshal to json")
}
if a.String() != tc.id {
t.Errorf("Unmarshalled account id invalid")
}
a2 := AccountID{}
if err := a2.Scan(a.String()); err != nil {
t.Errorf("Scanning value from sql.NullString")
}
if a2.String() != a.String() {
t.Errorf("Scanned value not valid")
}
}
}
func TestEVMAccountID(t *testing.T) {
for _, tc := range []struct {
id string
}{{
// Ethereum mainnet
id: "eip155:1:0xab16a96D359eC26a11e2C2b3d8f8B8942d5Bfcdb",
}} {
a := EVMAccountID{}
if err := a.Parse(tc.id); err != nil {
t.Errorf("Failed to parse account id")
}
if a.String() != tc.id {
t.Errorf("Failed to serialize account id to string")
}
if _, err := NewEVMAccountID(a.ChainID, a.AccountID.Address); err != nil {
t.Errorf("Failed to create account id from address")
}
b, err := json.Marshal(a)
if err != nil {
t.Errorf("Failed to marshal to json")
}
a = EVMAccountID{}
if err := json.Unmarshal(b, &a); err != nil {
t.Errorf("Failed to unmarshal to json")
}
if a.String() != tc.id {
t.Errorf("Unmarshalled account id invalid")
}
a2 := EVMAccountID{}
if err := a2.Scan(a.String()); err != nil {
t.Errorf("Scanning value from sql.NullString")
}
if a2.String() != a.String() {
t.Errorf("Scanned value not valid")
}
}
}
func TestInvalidEVMAccountID(t *testing.T) {
for _, tc := range []struct {
id string
err error
}{{
// Ethereum mainnet
id: "eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdx",
err: fmt.Errorf("invalid eth address: %s", "0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdx"),
}, {
// Ethereum mainnet
id: "eip155:1:0xab16a96d35",
err: fmt.Errorf("invalid eth address: %s", "0xab16a96d35"),
}, {
// Ethereum mainnet
id: "cosmos:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdd",
err: fmt.Errorf("invalid chain namespace: %s", "cosmos"),
}} {
a := EVMAccountID{}
if err := a.Parse(tc.id); err != nil {
t.Errorf("Failed to parse account id")
}
if a.String() != tc.id {
t.Errorf("Failed to serialize account id to string")
}
err := a.Validate()
if err == nil {
t.Errorf("Validate account id should error")
}
if errors.Is(err, tc.err) {
t.Errorf("expected error: %s", tc.err)
}
_, err = NewEVMAccountID(a.ChainID, a.AccountID.Address)
if err == nil {
t.Errorf("Create account id should error")
}
if err.Error() != tc.err.Error() {
t.Errorf("expected error: %s, got: %s", tc.err, err)
}
}
}