Merge rust-bitcoin/rust-bitcoin#2098: Upgrade secp dependency

678eee8d73 Use Message::from_digest (Tobin C. Harding)
6f30ac9d02 Upgrade the secp dependency (Tobin C. Harding)
818a3c5424 Use Keypair instead of KeyPair in identifiers (Tobin C. Harding)

Pull request description:

  Upgrade the `secp256k1` dependency to the newly released `v0.28.0`.

  FTR this includes two simple changes:
  - Use `Message::from_digest_slice` instead of `Message::from_slice`.
  - Use `secp256k1::Keypair` instead of `secp256k1::KeyPair`.

  But to stay in line with the keypair change we deprecate and rename the tweaked alias.

ACKs for top commit:
  vincenzopalazzo:
    ACK 678eee8d73
  apoelstra:
    ACK 678eee8d73
  sanket1729:
    ACK 678eee8d73.

Tree-SHA512: 3a86eccc4a638e5e17c4e4a4a310c8812d6c9a0ea19be679234b2390ca586befe755984d1321c22f9adb6d5efd412f4638e99364ec18e82d74ca55ecf00f8587
This commit is contained in:
sanket1729 2023-10-11 18:00:46 -07:00
commit d67590ee79
No known key found for this signature in database
GPG Key ID: 648FFB183E0870A2
11 changed files with 64 additions and 116 deletions

View File

