forked from btcsuite/btcwallet
-
Notifications
You must be signed in to change notification settings - Fork 3
/
wallet.go
2163 lines (1934 loc) · 62.1 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) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wallet
import (
"bytes"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"sync"
"time"
"github.com/phoreproject/btcd/blockchain"
"github.com/phoreproject/btcd/btcec"
"github.com/phoreproject/btcd/btcjson"
"github.com/phoreproject/btcd/chaincfg"
"github.com/phoreproject/btcd/chaincfg/chainhash"
"github.com/phoreproject/btcd/txscript"
"github.com/phoreproject/btcd/wire"
"github.com/btcsuite/btcrpcclient"
"github.com/phoreproject/btcutil"
"github.com/phoreproject/btcutil/hdkeychain"
"github.com/phoreproject/btcwallet/chain"
"github.com/phoreproject/btcwallet/waddrmgr"
"github.com/phoreproject/btcwallet/wallet/txauthor"
"github.com/phoreproject/btcwallet/wallet/txrules"
"github.com/phoreproject/btcwallet/walletdb"
"github.com/phoreproject/btcwallet/wtxmgr"
)
const (
// InsecurePubPassphrase is the default outer encryption passphrase used
// for public data (everything but private keys). Using a non-default
// public passphrase can prevent an attacker without the public
// passphrase from discovering all past and future wallet addresses if
// they gain access to the wallet database.
//
// NOTE: at time of writing, public encryption only applies to public
// data in the waddrmgr namespace. Transactions are not yet encrypted.
InsecurePubPassphrase = "public"
walletDbWatchingOnlyName = "wowallet.db"
)
// ErrNotSynced describes an error where an operation cannot complete
// due wallet being out of sync (and perhaps currently syncing with)
// the remote chain server.
var ErrNotSynced = errors.New("wallet is not synchronized with the chain server")
// Namespace bucket keys.
var (
waddrmgrNamespaceKey = []byte("waddrmgr")
wtxmgrNamespaceKey = []byte("wtxmgr")
)
// Wallet is a structure containing all the components for a
// complete wallet. It contains the Armory-style key store
// addresses and keys),
type Wallet struct {
publicPassphrase []byte
// Data stores
db walletdb.DB
Manager *waddrmgr.Manager
TxStore *wtxmgr.Store
chainClient *chain.RPCClient
chainClientLock sync.Mutex
chainClientSynced bool
chainClientSyncMtx sync.Mutex
lockedOutpoints map[wire.OutPoint]struct{}
relayFee btcutil.Amount
relayFeeMu sync.Mutex
// Channels for rescan processing. Requests are added and merged with
// any waiting requests, before being sent to another goroutine to
// call the rescan RPC.
rescanAddJob chan *RescanJob
rescanBatch chan *rescanBatch
rescanNotifications chan interface{} // From chain server
rescanProgress chan *RescanProgressMsg
rescanFinished chan *RescanFinishedMsg
// Channel for transaction creation requests.
createTxRequests chan createTxRequest
// Channels for the manager locker.
unlockRequests chan unlockRequest
lockRequests chan struct{}
holdUnlockRequests chan chan HeldUnlock
lockState chan bool
changePassphrase chan changePassphraseRequest
NtfnServer *NotificationServer
chainParams *chaincfg.Params
wg sync.WaitGroup
started bool
quit chan struct{}
quitMu sync.Mutex
}
// Start starts the goroutines necessary to manage a wallet.
func (w *Wallet) Start() {
w.quitMu.Lock()
select {
case <-w.quit:
// Restart the wallet goroutines after shutdown finishes.
w.WaitForShutdown()
w.quit = make(chan struct{})
default:
// Ignore when the wallet is still running.
if w.started {
w.quitMu.Unlock()
return
}
w.started = true
}
w.quitMu.Unlock()
w.wg.Add(2)
go w.txCreator()
go w.walletLocker()
}
// SynchronizeRPC associates the wallet with the consensus RPC client,
// synchronizes the wallet with the latest changes to the blockchain, and
// continuously updates the wallet through RPC notifications.
//
// This method is unstable and will be removed when all syncing logic is moved
// outside of the wallet package.
func (w *Wallet) SynchronizeRPC(chainClient *chain.RPCClient) {
w.quitMu.Lock()
select {
case <-w.quit:
w.quitMu.Unlock()
return
default:
}
w.quitMu.Unlock()
// TODO: Ignoring the new client when one is already set breaks callers
// who are replacing the client, perhaps after a disconnect.
w.chainClientLock.Lock()
if w.chainClient != nil {
w.chainClientLock.Unlock()
return
}
w.chainClient = chainClient
w.chainClientLock.Unlock()
// TODO: It would be preferable to either run these goroutines
// separately from the wallet (use wallet mutator functions to
// make changes from the RPC client) and not have to stop and
// restart them each time the client disconnects and reconnets.
w.wg.Add(4)
go w.handleChainNotifications()
go w.rescanBatchHandler()
go w.rescanProgressHandler()
go w.rescanRPCHandler()
}
// requireChainClient marks that a wallet method can only be completed when the
// consensus RPC server is set. This function and all functions that call it
// are unstable and will need to be moved when the syncing code is moved out of
// the wallet.
func (w *Wallet) requireChainClient() (*chain.RPCClient, error) {
w.chainClientLock.Lock()
chainClient := w.chainClient
w.chainClientLock.Unlock()
if chainClient == nil {
return nil, errors.New("blockchain RPC is inactive")
}
return chainClient, nil
}
// ChainClient returns the optional consensus RPC client associated with the
// wallet.
//
// This function is unstable and will be removed once sync logic is moved out of
// the wallet.
func (w *Wallet) ChainClient() *chain.RPCClient {
w.chainClientLock.Lock()
chainClient := w.chainClient
w.chainClientLock.Unlock()
return chainClient
}
// RelayFee returns the current minimum relay fee (per kB of serialized
// transaction) used when constructing transactions.
func (w *Wallet) RelayFee() btcutil.Amount {
w.relayFeeMu.Lock()
relayFee := w.relayFee
w.relayFeeMu.Unlock()
return relayFee
}
// SetRelayFee sets a new minimum relay fee (per kB of serialized
// transaction) used when constructing transactions.
func (w *Wallet) SetRelayFee(relayFee btcutil.Amount) {
w.relayFeeMu.Lock()
w.relayFee = relayFee
w.relayFeeMu.Unlock()
}
// quitChan atomically reads the quit channel.
func (w *Wallet) quitChan() <-chan struct{} {
w.quitMu.Lock()
c := w.quit
w.quitMu.Unlock()
return c
}
// Stop signals all wallet goroutines to shutdown.
func (w *Wallet) Stop() {
w.quitMu.Lock()
quit := w.quit
w.quitMu.Unlock()
select {
case <-quit:
default:
close(quit)
w.chainClientLock.Lock()
if w.chainClient != nil {
w.chainClient.Stop()
w.chainClient = nil
}
w.chainClientLock.Unlock()
}
}
// ShuttingDown returns whether the wallet is currently in the process of
// shutting down or not.
func (w *Wallet) ShuttingDown() bool {
select {
case <-w.quitChan():
return true
default:
return false
}
}
// WaitForShutdown blocks until all wallet goroutines have finished executing.
func (w *Wallet) WaitForShutdown() {
w.chainClientLock.Lock()
if w.chainClient != nil {
w.chainClient.WaitForShutdown()
}
w.chainClientLock.Unlock()
w.wg.Wait()
}
// SynchronizingToNetwork returns whether the wallet is currently synchronizing
// with the Bitcoin network.
func (w *Wallet) SynchronizingToNetwork() bool {
// At the moment, RPC is the only synchronization method. In the
// future, when SPV is added, a separate check will also be needed, or
// SPV could always be enabled if RPC was not explicitly specified when
// creating the wallet.
w.chainClientSyncMtx.Lock()
syncing := w.chainClient != nil
w.chainClientSyncMtx.Unlock()
return syncing
}
// ChainSynced returns whether the wallet has been attached to a chain server
// and synced up to the best block on the main chain.
func (w *Wallet) ChainSynced() bool {
w.chainClientSyncMtx.Lock()
synced := w.chainClientSynced
w.chainClientSyncMtx.Unlock()
return synced
}
// SetChainSynced marks whether the wallet is connected to and currently in sync
// with the latest block notified by the chain server.
//
// NOTE: Due to an API limitation with btcrpcclient, this may return true after
// the client disconnected (and is attempting a reconnect). This will be unknown
// until the reconnect notification is received, at which point the wallet can be
// marked out of sync again until after the next rescan completes.
func (w *Wallet) SetChainSynced(synced bool) {
w.chainClientSyncMtx.Lock()
w.chainClientSynced = synced
w.chainClientSyncMtx.Unlock()
}
// activeData returns the currently-active receiving addresses and all unspent
// outputs. This is primarely intended to provide the parameters for a
// rescan request.
func (w *Wallet) activeData() ([]btcutil.Address, []wtxmgr.Credit, error) {
var addrs []btcutil.Address
err := w.Manager.ForEachActiveAddress(func(addr btcutil.Address) error {
addrs = append(addrs, addr)
return nil
})
if err != nil {
return nil, nil, err
}
unspent, err := w.TxStore.UnspentOutputs()
return addrs, unspent, err
}
// syncWithChain brings the wallet up to date with the current chain server
// connection. It creates a rescan request and blocks until the rescan has
// finished.
//
func (w *Wallet) syncWithChain() error {
chainClient, err := w.requireChainClient()
if err != nil {
return err
}
// Request notifications for connected and disconnected blocks.
//
// TODO(jrick): Either request this notification only once, or when
// btcrpcclient is modified to allow some notification request to not
// automatically resent on reconnect, include the notifyblocks request
// as well. I am leaning towards allowing off all btcrpcclient
// notification re-registrations, in which case the code here should be
// left as is.
err = chainClient.NotifyBlocks()
if err != nil {
return err
}
// Request notifications for transactions sending to all wallet
// addresses.
addrs, unspent, err := w.activeData()
if err != nil {
return err
}
// TODO(jrick): How should this handle a synced height earlier than
// the chain server best block?
// When no addresses have been generated for the wallet, the rescan can
// be skipped.
//
// TODO: This is only correct because activeData above returns all
// addresses ever created, including those that don't need to be watched
// anymore. This code should be updated when this assumption is no
// longer true, but worst case would result in an unnecessary rescan.
if len(addrs) == 0 && len(unspent) == 0 {
// TODO: It would be ideal if on initial sync wallet saved the
// last several recent blocks rather than just one. This would
// avoid a full rescan for a one block reorg of the current
// chain tip.
hash, height, err := chainClient.GetBestBlock()
if err != nil {
return err
}
return w.Manager.SetSyncedTo(&waddrmgr.BlockStamp{
Hash: *hash,
Height: height,
})
}
// Compare previously-seen blocks against the chain server. If any of
// these blocks no longer exist, rollback all of the missing blocks
// before catching up with the rescan.
iter := w.Manager.NewIterateRecentBlocks()
rollback := iter == nil
syncBlock := waddrmgr.BlockStamp{
Hash: *w.chainParams.GenesisHash,
Height: 0,
}
for cont := iter != nil; cont; cont = iter.Prev() {
bs := iter.BlockStamp()
log.Debugf("Checking for previous saved block with height %v hash %v",
bs.Height, bs.Hash)
_, err = chainClient.GetBlock(&bs.Hash)
if err != nil {
rollback = true
continue
}
log.Debug("Found matching block.")
syncBlock = bs
break
}
if rollback {
err = w.Manager.SetSyncedTo(&syncBlock)
if err != nil {
return err
}
// Rollback unconfirms transactions at and beyond the passed
// height, so add one to the new synced-to height to prevent
// unconfirming txs from the synced-to block.
err = w.TxStore.Rollback(syncBlock.Height + 1)
if err != nil {
return err
}
}
return w.Rescan(addrs, unspent)
}
type (
createTxRequest struct {
account uint32
outputs []*wire.TxOut
minconf int32
resp chan createTxResponse
}
createTxResponse struct {
tx *txauthor.AuthoredTx
err error
}
)
// txCreator is responsible for the input selection and creation of
// transactions. These functions are the responsibility of this method
// (designed to be run as its own goroutine) since input selection must be
// serialized, or else it is possible to create double spends by choosing the
// same inputs for multiple transactions. Along with input selection, this
// method is also responsible for the signing of transactions, since we don't
// want to end up in a situation where we run out of inputs as multiple
// transactions are being created. In this situation, it would then be possible
// for both requests, rather than just one, to fail due to not enough available
// inputs.
func (w *Wallet) txCreator() {
quit := w.quitChan()
out:
for {
select {
case txr := <-w.createTxRequests:
tx, err := w.txToOutputs(txr.outputs, txr.account, txr.minconf)
txr.resp <- createTxResponse{tx, err}
case <-quit:
break out
}
}
w.wg.Done()
}
// CreateSimpleTx creates a new signed transaction spending unspent P2PKH
// outputs with at laest minconf confirmations spending to any number of
// address/amount pairs. Change and an appropriate transaction fee are
// automatically included, if necessary. All transaction creation through this
// function is serialized to prevent the creation of many transactions which
// spend the same outputs.
func (w *Wallet) CreateSimpleTx(account uint32, outputs []*wire.TxOut,
minconf int32) (*txauthor.AuthoredTx, error) {
req := createTxRequest{
account: account,
outputs: outputs,
minconf: minconf,
resp: make(chan createTxResponse),
}
w.createTxRequests <- req
resp := <-req.resp
return resp.tx, resp.err
}
type (
unlockRequest struct {
passphrase []byte
lockAfter <-chan time.Time // nil prevents the timeout.
err chan error
}
changePassphraseRequest struct {
old, new []byte
err chan error
}
// HeldUnlock is a tool to prevent the wallet from automatically
// locking after some timeout before an operation which needed
// the unlocked wallet has finished. Any aquired HeldUnlock
// *must* be released (preferably with a defer) or the wallet
// will forever remain unlocked.
HeldUnlock chan struct{}
)
// walletLocker manages the locked/unlocked state of a wallet.
func (w *Wallet) walletLocker() {
var timeout <-chan time.Time
holdChan := make(HeldUnlock)
quit := w.quitChan()
out:
for {
select {
case req := <-w.unlockRequests:
err := w.Manager.Unlock(req.passphrase)
if err != nil {
req.err <- err
continue
}
timeout = req.lockAfter
if timeout == nil {
log.Info("The wallet has been unlocked without a time limit")
} else {
log.Info("The wallet has been temporarily unlocked")
}
req.err <- nil
continue
case req := <-w.changePassphrase:
err := w.Manager.ChangePassphrase(req.old, req.new, true,
&waddrmgr.DefaultScryptOptions)
req.err <- err
continue
case req := <-w.holdUnlockRequests:
if w.Manager.IsLocked() {
close(req)
continue
}
req <- holdChan
<-holdChan // Block until the lock is released.
// If, after holding onto the unlocked wallet for some
// time, the timeout has expired, lock it now instead
// of hoping it gets unlocked next time the top level
// select runs.
select {
case <-timeout:
// Let the top level select fallthrough so the
// wallet is locked.
default:
continue
}
case w.lockState <- w.Manager.IsLocked():
continue
case <-quit:
break out
case <-w.lockRequests:
case <-timeout:
}
// Select statement fell through by an explicit lock or the
// timer expiring. Lock the manager here.
timeout = nil
err := w.Manager.Lock()
if err != nil && !waddrmgr.IsError(err, waddrmgr.ErrLocked) {
log.Errorf("Could not lock wallet: %v", err)
} else {
log.Info("The wallet has been locked")
}
}
w.wg.Done()
}
// Unlock unlocks the wallet's address manager and relocks it after timeout has
// expired. If the wallet is already unlocked and the new passphrase is
// correct, the current timeout is replaced with the new one. The wallet will
// be locked if the passphrase is incorrect or any other error occurs during the
// unlock.
func (w *Wallet) Unlock(passphrase []byte, lock <-chan time.Time) error {
err := make(chan error, 1)
w.unlockRequests <- unlockRequest{
passphrase: passphrase,
lockAfter: lock,
err: err,
}
return <-err
}
// Lock locks the wallet's address manager.
func (w *Wallet) Lock() {
w.lockRequests <- struct{}{}
}
// Locked returns whether the account manager for a wallet is locked.
func (w *Wallet) Locked() bool {
return <-w.lockState
}
// HoldUnlock prevents the wallet from being locked. The HeldUnlock object
// *must* be released, or the wallet will forever remain unlocked.
//
// TODO: To prevent the above scenario, perhaps closures should be passed
// to the walletLocker goroutine and disallow callers from explicitly
// handling the locking mechanism.
func (w *Wallet) HoldUnlock() (HeldUnlock, error) {
req := make(chan HeldUnlock)
w.holdUnlockRequests <- req
hl, ok := <-req
if !ok {
// TODO(davec): This should be defined and exported from
// waddrmgr.
return nil, waddrmgr.ManagerError{
ErrorCode: waddrmgr.ErrLocked,
Description: "address manager is locked",
}
}
return hl, nil
}
// Release releases the hold on the unlocked-state of the wallet and allows the
// wallet to be locked again. If a lock timeout has already expired, the
// wallet is locked again as soon as Release is called.
func (c HeldUnlock) Release() {
c <- struct{}{}
}
// ChangePassphrase attempts to change the passphrase for a wallet from old
// to new. Changing the passphrase is synchronized with all other address
// manager locking and unlocking. The lock state will be the same as it was
// before the password change.
func (w *Wallet) ChangePassphrase(old, new []byte) error {
err := make(chan error, 1)
w.changePassphrase <- changePassphraseRequest{
old: old,
new: new,
err: err,
}
return <-err
}
// AccountUsed returns whether there are any recorded transactions spending to
// a given account. It returns true if atleast one address in the account was
// used and false if no address in the account was used.
func (w *Wallet) AccountUsed(account uint32) (bool, error) {
var used bool
var err error
merr := w.Manager.ForEachAccountAddress(account,
func(maddr waddrmgr.ManagedAddress) error {
used, err = maddr.Used()
if err != nil {
return err
}
if used {
return waddrmgr.Break
}
return nil
})
if merr == waddrmgr.Break {
merr = nil
}
return used, merr
}
// CalculateBalance sums the amounts of all unspent transaction
// outputs to addresses of a wallet and returns the balance.
//
// If confirmations is 0, all UTXOs, even those not present in a
// block (height -1), will be used to get the balance. Otherwise,
// a UTXO must be in a block. If confirmations is 1 or greater,
// the balance will be calculated based on how many how many blocks
// include a UTXO.
func (w *Wallet) CalculateBalance(confirms int32) (btcutil.Amount, error) {
blk := w.Manager.SyncedTo()
return w.TxStore.Balance(confirms, blk.Height)
}
// Balances records total, spendable (by policy), and immature coinbase
// reward balance amounts.
type Balances struct {
Total btcutil.Amount
Spendable btcutil.Amount
ImmatureReward btcutil.Amount
}
// CalculateAccountBalances sums the amounts of all unspent transaction
// outputs to the given account of a wallet and returns the balance.
//
// This function is much slower than it needs to be since transactions outputs
// are not indexed by the accounts they credit to, and all unspent transaction
// outputs must be iterated.
func (w *Wallet) CalculateAccountBalances(account uint32, confirms int32) (Balances, error) {
var bals Balances
// Get current block. The block height used for calculating
// the number of tx confirmations.
syncBlock := w.Manager.SyncedTo()
unspent, err := w.TxStore.UnspentOutputs()
if err != nil {
return bals, err
}
for i := range unspent {
output := &unspent[i]
var outputAcct uint32
_, addrs, _, err := txscript.ExtractPkScriptAddrs(
output.PkScript, w.chainParams)
if err == nil && len(addrs) > 0 {
outputAcct, err = w.Manager.AddrAccount(addrs[0])
}
if err != nil || outputAcct != account {
continue
}
bals.Total += output.Amount
if output.FromCoinBase && !confirmed(int32(w.chainParams.CoinbaseMaturity),
output.Height, syncBlock.Height) {
bals.ImmatureReward += output.Amount
} else if confirmed(confirms, output.Height, syncBlock.Height) {
bals.Spendable += output.Amount
}
}
return bals, nil
}
// CurrentAddress gets the most recently requested Bitcoin payment address
// from a wallet. If the address has already been used (there is at least
// one transaction spending to it in the blockchain or btcd mempool), the next
// chained address is returned.
func (w *Wallet) CurrentAddress(account uint32) (btcutil.Address, error) {
addr, err := w.Manager.LastExternalAddress(account)
if err != nil {
// If no address exists yet, create the first external address
if waddrmgr.IsError(err, waddrmgr.ErrAddressNotFound) {
return w.NewAddress(account)
}
return nil, err
}
// Get next chained address if the last one has already been used.
used, err := addr.Used()
if err != nil {
return nil, err
}
if used {
return w.NewAddress(account)
}
return addr.Address(), nil
}
// RenameAccount sets the name for an account number to newName.
func (w *Wallet) RenameAccount(account uint32, newName string) error {
err := w.Manager.RenameAccount(account, newName)
if err != nil {
return err
}
props, err := w.Manager.AccountProperties(account)
if err != nil {
log.Errorf("Cannot fetch new account properties for notification "+
"during account rename: %v", err)
} else {
w.NtfnServer.notifyAccountProperties(props)
}
return nil
}
// NextAccount creates the next account and returns its account number. The
// name must be unique to the account.
func (w *Wallet) NextAccount(name string) (uint32, error) {
account, err := w.Manager.NewAccount(name)
if err != nil {
return 0, err
}
props, err := w.Manager.AccountProperties(account)
if err != nil {
log.Errorf("Cannot fetch new account properties for notification "+
"after account creation: %v", err)
} else {
w.NtfnServer.notifyAccountProperties(props)
}
return account, nil
}
// CreditCategory describes the type of wallet transaction output. The category
// of "sent transactions" (debits) is always "send", and is not expressed by
// this type.
//
// TODO: This is a requirement of the RPC server and should be moved.
type CreditCategory byte
// These constants define the possible credit categories.
const (
CreditReceive CreditCategory = iota
CreditGenerate
CreditImmature
)
// String returns the category as a string. This string may be used as the
// JSON string for categories as part of listtransactions and gettransaction
// RPC responses.
func (c CreditCategory) String() string {
switch c {
case CreditReceive:
return "receive"
case CreditGenerate:
return "generate"
case CreditImmature:
return "immature"
default:
return "unknown"
}
}
// RecvCategory returns the category of received credit outputs from a
// transaction record. The passed block chain height is used to distinguish
// immature from mature coinbase outputs.
//
// TODO: This is intended for use by the RPC server and should be moved out of
// this package at a later time.
func RecvCategory(details *wtxmgr.TxDetails, syncHeight int32, net *chaincfg.Params) CreditCategory {
if blockchain.IsCoinBaseTx(&details.MsgTx) {
if confirmed(int32(net.CoinbaseMaturity), details.Block.Height,
syncHeight) {
return CreditGenerate
}
return CreditImmature
}
return CreditReceive
}
// ListTransactions creates a object that may be marshalled to a response result
// for a listtransactions RPC.
//
// TODO: This should be moved to the legacyrpc package.
func ListTransactions(details *wtxmgr.TxDetails, addrMgr *waddrmgr.Manager,
syncHeight int32, net *chaincfg.Params) []btcjson.ListTransactionsResult {
var (
blockHashStr string
blockTime int64
confirmations int64
)
if details.Block.Height != -1 {
blockHashStr = details.Block.Hash.String()
blockTime = details.Block.Time.Unix()
confirmations = int64(confirms(details.Block.Height, syncHeight))
}
results := []btcjson.ListTransactionsResult{}
txHashStr := details.Hash.String()
received := details.Received.Unix()
generated := blockchain.IsCoinBaseTx(&details.MsgTx)
recvCat := RecvCategory(details, syncHeight, net).String()
send := len(details.Debits) != 0
// Fee can only be determined if every input is a debit.
var feeF64 float64
if len(details.Debits) == len(details.MsgTx.TxIn) {
var debitTotal btcutil.Amount
for _, deb := range details.Debits {
debitTotal += deb.Amount
}
var outputTotal btcutil.Amount
for _, output := range details.MsgTx.TxOut {
outputTotal += btcutil.Amount(output.Value)
}
// Note: The actual fee is debitTotal - outputTotal. However,
// this RPC reports negative numbers for fees, so the inverse
// is calculated.
feeF64 = (outputTotal - debitTotal).ToBTC()
}
outputs:
for i, output := range details.MsgTx.TxOut {
// Determine if this output is a credit, and if so, determine
// its spentness.
var isCredit bool
var spentCredit bool
for _, cred := range details.Credits {
if cred.Index == uint32(i) {
// Change outputs are ignored.
if cred.Change {
continue outputs
}
isCredit = true
spentCredit = cred.Spent
break
}
}
var address string
var accountName string
_, addrs, _, _ := txscript.ExtractPkScriptAddrs(output.PkScript, net)
if len(addrs) == 1 {
addr := addrs[0]
address = addr.EncodeAddress()
account, err := addrMgr.AddrAccount(addrs[0])
if err == nil {
accountName, err = addrMgr.AccountName(account)
if err != nil {
accountName = ""
}
}
}
amountF64 := btcutil.Amount(output.Value).ToBTC()
result := btcjson.ListTransactionsResult{
// Fields left zeroed:
// InvolvesWatchOnly
// BlockIndex
//
// Fields set below:
// Account (only for non-"send" categories)
// Category
// Amount
// Fee
Address: address,
Vout: uint32(i),
Confirmations: confirmations,
Generated: generated,
BlockHash: blockHashStr,
BlockTime: blockTime,
TxID: txHashStr,
WalletConflicts: []string{},
Time: received,
TimeReceived: received,
}
// Add a received/generated/immature result if this is a credit.
// If the output was spent, create a second result under the
// send category with the inverse of the output amount. It is
// therefore possible that a single output may be included in
// the results set zero, one, or two times.
//
// Since credits are not saved for outputs that are not
// controlled by this wallet, all non-credits from transactions
// with debits are grouped under the send category.
if send || spentCredit {
result.Category = "send"
result.Amount = -amountF64
result.Fee = &feeF64
results = append(results, result)
}
if isCredit {
result.Account = accountName
result.Category = recvCat
result.Amount = amountF64
result.Fee = nil
results = append(results, result)
}
}
return results
}
// ListSinceBlock returns a slice of objects with details about transactions
// since the given block. If the block is -1 then all transactions are included.
// This is intended to be used for listsinceblock RPC replies.
func (w *Wallet) ListSinceBlock(start, end, syncHeight int32) ([]btcjson.ListTransactionsResult, error) {
txList := []btcjson.ListTransactionsResult{}
err := w.TxStore.RangeTransactions(start, end, func(details []wtxmgr.TxDetails) (bool, error) {
for _, detail := range details {
jsonResults := ListTransactions(&detail, w.Manager,
syncHeight, w.chainParams)
txList = append(txList, jsonResults...)
}
return false, nil
})
return txList, err
}
// ListTransactions returns a slice of objects with details about a recorded
// transaction. This is intended to be used for listtransactions RPC
// replies.
func (w *Wallet) ListTransactions(from, count int) ([]btcjson.ListTransactionsResult, error) {
txList := []btcjson.ListTransactionsResult{}
// Get current block. The block height used for calculating
// the number of tx confirmations.
syncBlock := w.Manager.SyncedTo()
// Need to skip the first from transactions, and after those, only
// include the next count transactions.
skipped := 0
n := 0
// Return newer results first by starting at mempool height and working
// down to the genesis block.
err := w.TxStore.RangeTransactions(-1, 0, func(details []wtxmgr.TxDetails) (bool, error) {
// Iterate over transactions at this height in reverse order.
// This does nothing for unmined transactions, which are
// unsorted, but it will process mined transactions in the
// reverse order they were marked mined.
for i := len(details) - 1; i >= 0; i-- {
if from > skipped {
skipped++
continue
}
n++
if n > count {
return true, nil
}
jsonResults := ListTransactions(&details[i],
w.Manager, syncBlock.Height, w.chainParams)