Skip to content

Commit 5d83d0e

Browse files
committed
tapchannel: properly create ContractResolution in createAndSignSweepVpackets
With all the prior commits in place, we can now create the new contract resolution that includes the 1st and 2nd level packets, and the information that we'll need to re-sign the second level packets later.
1 parent 3cbc5f3 commit 5d83d0e

File tree

1 file changed

+56
-22
lines changed

1 file changed

+56
-22
lines changed

tapchannel/aux_sweeper.go

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,35 +1675,69 @@ func (a *AuxSweeper) resolveContract(
16751675
log.Infof("Sweeping %v asset outputs: %v", len(assetOutputs),
16761676
limitSpewer.Sdump(assetOutputs))
16771677

1678-
firstLevelSweepDesc := lfn.AndThen(
1679-
sweepDesc,
1680-
func(sweepDesc tapscriptSweepDescs) lfn.Result[tapscriptSweepDesc] { //nolint:lll
1681-
return lfn.Ok(sweepDesc.firstLevel)
1682-
},
1683-
)
1678+
tapSweepDesc, err := sweepDesc.Unpack()
1679+
if err != nil {
1680+
return lfn.Err[tlv.Blob](err)
1681+
}
16841682

1685-
// With the sweep desc constructed above, we'll create vPackets for
1686-
// each of the local assets, then sign them all.
1687-
sPkts := a.createAndSignSweepVpackets(
1688-
assetOutputs, req, firstLevelSweepDesc,
1689-
)
1683+
// With the sweep desc constructed above, we'll create vPackets for each
1684+
// of the local assets, then sign them all.
1685+
firstLevelPkts, err := a.createAndSignSweepVpackets(
1686+
assetOutputs, req, lfn.Ok(tapSweepDesc.firstLevel),
1687+
).Unpack()
1688+
if err != nil {
1689+
return lfn.Err[tlv.Blob](err)
1690+
}
1691+
1692+
// We can't yet make the second level proof as we don't know exactly
1693+
// what the final txid of the sweep transaction will be. So we'll use a
1694+
// blank proof in place.
1695+
//
1696+
// TODO(roasbeef): need to set internal key? already known
1697+
var proof proof.Proof
1698+
1699+
// We'll make a place holder for the second level output based on the
1700+
// assetID+value tuple.
1701+
secondLevelInputs := []*cmsg.AssetOutput{cmsg.NewAssetOutput(
1702+
assetOutputs[0].AssetID.Val, assetOutputs[0].Amount.Val, proof,
1703+
)}
1704+
1705+
// Unlike the first level packets, we can't yet sign the second level
1706+
// packets yet, as we don't know what the sweeping transaction will look
1707+
// like. So we'll just create them.
1708+
secondLevelPkts, err := lfn.MapOption(
1709+
func(desc tapscriptSweepDesc) lfn.Result[[]*tappsbt.VPacket] {
1710+
return a.createSweepVpackets(
1711+
secondLevelInputs, lfn.Ok(desc), req,
1712+
)
1713+
},
1714+
)(tapSweepDesc.secondLevel).UnwrapOr(
1715+
lfn.Ok[[]*tappsbt.VPacket](nil),
1716+
).Unpack()
1717+
if err != nil {
1718+
return lfn.Err[tlv.Blob](err)
1719+
}
16901720

16911721
// With the vPackets fully generated and signed above, we'll serialize
16921722
// it into a resolution blob to return.
1693-
return lfn.AndThen(
1694-
sPkts, func(vPkts []*tappsbt.VPacket) lfn.Result[tlv.Blob] {
1695-
res := cmsg.NewContractResolution(
1696-
vPkts, nil, lfn.None[cmsg.TapscriptSigDesc](),
1723+
secondLevelSigDesc := lfn.MapOption(
1724+
func(d tapscriptSweepDesc) cmsg.TapscriptSigDesc {
1725+
return cmsg.NewTapscriptSigDesc(
1726+
d.scriptTree.TapTweak(), d.ctrlBlockBytes,
16971727
)
1698-
1699-
var b bytes.Buffer
1700-
if err := res.Encode(&b); err != nil {
1701-
return lfn.Err[returnType](err)
1702-
}
1703-
1704-
return lfn.Ok(b.Bytes())
17051728
},
1729+
)(tapSweepDesc.secondLevel)
1730+
1731+
res := cmsg.NewContractResolution(
1732+
firstLevelPkts, secondLevelPkts, secondLevelSigDesc,
17061733
)
1734+
1735+
var b bytes.Buffer
1736+
if err := res.Encode(&b); err != nil {
1737+
return lfn.Err[tlv.Blob](err)
1738+
}
1739+
1740+
return lfn.Ok(b.Bytes())
17071741
}
17081742

17091743
// preimageDesc is a helper struct that contains the preimage and the witness

0 commit comments

Comments
 (0)