Skip to content

Commit 34a73ec

Browse files
authored
Merge pull request #227 from sanket1729/psbt_hash
Add psbt preimage satisfaction logic
2 parents 628de85 + c267633 commit 34a73ec

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub use descriptor::{Descriptor, DescriptorPublicKey, DescriptorTrait};
127127
pub use interpreter::Interpreter;
128128
pub use miniscript::context::{BareCtx, Legacy, ScriptContext, Segwitv0};
129129
pub use miniscript::decode::Terminal;
130-
pub use miniscript::satisfy::{BitcoinSig, Satisfier};
130+
pub use miniscript::satisfy::{BitcoinSig, Preimage32, Satisfier};
131131
pub use miniscript::Miniscript;
132132

133133
///Public key trait which can be converted to Hash type

src/miniscript/satisfy.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ use Terminal;
3737

3838
/// Type alias for a signature/hashtype pair
3939
pub type BitcoinSig = (secp256k1::Signature, bitcoin::SigHashType);
40+
/// Type alias for 32 byte Preimage.
41+
pub type Preimage32 = [u8; 32];
4042

4143
/// Helper function to create BitcoinSig from Rawsig
4244
/// Useful for downstream when implementing Satisfier.
@@ -71,22 +73,22 @@ pub trait Satisfier<Pk: MiniscriptKey + ToPublicKey> {
7173
}
7274

