Skip to content

Commit

Permalink
psbt: add PrevOutFetcher helper func
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Jun 15, 2024
1 parent 0191dd5 commit 51321c8
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions btcutil/psbt/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,3 +469,36 @@ func FindLeafScript(pInput *PInput,
return nil, fmt.Errorf("leaf script for target leaf hash %x not "+
"found in input", targetLeafHash)
}

// PrevOutputFetcher returns a txscript.PrevOutFetcher built from the UTXO
// information in a PSBT packet.
func PrevOutputFetcher(packet *Packet) *txscript.MultiPrevOutFetcher {
fetcher := txscript.NewMultiPrevOutFetcher(nil)
for idx, txIn := range packet.UnsignedTx.TxIn {
in := packet.Inputs[idx]

// Skip any input that has no UTXO.
if in.WitnessUtxo == nil && in.NonWitnessUtxo == nil {
continue
}

if in.NonWitnessUtxo != nil {
prevIndex := txIn.PreviousOutPoint.Index
fetcher.AddPrevOut(
txIn.PreviousOutPoint,
in.NonWitnessUtxo.TxOut[prevIndex],
)

continue
}

// Fall back to witness UTXO only for older wallets.
if in.WitnessUtxo != nil {
fetcher.AddPrevOut(
txIn.PreviousOutPoint, in.WitnessUtxo,
)
}
}

return fetcher
}

0 comments on commit 51321c8

Please sign in to comment.