From cb7f34895c164cde330db7577cca629934524298 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Mon, 12 Mar 2018 16:31:56 -0700 Subject: [PATCH] keychain/btwallet: support coin type configuration This commit allows for secret keyrings to be initialized with a specific coin type, which allows us to use different derivation paths for bitcion and litecoin. It also provide default constants for Bitcion and Litecoin BIP 44 coin types. --- keychain/btcwallet.go | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/keychain/btcwallet.go b/keychain/btcwallet.go index 2d9f2d8b..8826e495 100644 --- a/keychain/btcwallet.go +++ b/keychain/btcwallet.go @@ -10,15 +10,21 @@ import ( "github.com/roasbeef/btcwallet/walletdb" ) -var ( - // lightningKeyScope is the key scope that will be used within the - // waddrmgr to create an HD chain for deriving all of our required - // keys. - lightningKeyScope = waddrmgr.KeyScope{ - Purpose: BIP0043Purpose, - Coin: 0, - } +const ( + // CoinTypeBitcoin specifies the BIP44 coin type for Bitcoin key + // derivation. + CoinTypeBitcoin uint32 = 0 + // CoinTypeTestnet specifies the BIP44 coin type for all testnet key + // derivation. + CoinTypeTestnet = 1 + + // CoinTypeLitecoin specifies the BIP44 coin type for Litecoin key + // derivation. + CoinTypeLitecoin = 2 +) + +var ( // lightningAddrSchema is the scope addr schema for all keys that we // derive. We'll treat them all as p2wkh addresses, as atm we must // specify a particular type. @@ -44,6 +50,10 @@ type BtcWalletKeyRing struct { // transactions in order to derive addresses and lookup relevant keys wallet *wallet.Wallet + // chainKeyScope defines the purpose and coin type to be used when generating + // keys for this keyring. + chainKeyScope waddrmgr.KeyScope + // lightningScope is a pointer to the scope that we'll be using as a // sub key manager to derive all the keys that we require. lightningScope *waddrmgr.ScopedKeyManager @@ -54,9 +64,18 @@ type BtcWalletKeyRing struct { // // NOTE: The passed waddrmgr.Manager MUST be unlocked in order for the keychain // to function. -func NewBtcWalletKeyRing(w *wallet.Wallet) SecretKeyRing { +func NewBtcWalletKeyRing(w *wallet.Wallet, coinType uint32) SecretKeyRing { + // Construct the key scope that will be used within the waddrmgr to + // create an HD chain for deriving all of our required keys. A different + // scope is used for each specific coin type. + chainKeyScope := waddrmgr.KeyScope{ + Purpose: BIP0043Purpose, + Coin: coinType, + } + return &BtcWalletKeyRing{ - wallet: w, + wallet: w, + chainKeyScope: chainKeyScope, } } @@ -80,9 +99,7 @@ func (b *BtcWalletKeyRing) keyScope() (*waddrmgr.ScopedKeyManager, error) { // If the manager is indeed unlocked, then we'll fetch the scope, cache // it, and return to the caller. - lnScope, err := b.wallet.Manager.FetchScopedKeyManager( - lightningKeyScope, - ) + lnScope, err := b.wallet.Manager.FetchScopedKeyManager(b.chainKeyScope) if err != nil { return nil, err }