decred.org/dcrwallet/v3@v3.1.0/wallet/cointype.go (about)

     1  // Copyright (c) 2017 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package wallet
     6  
     7  import (
     8  	"context"
     9  
    10  	"decred.org/dcrwallet/v3/errors"
    11  	"decred.org/dcrwallet/v3/wallet/udb"
    12  	"decred.org/dcrwallet/v3/wallet/walletdb"
    13  	"github.com/decred/dcrd/hdkeychain/v3"
    14  )
    15  
    16  // UpgradeToSLIP0044CoinType upgrades the wallet from the legacy BIP0044 coin
    17  // type to one of the coin types assigned to Decred in SLIP0044.  This should be
    18  // called after a new wallet is created with a random (not imported) seed.
    19  //
    20  // This function does not register addresses from the new account 0 with the
    21  // wallet's network backend.  This is intentional as it allows offline
    22  // activities, such as wallet creation, to perform this upgrade.
    23  func (w *Wallet) UpgradeToSLIP0044CoinType(ctx context.Context) error {
    24  	const op errors.Op = "wallet.UpgradeToSLIP0044CoinType"
    25  
    26  	var acctXpub, extBranchXpub, intBranchXpub *hdkeychain.ExtendedKey
    27  
    28  	err := walletdb.Update(ctx, w.db, func(dbtx walletdb.ReadWriteTx) error {
    29  		err := w.manager.UpgradeToSLIP0044CoinType(dbtx)
    30  		if err != nil {
    31  			return err
    32  		}
    33  
    34  		acctXpub, err = w.manager.AccountExtendedPubKey(dbtx, 0)
    35  		if err != nil {
    36  			return err
    37  		}
    38  
    39  		extBranchXpub, err = w.manager.AccountBranchExtendedPubKey(dbtx, 0,
    40  			udb.ExternalBranch)
    41  		if err != nil {
    42  			return err
    43  		}
    44  		intBranchXpub, err = w.manager.AccountBranchExtendedPubKey(dbtx, 0,
    45  			udb.InternalBranch)
    46  		return err
    47  	})
    48  	if err != nil {
    49  		return errors.E(op, err)
    50  	}
    51  
    52  	w.addressBuffersMu.Lock()
    53  	w.addressBuffers[0] = &bip0044AccountData{
    54  		xpub:        acctXpub,
    55  		albExternal: addressBuffer{branchXpub: extBranchXpub, lastUsed: ^uint32(0)},
    56  		albInternal: addressBuffer{branchXpub: intBranchXpub, lastUsed: ^uint32(0)},
    57  	}
    58  	w.addressBuffersMu.Unlock()
    59  
    60  	return nil
    61  }