forked from btcsuite/btcutil
/
addrconvs_test.go
185 lines (162 loc) · 6.33 KB
/
addrconvs_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
185
// Copyright (c) 2013-2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package btcutil_test
import (
"bytes"
"github.com/conformal/btcutil"
"github.com/conformal/btcwire"
"testing"
)
var encodePrivateKeyTests = []struct {
in []byte
net btcwire.BitcoinNet
compressed bool
out string
}{
{[]byte{
0x0c, 0x28, 0xfc, 0xa3, 0x86, 0xc7, 0xa2, 0x27,
0x60, 0x0b, 0x2f, 0xe5, 0x0b, 0x7c, 0xae, 0x11,
0xec, 0x86, 0xd3, 0xbf, 0x1f, 0xbe, 0x47, 0x1b,
0xe8, 0x98, 0x27, 0xe1, 0x9d, 0x72, 0xaa, 0x1d,
}, btcwire.MainNet, false, "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ"},
{[]byte{
0xdd, 0xa3, 0x5a, 0x14, 0x88, 0xfb, 0x97, 0xb6,
0xeb, 0x3f, 0xe6, 0xe9, 0xef, 0x2a, 0x25, 0x81,
0x4e, 0x39, 0x6f, 0xb5, 0xdc, 0x29, 0x5f, 0xe9,
0x94, 0xb9, 0x67, 0x89, 0xb2, 0x1a, 0x03, 0x98,
}, btcwire.TestNet3, true, "cV1Y7ARUr9Yx7BR55nTdnR7ZXNJphZtCCMBTEZBJe1hXt2kB684q"},
}
var encodeTests = []struct {
raw []byte
net btcwire.BitcoinNet
res string
err error
}{
{[]byte{0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc, 0xc5, 0x4c, 0xe7, 0xd2, 0xa4, 0x91, 0xbb, 0x4a, 0x0e, 0x84},
btcwire.MainNet, "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil},
{[]byte{0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b, 0xf4, 0x05, 0x12, 0xbc, 0xa2, 0xce, 0xb1, 0xdd, 0x80, 0xad, 0xaa},
btcwire.MainNet, "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG", nil},
{[]byte{0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83, 0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f},
btcwire.TestNet, "", btcutil.ErrUnknownNet},
{[]byte{0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83, 0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f},
btcwire.TestNet3, "mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz", nil},
// Raw address not 20 bytes (padded with leading 0s)
{[]byte{0x00, 0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b, 0xf4, 0x05, 0x12, 0xbc, 0xa2, 0xce, 0xb1, 0xdd, 0x80, 0xad, 0xaa},
btcwire.MainNet, "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG", btcutil.ErrMalformedAddress},
// Bad network
{make([]byte, 20), 0, "", btcutil.ErrUnknownNet},
}
func TestEncodeAddresses(t *testing.T) {
for i := range encodeTests {
res, err := btcutil.EncodeAddress(encodeTests[i].raw,
encodeTests[i].net)
if err != encodeTests[i].err {
t.Error(err)
continue
}
if err == nil && res != encodeTests[i].res {
t.Errorf("Results differ: Expected '%s', returned '%s'",
encodeTests[i].res, res)
}
}
}
var decodeTests = []struct {
addr string
res []byte
net btcwire.BitcoinNet
err error
}{
{"1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX",
[]byte{0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc, 0xc5, 0x4c, 0xe7, 0xd2, 0xa4, 0x91, 0xbb, 0x4a, 0x0e, 0x84},
btcwire.MainNet, nil},
{"mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
[]byte{0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83, 0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f},
btcwire.TestNet3, nil},
// Wrong length
{"01MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil, btcwire.MainNet, btcutil.ErrMalformedAddress},
// Bad magic
{"2MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX", nil, btcwire.MainNet, btcutil.ErrUnknownNet},
// Bad checksum
{"1MirQ9bwyQcGVJPwKUgapu5ouK2E2dpuqz", nil, btcwire.MainNet, btcutil.ErrMalformedAddress},
}
func TestDecodeAddresses(t *testing.T) {
for i := range decodeTests {
res, net, err := btcutil.DecodeAddress(decodeTests[i].addr)
if err != decodeTests[i].err {
t.Error(err)
}
if err != nil {
continue
}
if !bytes.Equal(res, decodeTests[i].res) {
t.Errorf("Results differ: Expected '%v', returned '%v'",
decodeTests[i].res, res)
}
if net != decodeTests[i].net {
t.Errorf("Networks differ: Expected '%v', returned '%v'",
decodeTests[i].net, net)
}
}
}
func TestEncodeDecodePrivateKey(t *testing.T) {
for x, test := range encodePrivateKeyTests {
wif, err := btcutil.EncodePrivateKey(test.in, test.net, test.compressed)
if err != nil {
t.Errorf("%x: %v", x, err)
continue
}
if wif != test.out {
t.Errorf("TestEncodeDecodePrivateKey failed: want '%s', got '%s'",
test.out, wif)
continue
}
key, _, compressed, err := btcutil.DecodePrivateKey(test.out)
if err != nil {
t.Error(err)
continue
}
if !bytes.Equal(key, test.in) || compressed != test.compressed {
t.Errorf("TestEncodeDecodePrivateKey failed: want '%x', got '%x'",
test.out, key)
}
}
}
var encodeScriptHashTests = []struct {
raw []byte
net btcwire.BitcoinNet
res string
err error
}{
{[]byte{0xf8, 0x15, 0xb0, 0x36, 0xd9, 0xbb, 0xbc, 0xe5, 0xe9, 0xf2, 0xa0, 0x0a, 0xbd, 0x1b, 0xf3, 0xdc, 0x91, 0xe9, 0x55, 0x10},
btcwire.MainNet, "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC", nil},
{[]byte{0xe8, 0xc3, 0x00, 0xc8, 0x79, 0x86, 0xef, 0xa8, 0x4c, 0x37, 0xc0, 0x51, 0x99, 0x29, 0x01, 0x9e, 0xf8, 0x6e, 0xb5, 0xb4},
btcwire.MainNet, "3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8", nil},
// Raw address not 20 bytes (padded with leading 0s)
{[]byte{0x00, 0xf8, 0x15, 0xb0, 0x36, 0xd9, 0xbb, 0xbc, 0xe5, 0xe9, 0xf2, 0xa0, 0x0a, 0xbd, 0x1b, 0xf3, 0xdc, 0x91, 0xe9, 0x55, 0x10},
btcwire.MainNet, "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC", btcutil.ErrMalformedAddress},
{[]byte{0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83, 0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f},
btcwire.TestNet, "", btcutil.ErrUnknownNet},
// from bitcoind base58_keys_valid
{[]byte{0xc5, 0x79, 0x34, 0x2c, 0x2c, 0x4c, 0x92, 0x20, 0x20, 0x5e, 0x2c, 0xdc, 0x28, 0x56, 0x17, 0x04, 0x0c, 0x92, 0x4a, 0x0a},
btcwire.TestNet3, "2NBFNJTktNa7GZusGbDbGKRZTxdK9VVez3n", nil},
// from bitcoind base58_keys_valid
{[]byte{0x63, 0xbc, 0xc5, 0x65, 0xf9, 0xe6, 0x8e, 0xe0, 0x18, 0x9d, 0xd5, 0xcc, 0x67, 0xf1, 0xb0, 0xe5, 0xf0, 0x2f, 0x45, 0xcb},
btcwire.MainNet, "3AnNxabYGoTxYiTEZwFEnerUoeFXK2Zoks", nil},
// Bad network
{make([]byte, 20), 0, "", btcutil.ErrUnknownNet},
}
func TestEncodeScriptHashes(t *testing.T) {
for i := range encodeScriptHashTests {
res, err := btcutil.EncodeScriptHash(encodeScriptHashTests[i].raw,
encodeScriptHashTests[i].net)
if err != encodeScriptHashTests[i].err {
t.Errorf("Error Results differ: Expected '%v', returned '%v'", encodeScriptHashTests[i].err, err)
continue
}
if err == nil && res != encodeScriptHashTests[i].res {
t.Errorf("Results differ: Expected '%s', returned '%s'",
encodeScriptHashTests[i].res, res)
}
}
}