@ -38,7 +38,7 @@ dependencies = [
"bech32", "bech32",
"bincode", "bincode",
"bitcoin-internals", "bitcoin-internals",
"bitcoin_hashes 0.13.0", "bitcoin_hashes",
"bitcoinconsensus", "bitcoinconsensus",
"core2", "core2",
"hex-conservative", "hex-conservative",
@ -69,21 +69,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "bitcoin-private"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73290177011694f38ec25e165d0387ab7ea749a4b81cd4c80dae5988229f7a57"
[[package]]
name = "bitcoin_hashes"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d7066118b13d4b20b23645932dfb3a81ce7e29f95726c2036fa33cd7b092501"
dependencies = [
"bitcoin-private",
]
[[package]] [[package]]
name = "bitcoin_hashes" name = "bitcoin_hashes"
version = "0.13.0" version = "0.13.0"
@ -359,11 +344,11 @@ dependencies = [
[[package]] [[package]]
name = "secp256k1" name = "secp256k1"
version = "0.27.0" version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25996b82292a7a57ed3508f052cfff8640d38d32018784acd714758b43da9c8f" checksum = "2acea373acb8c21ecb5a23741452acd2593ed44ee3d343e72baaa143bc89d0d5"
dependencies = [ dependencies = [
"bitcoin_hashes 0.12.0", "bitcoin_hashes",
"rand", "rand",
"secp256k1-sys", "secp256k1-sys",
"serde", "serde",
@ -371,9 +356,9 @@ dependencies = [
[[package]] [[package]]
name = "secp256k1-sys" name = "secp256k1-sys"
version = "0.8.1" version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70a129b9e9efbfb223753b9163c4ab3b13cff7fd9c7f010fbac25ab4099fa07e" checksum = "09e67c467c38fd24bd5499dc9a18183b31575c12ee549197e3e20d57aa4fe3b7"
dependencies = [ dependencies = [
"cc", "cc",
] ]

View File

@ -37,7 +37,7 @@ dependencies = [
"bech32", "bech32",
"bincode", "bincode",
"bitcoin-internals", "bitcoin-internals",
"bitcoin_hashes 0.13.0", "bitcoin_hashes",
"bitcoinconsensus", "bitcoinconsensus",
"core2", "core2",
"hex-conservative", "hex-conservative",
@ -68,21 +68,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "bitcoin-private"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73290177011694f38ec25e165d0387ab7ea749a4b81cd4c80dae5988229f7a57"
[[package]]
name = "bitcoin_hashes"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d7066118b13d4b20b23645932dfb3a81ce7e29f95726c2036fa33cd7b092501"
dependencies = [
"bitcoin-private",
]
[[package]] [[package]]
name = "bitcoin_hashes" name = "bitcoin_hashes"
version = "0.13.0" version = "0.13.0"
@ -348,11 +333,11 @@ dependencies = [
[[package]] [[package]]
name = "secp256k1" name = "secp256k1"
version = "0.27.0" version = "0.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25996b82292a7a57ed3508f052cfff8640d38d32018784acd714758b43da9c8f" checksum = "2acea373acb8c21ecb5a23741452acd2593ed44ee3d343e72baaa143bc89d0d5"
dependencies = [ dependencies = [
"bitcoin_hashes 0.12.0", "bitcoin_hashes",
"rand", "rand",
"secp256k1-sys", "secp256k1-sys",
"serde", "serde",
@ -360,9 +345,9 @@ dependencies = [
[[package]] [[package]]
name = "secp256k1-sys" name = "secp256k1-sys"
version = "0.8.1" version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70a129b9e9efbfb223753b9163c4ab3b13cff7fd9c7f010fbac25ab4099fa07e" checksum = "09e67c467c38fd24bd5499dc9a18183b31575c12ee549197e3e20d57aa4fe3b7"
dependencies = [ dependencies = [
"cc", "cc",
] ]

View File

@ -38,7 +38,7 @@ internals = { package = "bitcoin-internals", version = "0.2.0" }
hex = { package = "hex-conservative", version = "0.1.1", default-features = false } hex = { package = "hex-conservative", version = "0.1.1", default-features = false }
bech32 = { version = "0.10.0-beta", default-features = false } bech32 = { version = "0.10.0-beta", default-features = false }
hashes = { package = "bitcoin_hashes", version = "0.13.0", default-features = false } hashes = { package = "bitcoin_hashes", version = "0.13.0", default-features = false }
secp256k1 = { version = "0.27.0", default-features = false, features = ["bitcoin_hashes"] } secp256k1 = { version = "0.28.0", default-features = false, features = ["hashes"] }
hex_lit = "0.1.1" hex_lit = "0.1.1"
base64 = { version = "0.21.3", optional = true } base64 = { version = "0.21.3", optional = true }

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) .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_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();

View File

@ -733,15 +733,13 @@ fn sign_psbt_taproot(
hash_ty: TapSighashType, hash_ty: TapSighashType,
secp: &Secp256k1<secp256k1::All>, secp: &Secp256k1<secp256k1::All>,
) { ) {
let keypair = secp256k1::KeyPair::from_seckey_slice(secp, secret_key.as_ref()).unwrap(); let keypair = secp256k1::Keypair::from_seckey_slice(secp, secret_key.as_ref()).unwrap();
let keypair = match leaf_hash { let keypair = match leaf_hash {
None => keypair.tap_tweak(secp, psbt_input.tap_merkle_root).to_inner(), None => keypair.tap_tweak(secp, psbt_input.tap_merkle_root).to_inner(),
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_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 };

View File

@ -21,7 +21,7 @@ use secp256k1::{self, Secp256k1, XOnlyPublicKey};
use serde; use serde;
use crate::base58; use crate::base58;
use crate::crypto::key::{self, KeyPair, PrivateKey, PublicKey}; use crate::crypto::key::{self, Keypair, PrivateKey, PublicKey};
use crate::internal_macros::impl_bytes_newtype; use crate::internal_macros::impl_bytes_newtype;
use crate::io::Write; use crate::io::Write;
use crate::network::Network; use crate::network::Network;
@ -579,8 +579,8 @@ impl Xpriv {
/// Constructs BIP340 keypair for Schnorr signatures and Taproot use matching the internal /// Constructs BIP340 keypair for Schnorr signatures and Taproot use matching the internal
/// secret key representation. /// secret key representation.
pub fn to_keypair<C: secp256k1::Signing>(self, secp: &Secp256k1<C>) -> KeyPair { pub fn to_keypair<C: secp256k1::Signing>(self, secp: &Secp256k1<C>) -> Keypair {
KeyPair::from_seckey_slice(secp, &self.private_key[..]) Keypair::from_seckey_slice(secp, &self.private_key[..])
.expect("BIP32 internal private key representation is broken") .expect("BIP32 internal private key representation is broken")
} }

View File

@ -14,7 +14,7 @@ use hex::FromHex;
use internals::write_err; use internals::write_err;
#[cfg(feature = "rand-std")] #[cfg(feature = "rand-std")]
pub use secp256k1::rand; pub use secp256k1::rand;
pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey}; pub use secp256k1::{self, constants, Keypair, Parity, Secp256k1, Verification, XOnlyPublicKey};
use crate::crypto::ecdsa; use crate::crypto::ecdsa;
use crate::network::Network; use crate::network::Network;
@ -507,17 +507,27 @@ impl fmt::Display for TweakedPublicKey {
} }
/// Untweaked BIP-340 key pair /// Untweaked BIP-340 key pair
pub type UntweakedKeyPair = KeyPair; #[deprecated(since = "0.31.0", note = "use UntweakedKeypair instead")]
#[allow(deprecated)]
pub type UntweakedKeyPair = UntweakedKeypair;
/// Untweaked BIP-340 key pair
pub type UntweakedKeypair = Keypair;
/// Tweaked BIP-340 key pair
#[deprecated(since = "0.31.0", note = "use TweakedKeypair instead")]
#[allow(deprecated)]
pub type TweakedKeyPair = TweakedKeypair;
/// Tweaked BIP-340 key pair /// Tweaked BIP-340 key pair
/// ///
/// # Examples /// # Examples
/// ``` /// ```
/// # #[cfg(feature = "rand-std")] { /// # #[cfg(feature = "rand-std")] {
/// # use bitcoin::key::{KeyPair, TweakedKeyPair, TweakedPublicKey}; /// # use bitcoin::key::{Keypair, TweakedKeypair, TweakedPublicKey};
/// # use bitcoin::secp256k1::{rand, Secp256k1}; /// # use bitcoin::secp256k1::{rand, Secp256k1};
/// # let secp = Secp256k1::new(); /// # let secp = Secp256k1::new();
/// # let keypair = TweakedKeyPair::dangerous_assume_tweaked(KeyPair::new(&secp, &mut rand::thread_rng())); /// # let keypair = TweakedKeypair::dangerous_assume_tweaked(Keypair::new(&secp, &mut rand::thread_rng()));
/// // There are various conversion methods available to get a tweaked pubkey from a tweaked keypair. /// // There are various conversion methods available to get a tweaked pubkey from a tweaked keypair.
/// let (_pk, _parity) = keypair.public_parts(); /// let (_pk, _parity) = keypair.public_parts();
/// let _pk = TweakedPublicKey::from_keypair(keypair); /// let _pk = TweakedPublicKey::from_keypair(keypair);
@ -528,7 +538,7 @@ pub type UntweakedKeyPair = KeyPair;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))] #[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
#[cfg_attr(feature = "serde", serde(transparent))] #[cfg_attr(feature = "serde", serde(transparent))]
pub struct TweakedKeyPair(KeyPair); pub struct TweakedKeypair(Keypair);
/// A trait for tweaking BIP340 key types (x-only public keys and key pairs). /// A trait for tweaking BIP340 key types (x-only public keys and key pairs).
pub trait TapTweak { pub trait TapTweak {
@ -538,7 +548,7 @@ pub trait TapTweak {
type TweakedKey; type TweakedKey;
/// Tweaks an untweaked key with corresponding public key value and optional script tree merkle /// Tweaks an untweaked key with corresponding public key value and optional script tree merkle
/// root. For the [`KeyPair`] type this also tweaks the private key in the pair. /// root. For the [`Keypair`] type this also tweaks the private key in the pair.
/// ///
/// This is done by using the equation Q = P + H(P|c)G, where /// This is done by using the equation Q = P + H(P|c)G, where
/// * Q is the tweaked public key /// * Q is the tweaked public key
@ -593,11 +603,11 @@ impl TapTweak for UntweakedPublicKey {
fn dangerous_assume_tweaked(self) -> TweakedPublicKey { TweakedPublicKey(self) } fn dangerous_assume_tweaked(self) -> TweakedPublicKey { TweakedPublicKey(self) }
} }
impl TapTweak for UntweakedKeyPair { impl TapTweak for UntweakedKeypair {
type TweakedAux = TweakedKeyPair; type TweakedAux = TweakedKeypair;
type TweakedKey = TweakedKeyPair; type TweakedKey = TweakedKeypair;
/// Tweaks private and public keys within an untweaked [`KeyPair`] with corresponding public key /// Tweaks private and public keys within an untweaked [`Keypair`] with corresponding public key
/// value and optional script tree merkle root. /// value and optional script tree merkle root.
/// ///
/// This is done by tweaking private key within the pair using the equation q = p + H(P|c), where /// This is done by tweaking private key within the pair using the equation q = p + H(P|c), where
@ -613,20 +623,20 @@ impl TapTweak for UntweakedKeyPair {
self, self,
secp: &Secp256k1<C>, secp: &Secp256k1<C>,
merkle_root: Option<TapNodeHash>, merkle_root: Option<TapNodeHash>,
) -> TweakedKeyPair { ) -> TweakedKeypair {
let (pubkey, _parity) = XOnlyPublicKey::from_keypair(&self); let (pubkey, _parity) = XOnlyPublicKey::from_keypair(&self);
let tweak = TapTweakHash::from_key_and_tweak(pubkey, merkle_root).to_scalar(); let tweak = TapTweakHash::from_key_and_tweak(pubkey, merkle_root).to_scalar();
let tweaked = self.add_xonly_tweak(secp, &tweak).expect("Tap tweak failed"); let tweaked = self.add_xonly_tweak(secp, &tweak).expect("Tap tweak failed");
TweakedKeyPair(tweaked) TweakedKeypair(tweaked)
} }
fn dangerous_assume_tweaked(self) -> TweakedKeyPair { TweakedKeyPair(self) } fn dangerous_assume_tweaked(self) -> TweakedKeypair { TweakedKeypair(self) }
} }
impl TweakedPublicKey { impl TweakedPublicKey {
/// Returns the [`TweakedPublicKey`] for `keypair`. /// Returns the [`TweakedPublicKey`] for `keypair`.
#[inline] #[inline]
pub fn from_keypair(keypair: TweakedKeyPair) -> Self { pub fn from_keypair(keypair: TweakedKeypair) -> Self {
let (xonly, _parity) = keypair.0.x_only_public_key(); let (xonly, _parity) = keypair.0.x_only_public_key();
TweakedPublicKey(xonly) TweakedPublicKey(xonly)
} }
@ -651,20 +661,20 @@ impl TweakedPublicKey {
pub fn serialize(&self) -> [u8; constants::SCHNORR_PUBLIC_KEY_SIZE] { self.0.serialize() } pub fn serialize(&self) -> [u8; constants::SCHNORR_PUBLIC_KEY_SIZE] { self.0.serialize() }
} }
impl TweakedKeyPair { impl TweakedKeypair {
/// Creates a new [`TweakedKeyPair`] from a [`KeyPair`]. No tweak is applied, consider /// Creates a new [`TweakedKeypair`] from a [`Keypair`]. No tweak is applied, consider
/// calling `tap_tweak` on an [`UntweakedKeyPair`] instead of using this constructor. /// calling `tap_tweak` on an [`UntweakedKeypair`] instead of using this constructor.
/// ///
/// This method is dangerous and can lead to loss of funds if used incorrectly. /// This method is dangerous and can lead to loss of funds if used incorrectly.
/// Specifically, in multi-party protocols a peer can provide a value that allows them to steal. /// Specifically, in multi-party protocols a peer can provide a value that allows them to steal.
#[inline] #[inline]
pub fn dangerous_assume_tweaked(pair: KeyPair) -> TweakedKeyPair { TweakedKeyPair(pair) } pub fn dangerous_assume_tweaked(pair: Keypair) -> TweakedKeypair { TweakedKeypair(pair) }
/// Returns the underlying key pair. /// Returns the underlying key pair.
#[inline] #[inline]
pub fn to_inner(self) -> KeyPair { self.0 } pub fn to_inner(self) -> Keypair { self.0 }
/// Returns the [`TweakedPublicKey`] and its [`Parity`] for this [`TweakedKeyPair`]. /// Returns the [`TweakedPublicKey`] and its [`Parity`] for this [`TweakedKeypair`].
#[inline] #[inline]
pub fn public_parts(&self) -> (TweakedPublicKey, Parity) { pub fn public_parts(&self) -> (TweakedPublicKey, Parity) {
let (xonly, parity) = self.0.x_only_public_key(); let (xonly, parity) = self.0.x_only_public_key();
@ -677,14 +687,14 @@ impl From<TweakedPublicKey> for XOnlyPublicKey {
fn from(pair: TweakedPublicKey) -> Self { pair.0 } fn from(pair: TweakedPublicKey) -> Self { pair.0 }
} }
impl From<TweakedKeyPair> for KeyPair { impl From<TweakedKeypair> for Keypair {
#[inline] #[inline]
fn from(pair: TweakedKeyPair) -> Self { pair.0 } fn from(pair: TweakedKeypair) -> Self { pair.0 }
} }
impl From<TweakedKeyPair> for TweakedPublicKey { impl From<TweakedKeypair> for TweakedPublicKey {
#[inline] #[inline]
fn from(pair: TweakedKeyPair) -> Self { TweakedPublicKey::from_keypair(pair) } fn from(pair: TweakedKeypair) -> Self { TweakedPublicKey::from_keypair(pair) }
} }
/// A key-related error. /// A key-related error.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -1066,7 +1076,7 @@ mod tests {
use secp256k1::rand; use secp256k1::rand;
let secp = Secp256k1::new(); let secp = Secp256k1::new();
let kp = KeyPair::new(&secp, &mut rand::thread_rng()); let kp = Keypair::new(&secp, &mut rand::thread_rng());
let _ = PublicKey::new(kp); let _ = PublicKey::new(kp);
let _ = PublicKey::new_uncompressed(kp); let _ = PublicKey::new_uncompressed(kp);

View File

@ -1743,7 +1743,7 @@ mod tests {
}; };
// tests // tests
let keypair = secp256k1::KeyPair::from_secret_key(secp, &internal_priv_key); let keypair = secp256k1::Keypair::from_secret_key(secp, &internal_priv_key);
let (internal_key, _parity) = XOnlyPublicKey::from_keypair(&keypair); let (internal_key, _parity) = XOnlyPublicKey::from_keypair(&keypair);
let tweak = TapTweakHash::from_key_and_tweak(internal_key, merkle_root); let tweak = TapTweakHash::from_key_and_tweak(internal_key, merkle_root);
let tweaked_keypair = keypair.add_xonly_tweak(secp, &tweak.to_scalar()).unwrap(); let tweaked_keypair = keypair.add_xonly_tweak(secp, &tweak.to_scalar()).unwrap();
@ -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_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);

View File

@ -8,7 +8,7 @@
use core::fmt; use core::fmt;
use internals::write_err; use internals::write_err;
pub use secp256k1::{self, constants, KeyPair, Parity, Secp256k1, Verification, XOnlyPublicKey}; pub use secp256k1::{self, constants, Keypair, Parity, Secp256k1, Verification, XOnlyPublicKey};
use crate::prelude::*; use crate::prelude::*;
use crate::sighash::{InvalidSighashTypeError, TapSighashType}; use crate::sighash::{InvalidSighashTypeError, TapSighashType};

View File

@ -405,51 +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_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_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_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_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_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.

View File

@ -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_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_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 };