Skip to content

Commit e26d4e6

Browse files
committed
Use bitcoin::EcdsaSig from rust-bitcoin
1 parent 89e7cb3 commit e26d4e6

File tree

11 files changed

+128
-131
lines changed

11 files changed

+128
-131
lines changed

examples/sign_multisig.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ fn main() {
6161
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6262
]).expect("key 3"),
6363
];
64-
let bitcoin_sig = (
64+
let bitcoin_sig = bitcoin::EcdsaSig {
6565
// copied at random off the blockchain; this is not actually a valid
6666
// signature for this transaction; Miniscript does not verify
67-
secp256k1::ecdsa::Signature::from_str(
67+
sig: secp256k1::ecdsa::Signature::from_str(
6868
"3045\
6969
0221\
7070
00f7c3648c390d87578cd79c8016940aa8e3511c4104cb78daa8fb8e429375efc1\
7171
0220\
7272
531d75c136272f127a5dc14acc0722301cbddc222262934151f140da345af177",
7373
)
7474
.unwrap(),
75-
bitcoin::EcdsaSigHashType::All,
76-
);
75+
hash_ty: bitcoin::EcdsaSigHashType::All,
76+
};
7777

7878
let descriptor_str = format!(
7979
"wsh(multi(2,{},{},{}))",
@@ -112,7 +112,7 @@ fn main() {
112112
// Attempt to satisfy at age 0, height 0
113113
let original_txin = tx.input[0].clone();
114114

115-
let mut sigs = HashMap::<bitcoin::PublicKey, miniscript::BitcoinSig>::new();
115+
let mut sigs = HashMap::<bitcoin::PublicKey, miniscript::bitcoin::EcdsaSig>::new();
116116

117117
// Doesn't work with no signatures
118118
assert!(my_descriptor.satisfy(&mut tx.input[0], &sigs).is_err());

examples/verify_tx.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ fn main() {
138138
.expect("Can only fail in sighash single when corresponding output is not present");
139139
// Restrict to sighash_all just to demonstrate how to add additional filters
140140
// `&_` needed here because of https://github.com/rust-lang/rust/issues/79187
141-
let vfyfn = move |pk: &_, bitcoinsig: miniscript::BitcoinSig| {
142-
bitcoinsig.1 == bitcoin::EcdsaSigHashType::All && vfyfn(pk, bitcoinsig)
141+
let vfyfn = move |pk: &_, bitcoinsig: miniscript::bitcoin::EcdsaSig| {
142+
bitcoinsig.hash_ty == bitcoin::EcdsaSigHashType::All && vfyfn(pk, bitcoinsig)
143143
};
144144

145145
println!("\nExample two");
@@ -165,9 +165,9 @@ fn main() {
165165
)
166166
.unwrap();
167167

168-
let iter = interpreter.iter(|pk, (sig, sighashtype)| {
169-
sighashtype == bitcoin::EcdsaSigHashType::All
170-
&& secp.verify_ecdsa(&message, &sig, &pk.key).is_ok()
168+
let iter = interpreter.iter(|pk, ecdsa_sig| {
169+
ecdsa_sig.hash_ty == bitcoin::EcdsaSigHashType::All
170+
&& secp.verify_ecdsa(&message, &ecdsa_sig.sig, &pk.key).is_ok()
171171
});
172172
println!("\nExample three");
173173
for elem in iter {

src/descriptor/bare.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,8 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
335335
Pk: ToPublicKey,
336336
S: Satisfier<Pk>,
337337
{
338-
if let Some(sig) = satisfier.lookup_sig(&self.pk) {
339-
let mut sig_vec = sig.0.serialize_der().to_vec();
340-
sig_vec.push(sig.1.as_u32() as u8);
338+
if let Some(sig) = satisfier.lookup_ecdsa_sig(&self.pk) {
339+
let sig_vec = sig.to_vec();
341340
let script_sig = script::Builder::new()
342341
.push_slice(&sig_vec[..])
343342
.push_key(&self.pk.to_public_key())

src/descriptor/mod.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -655,13 +655,12 @@ mod tests {
655655
use bitcoin::hashes::hex::FromHex;
656656
use bitcoin::hashes::{hash160, sha256};
657657
use bitcoin::util::bip32;
658-
use bitcoin::{self, secp256k1, PublicKey};
658+
use bitcoin::{self, secp256k1, EcdsaSigHashType, PublicKey};
659659
use descriptor::key::Wildcard;
660660
use descriptor::{
661661
DescriptorPublicKey, DescriptorSecretKey, DescriptorSinglePub, DescriptorXKey,
662662
};
663663
use hex_script;
664-
use miniscript::satisfy::BitcoinSig;
665664
use std::cmp;
666665
use std::collections::HashMap;
667666
use std::str::FromStr;
@@ -950,9 +949,12 @@ mod tests {
950949
}
951950

952951
impl Satisfier<bitcoin::PublicKey> for SimpleSat {
953-
fn lookup_sig(&self, pk: &bitcoin::PublicKey) -> Option<BitcoinSig> {
952+
fn lookup_ecdsa_sig(&self, pk: &bitcoin::PublicKey) -> Option<bitcoin::EcdsaSig> {
954953
if *pk == self.pk {
955-
Some((self.sig, bitcoin::EcdsaSigHashType::All))
954+
Some(bitcoin::EcdsaSig {
955+
sig: self.sig,
956+
hash_ty: bitcoin::EcdsaSigHashType::All,
957+
})
956958
} else {
957959
None
958960
}
@@ -1161,8 +1163,20 @@ mod tests {
11611163
let satisfier = {
11621164
let mut satisfier = HashMap::with_capacity(2);
11631165

1164-
satisfier.insert(a, (sig_a.clone(), ::bitcoin::EcdsaSigHashType::All));
1165-
satisfier.insert(b, (sig_b.clone(), ::bitcoin::EcdsaSigHashType::All));
1166+
satisfier.insert(
1167+
a,
1168+
bitcoin::EcdsaSig {
1169+
sig: sig_a,
1170+
hash_ty: EcdsaSigHashType::All,
1171+
},
1172+
);
1173+
satisfier.insert(
1174+
b,
1175+
bitcoin::EcdsaSig {
1176+
sig: sig_b,
1177+
hash_ty: EcdsaSigHashType::All,
1178+
},
1179+
);
11661180

11671181
satisfier
11681182
};

src/descriptor/segwitv0.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,8 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
436436
Pk: ToPublicKey,
437437
S: Satisfier<Pk>,
438438
{
439-
if let Some(sig) = satisfier.lookup_sig(&self.pk) {
440-
let mut sig_vec = sig.0.serialize_der().to_vec();
441-
sig_vec.push(sig.1.as_u32() as u8);
439+
if let Some(sig) = satisfier.lookup_ecdsa_sig(&self.pk) {
440+
let sig_vec = sig.to_vec();
442441
let script_sig = Script::new();
443442
let witness = vec![sig_vec, self.pk.to_public_key().to_bytes()];
444443
Ok((witness, script_sig))

src/interpreter/mod.rs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use miniscript::context::NoChecks;
2727
use miniscript::ScriptContext;
2828
use Miniscript;
2929
use Terminal;
30-
use {BitcoinSig, Descriptor, ToPublicKey};
30+
use {Descriptor, ToPublicKey};
3131

3232
mod error;
3333
mod inner;
@@ -82,7 +82,7 @@ impl<'txin> Interpreter<'txin> {
8282
///
8383
/// Running the iterator through will consume the internal stack of the
8484
/// `Iterpreter`, and it should not be used again after this.
85-
pub fn iter<'iter, F: FnMut(&bitcoin::PublicKey, BitcoinSig) -> bool>(
85+
pub fn iter<'iter, F: FnMut(&bitcoin::PublicKey, bitcoin::EcdsaSig) -> bool>(
8686
&'iter mut self,
8787
verify_sig: F,
8888
) -> Iter<'txin, 'iter, F> {
@@ -190,7 +190,7 @@ impl<'txin> Interpreter<'txin> {
190190
unsigned_tx: &'a bitcoin::Transaction,
191191
input_idx: usize,
192192
amount: u64,
193-
) -> Result<impl Fn(&bitcoin::PublicKey, BitcoinSig) -> bool + 'a, Error> {
193+
) -> Result<impl Fn(&bitcoin::PublicKey, bitcoin::EcdsaSig) -> bool + 'a, Error> {
194194
// Precompute all sighash types because the borrowck doesn't like us
195195
// pulling self into the closure
196196
let sighashes = [
@@ -232,19 +232,21 @@ impl<'txin> Interpreter<'txin> {
232232
)?,
233233
];
234234

235-
Ok(move |pk: &bitcoin::PublicKey, (sig, sighash_type)| {
236-
// This is an awkward way to do this lookup, but it lets us do exhaustiveness
237-
// checking in case future rust-bitcoin versions add new sighash types
238-
let sighash = match sighash_type {
239-
bitcoin::EcdsaSigHashType::All => sighashes[0],
240-
bitcoin::EcdsaSigHashType::None => sighashes[1],
241-
bitcoin::EcdsaSigHashType::Single => sighashes[2],
242-
bitcoin::EcdsaSigHashType::AllPlusAnyoneCanPay => sighashes[3],
243-
bitcoin::EcdsaSigHashType::NonePlusAnyoneCanPay => sighashes[4],
244-
bitcoin::EcdsaSigHashType::SinglePlusAnyoneCanPay => sighashes[5],
245-
};
246-
secp.verify_ecdsa(&sighash, &sig, &pk.key).is_ok()
247-
})
235+
Ok(
236+
move |pk: &bitcoin::PublicKey, ecdsa_sig: bitcoin::EcdsaSig| {
237+
// This is an awkward way to do this lookup, but it lets us do exhaustiveness
238+
// checking in case future rust-bitcoin versions add new sighash types
239+
let sighash = match ecdsa_sig.hash_ty {
240+
bitcoin::EcdsaSigHashType::All => sighashes[0],
241+
bitcoin::EcdsaSigHashType::None => sighashes[1],
242+
bitcoin::EcdsaSigHashType::Single => sighashes[2],
243+
bitcoin::EcdsaSigHashType::AllPlusAnyoneCanPay => sighashes[3],
244+
bitcoin::EcdsaSigHashType::NonePlusAnyoneCanPay => sighashes[4],
245+
bitcoin::EcdsaSigHashType::SinglePlusAnyoneCanPay => sighashes[5],
246+
};
247+
secp.verify_ecdsa(&sighash, &ecdsa_sig.sig, &pk.key).is_ok()
248+
},
249+
)
248250
}
249251
}
250252

@@ -327,7 +329,7 @@ struct NodeEvaluationState<'intp> {
327329
///
328330
/// In case the script is actually dissatisfied, this may return several values
329331
/// before ultimately returning an error.
330-
pub struct Iter<'intp, 'txin: 'intp, F: FnMut(&bitcoin::PublicKey, BitcoinSig) -> bool> {
332+
pub struct Iter<'intp, 'txin: 'intp, F: FnMut(&bitcoin::PublicKey, bitcoin::EcdsaSig) -> bool> {
331333
verify_sig: F,
332334
public_key: Option<&'intp bitcoin::PublicKey>,
333335
state: Vec<NodeEvaluationState<'intp>>,
@@ -341,7 +343,7 @@ pub struct Iter<'intp, 'txin: 'intp, F: FnMut(&bitcoin::PublicKey, BitcoinSig) -
341343
impl<'intp, 'txin: 'intp, F> Iterator for Iter<'intp, 'txin, F>
342344
where
343345
NoChecks: ScriptContext,
344-
F: FnMut(&bitcoin::PublicKey, BitcoinSig) -> bool,
346+
F: FnMut(&bitcoin::PublicKey, bitcoin::EcdsaSig) -> bool,
345347
{
346348
type Item = Result<SatisfiedConstraint<'intp, 'txin>, Error>;
347349

@@ -362,7 +364,7 @@ where
362364
impl<'intp, 'txin: 'intp, F> Iter<'intp, 'txin, F>
363365
where
364366
NoChecks: ScriptContext,
365-
F: FnMut(&bitcoin::PublicKey, BitcoinSig) -> bool,
367+
F: FnMut(&bitcoin::PublicKey, bitcoin::EcdsaSig) -> bool,
366368
{
367369
/// Helper function to push a NodeEvaluationState on state stack
368370
fn push_evaluation_state(
@@ -770,14 +772,15 @@ fn verify_sersig<'txin, F>(
770772
sigser: &[u8],
771773
) -> Result<secp256k1::ecdsa::Signature, Error>
772774
where
773-
F: FnOnce(&bitcoin::PublicKey, BitcoinSig) -> bool,
775+
F: FnOnce(&bitcoin::PublicKey, bitcoin::EcdsaSig) -> bool,
774776
{
775777
if let Some((sighash_byte, sig)) = sigser.split_last() {
776-
let sighashtype = bitcoin::EcdsaSigHashType::from_u32_standard(*sighash_byte as u32)
778+
let hash_ty = bitcoin::EcdsaSigHashType::from_u32_standard(*sighash_byte as u32)
777779
.map_err(|_| Error::NonStandardSigHash([sig, &[*sighash_byte]].concat().to_vec()))?;
778780
let sig = secp256k1::ecdsa::Signature::from_der(sig)?;
779-
if verify_sig(pk, (sig, sighashtype)) {
780-
Ok(sig)
781+
let ecdsa_sig = bitcoin::EcdsaSig { sig, hash_ty };
782+
if verify_sig(pk, ecdsa_sig) {
783+
Ok(ecdsa_sig.sig)
781784
} else {
782785
Err(Error::InvalidSignature(*pk))
783786
}
@@ -794,7 +797,6 @@ mod tests {
794797
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash};
795798
use bitcoin::secp256k1::{self, Secp256k1, VerifyOnly};
796799
use miniscript::context::NoChecks;
797-
use BitcoinSig;
798800
use Miniscript;
799801
use MiniscriptKey;
800802
use ToPublicKey;
@@ -839,16 +841,17 @@ mod tests {
839841
#[test]
840842
fn sat_constraints() {
841843
let (pks, der_sigs, secp_sigs, sighash, secp) = setup_keys_sigs(10);
842-
let vfyfn_ =
843-
|pk: &bitcoin::PublicKey, (sig, _)| secp.verify_ecdsa(&sighash, &sig, &pk.key).is_ok();
844+
let vfyfn_ = |pk: &bitcoin::PublicKey, ecdsa_sig: bitcoin::EcdsaSig| {
845+
secp.verify_ecdsa(&sighash, &ecdsa_sig.sig, &pk.key).is_ok()
846+
};
844847

845848
fn from_stack<'txin, 'elem, F>(
846849
verify_fn: F,
847850
stack: &'elem mut Stack<'txin>,
848851
ms: &'elem Miniscript<bitcoin::PublicKey, NoChecks>,
849852
) -> Iter<'elem, 'txin, F>
850853
where
851-
F: FnMut(&bitcoin::PublicKey, BitcoinSig) -> bool,
854+
F: FnMut(&bitcoin::PublicKey, bitcoin::EcdsaSig) -> bool,
852855
{
853856
Iter {
854857
verify_sig: verify_fn,

src/interpreter/stack.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use bitcoin;
1818
use bitcoin::blockdata::{opcodes, script};
1919
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash};
2020

21-
use {BitcoinSig, ToPublicKey};
21+
use ToPublicKey;
2222

2323
use super::{verify_sersig, Error, HashLockType, SatisfiedConstraint};
2424

@@ -132,7 +132,7 @@ impl<'txin> Stack<'txin> {
132132
pk: &'intp bitcoin::PublicKey,
133133
) -> Option<Result<SatisfiedConstraint<'intp, 'txin>, Error>>
134134
where
135-
F: FnMut(&bitcoin::PublicKey, BitcoinSig) -> bool,
135+
F: FnMut(&bitcoin::PublicKey, bitcoin::EcdsaSig) -> bool,
136136
{
137137
if let Some(sigser) = self.pop() {
138138
match sigser {
@@ -171,7 +171,7 @@ impl<'txin> Stack<'txin> {
171171
pkh: &'intp hash160::Hash,
172172
) -> Option<Result<SatisfiedConstraint<'intp, 'txin>, Error>>
173173
where
174-
F: FnOnce(&bitcoin::PublicKey, BitcoinSig) -> bool,
174+
F: FnOnce(&bitcoin::PublicKey, bitcoin::EcdsaSig) -> bool,
175175
{
176176
if let Some(Element::Push(pk)) = self.pop() {
177177
let pk_hash = hash160::Hash::hash(pk);
@@ -367,7 +367,7 @@ impl<'txin> Stack<'txin> {
367367
pk: &'intp bitcoin::PublicKey,
368368
) -> Option<Result<SatisfiedConstraint<'intp, 'txin>, Error>>
369369
where
370-
F: FnOnce(&bitcoin::PublicKey, BitcoinSig) -> bool,
370+
F: FnOnce(&bitcoin::PublicKey, bitcoin::EcdsaSig) -> bool,
371371
{
372372
if let Some(witness_sig) = self.pop() {
373373
if let Element::Push(sigser) = witness_sig {

src/lib.rs

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

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

0 commit comments

Comments
 (0)