Skip to content

Commit 19ac743

Browse files
authored
Merge pull request #142 from sanket1729/psbt_satisft
Psbt satisfy
2 parents 2d37e7f + 67ea9e1 commit 19ac743

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

src/psbt/finalizer.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
2222
use super::super::Error as MsError;
2323
use super::{sanity_check, Psbt};
24-
use super::{Error, InputError};
24+
use super::{Error, InputError, PsbtInputSatisfier};
2525
use bitcoin::secp256k1;
2626
use bitcoin::util::bip143::SigHashCache;
2727
use bitcoin::{self, PublicKey, Script, SigHashType};
@@ -301,13 +301,13 @@ pub fn finalize(psbt: &mut Psbt) -> Result<(), super::Error> {
301301
for index in 0..psbt.inputs.len() {
302302
// Get a descriptor for this input
303303
let desc = get_descriptor(&psbt, index).map_err(|e| Error::InputError(e, index))?;
304-
let input = &mut psbt.inputs[index];
305304

306305
//generate the satisfaction witness and scriptsig
307306
let (witness, script_sig) = desc
308-
.get_satisfication(&input)
307+
.get_satisfication(PsbtInputSatisfier::new(&psbt, index))
309308
.map_err(|e| Error::InputError(InputError::MiniscriptError(e), index))?;
310309

310+
let input = &mut psbt.inputs[index];
311311
//Fill in the satisfactions
312312
input.final_script_sig = if script_sig.is_empty() {
313313
None

src/psbt/mod.rs

+40-5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@
2121
2222
use std::{error, fmt};
2323

24-
use bitcoin::util::psbt;
2524
use bitcoin::util::psbt::PartiallySignedTransaction as Psbt;
2625

2726
use bitcoin;
2827
use bitcoin::Script;
29-
use miniscript::satisfy::bitcoinsig_from_rawsig;
28+
use miniscript::satisfy::{bitcoinsig_from_rawsig, After, Older};
3029
use BitcoinSig;
3130
use Satisfier;
3231
use {MiniscriptKey, ToPublicKey};
@@ -183,9 +182,35 @@ impl fmt::Display for Error {
183182
}
184183
}
185184

186-
impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for psbt::Input {
185+
/// Psbt satisfier for at inputs at a particular index
186+
/// Takes in &psbt because multiple inputs will share
187+
/// the same psbt structure
188+
/// All operations on this structure will panic if index
189+
/// is more than number of inputs in pbst
190+
pub struct PsbtInputSatisfier<'psbt> {
191+
/// pbst
192+
pub psbt: &'psbt Psbt,
193+
/// input index
194+
pub index: usize,
195+
}
196+
197+
impl<'psbt> PsbtInputSatisfier<'psbt> {
198+
/// create a new PsbtInputsatisfier from
199+
/// psbt and index
200+
pub fn new(psbt: &'psbt Psbt, index: usize) -> Self {
201+
Self {
202+
psbt: psbt,
203+
index: index,
204+
}
205+
}
206+
}
207+
208+
impl<'psbt, Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for PsbtInputSatisfier<'psbt> {
187209
fn lookup_sig(&self, pk: &Pk) -> Option<BitcoinSig> {
188-
if let Some(rawsig) = self.partial_sigs.get(&pk.to_public_key()) {
210+
if let Some(rawsig) = self.psbt.inputs[self.index]
211+
.partial_sigs
212+
.get(&pk.to_public_key())
213+
{
189214
// We have already previously checked that all signatures have the
190215
// correct sighash flag.
191216
bitcoinsig_from_rawsig(rawsig).ok()
@@ -195,7 +220,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for psbt::Input {
195220
}
196221

197222
fn lookup_pkh_sig(&self, pkh: &Pk::Hash) -> Option<(bitcoin::PublicKey, BitcoinSig)> {
198-
if let Some((pk, sig)) = self
223+
if let Some((pk, sig)) = self.psbt.inputs[self.index]
199224
.partial_sigs
200225
.iter()
201226
.filter(|&(pubkey, _sig)| pubkey.to_pubkeyhash() == Pk::hash_to_hash160(pkh))
@@ -209,6 +234,16 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for psbt::Input {
209234
None
210235
}
211236
}
237+
238+
fn check_after(&self, n: u32) -> bool {
239+
let cltv = self.psbt.global.unsigned_tx.lock_time;
240+
<Satisfier<Pk>>::check_after(&After(cltv), n)
241+
}
242+
243+
fn check_older(&self, n: u32) -> bool {
244+
let csv = self.psbt.global.unsigned_tx.input[self.index].sequence;
245+
<Satisfier<Pk>>::check_older(&Older(csv), n)
246+
}
212247
}
213248

214249
fn sanity_check(psbt: &Psbt) -> Result<(), Error> {

0 commit comments

Comments
 (0)