Skip to content

Commit eb4af2f

Browse files
committed
Merge remote-tracking branch 'benma/acctconfig'
2 parents 54b5eea + 3c54ef6 commit eb4af2f

File tree

21 files changed

+362
-316
lines changed

21 files changed

+362
-316
lines changed

backend/accountcodes.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package backend
1717
import (
1818
"fmt"
1919

20-
"github.com/digitalbitbox/bitbox-wallet-app/backend/accounts"
20+
accountsTypes "github.com/digitalbitbox/bitbox-wallet-app/backend/accounts/types"
2121
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/coin"
2222
"github.com/digitalbitbox/bitbox-wallet-app/backend/signing"
2323
)
@@ -33,18 +33,18 @@ import (
3333

3434
// regularAccountCode returns an account code based on a keystore root fingerprint, a coin code and
3535
// an account number.
36-
func regularAccountCode(rootFingerprint []byte, coinCode coin.Code, accountNumber uint16) accounts.Code {
37-
return accounts.Code(fmt.Sprintf("v0-%x-%s-%d", rootFingerprint, coinCode, accountNumber))
36+
func regularAccountCode(rootFingerprint []byte, coinCode coin.Code, accountNumber uint16) accountsTypes.Code {
37+
return accountsTypes.Code(fmt.Sprintf("v0-%x-%s-%d", rootFingerprint, coinCode, accountNumber))
3838
}
3939

4040
// splitAccountCode returns an account code for split accounts, made by exploding a unified account
4141
// into one account per signing configuration. This only applies to BTC/LTC.
42-
func splitAccountCode(parentCode accounts.Code, scriptType signing.ScriptType) accounts.Code {
43-
return accounts.Code(fmt.Sprintf("%s-%s", parentCode, scriptType))
42+
func splitAccountCode(parentCode accountsTypes.Code, scriptType signing.ScriptType) accountsTypes.Code {
43+
return accountsTypes.Code(fmt.Sprintf("%s-%s", parentCode, scriptType))
4444
}
4545

4646
// Erc20AccountCode returns the account code used for an ERC20 token.
4747
// It is derived from the account code of the parent ETH account and the token code.
48-
func Erc20AccountCode(ethereumAccountCode accounts.Code, tokenCode string) accounts.Code {
49-
return accounts.Code(fmt.Sprintf("%s-%s", ethereumAccountCode, tokenCode))
48+
func Erc20AccountCode(ethereumAccountCode accountsTypes.Code, tokenCode string) accountsTypes.Code {
49+
return accountsTypes.Code(fmt.Sprintf("%s-%s", ethereumAccountCode, tokenCode))
5050
}

backend/accounts.go

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/btcsuite/btcd/btcutil/hdkeychain"
2323
"github.com/digitalbitbox/bitbox-wallet-app/backend/accounts"
24+
accountsTypes "github.com/digitalbitbox/bitbox-wallet-app/backend/accounts/types"
2425
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/btc"
2526
coinpkg "github.com/digitalbitbox/bitbox-wallet-app/backend/coins/coin"
2627
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/eth"
@@ -64,9 +65,9 @@ func sortAccounts(accounts []*config.Account) {
6465
acct1 := accounts[i]
6566
acct2 := accounts[j]
6667
coinCmp := compareCoin(acct1.CoinCode, acct2.CoinCode)
67-
if coinCmp == 0 && len(acct1.Configurations) > 0 && len(acct2.Configurations) > 0 {
68-
signingCfg1 := acct1.Configurations[0]
69-
signingCfg2 := acct2.Configurations[0]
68+
if coinCmp == 0 && len(acct1.SigningConfigurations) > 0 && len(acct2.SigningConfigurations) > 0 {
69+
signingCfg1 := acct1.SigningConfigurations[0]
70+
signingCfg2 := acct2.SigningConfigurations[0]
7071
// An error should never happen here, but if it does, we just sort as if it was account
7172
// number 0.
7273
accountNumber1, _ := signingCfg1.AccountNumber()
@@ -167,7 +168,7 @@ func (backend *Backend) createAndPersistAccountConfig(
167168
name string,
168169
keystore keystore.Keystore,
169170
activeTokens []string,
170-
accountsConfig *config.AccountsConfig) (accounts.Code, error) {
171+
accountsConfig *config.AccountsConfig) (accountsTypes.Code, error) {
171172
rootFingerprint, err := keystore.RootFingerprint()
172173
if err != nil {
173174
return "", err
@@ -251,13 +252,13 @@ func nextAccountNumber(coinCode coinpkg.Code, keystore keystore.Keystore, accoun
251252
if coinCode != account.CoinCode {
252253
continue
253254
}
254-
if !account.Configurations.ContainsRootFingerprint(rootFingerprint) {
255+
if !account.SigningConfigurations.ContainsRootFingerprint(rootFingerprint) {
255256
continue
256257
}
257-
if len(account.Configurations) == 0 {
258+
if len(account.SigningConfigurations) == 0 {
258259
continue
259260
}
260-
accountNumber, err := account.Configurations[0].AccountNumber()
261+
accountNumber, err := account.SigningConfigurations[0].AccountNumber()
261262
if err != nil {
262263
continue
263264
}
@@ -295,8 +296,8 @@ func (backend *Backend) CanAddAccount(coinCode coinpkg.Code, keystore keystore.K
295296
// determined automatically to be the increment of the highest existing account.
296297
// `name` is the account name, shown to the user. If empty, a default name will be set.
297298
func (backend *Backend) CreateAndPersistAccountConfig(
298-
coinCode coinpkg.Code, name string, keystore keystore.Keystore) (accounts.Code, error) {
299-
var accountCode accounts.Code
299+
coinCode coinpkg.Code, name string, keystore keystore.Keystore) (accountsTypes.Code, error) {
300+
var accountCode accountsTypes.Code
300301
err := backend.config.ModifyAccountsConfig(func(accountsConfig *config.AccountsConfig) error {
301302
nextAccountNumber, err := nextAccountNumber(coinCode, keystore, accountsConfig)
302303
if err != nil {
@@ -314,7 +315,7 @@ func (backend *Backend) CreateAndPersistAccountConfig(
314315
}
315316

316317
// SetAccountActive activates/deactivates an account.
317-
func (backend *Backend) SetAccountActive(accountCode accounts.Code, active bool) error {
318+
func (backend *Backend) SetAccountActive(accountCode accountsTypes.Code, active bool) error {
318319
err := backend.config.ModifyAccountsConfig(func(accountsConfig *config.AccountsConfig) error {
319320
acct := accountsConfig.Lookup(accountCode)
320321
if acct == nil {
@@ -332,7 +333,7 @@ func (backend *Backend) SetAccountActive(accountCode accounts.Code, active bool)
332333

333334
// SetTokenActive activates/deactivates an token on an account. `tokenCode` must be an ERC20 token
334335
// code, e.g. "eth-erc20-usdt", "eth-erc20-bat", etc.
335-
func (backend *Backend) SetTokenActive(accountCode accounts.Code, tokenCode string, active bool) error {
336+
func (backend *Backend) SetTokenActive(accountCode accountsTypes.Code, tokenCode string, active bool) error {
336337
err := backend.config.ModifyAccountsConfig(func(accountsConfig *config.AccountsConfig) error {
337338
acct := accountsConfig.Lookup(accountCode)
338339
if acct == nil {
@@ -348,7 +349,7 @@ func (backend *Backend) SetTokenActive(accountCode accounts.Code, tokenCode stri
348349
}
349350

350351
// RenameAccount renames an account in the accounts database.
351-
func (backend *Backend) RenameAccount(accountCode accounts.Code, name string) error {
352+
func (backend *Backend) RenameAccount(accountCode accountsTypes.Code, name string) error {
352353
if name == "" {
353354
return errp.New("Name cannot be empty")
354355
}
@@ -378,32 +379,25 @@ func (backend *Backend) addAccount(account accounts.Interface) {
378379
}
379380

380381
// The accountsAndKeystoreLock must be held when calling this function.
381-
func (backend *Backend) createAndAddAccount(
382-
coin coinpkg.Coin,
383-
code accounts.Code,
384-
name string,
385-
signingConfigurations signing.Configurations,
386-
active bool,
387-
activeTokens []string,
388-
) {
382+
func (backend *Backend) createAndAddAccount(coin coinpkg.Coin, persistedConfig *config.Account) {
389383
var account accounts.Interface
390384
accountConfig := &accounts.AccountConfig{
391-
Active: active,
392-
Code: code,
393-
Name: name,
385+
Config: persistedConfig,
394386
DBFolder: backend.arguments.CacheDirectoryPath(),
395387
NotesFolder: backend.arguments.NotesDirectoryPath(),
396388
Keystore: backend.keystore,
397-
OnEvent: func(event accounts.Event) {
398-
backend.events <- AccountEvent{Type: "account", Code: code, Data: string(event)}
399-
if account != nil && event == accounts.EventSyncDone {
389+
OnEvent: func(event accountsTypes.Event) {
390+
backend.events <- AccountEvent{
391+
Type: "account", Code: persistedConfig.Code,
392+
Data: string(event),
393+
}
394+
if account != nil && event == accountsTypes.EventSyncDone {
400395
backend.notifyNewTxs(account)
401396
}
402397
},
403-
RateUpdater: backend.ratesUpdater,
404-
SigningConfigurations: signingConfigurations,
398+
RateUpdater: backend.ratesUpdater,
405399
GetNotifier: func(configurations signing.Configurations) accounts.Notifier {
406-
return backend.notifier.ForAccount(code)
400+
return backend.notifier.ForAccount(persistedConfig.Code)
407401
},
408402
GetSaveFilename: backend.environment.GetSaveFilename,
409403
UnsafeSystemOpen: backend.environment.SystemOpen,
@@ -424,23 +418,34 @@ func (backend *Backend) createAndAddAccount(
424418
backend.addAccount(account)
425419

426420
// Load ERC20 tokens enabled with this Ethereum account.
427-
for _, erc20TokenCode := range activeTokens {
428-
token, err := backend.Coin(coinpkg.Code(erc20TokenCode))
421+
for _, erc20TokenCode := range persistedConfig.ActiveTokens {
422+
erc20CoinCode := coinpkg.Code(erc20TokenCode)
423+
token, err := backend.Coin(erc20CoinCode)
429424
if err != nil {
430425
backend.log.WithError(err).Error("could not find ERC20 token")
431426
continue
432427
}
433-
erc20AccountCode := Erc20AccountCode(code, erc20TokenCode)
428+
erc20AccountCode := Erc20AccountCode(persistedConfig.Code, erc20TokenCode)
434429

435430
tokenName := token.Name()
436431

437-
accountNumber, err := accountConfig.SigningConfigurations[0].AccountNumber()
432+
accountNumber, err := accountConfig.Config.SigningConfigurations[0].AccountNumber()
438433
if err != nil {
439434
backend.log.WithError(err).Error("could not get account number")
440435
} else if accountNumber > 0 {
441436
tokenName = fmt.Sprintf("%s %d", tokenName, accountNumber+1)
442437
}
443-
backend.createAndAddAccount(token, erc20AccountCode, tokenName, signingConfigurations, active, nil)
438+
439+
erc20Config := &config.Account{
440+
Inactive: persistedConfig.Inactive,
441+
CoinCode: erc20CoinCode,
442+
Name: tokenName,
443+
Code: erc20AccountCode,
444+
SigningConfigurations: persistedConfig.SigningConfigurations,
445+
ActiveTokens: nil,
446+
}
447+
448+
backend.createAndAddAccount(token, erc20Config)
444449
}
445450
default:
446451
panic("unknown coin type")
@@ -469,8 +474,8 @@ func (backend *Backend) persistAccount(account config.Account, accountsConfig *c
469474
if account.CoinCode == account2.CoinCode {
470475
// We detect a duplicate account (subaccount in a unified account) if any of the
471476
// configurations is already present.
472-
for _, config := range account.Configurations {
473-
for _, config2 := range account2.Configurations {
477+
for _, config := range account.SigningConfigurations {
478+
for _, config2 := range account2.SigningConfigurations {
474479
if config.ExtendedPublicKey().String() == config2.ExtendedPublicKey().String() {
475480
return errp.WithStack(ErrAccountAlreadyExists)
476481
}
@@ -492,7 +497,7 @@ type scriptTypeWithKeypath struct {
492497
func (backend *Backend) persistBTCAccountConfig(
493498
keystore keystore.Keystore,
494499
coin coinpkg.Coin,
495-
code accounts.Code,
500+
code accountsTypes.Code,
496501
name string,
497502
configs []scriptTypeWithKeypath,
498503
accountsConfig *config.AccountsConfig,
@@ -535,10 +540,10 @@ func (backend *Backend) persistBTCAccountConfig(
535540

536541
if keystore.SupportsUnifiedAccounts() {
537542
return backend.persistAccount(config.Account{
538-
CoinCode: coin.Code(),
539-
Name: name,
540-
Code: code,
541-
Configurations: signingConfigurations,
543+
CoinCode: coin.Code(),
544+
Name: name,
545+
Code: code,
546+
SigningConfigurations: signingConfigurations,
542547
}, accountsConfig)
543548
}
544549

@@ -553,10 +558,10 @@ func (backend *Backend) persistBTCAccountConfig(
553558
}
554559

555560
err := backend.persistAccount(config.Account{
556-
CoinCode: coin.Code(),
557-
Name: suffixedName,
558-
Code: splitAccountCode(code, cfg.ScriptType()),
559-
Configurations: signing.Configurations{cfg},
561+
CoinCode: coin.Code(),
562+
Name: suffixedName,
563+
Code: splitAccountCode(code, cfg.ScriptType()),
564+
SigningConfigurations: signing.Configurations{cfg},
560565
}, accountsConfig)
561566
if err != nil {
562567
return err
@@ -568,7 +573,7 @@ func (backend *Backend) persistBTCAccountConfig(
568573
func (backend *Backend) persistETHAccountConfig(
569574
keystore keystore.Keystore,
570575
coin coinpkg.Coin,
571-
code accounts.Code,
576+
code accountsTypes.Code,
572577
keypath string,
573578
name string,
574579
activeTokens []string,
@@ -607,11 +612,11 @@ func (backend *Backend) persistETHAccountConfig(
607612
}
608613

609614
return backend.persistAccount(config.Account{
610-
CoinCode: coin.Code(),
611-
Name: name,
612-
Code: code,
613-
Configurations: signingConfigurations,
614-
ActiveTokens: activeTokens,
615+
CoinCode: coin.Code(),
616+
Name: name,
617+
Code: code,
618+
SigningConfigurations: signingConfigurations,
619+
ActiveTokens: activeTokens,
615620
}, accountsConfig)
616621
}
617622

@@ -627,12 +632,13 @@ func (backend *Backend) initPersistedAccounts() {
627632
return
628633
}
629634
keystoreConnected := func(account *config.Account) bool {
630-
return account.Configurations.ContainsRootFingerprint(rootFingerprint)
635+
return account.SigningConfigurations.ContainsRootFingerprint(rootFingerprint)
631636
}
632637

633638
persistedAccounts := backend.config.AccountsConfig()
634639
outer:
635640
for _, account := range backend.filterAccounts(&persistedAccounts, keystoreConnected) {
641+
account := account
636642
coin, err := backend.Coin(account.CoinCode)
637643
if err != nil {
638644
backend.log.Errorf("skipping persisted account %s/%s, could not find coin",
@@ -641,7 +647,7 @@ outer:
641647
}
642648
switch coin.(type) {
643649
case *btc.Coin:
644-
for _, cfg := range account.Configurations {
650+
for _, cfg := range account.SigningConfigurations {
645651
if !backend.keystore.SupportsAccount(coin, cfg.ScriptType()) {
646652
continue outer
647653
}
@@ -652,8 +658,7 @@ outer:
652658
}
653659
}
654660

655-
backend.createAndAddAccount(
656-
coin, account.Code, account.Name, account.Configurations, !account.Inactive, account.ActiveTokens)
661+
backend.createAndAddAccount(coin, account)
657662
}
658663
}
659664

@@ -726,7 +731,7 @@ func (backend *Backend) maybeAddP2TR(keystore keystore.Keystore, accounts []*con
726731
return err
727732
}
728733
if keystore.SupportsAccount(coin, signing.ScriptTypeP2TR) &&
729-
account.Configurations.FindScriptType(signing.ScriptTypeP2TR) == -1 {
734+
account.SigningConfigurations.FindScriptType(signing.ScriptTypeP2TR) == -1 {
730735
rootFingerprint, err := backend.keystore.RootFingerprint()
731736
if err != nil {
732737
return err
@@ -735,7 +740,7 @@ func (backend *Backend) maybeAddP2TR(keystore keystore.Keystore, accounts []*con
735740
if account.CoinCode == coinpkg.CodeBTC {
736741
bip44Coin = hardenedKeystart
737742
}
738-
accountNumber, err := account.Configurations[0].AccountNumber()
743+
accountNumber, err := account.SigningConfigurations[0].AccountNumber()
739744
if err != nil {
740745
return err
741746
}
@@ -747,8 +752,8 @@ func (backend *Backend) maybeAddP2TR(keystore keystore.Keystore, accounts []*con
747752
if err != nil {
748753
return err
749754
}
750-
account.Configurations = append(
751-
account.Configurations,
755+
account.SigningConfigurations = append(
756+
account.SigningConfigurations,
752757
signing.NewBitcoinConfiguration(
753758
signing.ScriptTypeP2TR,
754759
rootFingerprint,

0 commit comments

Comments
 (0)