This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_wrap_test.go
147 lines (125 loc) · 4.24 KB
/
cli_wrap_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
package omnicli
import (
"fmt"
"github.com/dabankio/omnicli/testtool"
"testing"
"github.com/dabankio/omnicli/btcjson"
)
func TestCliCreatemultisig(t *testing.T) {
cli, killomnicored, err := RunOmnicored(&RunOptions{NewTmpDir: true})
testtool.FailOnFlag(t, err != nil, "Failed to start btcd", err)
defer killomnicored()
type addrinfo struct {
addr, privkey, pubkey string
}
var addrs [3]addrinfo
{ //获取几个新地址
for i := 0; i < len(addrs); i++ {
add, err := cli.Getnewaddress(nil, nil)
testtool.FailOnFlag(t, err != nil, "Failed to get new address", err)
addrs[i] = addrinfo{addr: add}
validateResp, err := cli.Validateaddress(add)
testtool.FailOnErr(t, err, "failed to validate address")
addrs[i].pubkey = validateResp.Pubkey
privkey, err := cli.Dumpprivkey(add)
testtool.FailOnFlag(t, err != nil, "Failed to dump privkey", err)
addrs[i].privkey = privkey
}
fmt.Println("addrs", addrs)
}
var multisigResp btcjson.CreateMultiSigResult
{ //create multisig address
var keys []string
for _, info := range addrs {
keys = append(keys, info.pubkey)
// keys = append(keys, info.addr)
}
multisigResp, err = cli.Createmultisig(2, keys, nil)
testtool.FailOnFlag(t, err != nil, "Failed to create multi sig", err)
fmt.Println("keys", keys)
fmt.Println("multisig address:", jsonStr(multisigResp))
}
{
vRes, err := cli.Validateaddress(multisigResp.Address)
testtool.FailOnFlag(t, err != nil, "Failed to validate address info", err)
fmt.Println("validate multisig address", ToJsonIndent(vRes))
}
}
func TestCliAddmultisigaddress(t *testing.T) {
cli, killomnicored, err := RunOmnicored(&RunOptions{NewTmpDir: true})
testtool.FailOnFlag(t, err != nil, "Failed to start btcd", err)
defer killomnicored()
type addrinfo struct {
addr, privkey, pubkey string
}
var addrs [3]addrinfo
{ //获取几个新地址
for i := 0; i < len(addrs); i++ {
add, err := cli.Getnewaddress(nil, nil)
testtool.FailOnFlag(t, err != nil, "Failed to get new address", err)
addrs[i] = addrinfo{addr: add}
validateResp, err := cli.Validateaddress(add)
testtool.FailOnErr(t, err, "failed to validate address")
addrs[i].pubkey = validateResp.Pubkey
privkey, err := cli.Dumpprivkey(add)
testtool.FailOnFlag(t, err != nil, "Failed to dump privkey", err)
addrs[i].privkey = privkey
}
fmt.Println("addrs", addrs)
}
{ //create multisig address
var keys []string
for _, info := range addrs {
keys = append(keys, info.pubkey)
}
multisigResp, err := cli.Addmultisigaddress(btcjson.AddMultisigAddressCmd{
NRequired: 2, Keys: keys,
})
testtool.FailOnErr(t, err, "Failed to add multi sig address")
fmt.Println("multisig address:", multisigResp)
}
}
func TestCliGetAddressInfo(t *testing.T) {
cli, killomnicored, err := RunOmnicored(&RunOptions{NewTmpDir: true})
testtool.FailOnFlag(t, err != nil, "Failed to start btcd", err)
defer killomnicored()
var newAddr string
{
newAddr, err = cli.Getnewaddress(nil, nil)
testtool.FailOnFlag(t, err != nil, "Failed to get new address", err)
}
{
vRes, err := cli.Validateaddress(newAddr)
testtool.FailOnFlag(t, err != nil, "Failed to validate address", err)
fmt.Println("validate address res:", ToJsonIndent(vRes))
}
}
func TestCliGetWalletInfo(t *testing.T) {
cli, killomnicored, err := RunOmnicored(&RunOptions{NewTmpDir: true})
testtool.FailOnFlag(t, err != nil, "Failed to start btcd", err)
defer killomnicored()
info, err := cli.GetWalletInfo()
testtool.FailOnErr(t, err, "get wallet info")
fmt.Println(ToJsonIndent(info))
}
func TestCliListunspent(t *testing.T) {
cli, killomnicored, err := RunOmnicored(&RunOptions{NewTmpDir: true})
testtool.FailOnFlag(t, err != nil, "Failed to start d", err)
defer killomnicored()
var newaddr string
{
newaddr, err = cli.Getnewaddress(nil, nil)
testtool.FailOnFlag(t, err != nil, "Failed to get new address", err)
}
{
const leng = 102
hashs, err := cli.Generatetoaddress(leng, newaddr, nil)
testtool.FailOnFlag(t, err != nil, "Failed to gen to addr", err)
testtool.FailOnFlag(t, len(hashs) != leng, "len not equal", leng, hashs)
}
{
unspents, err := cli.Listunspent(0, 999, []string{newaddr})
testtool.FailOnFlag(t, err != nil, "Fail on listunspent", err)
fmt.Println(jsonStr(unspents))
}
}