Skip to content

Commit 29a0d1d

Browse files
committed
psbt: add PrevOutFetcher helper func
1 parent 7f6059d commit 29a0d1d

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

psbt/utils.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,36 @@ func FindLeafScript(pInput *PInput,
478478
return nil, fmt.Errorf("leaf script for target leaf hash %x not "+
479479
"found in input", targetLeafHash)
480480
}
481+
482+
// PrevOutputFetcher returns a txscript.PrevOutFetcher built from the UTXO
483+
// information in a PSBT packet.
484+
func PrevOutputFetcher(packet *Packet) *txscript.MultiPrevOutFetcher {
485+
fetcher := txscript.NewMultiPrevOutFetcher(nil)
486+
for idx, txIn := range packet.UnsignedTx.TxIn {
487+
in := packet.Inputs[idx]
488+
489+
// Skip any input that has no UTXO.
490+
if in.WitnessUtxo == nil && in.NonWitnessUtxo == nil {
491+
continue
492+
}
493+
494+
if in.NonWitnessUtxo != nil {
495+
prevIndex := txIn.PreviousOutPoint.Index
496+
fetcher.AddPrevOut(
497+
txIn.PreviousOutPoint,
498+
in.NonWitnessUtxo.TxOut[prevIndex],
499+
)
500+
501+
continue
502+
}
503+
504+
// Fall back to witness UTXO only for older wallets.
505+
if in.WitnessUtxo != nil {
506+
fetcher.AddPrevOut(
507+
txIn.PreviousOutPoint, in.WitnessUtxo,
508+
)
509+
}
510+
}
511+
512+
return fetcher
513+
}

0 commit comments

Comments
 (0)