Skip to content

Commit

Permalink
multi-scope: set DefaultScope to KeyScopeBIP0044
Browse files Browse the repository at this point in the history
By design, all scopes shared the same account name/number space.
  • Loading branch information
roylee17 committed Sep 26, 2022
1 parent 49cb544 commit d5ed29d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
3 changes: 3 additions & 0 deletions waddrmgr/scoped_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ var (
Coin: 140,
}

// Set Default Scope to BIP0044 (legacy address type).
DefaultKeyScope = KeyScopeBIP0044

// DefaultKeyScopes is the set of default key scopes that will be
// created by the root manager upon initial creation.
DefaultKeyScopes = []KeyScope{
Expand Down
16 changes: 12 additions & 4 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,12 @@ func (w *Wallet) AddressInfo(a btcutil.Address) (waddrmgr.ManagedAddress, error)

// AccountNumber returns the account number for an account name under a
// particular key scope.
func (w *Wallet) AccountNumber(scope waddrmgr.KeyScope, accountName string) (uint32, error) {
func (w *Wallet) AccountNumber(accountName string) (uint32, error) {
// By design, the same account number is shared across all scopes.
return w.accountNumber(waddrmgr.DefaultKeyScope, accountName)
}

func (w *Wallet) accountNumber(scope waddrmgr.KeyScope, accountName string) (uint32, error) {
manager, err := w.Manager.FetchScopedKeyManager(scope)
if err != nil {
return 0, err
Expand All @@ -1698,15 +1703,19 @@ func (w *Wallet) AccountNumber(scope waddrmgr.KeyScope, accountName string) (uin
var account uint32
err = walletdb.View(w.db, func(tx walletdb.ReadTx) error {
addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
var err error
account, err = manager.LookupAccount(addrmgrNs, accountName)
return err
})
return account, err
}

// AccountName returns the name of an account.
func (w *Wallet) AccountName(scope waddrmgr.KeyScope, accountNumber uint32) (string, error) {
func (w *Wallet) AccountName(accountNumber uint32) (string, error) {
// By design, the same account name is shared across all scopes.
return w.accountName(waddrmgr.DefaultKeyScope, accountNumber)
}

func (w *Wallet) accountName(scope waddrmgr.KeyScope, accountNumber uint32) (string, error) {
manager, err := w.Manager.FetchScopedKeyManager(scope)
if err != nil {
return "", err
Expand All @@ -1715,7 +1724,6 @@ func (w *Wallet) AccountName(scope waddrmgr.KeyScope, accountNumber uint32) (str
var accountName string
err = walletdb.View(w.db, func(tx walletdb.ReadTx) error {
addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
var err error
accountName, err = manager.AccountName(addrmgrNs, accountNumber)
return err
})
Expand Down

0 comments on commit d5ed29d

Please sign in to comment.