Skip to content

Commit

Permalink
SFT-UNKN: Add event reporting.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeandudey committed Jan 19, 2025
1 parent 6619153 commit 43833e6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
33 changes: 17 additions & 16 deletions psbt/examples/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,25 @@ fn main() {
let file = std::fs::read(file).unwrap();
let xpriv = Xpriv::from_str(&xpriv).unwrap();

let details = match validate::<_, _, VerboseError<_>, 10>(file.as_slice(), SECP256K1, xpriv) {
Ok(v) => v,
Err(e) => {
match e {
Error::Parse(e) => match e {
nom::Err::Incomplete(_) => println!("unexpected end of file"),
nom::Err::Error(e) | nom::Err::Failure(e) => {
for (i, e) in e.errors.iter().enumerate() {
println!("Error {i}: {e:?}");
let details =
match validate::<_, _, VerboseError<_>, 10>(file.as_slice(), SECP256K1, xpriv | _ | ()) {
Ok(v) => v,
Err(e) => {
match e {
Error::Parse(e) => match e {
nom::Err::Incomplete(_) => println!("unexpected end of file"),
nom::Err::Error(e) | nom::Err::Failure(e) => {
for (i, e) in e.errors.iter().enumerate() {
println!("Error {i}: {e:?}");
}
}
}
},
Error::Validation(e) => println!("{e}"),
}
},
Error::Validation(e) => println!("{e}"),
}

std::process::exit(1);
}
};
std::process::exit(1);
}
};

println!("Transaction details:");
if details.is_self_send() {
Expand Down
25 changes: 24 additions & 1 deletion psbt/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ impl TransactionDetails {
}
}

pub fn validate<Input, C, E, const N: usize>(
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidationEvent {
/// Validation progress percentage update.
Progress(u64),
}

pub fn validate<Input, C, F, E, const N: usize>(
i: Input,
secp: &secp256k1::Secp256k1<C>,
master_key: Xpriv,
mut handle_event: F,
) -> Result<TransactionDetails, Error<E>>
where
Input: for<'a> nom::Compare<&'a [u8]>
Expand All @@ -58,13 +65,16 @@ where
+ nom::Slice<core::ops::Range<usize>>
+ nom::Slice<core::ops::RangeFrom<usize>>,
C: secp256k1::Signing + secp256k1::Verification,
F: FnMut(ValidationEvent),
E: core::fmt::Debug
+ nom::error::ContextError<Input>
+ nom::error::ParseError<Input>
+ nom::error::FromExternalError<Input, secp256k1::Error>
+ nom::error::FromExternalError<Input, bitcoin_hashes::FromSliceError>
+ nom::error::FromExternalError<Input, core::num::TryFromIntError>,
{
handle_event(ValidationEvent::Progress(0));

log::debug!("validating PSBT");

let (i, _) = tag::<_, Input, E>(b"psbt\xff")(i)?;
Expand All @@ -81,6 +91,9 @@ where
let wallet_fingerprint = master_key.fingerprint(secp);
log::debug!("wallet fingerprint {:?}", wallet_fingerprint);

let total_items = input_count + output_count;
let mut processed_items = 0;

log::debug!("validating inputs");
let mut input = i.clone();
for _ in 0..input_count {
Expand All @@ -95,6 +108,11 @@ where
Err(Err::Error(e)) => return Err(Err::Error(E::append(i, ErrorKind::Count, e)).into()),
Err(e) => return Err(e.into()),
}

processed_items += 1;
handle_event(ValidationEvent::Progress(
(processed_items * 100) / total_items,
));
}

log::debug!("validating outputs");
Expand Down Expand Up @@ -170,6 +188,11 @@ where
Err(Err::Error(e)) => return Err(Err::Error(E::append(i, ErrorKind::Count, e)).into()),
Err(e) => return Err(e.into()),
};

processed_items += 1;
handle_event(ValidationEvent::Progress(
(processed_items * 100) / total_items,
));
}

log::debug!("total with total_change: {total_with_change} sats");
Expand Down

0 comments on commit 43833e6

Please sign in to comment.