Skip to content

Commit 1b75eb5

Browse files
authored
Merge pull request #204 from sanket1729/cleanup
Minor cleanup for psbt finalizer
2 parents b342833 + 99868ce commit 1b75eb5

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

src/descriptor/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use bitcoin::secp256k1::{Secp256k1, Signing};
3838
use bitcoin::util::bip32;
3939
use bitcoin::{self, Script};
4040

41+
use errstr;
4142
use expression;
4243
use miniscript;
4344
use miniscript::context::{ScriptContext, ScriptContextError};
@@ -1445,7 +1446,15 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
14451446
where
14461447
<Pk as FromStr>::Err: ToString,
14471448
{
1449+
if tree.args.is_empty() {
1450+
return Err(errstr("no arguments given for sortedmulti"));
1451+
}
14481452
let k = expression::parse_num(tree.args[0].name)?;
1453+
if k > (tree.args.len() - 1) as u32 {
1454+
return Err(errstr(
1455+
"higher threshold than there were keys in sortedmulti",
1456+
));
1457+
}
14491458
let pks: Result<Vec<Pk>, _> = tree.args[1..]
14501459
.iter()
14511460
.map(|sub| expression::terminal(sub, Pk::from_str))
@@ -1685,6 +1694,19 @@ mod tests {
16851694
StdDescriptor::from_str("(\u{7f}()3").unwrap_err();
16861695
StdDescriptor::from_str("pk()").unwrap_err();
16871696
StdDescriptor::from_str("nl:0").unwrap_err(); //issue 63
1697+
let compressed_pk = DummyKey.to_string();
1698+
assert_eq!(
1699+
StdDescriptor::from_str("sh(sortedmulti)")
1700+
.unwrap_err()
1701+
.to_string(),
1702+
"unexpected «no arguments given for sortedmulti»"
1703+
); //issue 202
1704+
assert_eq!(
1705+
StdDescriptor::from_str(&format!("sh(sortedmulti(2,{}))", compressed_pk))
1706+
.unwrap_err()
1707+
.to_string(),
1708+
"unexpected «higher threshold than there were keys in sortedmulti»"
1709+
); //issue 202
16881710

16891711
StdDescriptor::from_str(TEST_PK).unwrap();
16901712

src/psbt/finalizer.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -251,42 +251,41 @@ pub fn finalize<C: secp256k1::Verification>(
251251

252252
// Check well-formedness of input data
253253
for (n, input) in psbt.inputs.iter().enumerate() {
254-
if let Some(target) = input.sighash_type {
255-
for (key, rawsig) in &input.partial_sigs {
256-
if rawsig.is_empty() {
254+
let target = input.sighash_type.unwrap_or(bitcoin::SigHashType::All);
255+
for (key, rawsig) in &input.partial_sigs {
256+
if rawsig.is_empty() {
257+
return Err(Error::InputError(
258+
InputError::InvalidSignature {
259+
pubkey: *key,
260+
sig: rawsig.clone(),
261+
},
262+
n,
263+
));
264+
}
265+
let (flag, sig) = rawsig.split_last().unwrap();
266+
let flag = bitcoin::SigHashType::from_u32(*flag as u32);
267+
if target != flag {
268+
return Err(Error::InputError(
269+
InputError::WrongSigHashFlag {
270+
required: target,
271+
got: flag,
272+
pubkey: *key,
273+
},
274+
n,
275+
));
276+
}
277+
match secp256k1::Signature::from_der(sig) {
278+
Err(..) => {
257279
return Err(Error::InputError(
258280
InputError::InvalidSignature {
259281
pubkey: *key,
260-
sig: rawsig.clone(),
261-
},
262-
n,
263-
));
264-
}
265-
let (flag, sig) = rawsig.split_last().unwrap();
266-
let flag = bitcoin::SigHashType::from_u32(*flag as u32);
267-
if target != flag {
268-
return Err(Error::InputError(
269-
InputError::WrongSigHashFlag {
270-
required: target,
271-
got: flag,
272-
pubkey: *key,
282+
sig: Vec::from(sig),
273283
},
274284
n,
275285
));
276286
}
277-
match secp256k1::Signature::from_der(sig) {
278-
Err(..) => {
279-
return Err(Error::InputError(
280-
InputError::InvalidSignature {
281-
pubkey: *key,
282-
sig: Vec::from(sig),
283-
},
284-
n,
285-
));
286-
}
287-
Ok(_sig) => {
288-
// Interpreter will check all the sigs later.
289-
}
287+
Ok(_sig) => {
288+
// Interpreter will check all the sigs later.
290289
}
291290
}
292291
}

0 commit comments

Comments
 (0)