This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
forked from decred/dcrrpcclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.go
3348 lines (2853 loc) · 119 KB
/
wallet.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2014-2016 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package dcrrpcclient
import (
"encoding/hex"
"encoding/json"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrjson"
"github.com/decred/dcrd/wire"
"github.com/decred/dcrutil"
)
// *****************************
// Transaction Listing Functions
// *****************************
// FutureGetTransactionResult is a future promise to deliver the result
// of a GetTransactionAsync RPC invocation (or an applicable error).
type FutureGetTransactionResult chan *response
// Receive waits for the response promised by the future and returns detailed
// information about a wallet transaction.
func (r FutureGetTransactionResult) Receive() (*dcrjson.GetTransactionResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal result as a gettransaction result object
var getTx dcrjson.GetTransactionResult
err = json.Unmarshal(res, &getTx)
if err != nil {
return nil, err
}
return &getTx, nil
}
// GetTransactionAsync 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 GetTransaction for the blocking version and more details.
func (c *Client) GetTransactionAsync(txHash *chainhash.Hash) FutureGetTransactionResult {
hash := ""
if txHash != nil {
hash = txHash.String()
}
cmd := dcrjson.NewGetTransactionCmd(hash, nil)
return c.sendCmd(cmd)
}
// GetTransaction returns detailed information about a wallet transaction.
//
// See GetRawTransaction to return the raw transaction instead.
func (c *Client) GetTransaction(txHash *chainhash.Hash) (*dcrjson.GetTransactionResult, error) {
return c.GetTransactionAsync(txHash).Receive()
}
// FutureListTransactionsResult is a future promise to deliver the result of a
// ListTransactionsAsync, ListTransactionsCountAsync, or
// ListTransactionsCountFromAsync RPC invocation (or an applicable error).
type FutureListTransactionsResult chan *response
// Receive waits for the response promised by the future and returns a list of
// the most recent transactions.
func (r FutureListTransactionsResult) Receive() ([]dcrjson.ListTransactionsResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal result as an array of listtransaction result objects.
var transactions []dcrjson.ListTransactionsResult
err = json.Unmarshal(res, &transactions)
if err != nil {
return nil, err
}
return transactions, nil
}
// ListTransactionsAsync 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 ListTransactions for the blocking version and more details.
func (c *Client) ListTransactionsAsync(account string) FutureListTransactionsResult {
cmd := dcrjson.NewListTransactionsCmd(&account, nil, nil, nil)
return c.sendCmd(cmd)
}
// ListTransactions returns a list of the most recent transactions.
//
// See the ListTransactionsCount and ListTransactionsCountFrom to control the
// number of transactions returned and starting point, respectively.
func (c *Client) ListTransactions(account string) ([]dcrjson.ListTransactionsResult, error) {
return c.ListTransactionsAsync(account).Receive()
}
// ListTransactionsCountAsync 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 ListTransactionsCount for the blocking version and more details.
func (c *Client) ListTransactionsCountAsync(account string, count int) FutureListTransactionsResult {
cmd := dcrjson.NewListTransactionsCmd(&account, &count, nil, nil)
return c.sendCmd(cmd)
}
// ListTransactionsCount returns a list of the most recent transactions up
// to the passed count.
//
// See the ListTransactions and ListTransactionsCountFrom functions for
// different options.
func (c *Client) ListTransactionsCount(account string, count int) ([]dcrjson.ListTransactionsResult, error) {
return c.ListTransactionsCountAsync(account, count).Receive()
}
// ListTransactionsCountFromAsync 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 ListTransactionsCountFrom for the blocking version and more details.
func (c *Client) ListTransactionsCountFromAsync(account string, count, from int) FutureListTransactionsResult {
cmd := dcrjson.NewListTransactionsCmd(&account, &count, &from, nil)
return c.sendCmd(cmd)
}
// ListTransactionsCountFrom returns a list of the most recent transactions up
// to the passed count while skipping the first 'from' transactions.
//
// See the ListTransactions and ListTransactionsCount functions to use defaults.
func (c *Client) ListTransactionsCountFrom(account string, count, from int) ([]dcrjson.ListTransactionsResult, error) {
return c.ListTransactionsCountFromAsync(account, count, from).Receive()
}
// FutureListUnspentResult is a future promise to deliver the result of a
// ListUnspentAsync, ListUnspentMinAsync, ListUnspentMinMaxAsync, or
// ListUnspentMinMaxAddressesAsync RPC invocation (or an applicable error).
type FutureListUnspentResult chan *response
// Receive waits for the response promised by the future and returns all
// unspent wallet transaction outputs returned by the RPC call. If the
// future wac returnd by a call to ListUnspentMinAsync, ListUnspentMinMaxAsync,
// or ListUnspentMinMaxAddressesAsync, the range may be limited by the
// parameters of the RPC invocation.
func (r FutureListUnspentResult) Receive() ([]dcrjson.ListUnspentResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal result as an array of listunspent results.
var unspent []dcrjson.ListUnspentResult
err = json.Unmarshal(res, &unspent)
if err != nil {
return nil, err
}
return unspent, nil
}
// ListUnspentAsync 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 ListUnspent for the blocking version and more details.
func (c *Client) ListUnspentAsync() FutureListUnspentResult {
cmd := dcrjson.NewListUnspentCmd(nil, nil, nil)
return c.sendCmd(cmd)
}
// ListUnspentMinAsync 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 ListUnspentMin for the blocking version and more details.
func (c *Client) ListUnspentMinAsync(minConf int) FutureListUnspentResult {
cmd := dcrjson.NewListUnspentCmd(&minConf, nil, nil)
return c.sendCmd(cmd)
}
// ListUnspentMinMaxAsync 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 ListUnspentMinMax for the blocking version and more details.
func (c *Client) ListUnspentMinMaxAsync(minConf, maxConf int) FutureListUnspentResult {
cmd := dcrjson.NewListUnspentCmd(&minConf, &maxConf, nil)
return c.sendCmd(cmd)
}
// ListUnspentMinMaxAddressesAsync 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 ListUnspentMinMaxAddresses for the blocking version and more details.
func (c *Client) ListUnspentMinMaxAddressesAsync(minConf, maxConf int, addrs []dcrutil.Address) FutureListUnspentResult {
addrStrs := make([]string, 0, len(addrs))
for _, a := range addrs {
addrStrs = append(addrStrs, a.EncodeAddress())
}
cmd := dcrjson.NewListUnspentCmd(&minConf, &maxConf, &addrStrs)
return c.sendCmd(cmd)
}
// ListUnspent returns all unspent transaction outputs known to a wallet, using
// the default number of minimum and maximum number of confirmations as a
// filter (1 and 9999999, respectively).
func (c *Client) ListUnspent() ([]dcrjson.ListUnspentResult, error) {
return c.ListUnspentAsync().Receive()
}
// ListUnspentMin returns all unspent transaction outputs known to a wallet,
// using the specified number of minimum conformations and default number of
// maximum confiramtions (9999999) as a filter.
func (c *Client) ListUnspentMin(minConf int) ([]dcrjson.ListUnspentResult, error) {
return c.ListUnspentMinAsync(minConf).Receive()
}
// ListUnspentMinMax returns all unspent transaction outputs known to a wallet,
// using the specified number of minimum and maximum number of confirmations as
// a filter.
func (c *Client) ListUnspentMinMax(minConf, maxConf int) ([]dcrjson.ListUnspentResult, error) {
return c.ListUnspentMinMaxAsync(minConf, maxConf).Receive()
}
// ListUnspentMinMaxAddresses returns all unspent transaction outputs that pay
// to any of specified addresses in a wallet using the specified number of
// minimum and maximum number of confirmations as a filter.
func (c *Client) ListUnspentMinMaxAddresses(minConf, maxConf int, addrs []dcrutil.Address) ([]dcrjson.ListUnspentResult, error) {
return c.ListUnspentMinMaxAddressesAsync(minConf, maxConf, addrs).Receive()
}
// FutureListSinceBlockResult is a future promise to deliver the result of a
// ListSinceBlockAsync or ListSinceBlockMinConfAsync RPC invocation (or an
// applicable error).
type FutureListSinceBlockResult chan *response
// Receive waits for the response promised by the future and returns all
// transactions added in blocks since the specified block hash, or all
// transactions if it is nil.
func (r FutureListSinceBlockResult) Receive() (*dcrjson.ListSinceBlockResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal result as a listsinceblock result object.
var listResult dcrjson.ListSinceBlockResult
err = json.Unmarshal(res, &listResult)
if err != nil {
return nil, err
}
return &listResult, nil
}
// ListSinceBlockAsync 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 ListSinceBlock for the blocking version and more details.
func (c *Client) ListSinceBlockAsync(blockHash *chainhash.Hash) FutureListSinceBlockResult {
var hash *string
if blockHash != nil {
hash = dcrjson.String(blockHash.String())
}
cmd := dcrjson.NewListSinceBlockCmd(hash, nil, nil)
return c.sendCmd(cmd)
}
// ListSinceBlock returns all transactions added in blocks since the specified
// block hash, or all transactions if it is nil, using the default number of
// minimum confirmations as a filter.
//
// See ListSinceBlockMinConf to override the minimum number of confirmations.
func (c *Client) ListSinceBlock(blockHash *chainhash.Hash) (*dcrjson.ListSinceBlockResult, error) {
return c.ListSinceBlockAsync(blockHash).Receive()
}
// ListSinceBlockMinConfAsync 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 ListSinceBlockMinConf for the blocking version and more details.
func (c *Client) ListSinceBlockMinConfAsync(blockHash *chainhash.Hash, minConfirms int) FutureListSinceBlockResult {
var hash *string
if blockHash != nil {
hash = dcrjson.String(blockHash.String())
}
cmd := dcrjson.NewListSinceBlockCmd(hash, &minConfirms, nil)
return c.sendCmd(cmd)
}
// ListSinceBlockMinConf returns all transactions added in blocks since the
// specified block hash, or all transactions if it is nil, using the specified
// number of minimum confirmations as a filter.
//
// See ListSinceBlock to use the default minimum number of confirmations.
func (c *Client) ListSinceBlockMinConf(blockHash *chainhash.Hash, minConfirms int) (*dcrjson.ListSinceBlockResult, error) {
return c.ListSinceBlockMinConfAsync(blockHash, minConfirms).Receive()
}
// **************************
// Transaction Send Functions
// **************************
// FutureLockUnspentResult is a future promise to deliver the error result of a
// LockUnspentAsync RPC invocation.
type FutureLockUnspentResult chan *response
// Receive waits for the response promised by the future and returns the result
// of locking or unlocking the unspent output(s).
func (r FutureLockUnspentResult) Receive() error {
_, err := receiveFuture(r)
return err
}
// LockUnspentAsync 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 LockUnspent for the blocking version and more details.
func (c *Client) LockUnspentAsync(unlock bool, ops []*wire.OutPoint) FutureLockUnspentResult {
outputs := make([]dcrjson.TransactionInput, len(ops))
for i, op := range ops {
outputs[i] = dcrjson.TransactionInput{
Txid: op.Hash.String(),
Vout: op.Index,
Tree: op.Tree,
}
}
cmd := dcrjson.NewLockUnspentCmd(unlock, outputs)
return c.sendCmd(cmd)
}
// LockUnspent marks outputs as locked or unlocked, depending on the value of
// the unlock bool. When locked, the unspent output will not be selected as
// input for newly created, non-raw transactions, and will not be returned in
// future ListUnspent results, until the output is marked unlocked again.
//
// If unlock is false, each outpoint in ops will be marked locked. If unlocked
// is true and specific outputs are specified in ops (len != 0), exactly those
// outputs will be marked unlocked. If unlocked is true and no outpoints are
// specified, all previous locked outputs are marked unlocked.
//
// The locked or unlocked state of outputs are not written to disk and after
// restarting a wallet process, this data will be reset (every output unlocked).
//
// NOTE: While this method would be a bit more readable if the unlock bool was
// reversed (that is, LockUnspent(true, ...) locked the outputs), it has been
// left as unlock to keep compatibility with the reference client API and to
// avoid confusion for those who are already familiar with the lockunspent RPC.
func (c *Client) LockUnspent(unlock bool, ops []*wire.OutPoint) error {
return c.LockUnspentAsync(unlock, ops).Receive()
}
// FutureListLockUnspentResult is a future promise to deliver the result of a
// ListLockUnspentAsync RPC invocation (or an applicable error).
type FutureListLockUnspentResult chan *response
// Receive waits for the response promised by the future and returns the result
// of all currently locked unspent outputs.
func (r FutureListLockUnspentResult) Receive() ([]*wire.OutPoint, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal as an array of transaction inputs.
var inputs []dcrjson.TransactionInput
err = json.Unmarshal(res, &inputs)
if err != nil {
return nil, err
}
// Create a slice of outpoints from the transaction input structs.
ops := make([]*wire.OutPoint, len(inputs))
for i, input := range inputs {
sha, err := chainhash.NewHashFromStr(input.Txid)
if err != nil {
return nil, err
}
ops[i] = wire.NewOutPoint(sha, input.Vout, input.Tree) // Decred TODO
}
return ops, nil
}
// ListLockUnspentAsync 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 ListLockUnspent for the blocking version and more details.
func (c *Client) ListLockUnspentAsync() FutureListLockUnspentResult {
cmd := dcrjson.NewListLockUnspentCmd()
return c.sendCmd(cmd)
}
// ListLockUnspent returns a slice of outpoints for all unspent outputs marked
// as locked by a wallet. Unspent outputs may be marked locked using
// LockOutput.
func (c *Client) ListLockUnspent() ([]*wire.OutPoint, error) {
return c.ListLockUnspentAsync().Receive()
}
// FutureSendToAddressResult is a future promise to deliver the result of a
// SendToAddressAsync RPC invocation (or an applicable error).
type FutureSendToAddressResult chan *response
// Receive waits for the response promised by the future and returns the hash
// of the transaction sending the passed amount to the given address.
func (r FutureSendToAddressResult) Receive() (*chainhash.Hash, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal result as a string.
var txHash string
err = json.Unmarshal(res, &txHash)
if err != nil {
return nil, err
}
return chainhash.NewHashFromStr(txHash)
}
// SendToAddressAsync 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 SendToAddress for the blocking version and more details.
func (c *Client) SendToAddressAsync(address dcrutil.Address, amount dcrutil.Amount) FutureSendToAddressResult {
addr := address.EncodeAddress()
cmd := dcrjson.NewSendToAddressCmd(addr, amount.ToCoin(), nil, nil)
return c.sendCmd(cmd)
}
// SendToAddress sends the passed amount to the given address.
//
// See SendToAddressComment to associate comments with the transaction in the
// wallet. The comments are not part of the transaction and are only internal
// to the wallet.
//
// NOTE: This function requires to the wallet to be unlocked. See the
// WalletPassphrase function for more details.
func (c *Client) SendToAddress(address dcrutil.Address, amount dcrutil.Amount) (*chainhash.Hash, error) {
return c.SendToAddressAsync(address, amount).Receive()
}
// SendToAddressCommentAsync 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 SendToAddressComment for the blocking version and more details.
func (c *Client) SendToAddressCommentAsync(address dcrutil.Address,
amount dcrutil.Amount, comment,
commentTo string) FutureSendToAddressResult {
addr := address.EncodeAddress()
cmd := dcrjson.NewSendToAddressCmd(addr, amount.ToCoin(), &comment,
&commentTo)
return c.sendCmd(cmd)
}
// SendToAddressComment sends the passed amount to the given address and stores
// the provided comment and comment to in the wallet. The comment parameter is
// intended to be used for the purpose of the transaction while the commentTo
// parameter is indended to be used for who the transaction is being sent to.
//
// The comments are not part of the transaction and are only internal
// to the wallet.
//
// See SendToAddress to avoid using comments.
//
// NOTE: This function requires to the wallet to be unlocked. See the
// WalletPassphrase function for more details.
func (c *Client) SendToAddressComment(address dcrutil.Address, amount dcrutil.Amount, comment, commentTo string) (*chainhash.Hash, error) {
return c.SendToAddressCommentAsync(address, amount, comment,
commentTo).Receive()
}
// FutureSendFromResult is a future promise to deliver the result of a
// SendFromAsync, SendFromMinConfAsync, or SendFromCommentAsync RPC invocation
// (or an applicable error).
type FutureSendFromResult chan *response
// Receive waits for the response promised by the future and returns the hash
// of the transaction sending amount to the given address using the provided
// account as a source of funds.
func (r FutureSendFromResult) Receive() (*chainhash.Hash, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal result as a string.
var txHash string
err = json.Unmarshal(res, &txHash)
if err != nil {
return nil, err
}
return chainhash.NewHashFromStr(txHash)
}
// SendFromAsync 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 SendFrom for the blocking version and more details.
func (c *Client) SendFromAsync(fromAccount string, toAddress dcrutil.Address, amount dcrutil.Amount) FutureSendFromResult {
addr := toAddress.EncodeAddress()
cmd := dcrjson.NewSendFromCmd(fromAccount, addr, amount.ToCoin(), nil,
nil, nil)
return c.sendCmd(cmd)
}
// SendFrom sends the passed amount to the given address using the provided
// account as a source of funds. Only funds with the default number of minimum
// confirmations will be used.
//
// See SendFromMinConf and SendFromComment for different options.
//
// NOTE: This function requires to the wallet to be unlocked. See the
// WalletPassphrase function for more details.
func (c *Client) SendFrom(fromAccount string, toAddress dcrutil.Address, amount dcrutil.Amount) (*chainhash.Hash, error) {
return c.SendFromAsync(fromAccount, toAddress, amount).Receive()
}
// SendFromMinConfAsync 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 SendFromMinConf for the blocking version and more details.
func (c *Client) SendFromMinConfAsync(fromAccount string, toAddress dcrutil.Address, amount dcrutil.Amount, minConfirms int) FutureSendFromResult {
addr := toAddress.EncodeAddress()
cmd := dcrjson.NewSendFromCmd(fromAccount, addr, amount.ToCoin(),
&minConfirms, nil, nil)
return c.sendCmd(cmd)
}
// SendFromMinConf sends the passed amount to the given address using the
// provided account as a source of funds. Only funds with the passed number of
// minimum confirmations will be used.
//
// See SendFrom to use the default number of minimum confirmations and
// SendFromComment for additional options.
//
// NOTE: This function requires to the wallet to be unlocked. See the
// WalletPassphrase function for more details.
func (c *Client) SendFromMinConf(fromAccount string, toAddress dcrutil.Address, amount dcrutil.Amount, minConfirms int) (*chainhash.Hash, error) {
return c.SendFromMinConfAsync(fromAccount, toAddress, amount,
minConfirms).Receive()
}
// SendFromCommentAsync 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 SendFromComment for the blocking version and more details.
func (c *Client) SendFromCommentAsync(fromAccount string,
toAddress dcrutil.Address, amount dcrutil.Amount, minConfirms int,
comment, commentTo string) FutureSendFromResult {
addr := toAddress.EncodeAddress()
cmd := dcrjson.NewSendFromCmd(fromAccount, addr, amount.ToCoin(),
&minConfirms, &comment, &commentTo)
return c.sendCmd(cmd)
}
// SendFromComment sends the passed amount to the given address using the
// provided account as a source of funds and stores the provided comment and
// comment to in the wallet. The comment parameter is intended to be used for
// the purpose of the transaction while the commentTo parameter is indended to
// be used for who the transaction is being sent to. Only funds with the passed
// number of minimum confirmations will be used.
//
// See SendFrom and SendFromMinConf to use defaults.
//
// NOTE: This function requires to the wallet to be unlocked. See the
// WalletPassphrase function for more details.
func (c *Client) SendFromComment(fromAccount string, toAddress dcrutil.Address,
amount dcrutil.Amount, minConfirms int,
comment, commentTo string) (*chainhash.Hash, error) {
return c.SendFromCommentAsync(fromAccount, toAddress, amount,
minConfirms, comment, commentTo).Receive()
}
// FutureSendManyResult is a future promise to deliver the result of a
// SendManyAsync, SendManyMinConfAsync, or SendManyCommentAsync RPC invocation
// (or an applicable error).
type FutureSendManyResult chan *response
// Receive waits for the response promised by the future and returns the hash
// of the transaction sending multiple amounts to multiple addresses using the
// provided account as a source of funds.
func (r FutureSendManyResult) Receive() (*chainhash.Hash, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmashal result as a string.
var txHash string
err = json.Unmarshal(res, &txHash)
if err != nil {
return nil, err
}
return chainhash.NewHashFromStr(txHash)
}
// SendManyAsync 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 SendMany for the blocking version and more details.
func (c *Client) SendManyAsync(fromAccount string, amounts map[dcrutil.Address]dcrutil.Amount) FutureSendManyResult {
convertedAmounts := make(map[string]float64, len(amounts))
for addr, amount := range amounts {
convertedAmounts[addr.EncodeAddress()] = amount.ToCoin()
}
cmd := dcrjson.NewSendManyCmd(fromAccount, convertedAmounts, nil, nil)
return c.sendCmd(cmd)
}
// SendMany 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.
//
// See SendManyMinConf and SendManyComment for different options.
//
// NOTE: This function requires to the wallet to be unlocked. See the
// WalletPassphrase function for more details.
func (c *Client) SendMany(fromAccount string, amounts map[dcrutil.Address]dcrutil.Amount) (*chainhash.Hash, error) {
return c.SendManyAsync(fromAccount, amounts).Receive()
}
// SendManyMinConfAsync 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 SendManyMinConf for the blocking version and more details.
func (c *Client) SendManyMinConfAsync(fromAccount string,
amounts map[dcrutil.Address]dcrutil.Amount,
minConfirms int) FutureSendManyResult {
convertedAmounts := make(map[string]float64, len(amounts))
for addr, amount := range amounts {
convertedAmounts[addr.EncodeAddress()] = amount.ToCoin()
}
cmd := dcrjson.NewSendManyCmd(fromAccount, convertedAmounts,
&minConfirms, nil)
return c.sendCmd(cmd)
}
// SendManyMinConf sends multiple amounts to multiple addresses using the
// provided account as a source of funds in a single transaction. Only funds
// with the passed number of minimum confirmations will be used.
//
// See SendMany to use the default number of minimum confirmations and
// SendManyComment for additional options.
//
// NOTE: This function requires to the wallet to be unlocked. See the
// WalletPassphrase function for more details.
func (c *Client) SendManyMinConf(fromAccount string,
amounts map[dcrutil.Address]dcrutil.Amount,
minConfirms int) (*chainhash.Hash, error) {
return c.SendManyMinConfAsync(fromAccount, amounts, minConfirms).Receive()
}
// SendManyCommentAsync 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 SendManyComment for the blocking version and more details.
func (c *Client) SendManyCommentAsync(fromAccount string,
amounts map[dcrutil.Address]dcrutil.Amount, minConfirms int,
comment string) FutureSendManyResult {
convertedAmounts := make(map[string]float64, len(amounts))
for addr, amount := range amounts {
convertedAmounts[addr.EncodeAddress()] = amount.ToCoin()
}
cmd := dcrjson.NewSendManyCmd(fromAccount, convertedAmounts,
&minConfirms, &comment)
return c.sendCmd(cmd)
}
// SendManyComment sends multiple amounts to multiple addresses using the
// provided account as a source of funds in a single transaction and stores the
// provided comment in the wallet. The comment parameter is intended to be used
// for the purpose of the transaction Only funds with the passed number of
// minimum confirmations will be used.
//
// See SendMany and SendManyMinConf to use defaults.
//
// NOTE: This function requires to the wallet to be unlocked. See the
// WalletPassphrase function for more details.
func (c *Client) SendManyComment(fromAccount string,
amounts map[dcrutil.Address]dcrutil.Amount, minConfirms int,
comment string) (*chainhash.Hash, error) {
return c.SendManyCommentAsync(fromAccount, amounts, minConfirms,
comment).Receive()
}
// Begin DECRED FUNCTIONS ---------------------------------------------------------
//
// SStx generation RPC call handling
// FuturePurchaseTicketResult a channel for the response promised by the future.
type FuturePurchaseTicketResult chan *response
// Receive waits for the response promised by the future and returns the hash
// of the transaction sending multiple amounts to multiple addresses using the
// provided account as a source of funds.
func (r FuturePurchaseTicketResult) Receive() ([]*chainhash.Hash, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmashal result as a string slice.
var txHashesStr []string
err = json.Unmarshal(res, &txHashesStr)
if err != nil {
return nil, err
}
txHashes := make([]*chainhash.Hash, len(txHashesStr))
for i := range txHashesStr {
h, err := chainhash.NewHashFromStr(txHashesStr[i])
if err != nil {
return nil, err
}
txHashes[i] = h
}
return txHashes, nil
}
// PurchaseTicketAsync takes an account and a spending limit and returns a
// future chan to recieve the transactions representing the purchased tickets
// when they come.
func (c *Client) PurchaseTicketAsync(fromAccount string,
spendLimit dcrutil.Amount, minConf *int, ticketAddress dcrutil.Address,
numTickets *int, poolAddress dcrutil.Address, poolFees *dcrutil.Amount,
expiry *int) FuturePurchaseTicketResult {
// An empty string is used to keep the sendCmd
// passing of the command from accidentally
// removing certain fields. We fill in the
// default values of the other arguments as
// well for the same reason.
minConfVal := 1
if minConf != nil {
minConfVal = *minConf
}
ticketAddrStr := ""
if ticketAddress != nil {
ticketAddrStr = ticketAddress.EncodeAddress()
}
numTicketsVal := 1
if numTickets != nil {
numTicketsVal = *numTickets
}
poolAddrStr := ""
if poolAddress != nil {
poolAddrStr = poolAddress.EncodeAddress()
}
poolFeesFloat := 0.0
if poolFees != nil {
poolFeesFloat = poolFees.ToCoin()
}
expiryVal := 0
if expiry != nil {
expiryVal = *expiry
}
cmd := dcrjson.NewPurchaseTicketCmd(fromAccount, spendLimit.ToCoin(),
&minConfVal, &ticketAddrStr, &numTicketsVal, &poolAddrStr,
&poolFeesFloat, &expiryVal, nil)
return c.sendCmd(cmd)
}
// PurchaseTicket takes an account and a spending limit and calls the async
// puchasetickets command.
func (c *Client) PurchaseTicket(fromAccount string,
spendLimit dcrutil.Amount, minConf *int, ticketAddress dcrutil.Address,
numTickets *int, poolAddress dcrutil.Address, poolFees *dcrutil.Amount,
expiry *int) ([]*chainhash.Hash, error) {
return c.PurchaseTicketAsync(fromAccount, spendLimit, minConf, ticketAddress,
numTickets, poolAddress, poolFees, expiry).Receive()
}
// SStx generation RPC call handling
// FutureSendToSStxResult is a future promise to deliver the result of a
// SendToSStxAsync, SendToSStxMinConfAsync, or SendToSStxCommentAsync RPC
// invocation (or an applicable error).
type FutureSendToSStxResult chan *response
// Receive waits for the response promised by the future and returns the hash
// of the transaction sending multiple amounts to multiple addresses using the
// provided account as a source of funds.
func (r FutureSendToSStxResult) Receive() (*chainhash.Hash, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmashal result as a string.
var txHash string
err = json.Unmarshal(res, &txHash)
if err != nil {
return nil, err
}
return chainhash.NewHashFromStr(txHash)
}
// SendToSStxAsync 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 SendToSStx for the blocking version and more details.
func (c *Client) SendToSStxAsync(fromAccount string,
inputs []dcrjson.SStxInput,
amounts map[dcrutil.Address]dcrutil.Amount,
couts []SStxCommitOut) FutureSendToSStxResult {
convertedAmounts := make(map[string]int64, len(amounts))
for addr, amount := range amounts {
convertedAmounts[addr.EncodeAddress()] = int64(amount)
}
convertedCouts := make([]dcrjson.SStxCommitOut, len(couts))
for i, cout := range couts {
convertedCouts[i].Addr = cout.Addr.String()
convertedCouts[i].CommitAmt = int64(cout.CommitAmt)
convertedCouts[i].ChangeAddr = cout.ChangeAddr.String()
convertedCouts[i].ChangeAmt = int64(cout.ChangeAmt)
}
cmd := dcrjson.NewSendToSStxCmd(fromAccount, convertedAmounts,
inputs, convertedCouts, nil, nil)
return c.sendCmd(cmd)
}
// SendToSStx purchases a stake ticket with the amounts specified, with
// block generation rewards going to the dests specified. Only funds with the
// default number of minimum confirmations will be used.
//
// See SendToSStxMinConf and SendToSStxComment for different options.
//
// NOTE: This function requires to the wallet to be unlocked. See the
// WalletPassphrase function for more details.
func (c *Client) SendToSStx(fromAccount string,
inputs []dcrjson.SStxInput,
amounts map[dcrutil.Address]dcrutil.Amount,
couts []SStxCommitOut) (*chainhash.Hash, error) {
return c.SendToSStxAsync(fromAccount, inputs, amounts, couts).Receive()
}
// SendToSStxMinConfAsync 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 SendToSStxMinConf for the blocking version and more details.
func (c *Client) SendToSStxMinConfAsync(fromAccount string,
inputs []dcrjson.SStxInput,
amounts map[dcrutil.Address]dcrutil.Amount,
couts []SStxCommitOut,
minConfirms int) FutureSendToSStxResult {
convertedAmounts := make(map[string]int64, len(amounts))
for addr, amount := range amounts {
convertedAmounts[addr.EncodeAddress()] = int64(amount)
}
convertedCouts := make([]dcrjson.SStxCommitOut, len(couts))
for i, cout := range couts {
convertedCouts[i].Addr = cout.Addr.String()
convertedCouts[i].CommitAmt = int64(cout.CommitAmt)
convertedCouts[i].ChangeAddr = cout.ChangeAddr.String()
convertedCouts[i].ChangeAmt = int64(cout.ChangeAmt)
}
cmd := dcrjson.NewSendToSStxCmd(fromAccount, convertedAmounts,
inputs, convertedCouts, &minConfirms, nil)
return c.sendCmd(cmd)
}
// SendToSStxMinConf purchases a stake ticket with the amounts specified, with
// block generation rewards going to the dests specified. Only funds with the
// passed number of minimum confirmations will be used.
//
// See SendToSStx to use the default number of minimum confirmations and
// SendToSStxComment for additional options.
//
// NOTE: This function requires to the wallet to be unlocked. See the
// WalletPassphrase function for more details.
func (c *Client) SendToSStxMinConf(fromAccount string,
inputs []dcrjson.SStxInput,
amounts map[dcrutil.Address]dcrutil.Amount,
couts []SStxCommitOut,
minConfirms int) (*chainhash.Hash, error) {
return c.SendToSStxMinConfAsync(fromAccount, inputs, amounts, couts, minConfirms).Receive()
}
// SendToSStxCommentAsync 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 SendToSStxComment for the blocking version and more details.
func (c *Client) SendToSStxCommentAsync(fromAccount string,
inputs []dcrjson.SStxInput,
amounts map[dcrutil.Address]dcrutil.Amount,
couts []SStxCommitOut,
minConfirms int,
comment string) FutureSendToSStxResult {
convertedAmounts := make(map[string]int64, len(amounts))
for addr, amount := range amounts {
convertedAmounts[addr.EncodeAddress()] = int64(amount)
}
convertedCouts := make([]dcrjson.SStxCommitOut, len(couts))
for i, cout := range couts {
convertedCouts[i].Addr = cout.Addr.String()
convertedCouts[i].CommitAmt = int64(cout.CommitAmt)
convertedCouts[i].ChangeAddr = cout.ChangeAddr.String()
convertedCouts[i].ChangeAmt = int64(cout.ChangeAmt)
}
cmd := dcrjson.NewSendToSStxCmd(fromAccount, convertedAmounts,
inputs, convertedCouts, &minConfirms, &comment)
return c.sendCmd(cmd)
}
// SendToSStxComment spurchases a stake ticket with the amounts specified, with
// block generation rewards going to the dests specified and stores an extra
// comment in the wallet. The comment parameter is intended to be used
// for the purpose of the transaction Only funds with the passed number of
// minimum confirmations will be used.
//
// See SendToSStx and SendToSStxMinConf to use defaults.
//
// NOTE: This function requires to the wallet to be unlocked. See the
// WalletPassphrase function for more details.
func (c *Client) SendToSStxComment(fromAccount string,
inputs []dcrjson.SStxInput,
amounts map[dcrutil.Address]dcrutil.Amount,
couts []SStxCommitOut,
minConfirms int,
comment string) (*chainhash.Hash, error) {
return c.SendToSStxCommentAsync(fromAccount, inputs, amounts, couts, minConfirms,
comment).Receive()
}
// --------------------------------------------------------------------------------
// SSGen generation RPC call handling
// FutureSendToSSGenResult is a future promise to deliver the result of a
// SendToSSGenAsync, or SendToSSGenCommentAsync RPC invocation (or an applicable
// error).
type FutureSendToSSGenResult chan *response
// Receive waits for the response promised by the future and returns the hash
// of the transaction sending multiple amounts to multiple addresses using the
// provided account as a source of funds.
func (r FutureSendToSSGenResult) Receive() (*chainhash.Hash, error) {
res, err := receiveFuture(r)