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:
parent
6f30ac9d02
commit
678eee8d73
|
@ -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)
|
.p2wpkh_signature_hash(inp_idx, &spk, Amount::from_sat(value), sig.hash_ty)
|
||||||
.expect("failed to compute sighash");
|
.expect("failed to compute sighash");
|
||||||
println!("Segwit p2wpkh sighash: {:x}", 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(sighash.to_byte_array());
|
||||||
let msg = secp256k1::Message::from_digest_slice(sighash.as_byte_array())
|
|
||||||
.expect("sighash is 32 bytes long");
|
|
||||||
println!("Message is {:x}", msg);
|
println!("Message is {:x}", msg);
|
||||||
let secp = secp256k1::Secp256k1::verification_only();
|
let secp = secp256k1::Secp256k1::verification_only();
|
||||||
secp.verify_ecdsa(&msg, &sig.sig, &pk.inner).unwrap();
|
secp.verify_ecdsa(&msg, &sig.sig, &pk.inner).unwrap();
|
||||||
|
|
|
@ -739,9 +739,7 @@ fn sign_psbt_taproot(
|
||||||
Some(_) => keypair, // no tweak for script spend
|
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(hash.to_byte_array());
|
||||||
let msg = secp256k1::Message::from_digest_slice(hash.as_byte_array())
|
|
||||||
.expect("tap sighash is 32 bytes long");
|
|
||||||
let sig = secp.sign_schnorr(&msg, &keypair);
|
let sig = secp.sign_schnorr(&msg, &keypair);
|
||||||
|
|
||||||
let final_signature = taproot::Signature { sig, hash_ty };
|
let final_signature = taproot::Signature { sig, hash_ty };
|
||||||
|
|
|
@ -1762,9 +1762,7 @@ mod tests {
|
||||||
.taproot_signature_hash(tx_ind, &Prevouts::All(&utxos), None, None, hash_ty)
|
.taproot_signature_hash(tx_ind, &Prevouts::All(&utxos), None, None, hash_ty)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
|
let msg = secp256k1::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 key_spend_sig = secp.sign_schnorr_with_aux_rand(&msg, &tweaked_keypair, &[0u8; 32]);
|
let key_spend_sig = secp.sign_schnorr_with_aux_rand(&msg, &tweaked_keypair, &[0u8; 32]);
|
||||||
|
|
||||||
assert_eq!(expected.internal_pubkey, internal_key);
|
assert_eq!(expected.internal_pubkey, internal_key);
|
||||||
|
|
|
@ -405,56 +405,31 @@ impl Psbt {
|
||||||
match self.output_type(input_index)? {
|
match self.output_type(input_index)? {
|
||||||
Bare => {
|
Bare => {
|
||||||
let sighash = cache.legacy_signature_hash(input_index, spk, hash_ty.to_u32())?;
|
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(sighash.to_byte_array()), hash_ty))
|
||||||
Ok((
|
|
||||||
Message::from_digest_slice(sighash.as_byte_array())
|
|
||||||
.expect("sighash is 32 bytes long"),
|
|
||||||
hash_ty,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
Sh => {
|
Sh => {
|
||||||
let script_code =
|
let script_code =
|
||||||
input.redeem_script.as_ref().ok_or(SignError::MissingRedeemScript)?;
|
input.redeem_script.as_ref().ok_or(SignError::MissingRedeemScript)?;
|
||||||
let sighash =
|
let sighash =
|
||||||
cache.legacy_signature_hash(input_index, script_code, hash_ty.to_u32())?;
|
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(sighash.to_byte_array()), hash_ty))
|
||||||
Ok((
|
|
||||||
Message::from_digest_slice(sighash.as_byte_array())
|
|
||||||
.expect("sighash is 32 bytes long"),
|
|
||||||
hash_ty,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
Wpkh => {
|
Wpkh => {
|
||||||
let sighash = cache.p2wpkh_signature_hash(input_index, spk, utxo.value, hash_ty)?;
|
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(sighash.to_byte_array()), hash_ty))
|
||||||
Ok((
|
|
||||||
Message::from_digest_slice(sighash.as_byte_array())
|
|
||||||
.expect("sighash is 32 bytes long"),
|
|
||||||
hash_ty,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
ShWpkh => {
|
ShWpkh => {
|
||||||
let redeem_script = input.redeem_script.as_ref().expect("checked above");
|
let redeem_script = input.redeem_script.as_ref().expect("checked above");
|
||||||
let sighash =
|
let sighash =
|
||||||
cache.p2wpkh_signature_hash(input_index, redeem_script, utxo.value, hash_ty)?;
|
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(sighash.to_byte_array()), hash_ty))
|
||||||
Ok((
|
|
||||||
Message::from_digest_slice(sighash.as_byte_array())
|
|
||||||
.expect("sighash is 32 bytes long"),
|
|
||||||
hash_ty,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
Wsh | ShWsh => {
|
Wsh | ShWsh => {
|
||||||
let witness_script =
|
let witness_script =
|
||||||
input.witness_script.as_ref().ok_or(SignError::MissingWitnessScript)?;
|
input.witness_script.as_ref().ok_or(SignError::MissingWitnessScript)?;
|
||||||
let sighash =
|
let sighash =
|
||||||
cache.p2wsh_signature_hash(input_index, witness_script, utxo.value, hash_ty)?;
|
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(sighash.to_byte_array()), hash_ty))
|
||||||
Ok((
|
|
||||||
Message::from_digest_slice(sighash.as_byte_array())
|
|
||||||
.expect("sighash is 32 bytes long"),
|
|
||||||
hash_ty,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
Tr => {
|
Tr => {
|
||||||
// This PSBT signing API is WIP, taproot to come shortly.
|
// This PSBT signing API is WIP, taproot to come shortly.
|
||||||
|
|
|
@ -131,10 +131,7 @@ mod message_signing {
|
||||||
secp_ctx: &secp256k1::Secp256k1<C>,
|
secp_ctx: &secp256k1::Secp256k1<C>,
|
||||||
msg_hash: sha256d::Hash,
|
msg_hash: sha256d::Hash,
|
||||||
) -> Result<PublicKey, MessageSignatureError> {
|
) -> Result<PublicKey, MessageSignatureError> {
|
||||||
// TODO: After upgrade of secp change this to Message::from_digest(sighash.to_byte_array()).
|
let msg = secp256k1::Message::from_digest(msg_hash.to_byte_array());
|
||||||
let msg = secp256k1::Message::from_digest_slice(msg_hash.as_byte_array())
|
|
||||||
.expect("sh256d hash is 32 bytes long");
|
|
||||||
|
|
||||||
let pubkey = secp_ctx.recover_ecdsa(&msg, &self.signature)?;
|
let pubkey = secp_ctx.recover_ecdsa(&msg, &self.signature)?;
|
||||||
Ok(PublicKey { inner: pubkey, compressed: self.compressed })
|
Ok(PublicKey { inner: pubkey, compressed: self.compressed })
|
||||||
}
|
}
|
||||||
|
@ -231,10 +228,7 @@ mod tests {
|
||||||
let secp = secp256k1::Secp256k1::new();
|
let secp = secp256k1::Secp256k1::new();
|
||||||
let message = "rust-bitcoin MessageSignature test";
|
let message = "rust-bitcoin MessageSignature test";
|
||||||
let msg_hash = super::signed_msg_hash(message);
|
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(msg_hash.to_byte_array());
|
||||||
let msg = secp256k1::Message::from_digest_slice(msg_hash.as_byte_array())
|
|
||||||
.expect("sh256d hash is 32 bytes long");
|
|
||||||
|
|
||||||
let privkey = secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng());
|
let privkey = secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng());
|
||||||
let secp_sig = secp.sign_ecdsa_recoverable(&msg, &privkey);
|
let secp_sig = secp.sign_ecdsa_recoverable(&msg, &privkey);
|
||||||
let signature = super::MessageSignature { signature: secp_sig, compressed: true };
|
let signature = super::MessageSignature { signature: secp_sig, compressed: true };
|
||||||
|
|
Loading…
Reference in New Issue