forked from btcsuite/btcwallet
/
account.go
650 lines (555 loc) · 18.9 KB
/
account.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
/*
* Copyright (c) 2013, 2014 Conformal Systems LLC <info@conformal.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package main
import (
"bytes"
"encoding/base64"
"fmt"
"github.com/conformal/btcutil"
"github.com/conformal/btcwallet/tx"
"github.com/conformal/btcwallet/wallet"
"github.com/conformal/btcwire"
"path/filepath"
"sync"
)
// addressAccountMap holds a map of addresses to names of the
// accounts that hold each address.
//
// TODO: move this to AccountManager
var addressAccountMap = struct {
sync.RWMutex
m map[string]string
}{
m: make(map[string]string),
}
// MarkAddressForAccount marks an address as belonging to an account.
func MarkAddressForAccount(address, account string) {
addressAccountMap.Lock()
addressAccountMap.m[address] = account
addressAccountMap.Unlock()
}
// LookupAccountByAddress returns the account name for address. error
// will be set to ErrNotFound if the address has not been marked as
// associated with any account.
func LookupAccountByAddress(address string) (string, error) {
addressAccountMap.RLock()
defer addressAccountMap.RUnlock()
account, ok := addressAccountMap.m[address]
if !ok {
return "", ErrNotFound
}
return account, nil
}
// Account is a structure containing all the components for a
// complete wallet. It contains the Armory-style wallet (to store
// addresses and keys), and tx and utxo stores, and a mutex to prevent
// incorrect multiple access.
type Account struct {
name string
fullRescan bool
*wallet.Wallet
tx.UtxoStore
tx.TxStore
}
// Lock locks the underlying wallet for an account.
func (a *Account) Lock() error {
switch err := a.Wallet.Lock(); err {
case nil:
NotifyWalletLockStateChange(a.Name(), true)
return nil
case wallet.ErrWalletLocked:
// Do not pass wallet already locked errors to the caller.
return nil
default:
return err
}
}
// Unlock unlocks the underlying wallet for an account.
func (a *Account) Unlock(passphrase []byte) error {
if err := a.Wallet.Unlock(passphrase); err != nil {
return err
}
NotifyWalletLockStateChange(a.Name(), false)
return nil
}
// AddressUsed returns whether there are any recorded transactions spending to
// a given address. Assumming correct TxStore usage, this will return true iff
// there are any transactions with outputs to this address in the blockchain or
// the btcd mempool.
func (a *Account) AddressUsed(addr btcutil.Address) bool {
// This can be optimized by recording this data as it is read when
// opening an account, and keeping it up to date each time a new
// received tx arrives.
pkHash := addr.ScriptAddress()
for i := range a.TxStore {
rtx, ok := a.TxStore[i].(*tx.RecvTx)
if !ok {
continue
}
if bytes.Equal(rtx.ReceiverHash, pkHash) {
return true
}
}
return false
}
// CalculateBalance sums the amounts of all unspent transaction
// outputs to addresses of a wallet and returns the balance as a
// float64.
//
// 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 (a *Account) CalculateBalance(confirms int) float64 {
bs, err := GetCurBlock()
if bs.Height == int32(btcutil.BlockHeightUnknown) || err != nil {
return 0.
}
var bal uint64 // Measured in satoshi
for _, u := range a.UtxoStore {
// Utxos not yet in blocks (height -1) should only be
// added if confirmations is 0.
if confirmed(confirms, u.Height, bs.Height) {
bal += u.Amt
}
}
return float64(bal) / float64(btcutil.SatoshiPerBitcoin)
}
// CalculateAddressBalance sums the amounts of all unspent transaction
// outputs to a single address's pubkey hash and returns the balance
// as a float64.
//
// 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 (a *Account) CalculateAddressBalance(addr *btcutil.AddressPubKeyHash, confirms int) float64 {
bs, err := GetCurBlock()
if bs.Height == int32(btcutil.BlockHeightUnknown) || err != nil {
return 0.
}
var bal uint64 // Measured in satoshi
for _, u := range a.UtxoStore {
// Utxos not yet in blocks (height -1) should only be
// added if confirmations is 0.
if confirmed(confirms, u.Height, bs.Height) {
if bytes.Equal(addr.ScriptAddress(), u.AddrHash[:]) {
bal += u.Amt
}
}
}
return float64(bal) / float64(btcutil.SatoshiPerBitcoin)
}
// CurrentAddress gets the most recently requested Bitcoin payment address
// from an account. 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 (a *Account) CurrentAddress() (btcutil.Address, error) {
addr := a.Wallet.LastChainedAddress()
// Get next chained address if the last one has already been used.
if a.AddressUsed(addr) {
return a.NewAddress()
}
return addr, nil
}
// ListSinceBlock returns a slice of maps with details about transactions since
// the given block. If the block is -1 then all transactions are included.
// transaction. This is intended to be used for listsinceblock RPC
// replies.
func (a *Account) ListSinceBlock(since, curBlockHeight int32, minconf int) ([]map[string]interface{}, error) {
var txInfoList []map[string]interface{}
for _, tx := range a.TxStore {
// check block number.
if since != -1 && tx.GetBlockHeight() <= since {
continue
}
txInfoList = append(txInfoList,
tx.TxInfo(a.name, curBlockHeight, a.Net())...)
}
return txInfoList, nil
}
// ListTransactions returns a slice of maps with details about a recorded
// transaction. This is intended to be used for listtransactions RPC
// replies.
func (a *Account) ListTransactions(from, count int) ([]map[string]interface{}, error) {
// Get current block. The block height used for calculating
// the number of tx confirmations.
bs, err := GetCurBlock()
if err != nil {
return nil, err
}
var txInfoList []map[string]interface{}
lastLookupIdx := len(a.TxStore) - count
// Search in reverse order: lookup most recently-added first.
for i := len(a.TxStore) - 1; i >= from && i >= lastLookupIdx; i-- {
txInfoList = append(txInfoList,
a.TxStore[i].TxInfo(a.name, bs.Height, a.Net())...)
}
return txInfoList, nil
}
// ListAddressTransactions returns a slice of maps with details about a
// recorded transactions to or from any address belonging to a set. This is
// intended to be used for listaddresstransactions RPC replies.
func (a *Account) ListAddressTransactions(pkHashes map[string]struct{}) (
[]map[string]interface{}, error) {
// Get current block. The block height used for calculating
// the number of tx confirmations.
bs, err := GetCurBlock()
if err != nil {
return nil, err
}
var txInfoList []map[string]interface{}
for i := range a.TxStore {
rtx, ok := a.TxStore[i].(*tx.RecvTx)
if !ok {
continue
}
if _, ok := pkHashes[string(rtx.ReceiverHash[:])]; ok {
info := rtx.TxInfo(a.name, bs.Height, a.Net())
txInfoList = append(txInfoList, info...)
}
}
return txInfoList, nil
}
// ListAllTransactions returns a slice of maps with details about a recorded
// transaction. This is intended to be used for listalltransactions RPC
// replies.
func (a *Account) ListAllTransactions() ([]map[string]interface{}, error) {
// Get current block. The block height used for calculating
// the number of tx confirmations.
bs, err := GetCurBlock()
if err != nil {
return nil, err
}
// Search in reverse order: lookup most recently-added first.
var txInfoList []map[string]interface{}
for i := len(a.TxStore) - 1; i >= 0; i-- {
txInfoList = append(txInfoList,
a.TxStore[i].TxInfo(a.name, bs.Height, a.Net())...)
}
return txInfoList, nil
}
// DumpPrivKeys returns the WIF-encoded private keys for all addresses with
// private keys in a wallet.
func (a *Account) DumpPrivKeys() ([]string, error) {
// Iterate over each active address, appending the private
// key to privkeys.
var privkeys []string
for addr, info := range a.Wallet.ActiveAddresses() {
key, err := a.Wallet.AddressKey(addr)
if err != nil {
return nil, err
}
encKey, err := btcutil.EncodePrivateKey(key.D.Bytes(),
a.Wallet.Net(), info.Compressed)
if err != nil {
return nil, err
}
privkeys = append(privkeys, encKey)
}
return privkeys, nil
}
// DumpWIFPrivateKey returns the WIF encoded private key for a
// single wallet address.
func (a *Account) DumpWIFPrivateKey(addr btcutil.Address) (string, error) {
// Get private key from wallet if it exists.
key, err := a.Wallet.AddressKey(addr)
if err != nil {
return "", err
}
// Get address info. This is needed to determine whether
// the pubkey is compressed or not.
info, err := a.Wallet.AddressInfo(addr)
if err != nil {
return "", err
}
// Return WIF-encoding of the private key.
return btcutil.EncodePrivateKey(key.D.Bytes(), a.Net(), info.Compressed)
}
// ImportPrivateKey imports a private key to the account's wallet and
// writes the new wallet to disk.
func (a *Account) ImportPrivateKey(pk []byte, compressed bool, bs *wallet.BlockStamp) (string, error) {
// Attempt to import private key into wallet.
addr, err := a.Wallet.ImportPrivateKey(pk, compressed, bs)
if err != nil {
return "", err
}
addrStr := addr.String()
// Immediately write wallet to disk.
AcctMgr.ds.ScheduleWalletWrite(a)
if err := AcctMgr.ds.FlushAccount(a); err != nil {
return "", fmt.Errorf("cannot write account: %v", err)
}
// Associate the imported address with this account.
MarkAddressForAccount(addrStr, a.Name())
log.Infof("Imported payment address %v", addrStr)
// Return the payment address string of the imported private key.
return addrStr, nil
}
// ExportToDirectory writes an account to a special export directory. Any
// previous files are overwritten.
func (a *Account) ExportToDirectory(dirBaseName string) error {
dir := filepath.Join(networkDir(cfg.Net()), dirBaseName)
if err := checkCreateDir(dir); err != nil {
return err
}
return AcctMgr.ds.ExportAccount(a, dir)
}
// ExportWatchingWallet returns a new account with a watching wallet
// exported by this a's wallet. Both wallets share the same tx and utxo
// stores, so locking one will lock the other as well. The returned account
// should be exported quickly, either to file or to an rpc caller, and then
// dropped from scope.
func (a *Account) ExportWatchingWallet() (*Account, error) {
ww, err := a.Wallet.ExportWatchingWallet()
if err != nil {
return nil, err
}
wa := *a
wa.Wallet = ww
return &wa, nil
}
// exportBase64 exports an account's serialized wallet, tx, and utxo
// stores as base64-encoded values in a map.
func (a *Account) exportBase64() (map[string]string, error) {
buf := &bytes.Buffer{}
m := make(map[string]string)
_, err := a.Wallet.WriteTo(buf)
if err != nil {
return nil, err
}
m["wallet"] = base64.StdEncoding.EncodeToString(buf.Bytes())
buf.Reset()
if _, err = a.TxStore.WriteTo(buf); err != nil {
return nil, err
}
m["tx"] = base64.StdEncoding.EncodeToString(buf.Bytes())
buf.Reset()
_, err = a.UtxoStore.WriteTo(buf)
if err != nil {
return nil, err
}
m["utxo"] = base64.StdEncoding.EncodeToString(buf.Bytes())
buf.Reset()
return m, nil
}
// Track requests btcd to send notifications of new transactions for
// each address stored in a wallet.
func (a *Account) Track() {
// Request notifications for transactions sending to all wallet
// addresses.
addrs := a.ActiveAddresses()
addrstrs := make([]string, len(addrs))
i := 0
for addr := range addrs {
addrstrs[i] = addr.EncodeAddress()
i++
}
err := NotifyNewTXs(CurrentServerConn(), addrstrs)
if err != nil {
log.Error("Unable to request transaction updates for address.")
}
for _, utxo := range a.UtxoStore {
ReqSpentUtxoNtfn(utxo)
}
}
// RescanActiveAddresses requests btcd to rescan the blockchain for new
// transactions to all active wallet addresses. This is needed for
// catching btcwallet up to a long-running btcd process, as otherwise
// it would have missed notifications as blocks are attached to the
// main chain.
func (a *Account) RescanActiveAddresses() {
// Determine the block to begin the rescan from.
beginBlock := int32(0)
if a.fullRescan {
// Need to perform a complete rescan since the wallet creation
// block.
beginBlock = a.EarliestBlockHeight()
log.Debugf("Rescanning account '%v' for new transactions since block height %v",
a.name, beginBlock)
} else {
// The last synced block height should be used the starting
// point for block rescanning. Grab the block stamp here.
bs := a.SyncedWith()
log.Debugf("Rescanning account '%v' for new transactions after block height %v hash %v",
a.name, bs.Height, bs.Hash)
// If we're synced with block x, must scan the blocks x+1 to best block.
beginBlock = bs.Height + 1
}
// Rescan active addresses starting at the determined block height.
Rescan(CurrentServerConn(), beginBlock, a.ActivePaymentAddresses())
AcctMgr.ds.FlushAccount(a)
}
// SortedActivePaymentAddresses returns a slice of all active payment
// addresses in an account.
func (a *Account) SortedActivePaymentAddresses() []string {
infos := a.Wallet.SortedActiveAddresses()
addrs := make([]string, len(infos))
for i, info := range infos {
addrs[i] = info.Address.EncodeAddress()
}
return addrs
}
// ActivePaymentAddresses returns a set of all active pubkey hashes
// in an account.
func (a *Account) ActivePaymentAddresses() map[string]struct{} {
infos := a.ActiveAddresses()
addrs := make(map[string]struct{}, len(infos))
for _, info := range infos {
addrs[info.Address.EncodeAddress()] = struct{}{}
}
return addrs
}
// NewAddress returns a new payment address for an account.
func (a *Account) NewAddress() (btcutil.Address, error) {
// Get current block's height and hash.
bs, err := GetCurBlock()
if err != nil {
return nil, err
}
// Get next address from wallet.
addr, err := a.Wallet.NextChainedAddress(&bs, cfg.KeypoolSize)
if err != nil {
return nil, err
}
// Immediately write updated wallet to disk.
AcctMgr.ds.ScheduleWalletWrite(a)
if err := AcctMgr.ds.FlushAccount(a); err != nil {
return nil, fmt.Errorf("account write failed: %v", err)
}
// Mark this new address as belonging to this account.
MarkAddressForAccount(addr.EncodeAddress(), a.Name())
// Request updates from btcd for new transactions sent to this address.
a.ReqNewTxsForAddress(addr)
return addr, nil
}
// NewChangeAddress returns a new change address for an account.
func (a *Account) NewChangeAddress() (btcutil.Address, error) {
// Get current block's height and hash.
bs, err := GetCurBlock()
if err != nil {
return nil, err
}
// Get next chained change address from wallet.
addr, err := a.Wallet.ChangeAddress(&bs, cfg.KeypoolSize)
if err != nil {
return nil, err
}
// Immediately write updated wallet to disk.
AcctMgr.ds.ScheduleWalletWrite(a)
if err := AcctMgr.ds.FlushAccount(a); err != nil {
return nil, fmt.Errorf("account write failed: %v", err)
}
// Mark this new address as belonging to this account.
MarkAddressForAccount(addr.EncodeAddress(), a.Name())
// Request updates from btcd for new transactions sent to this address.
a.ReqNewTxsForAddress(addr)
return addr, nil
}
// RecoverAddresses recovers the next n chained addresses of a wallet.
func (a *Account) RecoverAddresses(n int) error {
// Get info on the last chained address. The rescan starts at the
// earliest block height the last chained address might appear at.
last := a.Wallet.LastChainedAddress()
lastInfo, err := a.Wallet.AddressInfo(last)
if err != nil {
return err
}
addrs, err := a.Wallet.ExtendActiveAddresses(n, cfg.KeypoolSize)
if err != nil {
return err
}
// Run a goroutine to rescan blockchain for recovered addresses.
m := make(map[string]struct{})
for i := range addrs {
m[addrs[i].EncodeAddress()] = struct{}{}
}
go func(addrs map[string]struct{}) {
jsonErr := Rescan(CurrentServerConn(), lastInfo.FirstBlock, addrs)
if jsonErr != nil {
log.Errorf("Rescanning for recovered addresses failed: %v",
jsonErr.Message)
}
}(m)
return nil
}
// ReqNewTxsForAddress sends a message to btcd to request tx updates
// for addr for each new block that is added to the blockchain.
func (a *Account) ReqNewTxsForAddress(addr btcutil.Address) {
// Only support P2PKH addresses currently.
apkh, ok := addr.(*btcutil.AddressPubKeyHash)
if !ok {
return
}
log.Debugf("Requesting notifications of TXs sending to address %v", apkh)
err := NotifyNewTXs(CurrentServerConn(), []string{apkh.EncodeAddress()})
if err != nil {
log.Error("Unable to request transaction updates for address.")
}
}
// ReqSpentUtxoNtfn sends a message to btcd to request updates for when
// a stored UTXO has been spent.
func ReqSpentUtxoNtfn(u *tx.Utxo) {
log.Debugf("Requesting spent UTXO notifications for Outpoint hash %s index %d",
u.Out.Hash, u.Out.Index)
NotifySpent(CurrentServerConn(), (*btcwire.OutPoint)(&u.Out))
}
// TotalReceived iterates through an account's transaction history, returning the
// total amount of bitcoins received for any account address. Amounts received
// through multisig transactions are ignored.
func (a *Account) TotalReceived(confirms int) (float64, error) {
bs, err := GetCurBlock()
if err != nil {
return 0, err
}
var totalSatoshis int64
for _, e := range a.TxStore {
recvtx, ok := e.(*tx.RecvTx)
if !ok {
continue
}
// Ignore change.
addr, err := btcutil.NewAddressPubKeyHash(recvtx.ReceiverHash, cfg.Net())
if err != nil {
continue
}
info, err := a.Wallet.AddressInfo(addr)
if err != nil {
continue
}
if info.Change {
continue
}
// Tally if the appropiate number of block confirmations have passed.
if confirmed(confirms, recvtx.GetBlockHeight(), bs.Height) {
totalSatoshis += recvtx.Amount
}
}
return float64(totalSatoshis) / float64(btcutil.SatoshiPerBitcoin), nil
}
// confirmed checks whether a transaction at height txHeight has met
// minconf confirmations for a blockchain at height curHeight.
func confirmed(minconf int, txHeight, curHeight int32) bool {
if minconf == 0 {
return true
}
if txHeight != -1 && int(curHeight-txHeight+1) >= minconf {
return true
}
return false
}