-
Notifications
You must be signed in to change notification settings - Fork 44
/
cashaddr_test.go
184 lines (171 loc) · 5.33 KB
/
cashaddr_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package bchutil
import (
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"testing"
)
var TestVectorsP2PKH = [][]string{
{"1BpEi6DfDAUFd7GtittLSdBeYJvcoaVggu", "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a"},
{"1KXrWXciRDZUpQwQmuM1DbwsKDLYAYsVLR", "qr95sy3j9xwd2ap32xkykttr4cvcu7as4y0qverfuy"},
{"16w1D5WRVKJuZUsSRzdLp9w3YGcgoxDXb", "qqq3728yw0y47sqn6l2na30mcw6zm78dzqre909m2r"},
}
var TestVectorsP2SH = [][]string{
{"3CWFddi6m4ndiGyKqzYvsFYagqDLPVMTzC", "ppm2qsznhks23z7629mms6s4cwef74vcwvn0h829pq"},
{"3LDsS579y7sruadqu11beEJoTjdFiFCdX4", "pr95sy3j9xwd2ap32xkykttr4cvcu7as4yc93ky28e"},
{"31nwvkZwyPdgzjBJZXfDmSWsC4ZLKpYyUw", "pqq3728yw0y47sqn6l2na30mcw6zm78dzq5ucqzc37"},
}
var valid []string = []string{
"prefix:x64nx6hz",
"PREFIX:X64NX6HZ",
"p:gpf8m4h7",
"bitcoincash:qpzry9x8gf2tvdw0s3jn54khce6mua7lcw20ayyn",
"bchtest:testnetaddress4d6njnut",
"bchreg:555555555555555555555555555555555555555555555udxmlmrz",
}
func TestValid(t *testing.T) {
for _, s := range valid {
_, _, err := DecodeCashAddress(s)
if err != nil {
t.Error(err)
}
}
}
var Invalid []string = []string{
"prefix:x32nx6hz",
"prEfix:x64nx6hz",
"prefix:x64nx6Hz",
"pref1x:6m8cxv73",
"prefix:",
":u9wsx07j",
"bchreg:555555555555555555x55555555555555555555555555udxmlmrz",
"bchreg:555555555555555555555555555555551555555555555udxmlmrz",
"pre:fix:x32nx6hz",
"prefixx64nx6hz",
}
func TestInvalid(t *testing.T) {
for _, s := range Invalid {
_, _, err := DecodeCashAddress(s)
if err == nil {
t.Error("Failed to error on invalid string")
}
}
}
func TestDecodeCashAddress(t *testing.T) {
// Mainnet
addr, err := DecodeAddress("bitcoincash:qr95sy3j9xwd2ap32xkykttr4cvcu7as4y0qverfuy", &chaincfg.MainNetParams)
if err != nil {
t.Error(err)
}
if addr.String() != "qr95sy3j9xwd2ap32xkykttr4cvcu7as4y0qverfuy" {
t.Error("Address decoding error")
}
addr1, err := DecodeAddress("bitcoincash:ppm2qsznhks23z7629mms6s4cwef74vcwvn0h829pq", &chaincfg.MainNetParams)
if err != nil {
t.Error(err)
}
if addr1.String() != "ppm2qsznhks23z7629mms6s4cwef74vcwvn0h829pq" {
t.Error("Address decoding error")
}
// Testnet
addr, err = DecodeAddress("bchtest:qr95sy3j9xwd2ap32xkykttr4cvcu7as4ytjg7p7mc", &chaincfg.TestNet3Params)
if err != nil {
t.Error(err)
}
if addr.String() != "qr95sy3j9xwd2ap32xkykttr4cvcu7as4ytjg7p7mc" {
t.Error("Address decoding error")
}
// Regtest
addr, err = DecodeAddress("bchreg:qr95sy3j9xwd2ap32xkykttr4cvcu7as4y3w7lzdc7", &chaincfg.RegressionNetParams)
if err != nil {
t.Error(err)
}
if addr.String() != "qr95sy3j9xwd2ap32xkykttr4cvcu7as4y3w7lzdc7" {
t.Error("Address decoding error")
}
}
var dataElement = []byte{203, 72, 18, 50, 41, 156, 213, 116, 49, 81, 172, 75, 45, 99, 174, 25, 142, 123, 176, 169}
// Second address of https://github.com/Bitcoin-UAHF/spec/blob/master/cashaddr.md#examples-of-address-translation
func TestCashAddressPubKeyHash_EncodeAddress(t *testing.T) {
// Mainnet
addr, err := NewCashAddressPubKeyHash(dataElement, &chaincfg.MainNetParams)
if err != nil {
t.Error(err)
}
if addr.String() != "qr95sy3j9xwd2ap32xkykttr4cvcu7as4y0qverfuy" {
t.Error("Address decoding error")
}
// Testnet
addr, err = NewCashAddressPubKeyHash(dataElement, &chaincfg.TestNet3Params)
if err != nil {
t.Error(err)
}
if addr.String() != "qr95sy3j9xwd2ap32xkykttr4cvcu7as4ytjg7p7mc" {
t.Error("Address decoding error")
}
// Regtest
addr, err = NewCashAddressPubKeyHash(dataElement, &chaincfg.RegressionNetParams)
if err != nil {
t.Error(err)
}
if addr.String() != "qr95sy3j9xwd2ap32xkykttr4cvcu7as4y3w7lzdc7" {
t.Error("Address decoding error")
}
}
var dataElement2 = []byte{118, 160, 64, 83, 189, 160, 168, 139, 218, 81, 119, 184, 106, 21, 195, 178, 159, 85, 152, 115}
// 4th address of https://github.com/Bitcoin-UAHF/spec/blob/master/cashaddr.md#examples-of-address-translation
func TestCashAddressScriptHash_EncodeAddress(t *testing.T) {
// Mainnet
addr, err := NewCashAddressScriptHashFromHash(dataElement2, &chaincfg.MainNetParams)
if err != nil {
t.Error(err)
}
if addr.String() != "ppm2qsznhks23z7629mms6s4cwef74vcwvn0h829pq" {
t.Error("Address decoding error")
}
// Testnet
addr, err = NewCashAddressScriptHashFromHash(dataElement2, &chaincfg.TestNet3Params)
if err != nil {
t.Error(err)
}
if addr.String() != "ppm2qsznhks23z7629mms6s4cwef74vcwvhanqgjxu" {
t.Error("Address decoding error")
}
// Regtest
addr, err = NewCashAddressScriptHashFromHash(dataElement2, &chaincfg.RegressionNetParams)
if err != nil {
t.Error(err)
}
if addr.String() != "ppm2qsznhks23z7629mms6s4cwef74vcwvdp9ptp96" {
t.Error("Address decoding error")
}
}
func TestTestVectors(t *testing.T) {
for _, v := range TestVectorsP2PKH {
addr, err := btcutil.DecodeAddress(v[0], &chaincfg.MainNetParams)
if err != nil {
t.Error(err)
return
}
addr2, err := NewCashAddressPubKeyHash(addr.ScriptAddress(), &chaincfg.MainNetParams)
if err != nil {
t.Error(err)
}
if addr2.String() != v[1] {
t.Error("Failed to derive correct address")
}
}
for _, v := range TestVectorsP2SH {
addr, err := btcutil.DecodeAddress(v[0], &chaincfg.MainNetParams)
if err != nil {
t.Error(err)
return
}
addr2, err := NewCashAddressScriptHashFromHash(addr.ScriptAddress(), &chaincfg.MainNetParams)
if err != nil {
t.Error(err)
}
if addr2.String() != v[1] {
t.Error("Failed to derive correct address")
}
}
}