Merge pull request #824 from cfromknecht/multi-chain-coin-type
Configurable CoinType for HD Derivation
This commit is contained in:
8 files changed
+149
-51
No files matched your search
@@ -32,14 +32,6 @@ var (
|
||||
// stored within the top-level waleltdb buckets of btcwallet.
|
||||
waddrmgrNamespaceKey = []byte("waddrmgr")
|
||||
|
||||
// lightningKeyScope is the key scope that will be used within the
|
||||
// waddrmgr to create an HD chain for deriving all of our required
|
||||
// keys. We'll ensure this this scope is created upon start.
|
||||
lightningKeyScope = waddrmgr.KeyScope{
|
||||
Purpose: keychain.BIP0043Purpose,
|
||||
Coin: 0,
|
||||
}
|
||||
|
||||
// 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.
|
||||
@@ -65,6 +57,8 @@ type BtcWallet struct {
|
||||
|
||||
netParams *chaincfg.Params
|
||||
|
||||
chainKeyScope waddrmgr.KeyScope
|
||||
|
||||
// utxoCache is a cache used to speed up repeated calls to
|
||||
// FetchInputInfo.
|
||||
utxoCache map[wire.OutPoint]*wire.TxOut
|
||||
@@ -81,6 +75,12 @@ func New(cfg Config) (*BtcWallet, error) {
|
||||
// Ensure the wallet exists or create it when the create flag is set.
|
||||
netDir := NetworkDir(cfg.DataDir, cfg.NetParams)
|
||||
|
||||
// Create the key scope for the coin type being managed by this wallet.
|
||||
chainKeyScope := waddrmgr.KeyScope{
|
||||
Purpose: keychain.BIP0043Purpose,
|
||||
Coin: cfg.CoinType,
|
||||
}
|
||||
|
||||
var pubPass []byte
|
||||
if cfg.PublicPass == nil {
|
||||
pubPass = defaultPubPassphrase
|
||||
@@ -114,12 +114,13 @@ func New(cfg Config) (*BtcWallet, error) {
|
||||
}
|
||||
|
||||
return &BtcWallet{
|
||||
cfg: &cfg,
|
||||
wallet: wallet,
|
||||
db: wallet.Database(),
|
||||
chain: cfg.ChainSource,
|
||||
netParams: cfg.NetParams,
|
||||
utxoCache: make(map[wire.OutPoint]*wire.TxOut),
|
||||
cfg: &cfg,
|
||||
wallet: wallet,
|
||||
db: wallet.Database(),
|
||||
chain: cfg.ChainSource,
|
||||
netParams: cfg.NetParams,
|
||||
chainKeyScope: chainKeyScope,
|
||||
utxoCache: make(map[wire.OutPoint]*wire.TxOut),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -165,7 +166,7 @@ func (b *BtcWallet) Start() error {
|
||||
// We'll now ensure that the KeyScope: (1017, 1) exists within the
|
||||
// internal waddrmgr. We'll need this in order to properly generate the
|
||||
// keys required for signing various contracts.
|
||||
_, err := b.wallet.Manager.FetchScopedKeyManager(lightningKeyScope)
|
||||
_, err := b.wallet.Manager.FetchScopedKeyManager(b.chainKeyScope)
|
||||
if err != nil {
|
||||
// If the scope hasn't yet been created (it wouldn't been
|
||||
// loaded by default if it was), then we'll manually create the
|
||||
@@ -174,7 +175,7 @@ func (b *BtcWallet) Start() error {
|
||||
addrmgrNs := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
||||
|
||||
_, err := b.wallet.Manager.NewScopedKeyManager(
|
||||
addrmgrNs, lightningKeyScope, lightningAddrSchema,
|
||||
addrmgrNs, b.chainKeyScope, lightningAddrSchema,
|
||||
)
|
||||
return err
|
||||
})
|
||||
|
||||
@@ -75,6 +75,9 @@ type Config struct {
|
||||
|
||||
// NetParams is the net parameters for the target chain.
|
||||
NetParams *chaincfg.Params
|
||||
|
||||
// CoinType specifies the BIP 44 coin type to be used for derivation.
|
||||
CoinType uint32
|
||||
}
|
||||
|
||||
// NetworkDir returns the directory name of a network directory to hold wallet
|
||||
|
||||
@@ -83,7 +83,7 @@ func (b *BtcWallet) fetchPrivKey(keyDesc *keychain.KeyDescriptor) (*btcec.Privat
|
||||
if !keyDesc.KeyLocator.IsEmpty() {
|
||||
// We'll assume the special lightning key scope in this case.
|
||||
scopedMgr, err := b.wallet.Manager.FetchScopedKeyManager(
|
||||
lightningKeyScope,
|
||||
b.chainKeyScope,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -2149,6 +2149,7 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
|
||||
NetParams: netParams,
|
||||
ChainSource: aliceClient,
|
||||
FeeEstimator: feeEstimator,
|
||||
CoinType: keychain.CoinTypeTestnet,
|
||||
}
|
||||
aliceWalletController, err = walletDriver.New(aliceWalletConfig)
|
||||
if err != nil {
|
||||
@@ -2157,6 +2158,7 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
|
||||
aliceSigner = aliceWalletController.(*btcwallet.BtcWallet)
|
||||
aliceKeyRing = keychain.NewBtcWalletKeyRing(
|
||||
aliceWalletController.(*btcwallet.BtcWallet).InternalWallet(),
|
||||
keychain.CoinTypeTestnet,
|
||||
)
|
||||
|
||||
bobWalletConfig := &btcwallet.Config{
|
||||
@@ -2166,6 +2168,7 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
|
||||
NetParams: netParams,
|
||||
ChainSource: bobClient,
|
||||
FeeEstimator: feeEstimator,
|
||||
CoinType: keychain.CoinTypeTestnet,
|
||||
}
|
||||
bobWalletController, err = walletDriver.New(bobWalletConfig)
|
||||
if err != nil {
|
||||
@@ -2174,6 +2177,7 @@ func runTests(t *testing.T, walletDriver *lnwallet.WalletDriver,
|
||||
bobSigner = bobWalletController.(*btcwallet.BtcWallet)
|
||||
bobKeyRing = keychain.NewBtcWalletKeyRing(
|
||||
bobWalletController.(*btcwallet.BtcWallet).InternalWallet(),
|
||||
keychain.CoinTypeTestnet,
|
||||
)
|
||||
bio = bobWalletController.(*btcwallet.BtcWallet)
|
||||
default:
|
||||
|
||||
Reference in new issue
Block a user