From 1da054625b7685326382aeb85b77c9f95e4f81f0 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 7 Feb 2017 20:01:12 -0800 Subject: [PATCH] lnwallet/btcwallet: fix bug in non-recognition of self-funds in chan force close MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fixes a bug which would cause the node on the _receiving_ side of a channel force close to be blind of their immediately funds on-chain. The root of the issue was that within the btcwallet implementation of the WalletController method ‘NewRawKey’, the wallet wouldn’t request notifications for the new address, as the direct function from the waddrmgr was used which skips registration of the new address. To fix, this, we now ensure that btcwallet will receive notifications for keys used within the raw p2wkh commitment output. This ensures that the wallet is aware of funds that are made available as a result of a channel force closure by the channel counter party. --- lnwallet/btcwallet/btcwallet.go | 13 ++++++++----- lnwallet/interface.go | 3 +++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lnwallet/btcwallet/btcwallet.go b/lnwallet/btcwallet/btcwallet.go index 6e9a929e..119f2efa 100644 --- a/lnwallet/btcwallet/btcwallet.go +++ b/lnwallet/btcwallet/btcwallet.go @@ -235,18 +235,21 @@ func (b *BtcWallet) GetPrivKey(a btcutil.Address) (*btcec.PrivateKey, error) { // // This is a part of the WalletController interface. func (b *BtcWallet) NewRawKey() (*btcec.PublicKey, error) { - nextAddr, err := b.wallet.Manager.NextExternalAddresses(defaultAccount, - 1, waddrmgr.WitnessPubKey) + addr, err := b.wallet.NewAddress(defaultAccount, + waddrmgr.WitnessPubKey) if err != nil { return nil, err } - pkAddr := nextAddr[0].(waddrmgr.ManagedPubKeyAddress) + pkAddr, err := b.wallet.Manager.Address(addr) + if err != nil { + return nil, err + } - return pkAddr.PubKey(), nil + return pkAddr.(waddrmgr.ManagedPubKeyAddress).PubKey(), nil } -// FetchRootKey returns a root key which is meanted to be used as an initial +// FetchRootKey returns a root key which is intended to be used as an initial // seed/salt to generate any Lightning specific secrets. // // This is a part of the WalletController interface. diff --git a/lnwallet/interface.go b/lnwallet/interface.go index 7b8bafb6..a5507b3b 100644 --- a/lnwallet/interface.go +++ b/lnwallet/interface.go @@ -131,6 +131,9 @@ type WalletController interface { // NewRawKey returns a raw private key controlled by the wallet. These // keys are used for the 2-of-2 multi-sig outputs for funding // transactions, as well as the pub key used for commitment transactions. + // + // NOTE: The wallet MUST watch for on-chain outputs created to a p2wpkh + // script using keys returned by this function. NewRawKey() (*btcec.PublicKey, error) // FetchRootKey returns a root key which will be used by the