Skip to content

Commit 2c2c427

Browse files
committed
sweepbatcher: factor out method createPsbt
Unload method publishBatchCoop. Also this code will be reused in new method for mixed batches soon.
1 parent 5cd6c0c commit 2c2c427

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

sweepbatcher/sweep_batch.go

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,47 @@ func (b *batch) publishBatch(ctx context.Context) (btcutil.Amount, error) {
922922
return fee, nil
923923
}
924924

925+
// createPsbt creates serialized PSBT and prevOuts map from unsignedTx and
926+
// the list of sweeps.
927+
func (b *batch) createPsbt(unsignedTx *wire.MsgTx, sweeps []sweep) ([]byte,
928+
map[wire.OutPoint]*wire.TxOut, error) {
929+
930+
// Create PSBT packet object.
931+
packet, err := psbt.NewFromUnsignedTx(unsignedTx)
932+
if err != nil {
933+
return nil, nil, fmt.Errorf("failed to create PSBT: %w", err)
934+
}
935+
936+
// Sanity check: the number of inputs in PSBT must be equal to the
937+
// number of sweeps.
938+
if len(packet.Inputs) != len(sweeps) {
939+
return nil, nil, fmt.Errorf("invalid number of packet inputs")
940+
}
941+
942+
// Create prevOuts map.
943+
prevOuts := make(map[wire.OutPoint]*wire.TxOut, len(sweeps))
944+
945+
// Fill input info in PSBT and prevOuts.
946+
for i, sweep := range sweeps {
947+
txOut := &wire.TxOut{
948+
Value: int64(sweep.value),
949+
PkScript: sweep.htlc.PkScript,
950+
}
951+
952+
prevOuts[sweep.outpoint] = txOut
953+
packet.Inputs[i].WitnessUtxo = txOut
954+
}
955+
956+
// Serialize PSBT.
957+
var psbtBuf bytes.Buffer
958+
err = packet.Serialize(&psbtBuf)
959+
if err != nil {
960+
return nil, nil, fmt.Errorf("failed to serialize PSBT: %w", err)
961+
}
962+
963+
return psbtBuf.Bytes(), prevOuts, nil
964+
}
965+
925966
// publishBatchCoop attempts to construct and publish a batch transaction that
926967
// collects all the required signatures interactively from the server. This
927968
// helps with collecting the funds immediately without revealing any information
@@ -1013,36 +1054,15 @@ func (b *batch) publishBatchCoop(ctx context.Context) (btcutil.Amount,
10131054
Value: int64(batchAmt - fee),
10141055
})
10151056

1016-
packet, err := psbt.NewFromUnsignedTx(batchTx)
1017-
if err != nil {
1018-
return fee, err, false
1019-
}
1020-
1021-
if len(packet.Inputs) != len(sweeps) {
1022-
return fee, fmt.Errorf("invalid number of packet inputs"), false
1023-
}
1024-
1025-
prevOuts := make(map[wire.OutPoint]*wire.TxOut)
1026-
1027-
for i, sweep := range sweeps {
1028-
txOut := &wire.TxOut{
1029-
Value: int64(sweep.value),
1030-
PkScript: sweep.htlc.PkScript,
1031-
}
1032-
1033-
prevOuts[sweep.outpoint] = txOut
1034-
packet.Inputs[i].WitnessUtxo = txOut
1035-
}
1036-
1037-
var psbtBuf bytes.Buffer
1038-
err = packet.Serialize(&psbtBuf)
1057+
// Create PSBT and prevOuts.
1058+
psbtBytes, prevOuts, err := b.createPsbt(batchTx, sweeps)
10391059
if err != nil {
10401060
return fee, err, false
10411061
}
10421062

10431063
// Attempt to cooperatively sign the batch tx with the server.
10441064
err = b.coopSignBatchTx(
1045-
ctx, batchTx, sweeps, prevOuts, psbtBuf.Bytes(),
1065+
ctx, batchTx, sweeps, prevOuts, psbtBytes,
10461066
)
10471067
if err != nil {
10481068
return fee, err, false

0 commit comments

Comments
 (0)