Use Message::from_digest

We have a new API function available with recent version of `secp256k1`
to create a `Message` directly from a sighash byte array.

Use `Message::from_digest(sighash.to_byte_array())` to construct
messages ready to sign.
This commit is contained in:
Tobin C. Harding 2023-10-11 08:16:25 +11:00
parent 6f30ac9d02
commit 678eee8d73
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
5 changed files with 10 additions and 47 deletions

View File

@ -45,9 +45,7 @@ fn compute_sighash_p2wpkh(raw_tx: &[u8], inp_idx: usize, value: u64) {
.p2wpkh_signature_hash(inp_idx, &spk, Amount::from_sat(value), sig.hash_ty)
.expect("failed to compute sighash");
println!("Segwit p2wpkh sighash: {:x}", sighash);
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
let msg = secp256k1::Message::from_digest_slice(sighash.as_byte_array())
.expect("sighash is 32 bytes long");
let msg = secp256k1::Message::from_digest(sighash.to_byte_array());
println!("Message is {:x}", msg);
let secp = secp256k1::Secp256k1::verification_only();
secp.verify_ecdsa(&msg, &sig.sig, &pk.inner).unwrap();

View File

@ -739,9 +739,7 @@ fn sign_psbt_taproot(
Some(_) => keypair, // no tweak for script spend
};
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
let msg = secp256k1::Message::from_digest_slice(hash.as_byte_array())
.expect("tap sighash is 32 bytes long");
let msg = secp256k1::Message::from_digest(hash.to_byte_array());
let sig = secp.sign_schnorr(&msg, &keypair);
let final_signature = taproot::Signature { sig, hash_ty };

View File

@ -1762,9 +1762,7 @@ mod tests {
.taproot_signature_hash(tx_ind, &Prevouts::All(&utxos), None, None, hash_ty)
.unwrap();
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
let msg = secp256k1::Message::from_digest_slice(sighash.as_byte_array())
.expect("sighash is 32 bytes long");
let msg = secp256k1::Message::from_digest(sighash.to_byte_array());
let key_spend_sig = secp.sign_schnorr_with_aux_rand(&msg, &tweaked_keypair, &[0u8; 32]);
assert_eq!(expected.internal_pubkey, internal_key);

View File

@ -405,56 +405,31 @@ impl Psbt {
match self.output_type(input_index)? {
Bare => {
let sighash = cache.legacy_signature_hash(input_index, spk, hash_ty.to_u32())?;
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
Ok((
Message::from_digest_slice(sighash.as_byte_array())
.expect("sighash is 32 bytes long"),
hash_ty,
))
Ok((Message::from_digest(sighash.to_byte_array()), hash_ty))
}
Sh => {
let script_code =
input.redeem_script.as_ref().ok_or(SignError::MissingRedeemScript)?;
let sighash =
cache.legacy_signature_hash(input_index, script_code, hash_ty.to_u32())?;
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
Ok((
Message::from_digest_slice(sighash.as_byte_array())
.expect("sighash is 32 bytes long"),
hash_ty,
))
Ok((Message::from_digest(sighash.to_byte_array()), hash_ty))
}
Wpkh => {
let sighash = cache.p2wpkh_signature_hash(input_index, spk, utxo.value, hash_ty)?;
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
Ok((
Message::from_digest_slice(sighash.as_byte_array())
.expect("sighash is 32 bytes long"),
hash_ty,
))
Ok((Message::from_digest(sighash.to_byte_array()), hash_ty))
}
ShWpkh => {
let redeem_script = input.redeem_script.as_ref().expect("checked above");
let sighash =
cache.p2wpkh_signature_hash(input_index, redeem_script, utxo.value, hash_ty)?;
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
Ok((
Message::from_digest_slice(sighash.as_byte_array())
.expect("sighash is 32 bytes long"),
hash_ty,
))
Ok((Message::from_digest(sighash.to_byte_array()), hash_ty))
}
Wsh | ShWsh => {
let witness_script =
input.witness_script.as_ref().ok_or(SignError::MissingWitnessScript)?;
let sighash =
cache.p2wsh_signature_hash(input_index, witness_script, utxo.value, hash_ty)?;
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
Ok((
Message::from_digest_slice(sighash.as_byte_array())
.expect("sighash is 32 bytes long"),
hash_ty,
))
Ok((Message::from_digest(sighash.to_byte_array()), hash_ty))
}
Tr => {
// This PSBT signing API is WIP, taproot to come shortly.

View File

@ -131,10 +131,7 @@ mod message_signing {
secp_ctx: &secp256k1::Secp256k1<C>,
msg_hash: sha256d::Hash,
) -> Result<PublicKey, MessageSignatureError> {
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
let msg = secp256k1::Message::from_digest_slice(msg_hash.as_byte_array())
.expect("sh256d hash is 32 bytes long");
let msg = secp256k1::Message::from_digest(msg_hash.to_byte_array());
let pubkey = secp_ctx.recover_ecdsa(&msg, &self.signature)?;
Ok(PublicKey { inner: pubkey, compressed: self.compressed })
}
@ -231,10 +228,7 @@ mod tests {
let secp = secp256k1::Secp256k1::new();
let message = "rust-bitcoin MessageSignature test";
let msg_hash = super::signed_msg_hash(message);
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
let msg = secp256k1::Message::from_digest_slice(msg_hash.as_byte_array())
.expect("sh256d hash is 32 bytes long");
let msg = secp256k1::Message::from_digest(msg_hash.to_byte_array());
let privkey = secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng());
let secp_sig = secp.sign_ecdsa_recoverable(&msg, &privkey);
let signature = super::MessageSignature { signature: secp_sig, compressed: true };