-
Notifications
You must be signed in to change notification settings - Fork 82
/
account.go
694 lines (623 loc) · 20.7 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
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
// Copyright 2018 Shift Devices AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package eth
import (
"context"
"fmt"
"math/big"
"path"
"strings"
"time"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/digitalbitbox/bitbox-wallet-app/backend/accounts"
"github.com/digitalbitbox/bitbox-wallet-app/backend/accounts/errors"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/btc/synchronizer"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/coin"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/eth/db"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/eth/erc20"
ethtypes "github.com/digitalbitbox/bitbox-wallet-app/backend/coins/eth/types"
"github.com/digitalbitbox/bitbox-wallet-app/backend/keystore"
"github.com/digitalbitbox/bitbox-wallet-app/backend/rates"
"github.com/digitalbitbox/bitbox-wallet-app/backend/signing"
"github.com/digitalbitbox/bitbox-wallet-app/util/errp"
"github.com/digitalbitbox/bitbox-wallet-app/util/locker"
ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/sirupsen/logrus"
)
var pollInterval = 30 * time.Second
// Account is an Ethereum account, with one address.
type Account struct {
locker.Locker
synchronizer *synchronizer.Synchronizer
coin *Coin
dbFolder string
db db.Interface
code string
name string
getSigningConfiguration func() (*signing.Configuration, error)
signingConfiguration *signing.Configuration
keystores *keystore.Keystores
getNotifier func(*signing.Configuration) accounts.Notifier
notifier accounts.Notifier
offline bool
onEvent func(accounts.Event)
initialized bool
// enqueueUpdateCh is used to invoke an account update outside of the regular poll update
// interval.
enqueueUpdateCh chan struct{}
address Address
balance coin.Amount
blockNumber *big.Int
nextNonce uint64
transactions []accounts.Transaction
quitChan chan struct{}
log *logrus.Entry
rateUpdater *rates.RateUpdater
}
// NewAccount creates a new account.
func NewAccount(
accountCoin *Coin,
dbFolder string,
code string,
name string,
getSigningConfiguration func() (*signing.Configuration, error),
keystores *keystore.Keystores,
getNotifier func(*signing.Configuration) accounts.Notifier,
onEvent func(accounts.Event),
log *logrus.Entry,
rateUpdater *rates.RateUpdater,
) *Account {
log = log.WithField("group", "eth").
WithFields(logrus.Fields{"coin": accountCoin.String(), "code": code, "name": name})
log.Debug("Creating new account")
account := &Account{
coin: accountCoin,
dbFolder: dbFolder,
code: code,
name: name,
getSigningConfiguration: getSigningConfiguration,
signingConfiguration: nil,
keystores: keystores,
getNotifier: getNotifier,
onEvent: onEvent,
balance: coin.NewAmountFromInt64(0),
initialized: false,
enqueueUpdateCh: make(chan struct{}),
quitChan: make(chan struct{}),
log: log,
rateUpdater: rateUpdater,
}
account.synchronizer = synchronizer.NewSynchronizer(
func() { onEvent(accounts.EventSyncStarted) },
func() {
if !account.initialized {
account.initialized = true
onEvent(accounts.EventStatusChanged)
}
onEvent(accounts.EventSyncDone)
},
log,
)
return account
}
// Info implements accounts.Interface.
func (account *Account) Info() *accounts.Info {
return &accounts.Info{
SigningConfiguration: account.signingConfiguration,
}
}
// Code implements accounts.Interface.
func (account *Account) Code() string {
return account.code
}
// Name implements accounts.Interface.
func (account *Account) Name() string {
return account.name
}
// Coin implements accounts.Interface.
func (account *Account) Coin() coin.Coin {
return account.coin
}
// RateUpdater implement accounts.Interface, currently just returning a dummy value
func (account *Account) RateUpdater() *rates.RateUpdater {
return account.rateUpdater
}
// Initialize implements accounts.Interface.
func (account *Account) Initialize() error {
alreadyInitialized, err := func() (bool, error) {
defer account.Lock()()
if account.signingConfiguration != nil {
// Already initialized.
return true, nil
}
signingConfiguration, err := account.getSigningConfiguration()
if err != nil {
return false, err
}
account.signingConfiguration = signingConfiguration
account.notifier = account.getNotifier(signingConfiguration)
return false, nil
}()
if err != nil {
return err
}
if alreadyInitialized {
account.log.Debug("Account has already been initialized")
return nil
}
dbName := fmt.Sprintf("account-%s-%s.db", account.signingConfiguration.Hash(), account.code)
account.log.Debugf("Opening the database '%s' to persist the transactions.", dbName)
db, err := db.NewDB(path.Join(account.dbFolder, dbName))
if err != nil {
return err
}
account.db = db
account.log.Debugf("Opened the database '%s' to persist the transactions.", dbName)
if account.signingConfiguration.IsAddressBased() {
if !common.IsHexAddress(account.signingConfiguration.Address()) {
return errp.WithStack(errors.ErrInvalidAddress)
}
account.address = Address{
Address: common.HexToAddress(account.signingConfiguration.Address()),
}
} else {
account.address = Address{
Address: crypto.PubkeyToAddress(*account.signingConfiguration.PublicKeys()[0].ToECDSA()),
}
}
account.signingConfiguration = signing.NewConfiguration(
account.signingConfiguration.ScriptType(),
account.signingConfiguration.AbsoluteKeypath(),
account.signingConfiguration.ExtendedPublicKeys(),
account.address.String(),
account.signingConfiguration.SigningThreshold(),
)
account.coin.Initialize()
go account.poll()
return nil
}
func (account *Account) poll() {
timer := time.After(0)
for {
select {
case <-account.quitChan:
return
default:
select {
case <-account.quitChan:
return
case <-timer:
case <-account.enqueueUpdateCh:
account.log.Info("extraordinary account update invoked")
}
if err := account.update(); err != nil {
account.log.WithError(err).Error("error updating account")
if !account.offline {
account.offline = true
account.onEvent(accounts.EventStatusChanged)
}
} else if account.offline {
account.offline = false
account.onEvent(accounts.EventStatusChanged)
}
timer = time.After(pollInterval)
}
}
}
// updateOutgoingTransactions updates the height of the stored outgoing transactions.
// We update heights for tx with up to 12 confirmations, so re-orgs are taken into account.
// tipHeight is the current blockchain height.
func (account *Account) updateOutgoingTransactions(tipHeight uint64) {
defer account.synchronizer.IncRequestsCounter()()
dbTx, err := account.db.Begin()
if err != nil {
account.log.WithError(err).Error("could not open db")
return
}
defer dbTx.Rollback()
// Get our stored outgoing transactions.
outgoingTransactions, err := dbTx.OutgoingTransactions()
if err != nil {
account.log.WithError(err).Error("could not get outgoing transactions")
return
}
// Update the stored txs' metadata if up to 12 confirmations.
for _, tx := range outgoingTransactions {
remoteTx, err := account.coin.client.TransactionReceiptWithBlockNumber(context.TODO(), tx.Transaction.Hash())
if err != nil {
account.log.WithError(err).Error("could not fetch transaction")
continue
}
if remoteTx == nil {
continue
}
success := remoteTx.Status == types.ReceiptStatusSuccessful
if tx.Height == 0 || (tipHeight-remoteTx.BlockNumber) < ethtypes.NumConfirmationsComplete || tx.Success != success {
tx.Height = remoteTx.BlockNumber
tx.GasUsed = remoteTx.GasUsed
tx.Success = success
if err := dbTx.PutOutgoingTransaction(tx); err != nil {
account.log.WithError(err).Error("could not update outgoing tx")
continue
}
}
}
if err := dbTx.Commit(); err != nil {
account.log.WithError(err).Error("could not commit db tx")
return
}
}
// outgoingTransactions gets all locally stored outgoing transactions. It filters out the ones also
// present from the transactions source.
func (account *Account) outgoingTransactions(allTxs []accounts.Transaction) (
[]accounts.Transaction, error) {
dbTx, err := account.db.Begin()
if err != nil {
return nil, err
}
defer dbTx.Rollback()
outgoingTransactions, err := dbTx.OutgoingTransactions()
if err != nil {
return nil, err
}
allTxHashes := map[string]struct{}{}
for _, tx := range allTxs {
allTxHashes[tx.ID()] = struct{}{}
}
transactions := []accounts.Transaction{}
for _, tx := range outgoingTransactions {
tx := tx
// Skip txs already present from transactions source.
if _, ok := allTxHashes[tx.ID()]; ok {
continue
}
transactions = append(transactions,
ethtypes.NewTransactionWithConfirmations(tx, account.blockNumber.Uint64(), account.coin.erc20Token))
}
return transactions, nil
}
func (account *Account) update() error {
defer account.synchronizer.IncRequestsCounter()()
header, err := account.coin.client.HeaderByNumber(context.TODO(), nil)
if err != nil {
return errp.WithStack(err)
}
account.blockNumber = header.Number
transactionsSource := account.coin.TransactionsSource()
go account.updateOutgoingTransactions(account.blockNumber.Uint64())
// Get confirmed transactions.
var confirmedTansactions []accounts.Transaction
if transactionsSource != nil {
var err error
confirmedTansactions, err = transactionsSource.Transactions(
account.address.Address, account.blockNumber, account.coin.erc20Token)
if err != nil {
return err
}
}
// Get our stored outgoing transactions. Filter out all transactions from the transactions
// source, which should contain all confirmed tx.
outgoingTransactions, err := account.outgoingTransactions(confirmedTansactions)
if err != nil {
return err
}
// Nonce to be used for the next tx, fetched from the ETH node. It might be out of date due to
// latency, which is addressed below by using the locally stored nonce.
nodeNonce, err := account.coin.client.PendingNonceAt(context.TODO(), account.address.Address)
if err != nil {
return err
}
account.nextNonce = nodeNonce
// In case the nodeNonce is not up to date, we fall back to our stored last nonce to compute the
// next nonce.
if len(outgoingTransactions) > 0 {
localNonce := outgoingTransactions[len(outgoingTransactions)-1].(*ethtypes.TransactionWithConfirmations).Transaction.Nonce() + 1
if localNonce > account.nextNonce {
account.nextNonce = localNonce
}
}
account.transactions = append(outgoingTransactions, confirmedTansactions...)
for _, transaction := range account.transactions {
if err := account.notifier.Put([]byte(transaction.ID())); err != nil {
return err
}
}
if account.coin.erc20Token != nil {
tok, err := erc20.NewIERC20(account.coin.erc20Token.ContractAddress(), account.coin.client)
if err != nil {
panic(err)
}
balance, err := tok.BalanceOf(&bind.CallOpts{}, account.address.Address)
if err != nil {
return errp.WithStack(err)
}
account.balance = coin.NewAmount(balance)
} else {
balance, err := account.coin.client.BalanceAt(context.TODO(),
account.address.Address, nil)
if err != nil {
return errp.WithStack(err)
}
account.balance = coin.NewAmount(balance)
}
return nil
}
// Initialized implements accounts.Interface.
func (account *Account) Initialized() bool {
return account.initialized
}
// Offline implements accounts.Interface.
func (account *Account) Offline() bool {
return account.offline
}
// FatalError implements accounts.Interface.
func (account *Account) FatalError() bool {
return false
}
// Close implements accounts.Interface.
func (account *Account) Close() {
account.log.Info("Waiting to close account")
account.synchronizer.WaitSynchronized()
account.log.Info("Closed account")
if account.db != nil {
if err := account.db.Close(); err != nil {
account.log.WithError(err).Error("couldn't close db")
}
account.log.Info("Closed DB")
}
account.initialized = false
close(account.quitChan)
account.onEvent(accounts.EventStatusChanged)
}
// Notifier implements accounts.Interface.
func (account *Account) Notifier() accounts.Notifier {
return account.notifier
}
// Transactions implements accounts.Interface.
func (account *Account) Transactions() ([]accounts.Transaction, error) {
account.synchronizer.WaitSynchronized()
return account.transactions, nil
}
// Balance implements accounts.Interface.
func (account *Account) Balance() (*accounts.Balance, error) {
account.synchronizer.WaitSynchronized()
return accounts.NewBalance(account.balance, coin.NewAmountFromInt64(0)), nil
}
// TxProposal holds all info needed to create and sign a transacstion.
type TxProposal struct {
Coin coin.Coin
Tx *types.Transaction
Fee *big.Int
// Value can be the same as Tx.Value(), but in case of e.g. ERC20, tx.Value() is zero, while the
// Token value is encoded in the contract input data.
Value *big.Int
// Signer contains the sighash algo, which depends on the block number.
Signer types.Signer
// KeyPath is the location of this account's address/pubkey/privkey.
Keypath signing.AbsoluteKeypath
}
func (account *Account) newTx(
recipientAddress string,
amount coin.SendAmount,
data []byte,
) (*TxProposal, error) {
if !common.IsHexAddress(recipientAddress) {
return nil, errp.WithStack(errors.ErrInvalidAddress)
}
suggestedGasPrice, err := account.coin.client.SuggestGasPrice(context.TODO())
if err != nil {
return nil, err
}
var value *big.Int
if amount.SendAll() {
value = account.balance.BigInt() // set here only temporarily to estimate the gas
} else {
allowZero := true
parsedAmount, err := amount.Amount(account.coin.unitFactor(false), allowZero)
if err != nil {
return nil, err
}
value = parsedAmount.BigInt()
}
address := common.HexToAddress(recipientAddress)
var message ethereum.CallMsg
if account.coin.erc20Token != nil {
parsed, err := abi.JSON(strings.NewReader(erc20.IERC20ABI))
if err != nil {
panic(errp.WithStack(err))
}
erc20ContractData, err := parsed.Pack("transfer", &address, value)
if err != nil {
panic(errp.WithStack(err))
}
contractAddress := account.coin.erc20Token.ContractAddress()
message = ethereum.CallMsg{
From: account.address.Address,
To: &contractAddress,
Gas: 0,
GasPrice: suggestedGasPrice,
Value: big.NewInt(0),
Data: erc20ContractData,
}
} else {
// Standard ethereum transaction
message = ethereum.CallMsg{
From: account.address.Address,
To: &address,
Gas: 0,
GasPrice: suggestedGasPrice,
Value: value,
Data: data,
}
}
// For ERC20 transfers, the EstimateGas call fails if we try to spend more than we have and we
// do not have enough ether to pay the fee.
// We make some checks upfront to catch this before calling out to the node and failing.
if !amount.SendAll() {
if account.coin.erc20Token != nil {
if value.Cmp(account.balance.BigInt()) == 1 {
return nil, errp.WithStack(errors.ErrInsufficientFunds)
}
}
}
gasLimit, err := account.coin.client.EstimateGas(context.TODO(), message)
if err != nil {
account.log.WithError(err).Error("Could not estimate the gas limit.")
return nil, errp.WithStack(errors.ErrInvalidData)
}
fee := new(big.Int).Mul(new(big.Int).SetUint64(gasLimit), suggestedGasPrice)
// Adjust amount with fee
if account.coin.erc20Token != nil {
// in erc 20 tokens, the amount is in the token unit, while the fee is in ETH, so there is
// no issue withSendAll.
if !amount.SendAll() && value.Cmp(account.balance.BigInt()) == 1 {
return nil, errp.WithStack(errors.ErrInsufficientFunds)
}
} else {
if amount.SendAll() {
// Set the value correctly and check that the fee is smaller than or equal to the balance.
value = new(big.Int).Sub(account.balance.BigInt(), fee)
message.Value = value
if message.Value.Sign() < 0 {
return nil, errp.WithStack(errors.ErrInsufficientFunds)
}
} else {
// Check that the entered value and the estimated fee are not greater than the balance.
total := new(big.Int).Add(message.Value, fee)
if total.Cmp(account.balance.BigInt()) == 1 {
return nil, errp.WithStack(errors.ErrInsufficientFunds)
}
}
}
tx := types.NewTransaction(account.nextNonce,
*message.To,
message.Value, gasLimit, suggestedGasPrice, message.Data)
return &TxProposal{
Coin: account.coin,
Tx: tx,
Fee: fee,
Value: value,
Signer: types.MakeSigner(account.coin.Net(), account.blockNumber),
Keypath: account.signingConfiguration.AbsoluteKeypath(),
}, nil
}
// storePendingOutgoingTransaction puts an outgoing tx into the db with height 0 (pending).
func (account *Account) storePendingOutgoingTransaction(transaction *types.Transaction) error {
dbTx, err := account.db.Begin()
if err != nil {
return err
}
defer dbTx.Rollback()
if err := dbTx.PutOutgoingTransaction(
ðtypes.TransactionWithMetadata{
Transaction: transaction,
Height: 0,
}); err != nil {
return err
}
if err := dbTx.Commit(); err != nil {
return err
}
account.log.Infof("stored pending outgoing tx with nonce: %d", transaction.Nonce())
return nil
}
// SendTx implements accounts.Interface.
func (account *Account) SendTx(
recipientAddress string,
amount coin.SendAmount,
_ accounts.FeeTargetCode,
_ map[wire.OutPoint]struct{},
data []byte) error {
account.log.Info("Signing and sending transaction")
txProposal, err := account.newTx(recipientAddress, amount, data)
if err != nil {
return err
}
if err := account.keystores.SignTransaction(txProposal); err != nil {
return err
}
if err := account.coin.client.SendTransaction(context.TODO(), txProposal.Tx); err != nil {
return errp.WithStack(err)
}
if err := account.storePendingOutgoingTransaction(txProposal.Tx); err != nil {
return err
}
account.enqueueUpdateCh <- struct{}{}
return nil
}
// FeeTargets implements accounts.Interface.
func (account *Account) FeeTargets() ([]accounts.FeeTarget, accounts.FeeTargetCode) {
return nil, ""
}
// TxProposal implements accounts.Interface.
func (account *Account) TxProposal(
recipientAddress string,
amount coin.SendAmount,
_ accounts.FeeTargetCode,
_ map[wire.OutPoint]struct{},
data []byte) (coin.Amount, coin.Amount, coin.Amount, error) {
txProposal, err := account.newTx(recipientAddress, amount, data)
if err != nil {
return coin.Amount{}, coin.Amount{}, coin.Amount{}, err
}
var total *big.Int
if account.coin.erc20Token != nil {
total = txProposal.Value
} else {
total = new(big.Int).Add(txProposal.Value, txProposal.Fee)
}
return coin.NewAmount(txProposal.Value), coin.NewAmount(txProposal.Fee), coin.NewAmount(total), nil
}
// GetUnusedReceiveAddresses implements accounts.Interface.
func (account *Account) GetUnusedReceiveAddresses() []accounts.Address {
return []accounts.Address{account.address}
}
// VerifyAddress implements accounts.Interface.
func (account *Account) VerifyAddress(addressID string) (bool, error) {
if account.signingConfiguration == nil {
return false, errp.New("account must be initialized")
}
account.synchronizer.WaitSynchronized()
defer account.RLock()()
canVerifyAddress, _, err := account.Keystores().CanVerifyAddresses(
account.signingConfiguration, account.Coin())
if err != nil {
return false, err
}
if canVerifyAddress {
return true, account.Keystores().VerifyAddress(account.signingConfiguration, account.Coin())
}
return false, nil
}
// CanVerifyAddresses implements accounts.Interface.
func (account *Account) CanVerifyAddresses() (bool, bool, error) {
if account.signingConfiguration == nil {
return false, false, errp.New("account must be initialized")
}
return account.Keystores().CanVerifyAddresses(account.signingConfiguration, account.Coin())
}
// ConvertToLegacyAddress implements accounts.Interface.
func (account *Account) ConvertToLegacyAddress(string) (btcutil.Address, error) {
panic("not used")
}
// Keystores implements accounts.Interface.
func (account *Account) Keystores() *keystore.Keystores {
return account.keystores
}