@@ -7700,12 +7700,21 @@ type CloseOutput struct {
77007700 IsLocal bool
77017701}
77027702
7703+ // CloseSortFunc is a function type alias for a function that sorts the closing
7704+ // transaction.
7705+ type CloseSortFunc func (* wire.MsgTx ) error
7706+
77037707// chanCloseOpt is a functional option that can be used to modify the co-op
77047708// close process.
77057709type chanCloseOpt struct {
77067710 musigSession * MusigSession
77077711
77087712 extraCloseOutputs []CloseOutput
7713+
7714+ // customSort is a custom function that can be used to sort the
7715+ // transaction outputs. If this isn't set, then the default BIP-69
7716+ // sorting is used.
7717+ customSort CloseSortFunc
77097718}
77107719
77117720// ChanCloseOpt is a closure type that cen be used to modify the set of default
@@ -7734,6 +7743,14 @@ func WithExtraCloseOutputs(extraOutputs []CloseOutput) ChanCloseOpt {
77347743 }
77357744}
77367745
7746+ // WithCustomCoopSort can be used to modify the way the co-op close transaction
7747+ // is sorted.
7748+ func WithCustomCoopSort (sorter CloseSortFunc ) ChanCloseOpt {
7749+ return func (opts * chanCloseOpt ) {
7750+ opts .customSort = sorter
7751+ }
7752+ }
7753+
77377754// CreateCloseProposal is used by both parties in a cooperative channel close
77387755// workflow to generate proposed close transactions and signatures. This method
77397756// should only be executed once all pending HTLCs (if any) on the channel have
@@ -7784,12 +7801,20 @@ func (lc *LightningChannel) CreateCloseProposal(proposedFee btcutil.Amount,
77847801 opts .extraCloseOutputs ,
77857802 ))
77867803 }
7804+ if opts .customSort != nil {
7805+ closeTxOpts = append (
7806+ closeTxOpts , WithCustomTxSort (opts .customSort ),
7807+ )
7808+ }
77877809
7788- closeTx := CreateCooperativeCloseTx (
7810+ closeTx , err := CreateCooperativeCloseTx (
77897811 fundingTxIn (lc .channelState ), lc .channelState .LocalChanCfg .DustLimit ,
77907812 lc .channelState .RemoteChanCfg .DustLimit , ourBalance , theirBalance ,
77917813 localDeliveryScript , remoteDeliveryScript , closeTxOpts ... ,
77927814 )
7815+ if err != nil {
7816+ return nil , nil , 0 , err
7817+ }
77937818
77947819 // Ensure that the transaction doesn't explicitly violate any
77957820 // consensus rules such as being too big, or having any value with a
@@ -7874,15 +7899,23 @@ func (lc *LightningChannel) CompleteCooperativeClose(
78747899 opts .extraCloseOutputs ,
78757900 ))
78767901 }
7902+ if opts .customSort != nil {
7903+ closeTxOpts = append (
7904+ closeTxOpts , WithCustomTxSort (opts .customSort ),
7905+ )
7906+ }
78777907
78787908 // Create the transaction used to return the current settled balance
78797909 // on this active channel back to both parties. In this current model,
78807910 // the initiator pays full fees for the cooperative close transaction.
7881- closeTx := CreateCooperativeCloseTx (
7911+ closeTx , err := CreateCooperativeCloseTx (
78827912 fundingTxIn (lc .channelState ), lc .channelState .LocalChanCfg .DustLimit ,
78837913 lc .channelState .RemoteChanCfg .DustLimit , ourBalance , theirBalance ,
78847914 localDeliveryScript , remoteDeliveryScript , closeTxOpts ... ,
78857915 )
7916+ if err != nil {
7917+ return nil , 0 , err
7918+ }
78867919
78877920 // Ensure that the transaction doesn't explicitly validate any
78887921 // consensus rules such as being too big, or having any value with a
@@ -8584,6 +8617,11 @@ type closeTxOpts struct {
85848617 // extraCloseOutputs is a set of additional outputs that should be
85858618 // added the co-op close transaction.
85868619 extraCloseOutputs []CloseOutput
8620+
8621+ // customSort is a custom function that can be used to sort the
8622+ // transaction outputs. If this isn't set, then the default BIP-69
8623+ // sorting is used.
8624+ customSort CloseSortFunc
85878625}
85888626
85898627// defaultCloseTxOpts returns a closeTxOpts struct with default values.
@@ -8612,6 +8650,14 @@ func WithExtraTxCloseOutputs(extraOutputs []CloseOutput) CloseTxOpt {
86128650 }
86138651}
86148652
8653+ // WithCustomTxSort can be used to modify the way the close transaction is
8654+ // sorted.
8655+ func WithCustomTxSort (sorter CloseSortFunc ) CloseTxOpt {
8656+ return func (opts * closeTxOpts ) {
8657+ opts .customSort = sorter
8658+ }
8659+ }
8660+
86158661// CreateCooperativeCloseTx creates a transaction which if signed by both
86168662// parties, then broadcast cooperatively closes an active channel. The creation
86178663// of the closure transaction is modified by a boolean indicating if the party
@@ -8621,7 +8667,7 @@ func WithExtraTxCloseOutputs(extraOutputs []CloseOutput) CloseTxOpt {
86218667func CreateCooperativeCloseTx (fundingTxIn wire.TxIn ,
86228668 localDust , remoteDust , ourBalance , theirBalance btcutil.Amount ,
86238669 ourDeliveryScript , theirDeliveryScript []byte ,
8624- closeOpts ... CloseTxOpt ) * wire.MsgTx {
8670+ closeOpts ... CloseTxOpt ) ( * wire.MsgTx , error ) {
86258671
86268672 opts := defaultCloseTxOpts ()
86278673 for _ , optFunc := range closeOpts {
@@ -8754,9 +8800,15 @@ func CreateCooperativeCloseTx(fundingTxIn wire.TxIn,
87548800 }
87558801 }
87568802
8757- txsort .InPlaceSort (closeTx )
8803+ if opts .customSort != nil {
8804+ if err := opts .customSort (closeTx ); err != nil {
8805+ return nil , err
8806+ }
8807+ } else {
8808+ txsort .InPlaceSort (closeTx )
8809+ }
87588810
8759- return closeTx
8811+ return closeTx , nil
87608812}
87618813
87628814// LocalBalanceDust returns true if when creating a co-op close transaction,
0 commit comments