keychain: ensure we properly set the KeyLocator for keys from DeriveNextKey

In this commit, we fix a slight bug in the existing implementation of
DeriveNextKey for btcwallet. Before this commit, we would only set the
public key, and not also the derivation path. It's important that we
also set the path information, as in the near future we'll be using the
KeyDescriptors returned from this method to create static channel back
ups. With these static backups, the key alone may be insufficient to
re-derive the private key as we may need to fallback to brute forcing in
order to re-derive the key as it's possible we add new key families in
the future.
This commit is contained in:
Olaoluwa Osuntokun committed 2018-08-14 19:11:40 -07:00
1 parent e97e4c1f5d
commit ad25ae1a07
3 files changed
+28 -9

No files matched your search

+21 -3
View File
@@ -139,7 +139,10 @@ func (b *BtcWalletKeyRing) createAccountIfNotExists(
//
// NOTE: This is part of the keychain.KeyRing interface.
func (b *BtcWalletKeyRing) DeriveNextKey(keyFam KeyFamily) (KeyDescriptor, error) {
var pubKey *btcec.PublicKey
var (
pubKey *btcec.PublicKey
keyLoc KeyLocator
)
db := b.wallet.Database()
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
@@ -165,7 +168,21 @@ func (b *BtcWalletKeyRing) DeriveNextKey(keyFam KeyFamily) (KeyDescriptor, error
return err
}
pubKey = addrs[0].(waddrmgr.ManagedPubKeyAddress).PubKey()
// Extract the first address, ensuring that it is of the proper
// interface type, otherwise we can't manipulate it below.
addr, ok := addrs[0].(waddrmgr.ManagedPubKeyAddress)
if !ok {
return fmt.Errorf("address is not a managed pubkey " +
"addr")
}
pubKey = addr.PubKey()
_, pathInfo, _ := addr.DerivationInfo()
keyLoc = KeyLocator{
Family: keyFam,
Index: pathInfo.Index,
}
return nil
})
@@ -174,7 +191,8 @@ func (b *BtcWalletKeyRing) DeriveNextKey(keyFam KeyFamily) (KeyDescriptor, error
}
return KeyDescriptor{
PubKey: pubKey,
PubKey: pubKey,
KeyLocator: keyLoc,
}, nil
}