-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathzcash.go
507 lines (423 loc) · 16.4 KB
/
zcash.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// Copyright (c) 2016 arithmetric
// Based on btcrpcclient by the btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package zcashrpcclient
import (
"encoding/json"
"github.com/arithmetric/zcashrpcclient/zcashjson"
"github.com/btcsuite/btcutil"
)
// ***************************
// Operation Listing Functions
// ***************************
// FutureZGetOperationResultResult is a future promise to deliver the result
// of a ZGetOperationResultAsync RPC invocation (or an applicable error).
type FutureZGetOperationResultResult chan *response
// Receive waits for the response promised by the future and returns detailed
// information about a wallet transaction.
func (r FutureZGetOperationResultResult) Receive() ([]zcashjson.ZGetOperationStatusResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal result as a z_getoperationresult result object
var results []zcashjson.ZGetOperationStatusResult
err = json.Unmarshal(res, &results)
if err != nil {
return nil, err
}
return results, nil
}
// ZGetOperationResultAsync returns an instance of a type that can be used to
// get the result of the RPC at some future time by invoking the Receive
// function on the returned instance.
//
// See ZGetOperationResult for the blocking version and more details.
func (c *Client) ZGetOperationResultAsync() FutureZGetOperationResultResult {
cmd := zcashjson.NewZGetOperationResultCmd(nil)
return c.sendCmd(cmd)
}
// ZGetOperationResult returns detailed information about a wallet transaction.
func (c *Client) ZGetOperationResult() ([]zcashjson.ZGetOperationStatusResult, error) {
return c.ZGetOperationResultAsync().Receive()
}
// FutureZGetOperationStatusResult is a future promise to deliver the result
// of a ZGetOperationStatusAsync RPC invocation (or an applicable error).
type FutureZGetOperationStatusResult chan *response
// Receive waits for the response promised by the future and returns detailed
// information about a wallet transaction.
func (r FutureZGetOperationStatusResult) Receive() ([]zcashjson.ZGetOperationStatusResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal result as a z_getoperationstatus result object
var results []zcashjson.ZGetOperationStatusResult
err = json.Unmarshal(res, &results)
if err != nil {
return nil, err
}
return results, nil
}
// ZGetOperationStatusAsync returns an instance of a type that can be used to
// get the result of the RPC at some future time by invoking the Receive
// function on the returned instance.
//
// See ZGetOperationStatus for the blocking version and more details.
func (c *Client) ZGetOperationStatusAsync() FutureZGetOperationStatusResult {
cmd := zcashjson.NewZGetOperationStatusCmd(nil)
return c.sendCmd(cmd)
}
// ZGetOperationStatus returns detailed information about a wallet transaction.
func (c *Client) ZGetOperationStatus() ([]zcashjson.ZGetOperationStatusResult, error) {
return c.ZGetOperationStatusAsync().Receive()
}
// FutureZListOperationIdsResult is a future promise to deliver the result of a
// ZListOperationIdsAsync RPC invocation (or an applicable error).
type FutureZListOperationIdsResult chan *response
// Receive waits for the response promised by the future and returns a list of
// the most recent transactions.
func (r FutureZListOperationIdsResult) Receive() ([]string, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal result as an array of result strings.
var operationIds []string
err = json.Unmarshal(res, &operationIds)
if err != nil {
return nil, err
}
return operationIds, nil
}
// ZListOperationIdsAsync returns an instance of a type that can be used to get
// the result of the RPC at some future time by invoking the Receive function on
// the returned instance.
//
// See ZListOperationIds for the blocking version and more details.
func (c *Client) ZListOperationIdsAsync() FutureZListOperationIdsResult {
cmd := zcashjson.NewZListOperationIdsCmd(nil)
return c.sendCmd(cmd)
}
// ZListOperationIds returns a list of the most recent transactions.
func (c *Client) ZListOperationIds() ([]string, error) {
return c.ZListOperationIdsAsync().Receive()
}
// **************************
// Transaction Send Functions
// **************************
// FutureZSendManyResult is a future promise to deliver the result of a
// ZSendManyAsync RPC invocation (or an applicable error).
type FutureZSendManyResult chan *response
// Receive waits for the response promised by the future and returns the
// operation ID.
func (r FutureZSendManyResult) Receive() (string, error) {
res, err := receiveFuture(r)
if err != nil {
return "", err
}
// Unmarshal result as an array of result strings.
var operationID string
err = json.Unmarshal(res, &operationID)
if err != nil {
return "", err
}
return operationID, nil
}
// ZSendManyAsync returns an instance of a type that can be used to get the
// result of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See ZSendMany for the blocking version and more details.
func (c *Client) ZSendManyAsync(fromAccount string, amounts []zcashjson.ZSendManyEntry) FutureZSendManyResult {
cmd := zcashjson.NewZSendManyCmd(fromAccount, amounts, nil)
return c.sendCmd(cmd)
}
// ZSendMany sends multiple amounts to multiple addresses using the provided
// account as a source of funds in a single transaction. Only funds with the
// default number of minimum confirmations will be used.
func (c *Client) ZSendMany(fromAccount string, amounts []zcashjson.ZSendManyEntry) (string, error) {
return c.ZSendManyAsync(fromAccount, amounts).Receive()
}
// *************************
// Address/Account Functions
// *************************
// FutureZGetNewAddressResult is a future promise to deliver the result of a
// ZGetNewAddressAsync RPC invocation (or an applicable error).
type FutureZGetNewAddressResult chan *response
// Receive waits for the response promised by the future and returns a new
// address.
func (r FutureZGetNewAddressResult) Receive() (string, error) {
res, err := receiveFuture(r)
if err != nil {
return "", err
}
// Unmarshal result as a string.
var zaddr string
err = json.Unmarshal(res, &zaddr)
if err != nil {
return "", err
}
return zaddr, nil
}
// ZGetNewAddressAsync returns an instance of a type that can be used to get the
// result of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See ZGetNewAddress for the blocking version and more details.
func (c *Client) ZGetNewAddressAsync() FutureZGetNewAddressResult {
cmd := zcashjson.NewZGetNewAddressCmd()
return c.sendCmd(cmd)
}
// ZGetNewAddress returns a new address.
func (c *Client) ZGetNewAddress() (string, error) {
return c.ZGetNewAddressAsync().Receive()
}
// ************************
// Amount/Balance Functions
// ************************
// FutureZListAddressesResult is a future promise to deliver the result of a
// ZListAddressesAsync RPC invocation (or an applicable error).
type FutureZListAddressesResult chan *response
// Receive waits for the response promised by the future and returns returns a
// map of account names and their associated balances.
func (r FutureZListAddressesResult) Receive() ([]string, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal result as a json object.
var zaddrs []string
err = json.Unmarshal(res, &zaddrs)
if err != nil {
return nil, err
}
return zaddrs, nil
}
// ZListAddressesAsync returns an instance of a type that can be used to get the
// result of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See ZListAddresses for the blocking version and more details.
func (c *Client) ZListAddressesAsync() FutureZListAddressesResult {
cmd := zcashjson.NewZListAddressesCmd(nil)
return c.sendCmd(cmd)
}
// ZListAddresses returns a map of account names and their associated balances
// using the default number of minimum confirmations.
func (c *Client) ZListAddresses() ([]string, error) {
return c.ZListAddressesAsync().Receive()
}
// FutureZGetBalanceResult is a future promise to deliver the result of a
// ZGetBalanceAsync or ZGetBalanceMinConfAsync RPC invocation (or an applicable
// error).
type FutureZGetBalanceResult chan *response
// Receive waits for the response promised by the future and returns the
// available balance from the server for the specified account.
func (r FutureZGetBalanceResult) Receive() (btcutil.Amount, error) {
res, err := receiveFuture(r)
if err != nil {
return 0, err
}
// Unmarshal result as a floating point number.
var balance float64
err = json.Unmarshal(res, &balance)
if err != nil {
return 0, err
}
amount, err := btcutil.NewAmount(balance)
if err != nil {
return 0, err
}
return amount, nil
}
// ZGetBalanceAsync returns an instance of a type that can be used to get the
// result of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See ZGetBalance for the blocking version and more details.
func (c *Client) ZGetBalanceAsync(address string) FutureZGetBalanceResult {
cmd := zcashjson.NewZGetBalanceCmd(&address, nil)
return c.sendCmd(cmd)
}
// ZGetBalance returns the available balance from the server for the specified
// account using the default number of minimum confirmations. The account may
// be "*" for all accounts.
func (c *Client) ZGetBalance(address string) (btcutil.Amount, error) {
return c.ZGetBalanceAsync(address).Receive()
}
// FutureZGetTotalBalanceResult is a future promise to deliver the result of a
// ZGetTotalBalanceAsync RPC invocation (or an applicable error).
type FutureZGetTotalBalanceResult chan *response
// Receive waits for the response promised by the future and returns the
// available balance from the server for the specified account.
func (r FutureZGetTotalBalanceResult) Receive() (*zcashjson.ZGetTotalBalanceResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal result as a floating point number.
var totals zcashjson.ZGetTotalBalanceResult
err = json.Unmarshal(res, &totals)
if err != nil {
return nil, err
}
return &totals, nil
}
// ZGetTotalBalanceAsync returns an instance of a type that can be used to get
// the result of the RPC at some future time by invoking the Receive function on
// the returned instance.
//
// See ZGetTotalBalance for the blocking version and more details.
func (c *Client) ZGetTotalBalanceAsync() FutureZGetTotalBalanceResult {
cmd := zcashjson.NewZGetTotalBalanceCmd()
return c.sendCmd(cmd)
}
// ZGetTotalBalance returns the transparent, private, and total balances for
// all addresses in the wallet.
func (c *Client) ZGetTotalBalance() (*zcashjson.ZGetTotalBalanceResult, error) {
return c.ZGetTotalBalanceAsync().Receive()
}
// FutureZListReceivedByAddressResult is a future promise to deliver the result
// of a ZListReceivedByAddressAsync RPC invocation (or an applicable error).
type FutureZListReceivedByAddressResult chan *response
// Receive waits for the response promised by the future and returns a list of
// balances by address.
func (r FutureZListReceivedByAddressResult) Receive() ([]zcashjson.ZListReceivedByAddressResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal as an array of z_listreceivedbyaddress result objects.
var received []zcashjson.ZListReceivedByAddressResult
err = json.Unmarshal(res, &received)
if err != nil {
return nil, err
}
return received, nil
}
// ZListReceivedByAddressAsync returns an instance of a type that can be used to
// get the result of the RPC at some future time by invoking the Receive
// function on the returned instance.
//
// See ZListReceivedByAddress for the blocking version and more details.
func (c *Client) ZListReceivedByAddressAsync(address string) FutureZListReceivedByAddressResult {
cmd := zcashjson.NewZListReceivedByAddressCmd(address, nil)
return c.sendCmd(cmd)
}
// ZListReceivedByAddress lists balances by address using the default number
// of minimum confirmations not including addresses that haven't received any
// payments or watching only addresses.
func (c *Client) ZListReceivedByAddress(address string) ([]zcashjson.ZListReceivedByAddressResult, error) {
return c.ZListReceivedByAddressAsync(address).Receive()
}
// ***********************
// Export/Import Functions
// ***********************
// FutureZExportKeyResult is a future promise to deliver the result of a
// ZExportKeyAsync RPC invocation (or an applicable error).
type FutureZExportKeyResult chan *response
// Receive waits for the response promised by the future and returns the private
// key corresponding to the passed address
func (r FutureZExportKeyResult) Receive() (string, error) {
res, err := receiveFuture(r)
if err != nil {
return "", err
}
// Unmarshal result as a string.
var key string
err = json.Unmarshal(res, &key)
if err != nil {
return "", err
}
return key, nil
}
// ZExportKeyAsync returns an instance of a type that can be used to get the
// result of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See ZExportKey for the blocking version and more details.
func (c *Client) ZExportKeyAsync(address string) FutureZExportKeyResult {
cmd := zcashjson.NewZExportKeyCmd(address)
return c.sendCmd(cmd)
}
// ZExportKey gets the private key corresponding to the passed address.
func (c *Client) ZExportKey(address string) (string, error) {
return c.ZExportKeyAsync(address).Receive()
}
// FutureZExportWalletResult is a future promise to deliver the result of a
// ZExportWalletAsync RPC invocation (or an applicable error).
type FutureZExportWalletResult chan *response
// Receive waits for the response promised by the future and returns the private
// key corresponding to the passed address
func (r FutureZExportWalletResult) Receive() error {
_, err := receiveFuture(r)
if err != nil {
return err
}
return nil
}
// ZExportWalletAsync returns an instance of a type that can be used to get the
// result of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See ZExportWallet for the blocking version and more details.
func (c *Client) ZExportWalletAsync(filename string) FutureZExportWalletResult {
cmd := zcashjson.NewZExportWalletCmd(filename)
return c.sendCmd(cmd)
}
// ZExportWallet gets the private key corresponding to the passed address.
func (c *Client) ZExportWallet(filename string) error {
return c.ZExportWalletAsync(filename).Receive()
}
// FutureZImportKeyResult is a future promise to deliver the result of an
// ZImportKeyAsync RPC invocation (or an applicable error).
type FutureZImportKeyResult chan *response
// Receive waits for the response promised by the future and returns the result
// of importing the passed private key.
func (r FutureZImportKeyResult) Receive() error {
_, err := receiveFuture(r)
if err != nil {
return err
}
return nil
}
// ZImportKeyAsync returns an instance of a type that can be used to get the
// result of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See ZImportKey for the blocking version and more details.
func (c *Client) ZImportKeyAsync(key string, rescan *bool) FutureZImportKeyResult {
cmd := zcashjson.NewZImportKeyCmd(key, rescan)
return c.sendCmd(cmd)
}
// ZImportKey imports the passed private key.
func (c *Client) ZImportKey(key string, rescan *bool) error {
return c.ZImportKeyAsync(key, rescan).Receive()
}
// FutureZImportWalletResult is a future promise to deliver the result of a
// ZImportWalletAsync RPC invocation (or an applicable error).
type FutureZImportWalletResult chan *response
// Receive waits for the response promised by the future and returns the private
// key corresponding to the passed address
func (r FutureZImportWalletResult) Receive() error {
_, err := receiveFuture(r)
if err != nil {
return err
}
return nil
}
// ZImportWalletAsync returns an instance of a type that can be used to get the
// result of the RPC at some future time by invoking the Receive function on the
// returned instance.
//
// See ZImportWallet for the blocking version and more details.
func (c *Client) ZImportWalletAsync(filename string) FutureZImportWalletResult {
cmd := zcashjson.NewZImportWalletCmd(filename)
return c.sendCmd(cmd)
}
// ZImportWallet gets the private key corresponding to the passed address.
func (c *Client) ZImportWallet(filename string) error {
return c.ZImportWalletAsync(filename).Receive()
}