7375
/// Given a SHA256 hash, look up its preimage
74-
fn lookup_sha256(&self, _: sha256::Hash) -> Option<[u8; 32]> {
76+
fn lookup_sha256(&self, _: sha256::Hash) -> Option<Preimage32> {
7577
None
7678
}
7779

7880
/// Given a HASH256 hash, look up its preimage
79-
fn lookup_hash256(&self, _: sha256d::Hash) -> Option<[u8; 32]> {
81+
fn lookup_hash256(&self, _: sha256d::Hash) -> Option<Preimage32> {
8082
None
8183
}
8284

8385
/// Given a RIPEMD160 hash, look up its preimage
84-
fn lookup_ripemd160(&self, _: ripemd160::Hash) -> Option<[u8; 32]> {
86+
fn lookup_ripemd160(&self, _: ripemd160::Hash) -> Option<Preimage32> {
8587
None
8688
}
8789

8890
/// Given a HASH160 hash, look up its preimage
89-
fn lookup_hash160(&self, _: hash160::Hash) -> Option<[u8; 32]> {
91+
fn lookup_hash160(&self, _: hash160::Hash) -> Option<Preimage32> {
9092
None
9193
}
9294

@@ -181,19 +183,19 @@ impl<'a, Pk: MiniscriptKey + ToPublicKey, S: Satisfier<Pk>> Satisfier<Pk> for &'
181183
(**self).lookup_pkh_sig(pkh)
182184
}
183185

184-
fn lookup_sha256(&self, h: sha256::Hash) -> Option<[u8; 32]> {
186+
fn lookup_sha256(&self, h: sha256::Hash) -> Option<Preimage32> {
185187
(**self).lookup_sha256(h)
186188
}
187189

188-
fn lookup_hash256(&self, h: sha256d::Hash) -> Option<[u8; 32]> {
190+
fn lookup_hash256(&self, h: sha256d::Hash) -> Option<Preimage32> {
189191
(**self).lookup_hash256(h)
190192
}
191193

192-
fn lookup_ripemd160(&self, h: ripemd160::Hash) -> Option<[u8; 32]> {
194+
fn lookup_ripemd160(&self, h: ripemd160::Hash) -> Option<Preimage32> {
193195
(**self).lookup_ripemd160(h)
194196
}
195197

196-
fn lookup_hash160(&self, h: hash160::Hash) -> Option<[u8; 32]> {
198+
fn lookup_hash160(&self, h: hash160::Hash) -> Option<Preimage32> {
197199
(**self).lookup_hash160(h)
198200
}
199201

@@ -219,19 +221,19 @@ impl<'a, Pk: MiniscriptKey + ToPublicKey, S: Satisfier<Pk>> Satisfier<Pk> for &'
219221
(**self).lookup_pkh_sig(pkh)
220222
}
221223

222-
fn lookup_sha256(&self, h: sha256::Hash) -> Option<[u8; 32]> {
224+
fn lookup_sha256(&self, h: sha256::Hash) -> Option<Preimage32> {
223225
(**self).lookup_sha256(h)
224226
}
225227

226-
fn lookup_hash256(&self, h: sha256d::Hash) -> Option<[u8; 32]> {
228+
fn lookup_hash256(&self, h: sha256d::Hash) -> Option<Preimage32> {
227229
(**self).lookup_hash256(h)
228230
}
229231

230-
fn lookup_ripemd160(&self, h: ripemd160::Hash) -> Option<[u8; 32]> {
232+
fn lookup_ripemd160(&self, h: ripemd160::Hash) -> Option<Preimage32> {
231233
(**self).lookup_ripemd160(h)
232234
}
233235

234-
fn lookup_hash160(&self, h: hash160::Hash) -> Option<[u8; 32]> {
236+
fn lookup_hash160(&self, h: hash160::Hash) -> Option<Preimage32> {
235237
(**self).lookup_hash160(h)
236238
}
237239

@@ -288,7 +290,7 @@ macro_rules! impl_tuple_satisfier {
288290
None
289291
}
290292

291-
fn lookup_sha256(&self, h: sha256::Hash) -> Option<[u8; 32]> {
293+
fn lookup_sha256(&self, h: sha256::Hash) -> Option<Preimage32> {
292294
let &($(ref $ty,)*) = self;
293295
$(
294296
if let Some(result) = $ty.lookup_sha256(h) {
@@ -298,7 +300,7 @@ macro_rules! impl_tuple_satisfier {
298300
None
299301
}
300302

301-
fn lookup_hash256(&self, h: sha256d::Hash) -> Option<[u8; 32]> {
303+
fn lookup_hash256(&self, h: sha256d::Hash) -> Option<Preimage32> {
302304
let &($(ref $ty,)*) = self;
303305
$(
304306
if let Some(result) = $ty.lookup_hash256(h) {
@@ -308,7 +310,7 @@ macro_rules! impl_tuple_satisfier {
308310
None
309311
}
310312

311-
fn lookup_ripemd160(&self, h: ripemd160::Hash) -> Option<[u8; 32]> {
313+
fn lookup_ripemd160(&self, h: ripemd160::Hash) -> Option<Preimage32> {
312314
let &($(ref $ty,)*) = self;
313315
$(
314316
if let Some(result) = $ty.lookup_ripemd160(h) {
@@ -318,7 +320,7 @@ macro_rules! impl_tuple_satisfier {
318320
None
319321
}
320322

321-
fn lookup_hash160(&self, h: hash160::Hash) -> Option<[u8; 32]> {
323+
fn lookup_hash160(&self, h: hash160::Hash) -> Option<Preimage32> {
322324
let &($(ref $ty,)*) = self;
323325
$(
324326
if let Some(result) = $ty.lookup_hash160(h) {

src/psbt/mod.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@
2222
use std::{error, fmt};
2323

2424
use bitcoin;
25+
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d};
2526
use bitcoin::secp256k1::{self, Secp256k1};
2627
use bitcoin::util::psbt::PartiallySignedTransaction as Psbt;
2728
use bitcoin::Script;
2829

2930
use interpreter;
3031
use miniscript::limits::SEQUENCE_LOCKTIME_DISABLE_FLAG;
3132
use miniscript::satisfy::{bitcoinsig_from_rawsig, After, Older};
32-
use BitcoinSig;
3333
use Satisfier;
34+
use {BitcoinSig, Preimage32};
3435
use {MiniscriptKey, ToPublicKey};
3536

3637
mod finalizer;
@@ -279,6 +280,44 @@ impl<'psbt, Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for PsbtInputSatisfie
279280
<Satisfier<Pk>>::check_older(&Older(seq), n)
280281
}
281282
}
283+
284+
fn lookup_hash160(&self, h: hash160::Hash) -> Option<Preimage32> {
285+
self.psbt.inputs[self.index]
286+
.hash160_preimages
287+
.get(&h)
288+
.and_then(try_vec_as_preimage32)
289+
}
290+
291+
fn lookup_sha256(&self, h: sha256::Hash) -> Option<Preimage32> {
292+
self.psbt.inputs[self.index]
293+
.sha256_preimages
294+
.get(&h)
295+
.and_then(try_vec_as_preimage32)
296+
}
297+
298+
fn lookup_hash256(&self, h: sha256d::Hash) -> Option<Preimage32> {
299+
self.psbt.inputs[self.index]
300+
.hash256_preimages
301+
.get(&h)
302+
.and_then(try_vec_as_preimage32)
303+
}
304+
305+
fn lookup_ripemd160(&self, h: ripemd160::Hash) -> Option<Preimage32> {
306+
self.psbt.inputs[self.index]
307+
.ripemd160_preimages
308+
.get(&h)
309+
.and_then(try_vec_as_preimage32)
310+
}
311+
}
312+
313+
fn try_vec_as_preimage32(vec: &Vec<u8>) -> Option<Preimage32> {
314+
if vec.len() == 32 {
315+
let mut arr = [0u8; 32];
316+
arr.copy_from_slice(&vec);
317+
Some(arr)
318+
} else {
319+
None
320+
}
282321
}
283322

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

0 commit comments

Comments
 (0)