lnwallet+peer: move internalKeyForAddr to lnwallet package

This way we can re-use it. We also make it slightly more generalized.
This commit is contained in:
Olaoluwa Osuntokun
2024-06-03 23:00:38 -07:00
committed by Oliver Gugger
parent 74d27f2c7c
commit 07ee114116
5 changed files with 160 additions and 79 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/btcsuite/btcd/btcutil/psbt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
@@ -594,6 +595,68 @@ type MessageSigner interface {
doubleHash bool) (*ecdsa.Signature, error)
}
// AddrWithKey wraps a normal addr, but also includes the internal key for the
// delivery addr if known.
type AddrWithKey struct {
lnwire.DeliveryAddress
InternalKey fn.Option[btcec.PublicKey]
// TODO(roasbeef): consolidate w/ instance in chan closer
}
// InternalKeyForAddr returns the internal key associated with a taproot
// address.
func InternalKeyForAddr(wallet WalletController, netParams *chaincfg.Params,
deliveryScript []byte) (fn.Option[keychain.KeyDescriptor], error) {
none := fn.None[keychain.KeyDescriptor]()
pkScript, err := txscript.ParsePkScript(deliveryScript)
if err != nil {
return none, err
}
addr, err := pkScript.Address(netParams)
if err != nil {
return none, err
}
// If it's not a taproot address, we don't require to know the internal
// key in the first place. So we don't return an error here, but also no
// internal key.
_, isTaproot := addr.(*btcutil.AddressTaproot)
if !isTaproot {
return none, nil
}
walletAddr, err := wallet.AddressInfo(addr)
if err != nil {
return none, err
}
// No wallet addr. No error, but we'll return an nil error value here,
// as callers can use the .Option() method to get an option value.
if walletAddr == nil {
return none, nil
}
pubKeyAddr, ok := walletAddr.(waddrmgr.ManagedPubKeyAddress)
if !ok {
return none, fmt.Errorf("expected pubkey addr, got %T",
pubKeyAddr)
}
_, derivationPath, _ := pubKeyAddr.DerivationInfo()
return fn.Some[keychain.KeyDescriptor](keychain.KeyDescriptor{
KeyLocator: keychain.KeyLocator{
Family: keychain.KeyFamily(derivationPath.Account),
Index: derivationPath.Index,
},
PubKey: pubKeyAddr.PubKey(),
}), nil
}
// WalletDriver represents a "driver" for a particular concrete
// WalletController implementation. A driver is identified by a globally unique
// string identifier along with a 'New()' method which is responsible for