@@ -18,7 +18,6 @@ import (
1818 "github.com/btcsuite/btcd/txscript"
1919 "github.com/btcsuite/btcd/wire"
2020 "github.com/btcsuite/btclog"
21- "github.com/btcsuite/btcwallet/waddrmgr"
2221 "github.com/davecgh/go-spew/spew"
2322 "github.com/lightningnetwork/lnd/buffer"
2423 "github.com/lightningnetwork/lnd/build"
@@ -36,6 +35,7 @@ import (
3635 "github.com/lightningnetwork/lnd/htlcswitch/hop"
3736 "github.com/lightningnetwork/lnd/input"
3837 "github.com/lightningnetwork/lnd/invoices"
38+ "github.com/lightningnetwork/lnd/keychain"
3939 "github.com/lightningnetwork/lnd/lnpeer"
4040 "github.com/lightningnetwork/lnd/lntypes"
4141 "github.com/lightningnetwork/lnd/lnutils"
@@ -901,70 +901,31 @@ func (p *Brontide) QuitSignal() <-chan struct{} {
901901 return p .quit
902902}
903903
904- // internalKeyForAddr returns the internal key associated with a taproot
905- // address.
906- func internalKeyForAddr (wallet * lnwallet.LightningWallet ,
907- deliveryScript []byte ) (fn.Option [btcec.PublicKey ], error ) {
908-
909- none := fn .None [btcec.PublicKey ]()
910-
911- pkScript , err := txscript .ParsePkScript (deliveryScript )
912- if err != nil {
913- return none , err
914- }
915- addr , err := pkScript .Address (& wallet .Cfg .NetParams )
916- if err != nil {
917- return none , err
918- }
919-
920- walletAddr , err := wallet .AddressInfo (addr )
921- if err != nil {
922- return none , err
923- }
924-
925- // If the address isn't known to the wallet, we can't determine the
926- // internal key.
927- if walletAddr == nil {
928- return none , nil
929- }
930-
931- // If it's not a taproot address, we don't require to know the internal
932- // key in the first place. So we don't return an error here, but also no
933- // internal key.
934- if walletAddr .AddrType () != waddrmgr .TaprootPubKey {
935- return none , nil
936- }
937-
938- pubKeyAddr , ok := walletAddr .(waddrmgr.ManagedPubKeyAddress )
939- if ! ok {
940- return none , fmt .Errorf ("expected pubkey addr, got %T" ,
941- pubKeyAddr )
942- }
943-
944- return fn .Some (* pubKeyAddr .PubKey ()), nil
945- }
946-
947904// addrWithInternalKey takes a delivery script, then attempts to supplement it
948905// with information related to the internal key for the addr, but only if it's
949906// a taproot addr.
950907func (p * Brontide ) addrWithInternalKey (
951- deliveryScript []byte ) fn. Result [ chancloser.DeliveryAddrWithKey ] {
908+ deliveryScript []byte ) ( * chancloser.DeliveryAddrWithKey , error ) {
952909
953- // TODO(roasbeef): not compatible with external shutdown addr?
954910 // Currently, custom channels cannot be created with external upfront
955911 // shutdown addresses, so this shouldn't be an issue. We only require
956912 // the internal key for taproot addresses to be able to provide a non
957913 // inclusion proof of any scripts.
958-
959- internalKey , err := internalKeyForAddr (p .cfg .Wallet , deliveryScript )
914+ internalKeyDesc , err := lnwallet .InternalKeyForAddr (
915+ p .cfg .Wallet , & p .cfg .Wallet .Cfg .NetParams , deliveryScript ,
916+ )
960917 if err != nil {
961- return fn. Err [chancloser. DeliveryAddrWithKey ]( err )
918+ return nil , fmt . Errorf ( "unable to fetch internal key: %w" , err )
962919 }
963920
964- return fn . Ok ( chancloser.DeliveryAddrWithKey {
921+ return & chancloser.DeliveryAddrWithKey {
965922 DeliveryAddress : deliveryScript ,
966- InternalKey : internalKey ,
967- })
923+ InternalKey : fn .MapOption (
924+ func (desc keychain.KeyDescriptor ) btcec.PublicKey {
925+ return * desc .PubKey
926+ },
927+ )(internalKeyDesc ),
928+ }, nil
968929}
969930
970931// loadActiveChannels creates indexes within the peer for tracking all active
@@ -1038,6 +999,8 @@ func (p *Brontide) loadActiveChannels(chans []*channeldb.OpenChannel) (
1038999 }
10391000 }
10401001
1002+ // TODO(roasbeef): also make aux resolver here
1003+
10411004 var chanOpts []lnwallet.ChannelOpt
10421005 p .cfg .AuxLeafStore .WhenSome (func (s lnwallet.AuxLeafStore ) {
10431006 chanOpts = append (chanOpts , lnwallet .WithLeafStore (s ))
@@ -1209,7 +1172,7 @@ func (p *Brontide) loadActiveChannels(chans []*channeldb.OpenChannel) (
12091172
12101173 addr , err := p .addrWithInternalKey (
12111174 info .DeliveryScript .Val ,
1212- ). Unpack ()
1175+ )
12131176 if err != nil {
12141177 shutdownInfoErr = fmt .Errorf ("unable to make " +
12151178 "delivery addr: %w" , err )
@@ -2977,7 +2940,7 @@ func (p *Brontide) fetchActiveChanCloser(chanID lnwire.ChannelID) (
29772940 return nil , fmt .Errorf ("unable to estimate fee" )
29782941 }
29792942
2980- addr , err := p .addrWithInternalKey (deliveryScript ). Unpack ()
2943+ addr , err := p .addrWithInternalKey (deliveryScript )
29812944 if err != nil {
29822945 return nil , fmt .Errorf ("unable to parse addr: %w" , err )
29832946 }
@@ -3224,7 +3187,7 @@ func (p *Brontide) restartCoopClose(lnChan *lnwallet.LightningChannel) (
32243187 closingParty = lntypes .Local
32253188 }
32263189
3227- addr , err := p .addrWithInternalKey (deliveryScript ). Unpack ()
3190+ addr , err := p .addrWithInternalKey (deliveryScript )
32283191 if err != nil {
32293192 return nil , fmt .Errorf ("unable to parse addr: %w" , err )
32303193 }
@@ -3256,7 +3219,7 @@ func (p *Brontide) restartCoopClose(lnChan *lnwallet.LightningChannel) (
32563219// createChanCloser constructs a ChanCloser from the passed parameters and is
32573220// used to de-duplicate code.
32583221func (p * Brontide ) createChanCloser (channel * lnwallet.LightningChannel ,
3259- deliveryScript chancloser.DeliveryAddrWithKey ,
3222+ deliveryScript * chancloser.DeliveryAddrWithKey ,
32603223 fee chainfee.SatPerKWeight , req * htlcswitch.ChanClose ,
32613224 closer lntypes.ChannelParty ) (* chancloser.ChanCloser , error ) {
32623225
@@ -3291,7 +3254,7 @@ func (p *Brontide) createChanCloser(channel *lnwallet.LightningChannel,
32913254 ChainParams : & p .cfg .Wallet .Cfg .NetParams ,
32923255 Quit : p .quit ,
32933256 },
3294- deliveryScript ,
3257+ * deliveryScript ,
32953258 fee ,
32963259 uint32 (startingHeight ),
32973260 req ,
@@ -3350,7 +3313,7 @@ func (p *Brontide) handleLocalCloseReq(req *htlcswitch.ChanClose) {
33503313 return
33513314 }
33523315 }
3353- addr , err := p .addrWithInternalKey (deliveryScript ). Unpack ()
3316+ addr , err := p .addrWithInternalKey (deliveryScript )
33543317 if err != nil {
33553318 err = fmt .Errorf ("unable to parse addr for channel " +
33563319 "%v: %w" , req .ChanPoint , err )
0 commit comments