Skip to content

Commit 249b17e

Browse files
author
ChallengeDev210
committed
Merge rust-bitcoin/rust-bitcoin#853: API to find funding utxos in psbt
5afb0ea API to get an iterator for funding utxos in psbt (violet360) Pull request description: ### Current status The API returns a vector of UTXOs and has return type `Result<Vec<&TxOut>, Error>` ### Expected The return statement should be of type `sighash::Prevouts` as pointed in #849 ACKs for top commit: Kixunil: ACK 5afb0ea tcharding: ACK 5afb0ea sanket1729: ACK 5afb0ea. Thanks for being patient with this. Tree-SHA512: 724fc3dffdbb1331584f89bbe84527e1af0d193a344fe43b36f2f2a628652d259001a3abf6b3909df53524cd3fbdbe3af760b7004d40d3bee1848fbb83efff5b
2 parents d9f844b + 3138f09 commit 249b17e

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
@@ -22,14 +22,14 @@
2222
use core::cmp;
2323

2424
use blockdata::script::Script;
25-
use blockdata::transaction::Transaction;
25+
use blockdata::transaction::{ TxOut, Transaction};
2626
use consensus::{encode, Encodable, Decodable};
2727
use consensus::encode::MAX_VEC_SIZE;
28+
pub use util::sighash::Prevouts;
2829

2930
use prelude::*;
3031

3132
use io;
32-
3333
mod error;
3434
pub use self::error::Error;
3535

@@ -77,6 +77,32 @@ pub struct PartiallySignedTransaction {
7777
}
7878

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

0 commit comments

Comments
 (0)