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.
This commit is contained in:
Conner Fromknecht 2018-03-12 16:31:56 -07:00
parent 2447f3097f
commit cb7f34895c
No known key found for this signature in database
GPG Key ID: 39DE78FBE6ACB0EF

View File

@ -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
}