Skip to content

Commit 3138f09

Browse files
committed
API to get an iterator for funding utxos in psbt
1 parent 315444d commit 3138f09

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/util/psbt/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ pub enum Error {
3737
/// Magic bytes for a PSBT must be the ASCII for "psbt" serialized in most
3838
/// significant byte order.
3939
InvalidMagic,
40+
/// Missing both the witness and non-witness utxo.
41+
MissingUtxo,
4042
/// The separator for a PSBT must be `0xff`.
4143
InvalidSeparator,
44+
/// Returned when output index is out of bounds in relation to the output in non-witness UTXO.
45+
PsbtUtxoOutOfbounds,
4246
/// Known keys must be according to spec.
4347
InvalidKey(raw::Key),
4448
/// Non-proprietary key type found when proprietary key was expected
@@ -98,6 +102,8 @@ impl fmt::Display for Error {
98102
}
99103
Error::NoMorePairs => f.write_str("no more key-value pairs for this psbt map"),
100104
Error::HashParseError(e) => write!(f, "Hash Parse Error: {}", e),
105+
Error::MissingUtxo => f.write_str("UTXO information is not present in PSBT"),
106+
Error::PsbtUtxoOutOfbounds => f.write_str("output index is out of bounds of non witness script output array"),
101107
Error::InvalidPreimageHashPair{ref preimage, ref hash, ref hash_type} => {
102108
// directly using debug forms of psbthash enums
103109
write!(f, "Preimage {:?} does not match {:?} hash {:?}", preimage, hash_type, hash )

src/util/psbt/mod.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
//!
2121
2222
use blockdata::script::Script;
23-
use blockdata::transaction::Transaction;
23+
use blockdata::transaction::{ TxOut, Transaction};
2424
use consensus::{encode, Encodable, Decodable};
2525
use consensus::encode::MAX_VEC_SIZE;
26+
pub use util::sighash::Prevouts;
2627

2728
use prelude::*;
2829

2930
use io;
30-
3131
mod error;
3232
pub use self::error::Error;
3333

@@ -72,6 +72,32 @@ pub struct PartiallySignedTransaction {
7272
}
7373

7474
impl PartiallySignedTransaction {
75+
/// Returns an iterator for the funding UTXOs of the psbt
76+
///
77+
/// For each PSBT input that contains UTXO information `Ok` is returned containing that information.
78+
/// The order of returned items is same as the order of inputs.
79+
///
80+
/// ## Errors
81+
///
82+
/// The function returns error when UTXO information is not present or is invalid.
83+
///
84+
/// ## Panics
85+
///
86+
/// The function panics if the length of transaction inputs is not equal to the length of PSBT inputs.
87+
pub fn iter_funding_utxos(&self) -> impl Iterator<Item = Result<&TxOut, Error>> {
88+
assert_eq!(self.inputs.len(), self.unsigned_tx.input.len());
89+
self.unsigned_tx.input.iter().zip(&self.inputs).map(|(tx_input, psbt_input)| {
90+
match (&psbt_input.witness_utxo, &psbt_input.non_witness_utxo) {
91+
(Some(witness_utxo), _) => Ok(witness_utxo),
92+
(None, Some(non_witness_utxo)) => {
93+
let vout = tx_input.previous_output.vout as usize;
94+
non_witness_utxo.output.get(vout).ok_or(Error::PsbtUtxoOutOfbounds)
95+
},
96+
(None, None) => Err(Error::MissingUtxo),
97+
}
98+
})
99+
}
100+
75101
/// Checks that unsigned transaction does not have scriptSig's or witness
76102
/// data
77103
fn unsigned_tx_checks(&self) -> Result<(), Error> {

0 commit comments

Comments
 (0)