Skip to content

Commit 5fa86b2

Browse files
committed
Move input sighash type match checks into sanity_check
1 parent 49fe1ca commit 5fa86b2

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

src/psbt/finalizer.rs

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use bitcoin::blockdata::witness::Witness;
2828
use bitcoin::secp256k1::{self, Secp256k1};
2929
use bitcoin::util::key::XOnlyPublicKey;
3030
use bitcoin::util::taproot::LeafVersion;
31-
use bitcoin::{self, EcdsaSigHashType, PublicKey, Script};
31+
use bitcoin::{self, PublicKey, Script};
3232
use descriptor::DescriptorTrait;
3333
use interpreter;
3434
use Descriptor;
@@ -345,39 +345,6 @@ pub fn finalize_helper<C: secp256k1::Verification>(
345345
) -> Result<(), super::Error> {
346346
sanity_check(psbt)?;
347347

348-
// Check well-formedness of input data
349-
for (n, input) in psbt.inputs.iter().enumerate() {
350-
// TODO: fix this after https://github.com/rust-bitcoin/rust-bitcoin/issues/838
351-
let target_ecdsa_sighash_ty = match input.sighash_type {
352-
Some(psbt_hash_ty) => psbt_hash_ty
353-
.ecdsa_hash_ty()
354-
.map_err(|e| Error::InputError(InputError::NonStandardSigHashType(e), n))?,
355-
None => EcdsaSigHashType::All,
356-
};
357-
for (key, ecdsa_sig) in &input.partial_sigs {
358-
let flag = bitcoin::EcdsaSigHashType::from_u32_standard(ecdsa_sig.hash_ty as u32)
359-
.map_err(|_| {
360-
super::Error::InputError(
361-
InputError::Interpreter(interpreter::Error::NonStandardSigHash(
362-
ecdsa_sig.to_vec(),
363-
)),
364-
n,
365-
)
366-
})?;
367-
if target_ecdsa_sighash_ty != flag {
368-
return Err(Error::InputError(
369-
InputError::WrongSigHashFlag {
370-
required: target_ecdsa_sighash_ty,
371-
got: flag,
372-
pubkey: bitcoin::PublicKey::new(*key),
373-
},
374-
n,
375-
));
376-
}
377-
// Signatures are well-formed in psbt partial sigs
378-
}
379-
}
380-
381348
// Actually construct the witnesses
382349
for index in 0..psbt.inputs.len() {
383350
let (witness, script_sig) = {

src/psbt/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ fn try_vec_as_preimage32(vec: &Vec<u8>) -> Option<Preimage32> {
359359
}
360360
}
361361

362+
// Basic sanity checks on psbts.
363+
// rust-bitcoin TODO: (Long term)
364+
// Brainstorm about how we can enforce these in type system while having a nice API
362365
fn sanity_check(psbt: &Psbt) -> Result<(), Error> {
363366
if psbt.unsigned_tx.input.len() != psbt.inputs.len() {
364367
return Err(Error::WrongInputCount {
@@ -368,6 +371,39 @@ fn sanity_check(psbt: &Psbt) -> Result<(), Error> {
368371
.into());
369372
}
370373

374+
// Check well-formedness of input data
375+
for (index, input) in psbt.inputs.iter().enumerate() {
376+
// TODO: fix this after https://github.com/rust-bitcoin/rust-bitcoin/issues/838
377+
let target_ecdsa_sighash_ty = match input.sighash_type {
378+
Some(psbt_hash_ty) => psbt_hash_ty
379+
.ecdsa_hash_ty()
380+
.map_err(|e| Error::InputError(InputError::NonStandardSigHashType(e), index))?,
381+
None => EcdsaSigHashType::All,
382+
};
383+
for (key, ecdsa_sig) in &input.partial_sigs {
384+
let flag = bitcoin::EcdsaSigHashType::from_u32_standard(ecdsa_sig.hash_ty as u32)
385+
.map_err(|_| {
386+
Error::InputError(
387+
InputError::Interpreter(interpreter::Error::NonStandardSigHash(
388+
ecdsa_sig.to_vec(),
389+
)),
390+
index,
391+
)
392+
})?;
393+
if target_ecdsa_sighash_ty != flag {
394+
return Err(Error::InputError(
395+
InputError::WrongSigHashFlag {
396+
required: target_ecdsa_sighash_ty,
397+
got: flag,
398+
pubkey: bitcoin::PublicKey::new(*key),
399+
},
400+
index,
401+
));
402+
}
403+
// Signatures are well-formed in psbt partial sigs
404+
}
405+
}
406+
371407
Ok(())
372408
}
373409

0 commit comments

Comments
 (0)