Skip to content

Commit 164a9a5

Browse files
committed
Fix bug in Psbt::spend_utxo when missing output
1 parent 3268994 commit 164a9a5

1 file changed

Lines changed: 47 additions & 1 deletion

File tree

bitcoin/src/psbt/mod.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ impl Psbt {
616616
witness_utxo
617617
} else if let Some(non_witness_utxo) = &input.non_witness_utxo {
618618
let vout = self.unsigned_tx.inputs[input_index].previous_output.vout;
619-
&non_witness_utxo.outputs[vout as usize]
619+
non_witness_utxo.outputs.get(vout as usize).ok_or(SignError::MissingSpendUtxo)?
620620
} else {
621621
return Err(SignError::MissingSpendUtxo);
622622
};
@@ -2552,6 +2552,52 @@ mod tests {
25522552
Err(ExtractTxError::MissingInputAmount { tx: _ })
25532553
))
25542554
}
2555+
2556+
#[test]
2557+
fn spending_psbt_with_missing_txout() {
2558+
let psbt = Psbt {
2559+
unsigned_tx: Transaction {
2560+
version: transaction::Version::TWO,
2561+
lock_time: absolute::LockTime::from_consensus(1257139),
2562+
inputs: vec![TxIn {
2563+
previous_output: OutPoint {
2564+
txid: "f61b1742ca13176464adb3cb66050c00787bb3a4eead37e985f2df1e37718126"
2565+
.parse()
2566+
.unwrap(),
2567+
vout: 0,
2568+
},
2569+
script_sig: ScriptSigBuf::new(),
2570+
sequence: Sequence::ENABLE_LOCKTIME_NO_RBF,
2571+
witness: Witness::default(),
2572+
}],
2573+
outputs: vec![
2574+
TxOut {
2575+
amount: Amount::from_sat_u32(99_999_699),
2576+
script_pubkey: ScriptPubKeyBuf::from_hex_no_length_prefix(
2577+
"76a914d0c59903c5bac2868760e90fd521a4665aa7652088ac",
2578+
)
2579+
.unwrap(),
2580+
},
2581+
],
2582+
},
2583+
xpub: Default::default(),
2584+
version: 0,
2585+
proprietary: Default::default(),
2586+
unknown: Default::default(),
2587+
inputs: vec![Input {
2588+
non_witness_utxo: Some(Transaction {
2589+
version: transaction::Version::TWO,
2590+
lock_time: absolute::LockTime::ZERO,
2591+
inputs: vec![],
2592+
outputs: vec![], // No outputs here
2593+
}),
2594+
..Default::default()
2595+
}],
2596+
outputs: vec![Output::default()],
2597+
};
2598+
2599+
assert!(matches!(psbt.spend_utxo(0), Err(SignError::MissingSpendUtxo)))
2600+
}
25552601

25562602
#[test]
25572603
#[cfg(all(feature = "rand", feature = "std"))]

0 commit comments

Comments
 (0)