Skip to content

Commit

Permalink
psbt: Add psbt::finalize_extract() shortcut
Browse files Browse the repository at this point in the history
And drop the `finalize` boolean option for psbt::sign()/psbt::extract(),
the intention is clearer with the named functions (psbt::sign_extract()
is available too).
  • Loading branch information
shesek committed Nov 9, 2024
1 parent 4784eee commit 4615555
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/stdlib/btc.minsc
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ fn psbt::finalize_witness($psbt, $input_witnesses) = psbt::update($psbt, [
)
]);

fn psbt::sign_extract($psbt, $keys) =
psbt::extract(psbt::finalize(psbt::sign($psbt, $keys)));
fn psbt::finalize_extract($psbt) = psbt::extract(psbt::finalize($psbt));
fn psbt::sign_extract($psbt, $keys) = psbt::extract(psbt::finalize(psbt::sign($psbt, $keys)));

//
// Script utilities
Expand Down
19 changes: 6 additions & 13 deletions src/stdlib/psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,16 @@ pub mod fns {
Ok((psbt, errors).into())
}

// psbt::sign(Psbt, Xpriv|Array<Xpriv>|Array<SinglePk:SingleSk> sign_keys, Bool finalize=false) -> Psbt
// psbt::sign(Psbt, Xpriv|Array<Xpriv>|Array<SinglePk:SingleSk> sign_keys) -> Psbt
//
// Attempt to sign all transaction inputs, raising an error if any fail.
pub fn sign(args: Array, _: &ScopeRef) -> Result<Value> {
let (mut psbt, keys, finalize): (_, _, Option<bool>) = args.args_into()?;
let (mut psbt, keys) = args.args_into()?;

let (_signed, failed) = sign_psbt(&mut psbt, keys)?;
ensure!(failed.is_empty(), Error::PsbtSigning(failed));
// XXX check signed?

if finalize.unwrap_or(false) {
psbt.finalize_mut(&EC).map_err(Error::PsbtFinalize)?;
}

Ok(psbt.into())
}

Expand All @@ -139,15 +135,12 @@ pub mod fns {
Ok((psbt, signed, failed).into())
}

/// psbt::extract(Psbt, Bool finalize=false) -> Transaction
/// psbt::extract(Psbt) -> Transaction
///
/// Extract the PSBT finalized transaction. Will run the Miniscript interpreter sanity checks.
/// Also possible using `tx(Psbt)` (without the `finalize` option, for pre-finalized PSBT only)
/// Extract the PSBT finalized transaction (The PSBT must already be finalized).
/// Will run the Miniscript interpreter sanity checks. Also possible using `tx(Psbt)`.
pub fn extract(args: Array, _: &ScopeRef) -> Result<Value> {
let (mut psbt, finalize): (Psbt, Option<bool>) = args.args_into()?;
if finalize.unwrap_or(false) {
psbt.finalize_mut(&EC).map_err(Error::PsbtFinalize)?;
}
let psbt: Psbt = args.arg_into()?;
// Uses rust-miniscript's PsbtExt::extract(), which only works with Miniscript-compatible Scripts
Ok(psbt.extract(&EC)?.into())
}
Expand Down

0 comments on commit 4615555

Please sign in to comment.