From aeede126048a0dd491c439e382d86942b716e1ec Mon Sep 17 00:00:00 2001 From: Arturo Marquez Date: Wed, 20 Jul 2022 19:23:28 -0500 Subject: [PATCH] Infallible conversions: `Hash` -> `Message` Replace all instances of `secp256k1::Message::from_slice(_).expect(_)` with `secp256k1::Message::from(_)`. Also adds an implementation of ThirtyTwoByteHash for TapSighashHash. Solves https://github.com/rust-bitcoin/rust-bitcoin/issues/824 --- Cargo.toml | 2 +- src/util/misc.rs | 5 ++--- src/util/sighash.rs | 2 +- src/util/taproot.rs | 6 ++++++ 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c4e94755..1e44352b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,7 @@ rustdoc-args = ["--cfg", "docsrs"] [dependencies] bech32 = { version = "0.8.1", default-features = false } bitcoin_hashes = { version = "0.11.0", default-features = false } -secp256k1 = { version = "0.24.0", default-features = false } +secp256k1 = { version = "0.24.0", default-features = false, features = ["bitcoin_hashes"] } core2 = { version = "0.3.0", optional = true, default-features = false } base64 = { version = "0.13.0", optional = true } diff --git a/src/util/misc.rs b/src/util/misc.rs index b26466a5..447eac12 100644 --- a/src/util/misc.rs +++ b/src/util/misc.rs @@ -141,8 +141,7 @@ mod message_signing { secp_ctx: &secp256k1::Secp256k1, msg_hash: sha256d::Hash ) -> Result { - let msg = secp256k1::Message::from_slice(&msg_hash[..]) - .expect("cannot fail"); + let msg = secp256k1::Message::from(msg_hash); let pubkey = secp_ctx.recover_ecdsa(&msg, &self.signature)?; Ok(PublicKey { inner: pubkey, @@ -319,7 +318,7 @@ mod tests { let secp = secp256k1::Secp256k1::new(); let message = "rust-bitcoin MessageSignature test"; let msg_hash = super::signed_msg_hash(&message); - let msg = secp256k1::Message::from_slice(&msg_hash).expect("message"); + let msg = secp256k1::Message::from(msg_hash); let privkey = secp256k1::SecretKey::new(&mut secp256k1::rand::thread_rng()); diff --git a/src/util/sighash.rs b/src/util/sighash.rs index 9463c0ab..95b2cf6e 100644 --- a/src/util/sighash.rs +++ b/src/util/sighash.rs @@ -1148,7 +1148,7 @@ mod tests { hash_ty ).unwrap(); - let msg = secp256k1::Message::from_slice(&sighash).unwrap(); + let msg = secp256k1::Message::from(sighash); let key_spend_sig = secp.sign_schnorr_with_aux_rand(&msg, &tweaked_keypair, &[0u8; 32]); assert_eq!(expected_internal_pk, internal_key); diff --git a/src/util/taproot.rs b/src/util/taproot.rs index eb1ca806..d5b63a2d 100644 --- a/src/util/taproot.rs +++ b/src/util/taproot.rs @@ -62,6 +62,12 @@ sha256t_hash_newtype!(TapSighashHash, TapSighashTag, MIDSTATE_TAPSIGHASH, 64, doc="Taproot-tagged hash for the taproot signature hash", false ); +impl secp256k1::ThirtyTwoByteHash for TapSighashHash { + fn into_32(self) -> [u8; 32] { + self.into_inner() + } +} + impl TapTweakHash { /// Creates a new BIP341 [`TapTweakHash`] from key and tweak. Produces `H_taptweak(P||R)` where /// `P` is the internal key and `R` is the merkle root.