|
| 1 | +package funding |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/btcsuite/btcd/chaincfg/chainhash" |
| 5 | + "github.com/lightningnetwork/lnd/channeldb" |
| 6 | + "github.com/lightningnetwork/lnd/fn" |
| 7 | + "github.com/lightningnetwork/lnd/lnwallet" |
| 8 | + "github.com/lightningnetwork/lnd/protofsm" |
| 9 | +) |
| 10 | + |
| 11 | +// AuxFundingController permits the implementation of the funding of custom |
| 12 | +// channels types. The controller serves as a MsgEndpoint which allows it to |
| 13 | +// intercept custom messages, or even the regular funding messages. The |
| 14 | +// controller might also pass along an aux funding desc based on an existing |
| 15 | +// pending channel ID. |
| 16 | +type AuxFundingController interface { |
| 17 | + // MsgEndpoint is the embedded interface that signals that the funding |
| 18 | + // controller is also a message endpoint. This'll allow it to handle |
| 19 | + // custom messages specific to the funding type. |
| 20 | + protofsm.MsgEndpoint |
| 21 | + |
| 22 | + // DescFromPendingChanID takes a pending channel ID, that may already be |
| 23 | + // known due to prior custom channel messages, and maybe returns an aux |
| 24 | + // funding desc which can be used to modify how a channel is funded. |
| 25 | + DescFromPendingChanID(pid PendingChanID, |
| 26 | + openChan *channeldb.OpenChannel, |
| 27 | + localKeyRing, remoteKeyRing lnwallet.CommitmentKeyRing, |
| 28 | + initiator bool) (fn.Option[lnwallet.AuxFundingDesc], error) |
| 29 | + |
| 30 | + // DeriveTapscriptRoot takes a pending channel ID and maybe returns a |
| 31 | + // tapscript root that should be used when creating any MuSig2 sessions |
| 32 | + // for a channel. |
| 33 | + DeriveTapscriptRoot(PendingChanID) (fn.Option[chainhash.Hash], error) |
| 34 | + |
| 35 | + // ChannelReady is called when a channel has been fully opened (multiple |
| 36 | + // confirmations) and is ready to be used. This can be used to perform |
| 37 | + // any final setup or cleanup. |
| 38 | + ChannelReady(openChan *channeldb.OpenChannel) error |
| 39 | + |
| 40 | + // ChannelFinalized is called when a channel has been fully finalized. |
| 41 | + // In this state, we've received the commitment sig from the remote |
| 42 | + // party, so we are safe to broadcast the funding transaction. |
| 43 | + ChannelFinalized(PendingChanID) error |
| 44 | +} |
| 45 | + |
| 46 | +// descFromPendingChanID takes a pending channel ID, that may already be known |
| 47 | +// due to prior custom channel messages, and maybe returns an aux funding desc |
| 48 | +// which can be used to modify how a channel is funded. |
| 49 | +func descFromPendingChanID(controller fn.Option[AuxFundingController], |
| 50 | + chanID PendingChanID, openChan *channeldb.OpenChannel, |
| 51 | + localKeyRing, remoteKeyRing lnwallet.CommitmentKeyRing, |
| 52 | + initiator bool) (fn.Option[lnwallet.AuxFundingDesc], error) { |
| 53 | + |
| 54 | + var result fn.Option[lnwallet.AuxFundingDesc] |
| 55 | + mapErr := fn.MapOptionZ(controller, func(c AuxFundingController) error { |
| 56 | + var err error |
| 57 | + result, err = c.DescFromPendingChanID( |
| 58 | + chanID, openChan, localKeyRing, remoteKeyRing, |
| 59 | + initiator, |
| 60 | + ) |
| 61 | + |
| 62 | + return err |
| 63 | + }) |
| 64 | + |
| 65 | + return result, mapErr |
| 66 | +} |
| 67 | + |
| 68 | +// deriveTapscriptRoot takes a pending channel ID and maybe returns a |
| 69 | +// tapscript root that should be used when creating any MuSig2 sessions for a |
| 70 | +// channel. |
| 71 | +func deriveTapscriptRoot(controller fn.Option[AuxFundingController], |
| 72 | + chanID PendingChanID) (fn.Option[chainhash.Hash], error) { |
| 73 | + |
| 74 | + var result fn.Option[chainhash.Hash] |
| 75 | + mapErr := fn.MapOptionZ(controller, func(c AuxFundingController) error { |
| 76 | + var err error |
| 77 | + result, err = c.DeriveTapscriptRoot(chanID) |
| 78 | + return err |
| 79 | + }) |
| 80 | + |
| 81 | + return result, mapErr |
| 82 | +} |
0 commit comments