Skip to content

Commit

Permalink
sweepbatcher: factor out method createPsbt
Browse files Browse the repository at this point in the history
Unload method publishBatchCoop. Also this code will be reused in new method
for mixed batches soon.
  • Loading branch information
starius committed Jul 18, 2024
1 parent 92e298e commit bed4b17
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions sweepbatcher/sweep_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,47 @@ func (b *batch) publishBatch(ctx context.Context) (btcutil.Amount, error) {
return fee, nil
}

// createPsbt creates serialized PSBT and prevOuts map from unsignedTx and
// the list of sweeps.
func (b *batch) createPsbt(unsignedTx *wire.MsgTx, sweeps []sweep) ([]byte,
map[wire.OutPoint]*wire.TxOut, error) {

// Create PSBT packet object.
packet, err := psbt.NewFromUnsignedTx(unsignedTx)
if err != nil {
return nil, nil, fmt.Errorf("failed to create PSBT: %w", err)
}

// Sanity check: the number of inputs in PSBT must be equal to the
// number of sweeps.
if len(packet.Inputs) != len(sweeps) {
return nil, nil, fmt.Errorf("invalid number of packet inputs")
}

// Create prevOuts map.
prevOuts := make(map[wire.OutPoint]*wire.TxOut, len(sweeps))

// Fill input info in PSBT and prevOuts.
for i, sweep := range sweeps {
txOut := &wire.TxOut{
Value: int64(sweep.value),
PkScript: sweep.htlc.PkScript,
}

prevOuts[sweep.outpoint] = txOut
packet.Inputs[i].WitnessUtxo = txOut
}

// Serialize PSBT.
var psbtBuf bytes.Buffer
err = packet.Serialize(&psbtBuf)
if err != nil {
return nil, nil, fmt.Errorf("failed to serialize PSBT: %w", err)
}

return psbtBuf.Bytes(), prevOuts, nil
}

// publishBatchCoop attempts to construct and publish a batch transaction that
// collects all the required signatures interactively from the server. This
// helps with collecting the funds immediately without revealing any information
Expand Down Expand Up @@ -898,36 +939,15 @@ func (b *batch) publishBatchCoop(ctx context.Context) (btcutil.Amount,
Value: int64(batchAmt - fee),
})

packet, err := psbt.NewFromUnsignedTx(batchTx)
if err != nil {
return fee, err, false
}

if len(packet.Inputs) != len(sweeps) {
return fee, fmt.Errorf("invalid number of packet inputs"), false
}

prevOuts := make(map[wire.OutPoint]*wire.TxOut)

for i, sweep := range sweeps {
txOut := &wire.TxOut{
Value: int64(sweep.value),
PkScript: sweep.htlc.PkScript,
}

prevOuts[sweep.outpoint] = txOut
packet.Inputs[i].WitnessUtxo = txOut
}

var psbtBuf bytes.Buffer
err = packet.Serialize(&psbtBuf)
// Create PSBT and prevOuts.
psbtBytes, prevOuts, err := b.createPsbt(batchTx, sweeps)
if err != nil {
return fee, err, false
}

// Attempt to cooperatively sign the batch tx with the server.
err = b.coopSignBatchTx(
ctx, batchTx, sweeps, prevOuts, psbtBuf.Bytes(),
ctx, batchTx, sweeps, prevOuts, psbtBytes,
)
if err != nil {
return fee, err, false
Expand Down

0 comments on commit bed4b17

Please sign in to comment.