-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlibzec_test.go
262 lines (236 loc) · 9.05 KB
/
libzec_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package libzec_test
import (
"context"
"crypto/ecdsa"
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"reflect"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/renproject/libzec-go"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/sirupsen/logrus"
"github.com/tyler-smith/go-bip39"
)
var _ = Describe("LibZEC", func() {
loadMasterKey := func(network uint32) (*hdkeychain.ExtendedKey, error) {
switch network {
case 1:
seed := bip39.NewSeed(os.Getenv("TESTNET_MNEMONIC"), os.Getenv("TESTNET_PASSPHRASE"))
return hdkeychain.NewMaster(seed, &chaincfg.TestNet3Params)
case 0:
seed := bip39.NewSeed(os.Getenv("MNEMONIC"), os.Getenv("PASSPHRASE"))
return hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
default:
return nil, NewErrUnsupportedNetwork(fmt.Sprintf("network id: %d", network))
}
}
loadKey := func(path ...uint32) (*ecdsa.PrivateKey, error) {
key, err := loadMasterKey(path[1])
if err != nil {
return nil, err
}
for _, val := range path {
key, err = key.Child(val)
if err != nil {
return nil, err
}
}
privKey, err := key.ECPrivKey()
if err != nil {
return nil, err
}
return privKey.ToECDSA(), nil
}
buildClients := func() []Client {
APIClient, err := NewMercuryClient("testnet")
if err != nil {
panic(err)
}
return []Client{APIClient}
}
getAccounts := func(client Client) (Account, Account) {
mainKey, err := loadKey(44, 1, 0, 0, 0) // "m/44'/1'/0'/0/0"
if err != nil {
panic(err)
}
mainAccount := NewAccount(client, mainKey, logrus.StandardLogger())
secKey, err := loadKey(44, 1, 1, 0, 0) // "m/44'/1'/1'/0/0"
if err != nil {
panic(err)
}
secondaryAccount := NewAccount(client, secKey, logrus.StandardLogger())
return mainAccount, secondaryAccount
}
Context("when interacting with mainnet", func() {
It("should get a valid address of an account", func() {
client, err := NewMercuryClient("mainnet")
Expect(err).Should(BeNil())
mainAccount, _ := getAccounts(client)
addr, err := mainAccount.Address()
Expect(err).Should(BeNil())
Expect(addr.IsForNet(&chaincfg.MainNetParams)).Should(BeTrue())
})
It("should get correct network of an account", func() {
client, err := NewMercuryClient("mainnet")
Expect(err).Should(BeNil())
mainAccount, _ := getAccounts(client)
Expect(mainAccount.NetworkParams()).Should(Equal(&chaincfg.MainNetParams))
})
It("should get a valid serialized public key of an account", func() {
client, err := NewMercuryClient("mainnet")
Expect(err).Should(BeNil())
mainAccount, _ := getAccounts(client)
pubKey, err := mainAccount.SerializedPublicKey()
Expect(err).Should(BeNil())
Expect(btcec.IsCompressedPubKey(pubKey)).Should(BeTrue())
_, err = btcec.ParsePubKey(pubKey, btcec.S256())
Expect(err).Should(BeNil())
})
It("should get the balance of an address", func() {
client, err := NewMercuryClient("mainnet")
Expect(err).Should(BeNil())
mainAccount, _ := getAccounts(client)
addr, err := mainAccount.Address()
Expect(err).Should(BeNil())
_, err = mainAccount.Balance(addr.String(), 0)
Expect(err).Should(BeNil())
})
})
for _, client := range buildClients() {
var secret [32]byte
rand.Read(secret[:])
Context("when interacting with testnet", func() {
It("should get a valid address of an account", func() {
mainAccount, _ := getAccounts(client)
addr, err := mainAccount.Address()
Expect(err).Should(BeNil())
Expect(addr.IsForNet(&chaincfg.TestNet3Params)).Should(BeTrue())
})
It("should get correct network of an account", func() {
mainAccount, _ := getAccounts(client)
Expect(mainAccount.NetworkParams()).Should(Equal(&chaincfg.TestNet3Params))
})
It("should get a utxo", func() {
mainAccount, _ := getAccounts(client)
addr, err := mainAccount.Address()
Expect(err).Should(BeNil())
utxos, err := mainAccount.GetUTXOs(addr.EncodeAddress(), 1, 0)
Expect(err).Should(BeNil())
actualUTXO := utxos[0]
fmt.Println(actualUTXO.TxHash, actualUTXO.Vout)
utxo, err := mainAccount.GetUTXO(actualUTXO.TxHash, actualUTXO.Vout)
Expect(err).Should(BeNil())
Expect(reflect.DeepEqual(actualUTXO, utxo)).Should(BeTrue())
})
It("should get a valid serialized public key of an account", func() {
mainAccount, _ := getAccounts(client)
pubKey, err := mainAccount.SerializedPublicKey()
Expect(err).Should(BeNil())
Expect(btcec.IsCompressedPubKey(pubKey)).Should(BeFalse())
_, err = btcec.ParsePubKey(pubKey, btcec.S256())
Expect(err).Should(BeNil())
})
It("should get the balance of an address", func() {
mainAccount, _ := getAccounts(client)
addr, err := mainAccount.Address()
Expect(err).Should(BeNil())
_, err = mainAccount.Balance(addr.String(), 0)
Expect(err).Should(BeNil())
})
It("should transfer 5000000 ZAT to another address", func() {
mainAccount, secondaryAccount := getAccounts(client)
secAddr, err := secondaryAccount.Address()
Expect(err).Should(BeNil())
initialBalance, err := secondaryAccount.Balance(secAddr.EncodeAddress(), 0)
Expect(err).Should(BeNil())
// building a transaction to transfer zcash to the secondary address
_, _, err = mainAccount.Transfer(context.Background(), secAddr.EncodeAddress(), 5010000, Fast, false)
Expect(err).Should(BeNil())
finalBalance, err := secondaryAccount.Balance(secAddr.EncodeAddress(), 0)
Expect(err).Should(BeNil())
Expect(finalBalance - initialBalance).Should(Equal(int64(5000000)))
})
It("should transfer 10000 ZAT to another address", func() {
mainKey, err := loadKey(44, 1, 0, 0, 0) // "m/44'/1'/0'/0/0"
Expect(err).Should(BeNil())
mainPrivKey := (*btcec.PrivateKey)(mainKey)
mainAccount, secondaryAccount := getAccounts(client)
mainAddr, err := mainAccount.Address()
Expect(err).Should(BeNil())
secAddr, err := secondaryAccount.Address()
Expect(err).Should(BeNil())
utxos, err := client.GetUTXOs(mainAddr.String(), 10, 0)
Expect(err).Should(BeNil())
builder := NewTxBuilder(client)
tx, err := builder.Build(mainKey.PublicKey, secAddr.String(), nil, 20000, utxos, nil)
Expect(err).Should(BeNil())
hashes := tx.Hashes()
sigs := make([]*btcec.Signature, len(hashes))
for i, hash := range hashes {
sigs[i], err = mainPrivKey.Sign(hash)
Expect(err).Should(BeNil())
}
Expect(tx.InjectSigs(sigs)).Should(BeNil())
initialBalance, err := secondaryAccount.Balance(secAddr.String(), 0)
Expect(err).Should(BeNil())
// building a transaction to transfer zcash to the secondary address
txHash, err := tx.Submit()
Expect(err).Should(BeNil())
fmt.Printf(mainAccount.FormatTransactionView("successfully submitted transfer tx", hex.EncodeToString(txHash)))
finalBalance, err := secondaryAccount.Balance(secAddr.String(), 0)
Expect(err).Should(BeNil())
Expect(finalBalance - initialBalance).Should(Equal(int64(10000)))
})
It("should transfer 10000 ZAT from a slave address", func() {
mainKey, err := loadKey(44, 1, 0, 0, 0) // "m/44'/1'/0'/0/0"
Expect(err).Should(BeNil())
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
mainPrivKey := (*btcec.PrivateKey)(mainKey)
mainAccount, secondaryAccount := getAccounts(client)
nonce := [32]byte{}
rand.Read(nonce[:])
pubKeyBytes, err := client.SerializePublicKey((*btcec.PublicKey)(&mainPrivKey.PublicKey))
Expect(err).Should(BeNil())
slaveAddr, err := mainAccount.SlaveAddress(btcutil.Hash160(pubKeyBytes), nonce[:])
Expect(err).Should(BeNil())
slaveScript, err := mainAccount.SlaveScript(btcutil.Hash160(pubKeyBytes), nonce[:])
Expect(err).Should(BeNil())
_, _, err = mainAccount.Transfer(ctx, slaveAddr.String(), 30000, Fast, false)
Expect(err).Should(BeNil())
mainAddr, err := mainAccount.Address()
Expect(err).Should(BeNil())
mwUTXOs, err := client.GetUTXOs(mainAddr.String(), 10, 0)
Expect(err).Should(BeNil())
scriptUTXOs, err := client.GetUTXOs(slaveAddr.String(), 10, 0)
Expect(err).Should(BeNil())
builder := NewTxBuilder(client)
tx, err := builder.Build(mainKey.PublicKey, mainAddr.String(), slaveScript, 20000, mwUTXOs, scriptUTXOs)
Expect(err).Should(BeNil())
hashes := tx.Hashes()
sigs := make([]*btcec.Signature, len(hashes))
for i, hash := range hashes {
sigs[i], err = mainPrivKey.Sign(hash)
Expect(err).Should(BeNil())
}
Expect(tx.InjectSigs(sigs)).Should(BeNil())
initialBalance, err := secondaryAccount.Balance(mainAddr.String(), 0)
Expect(err).Should(BeNil())
// building a transaction to receive bitcoin from a script address
txHash, err := tx.Submit()
Expect(err).Should(BeNil())
fmt.Printf(mainAccount.FormatTransactionView("successfully submitted transfer tx", hex.EncodeToString(txHash)))
finalBalance, err := secondaryAccount.Balance(mainAddr.String(), 0)
Expect(err).Should(BeNil())
Expect(finalBalance - initialBalance).Should(Equal(int64(10000)))
})
})
}
})