Skip to content

Commit 4e66ae7

Browse files
committed
tapchannel: make funding code order unaware
With the new funding logic now potentially adding the change output first, we need to make sure the channel funding logic doesn't use hard-coded indexes.
1 parent 4e68ceb commit 4e66ae7

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

tapchannel/aux_funding_controller.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -914,13 +914,16 @@ func (f *FundingController) sendInputOwnershipProofs(peerPub btcec.PublicKey,
914914
// We'll now send the signed inputs to the remote party.
915915
//
916916
// TODO(roasbeef): generalize for multi-asset
917-
fundingAsset := vPkt.Outputs[0].Asset.Copy()
917+
fundingOut, err := vPkt.FirstNonSplitRootOutput()
918+
if err != nil {
919+
return fmt.Errorf("unable to get funding asset: %w", err)
920+
}
918921
assetOutputMsg := cmsg.NewTxAssetOutputProof(
919-
fundingState.pid, *fundingAsset, true,
922+
fundingState.pid, *fundingOut.Asset, true,
920923
)
921924

922925
log.Debugf("Sending TLV for funding asset output to remote party: %v",
923-
limitSpewer.Sdump(fundingAsset))
926+
limitSpewer.Sdump(fundingOut.Asset))
924927

925928
err = f.cfg.PeerMessenger.SendMessage(ctx, peerPub, assetOutputMsg)
926929
if err != nil {
@@ -1167,12 +1170,22 @@ func (f *FundingController) completeChannelFunding(ctx context.Context,
11671170
fundingPackets := fundedVpkt.VPackets
11681171
for idx := range fundingPackets {
11691172
fundingPkt := fundingPackets[idx]
1170-
fundingPkt.Outputs[0].AnchorOutputBip32Derivation = nil
1171-
fundingPkt.Outputs[0].AnchorOutputTaprootBip32Derivation = nil
1173+
1174+
// The funding output is the first non-split output (the split
1175+
// output is only present if there is change from the channel
1176+
// funding).
1177+
fundingOut, err := fundingPkt.FirstNonSplitRootOutput()
1178+
if err != nil {
1179+
return nil, fmt.Errorf("unable to find funding output "+
1180+
"in funded packet: %w", err)
1181+
}
1182+
1183+
fundingOut.AnchorOutputBip32Derivation = nil
1184+
fundingOut.AnchorOutputTaprootBip32Derivation = nil
11721185
fundingInternalKeyDesc := keychain.KeyDescriptor{
11731186
PubKey: fundingInternalKey,
11741187
}
1175-
fundingPkt.Outputs[0].SetAnchorInternalKey(
1188+
fundingOut.SetAnchorInternalKey(
11761189
fundingInternalKeyDesc, f.cfg.ChainParams.HDCoinType,
11771190
)
11781191
}
@@ -1614,11 +1627,16 @@ func (f *FundingController) processFundingReq(fundingFlows fundingFlowIndex,
16141627
// we can derive the tapscript root that'll be used alongside the
16151628
// internal key (which we'll only learn from lnd later as we finalize
16161629
// the funding PSBT).
1617-
fundingAssets := fn.Map(
1618-
fundingVpkt.VPackets, func(pkt *tappsbt.VPacket) *asset.Asset {
1619-
return pkt.Outputs[0].Asset.Copy()
1620-
},
1621-
)
1630+
fundingAssets := make([]*asset.Asset, 0, len(fundingVpkt.VPackets))
1631+
for _, pkt := range fundingVpkt.VPackets {
1632+
fundingOut, err := pkt.FirstNonSplitRootOutput()
1633+
if err != nil {
1634+
return fmt.Errorf("unable to find funding output in "+
1635+
"packet: %w", err)
1636+
}
1637+
1638+
fundingAssets = append(fundingAssets, fundingOut.Asset.Copy())
1639+
}
16221640
fundingCommitVersion, err := tappsbt.CommitmentVersion(
16231641
fundingVpkt.VPackets[0].Version,
16241642
)

0 commit comments

Comments
 (0)