From 696bda1ad9a9f7b9814e82efcec92fb5eb9e96eb Mon Sep 17 00:00:00 2001 From: Brian Hoffman Date: Fri, 25 Oct 2024 12:52:25 -0400 Subject: [PATCH 1/5] Introduce nonce to schnorr signatures and BIP322Result type --- Cargo.lock | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 4 +-- src/sign.rs | 49 ++++++++++++++++++++++++-- src/util.rs | 6 ++-- src/verify.rs | 51 +++++++++++++++++++++------ 6 files changed, 191 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6479e57e..61ec137b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -39,6 +39,7 @@ dependencies = [ "hex", "miniscript", "pretty_assertions", + "secp256k1", "snafu", ] @@ -90,6 +91,12 @@ dependencies = [ "hex-conservative", ] +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "cc" version = "1.1.18" @@ -99,12 +106,29 @@ dependencies = [ "shlex", ] +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "diff" version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "heck" version = "0.5.0" @@ -132,6 +156,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd" +[[package]] +name = "libc" +version = "0.2.161" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" + [[package]] name = "miniscript" version = "12.2.0" @@ -142,6 +172,15 @@ dependencies = [ "bitcoin", ] +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + [[package]] name = "pretty_assertions" version = "1.4.1" @@ -170,6 +209,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "secp256k1" version = "0.29.1" @@ -177,6 +246,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9465315bc9d4566e1724f0fffcbcc446268cb522e60f9a27bcded6b19c108113" dependencies = [ "bitcoin_hashes", + "rand", "secp256k1-sys", ] @@ -233,8 +303,35 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "yansi" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index ee451c12..b661897c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ bitcoin = "0.32.3" hex = "0.4.3" miniscript = "12.2.0" snafu = { version = "0.8.5", default-features = false, features = ["rust_1_61", "std"] } +secp256k1 = { version = "0.29.1", features = ["global-context", "rand-std"] } [dev-dependencies] pretty_assertions = "1.4.1" diff --git a/src/lib.rs b/src/lib.rs index 94ed2c2f..3f0bd14d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,11 +7,11 @@ use { blockdata::script, consensus::Decodable, consensus::Encodable, - key::{Keypair, TapTweak}, + key::{Keypair, Secp256k1, TapTweak, UntweakedKeypair}, opcodes, psbt::Psbt, script::PushBytes, - secp256k1::{self, schnorr::Signature, Message, Secp256k1, XOnlyPublicKey}, + secp256k1::{self, schnorr::Signature, Message, SecretKey, XOnlyPublicKey}, sighash::{self, SighashCache, TapSighashType}, transaction::Version, Address, Amount, EcdsaSighashType, OutPoint, PrivateKey, PublicKey, ScriptBuf, Sequence, diff --git a/src/sign.rs b/src/sign.rs index 3cf84fe6..bd899060 100644 --- a/src/sign.rs +++ b/src/sign.rs @@ -95,6 +95,49 @@ pub fn sign_full( to_sign.extract_tx().context(error::TransactionExtract) } +pub fn sign_message_bip322( + keypair: &UntweakedKeypair, + msg: &[u8], + network: bitcoin::Network, +) -> [u8; 64] { + let secp = Secp256k1::new(); + let xpubk = XOnlyPublicKey::from_keypair(keypair).0; + let private_key = PrivateKey::new(SecretKey::from_keypair(keypair), network); + + let address = Address::p2tr(&secp, xpubk, None, network); + + let to_spend = create_to_spend(&address, msg).unwrap(); + let mut to_sign = create_to_sign(&to_spend, None).unwrap(); + + let witness = match address.witness_program() { + Some(witness_program) => { + let version = witness_program.version().to_num(); + let program_len = witness_program.program().len(); + + match version { + 1 => { + if program_len != 32 { + panic!("not key spend path"); + } + create_message_signature_taproot(&to_spend, &to_sign, private_key) + } + _ => { + panic!("unsuported address"); + } + } + } + None => { + panic!("unsuported address"); + } + }; + + to_sign.inputs[0].final_script_witness = Some(witness); + + let signature = to_sign.extract_tx().unwrap().input[0].witness.clone(); + + signature.to_vec()[0][..64].try_into().unwrap() +} + fn create_message_signature_p2wpkh( to_spend_tx: &Transaction, to_sign: &Psbt, @@ -175,8 +218,8 @@ fn create_message_signature_taproot( .tap_tweak(&secp, to_sign.inputs[0].tap_merkle_root) .to_inner(); - let signature = secp.sign_schnorr_no_aux_rand( - &secp256k1::Message::from_digest_slice(sighash.as_ref()) + let sig = secp.sign_schnorr( + &bitcoin::secp256k1::Message::from_digest_slice(sighash.as_ref()) .expect("should be cryptographically secure hash"), &key_pair, ); @@ -187,7 +230,7 @@ fn create_message_signature_taproot( witness.push( bitcoin::taproot::Signature { - signature, + signature: sig, sighash_type, } .to_vec(), diff --git a/src/util.rs b/src/util.rs index 362456f6..64ee3099 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,7 @@ use super::*; +pub type BIP322Result = std::result::Result; + const TAG: &str = "BIP0322-signed-message"; /// Create the tagged message hash. @@ -14,7 +16,7 @@ pub fn message_hash(message: &[u8]) -> Vec { } /// Create the `to_spend` transaction. -pub fn create_to_spend(address: &Address, message: &[u8]) -> Result { +pub fn create_to_spend(address: &Address, message: &[u8]) -> BIP322Result { Ok(Transaction { version: Version(0), lock_time: LockTime::ZERO, @@ -40,7 +42,7 @@ pub fn create_to_spend(address: &Address, message: &[u8]) -> Result } /// Create the `to_sign` transaction. -pub fn create_to_sign(to_spend: &Transaction, witness: Option) -> Result { +pub fn create_to_sign(to_spend: &Transaction, witness: Option) -> BIP322Result { let inputs = vec![TxIn { previous_output: OutPoint { txid: to_spend.compute_txid(), diff --git a/src/verify.rs b/src/verify.rs index dafa19df..a134474a 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -1,5 +1,7 @@ use super::*; +use crate::util::BIP322Result; + /// Verifies the BIP-322 simple from spec-compliant string encodings. pub fn verify_simple_encoded(address: &str, message: &str, signature: &str) -> Result<()> { let address = Address::from_str(address) @@ -40,7 +42,7 @@ pub fn verify_full_encoded(address: &str, message: &str, to_sign: &str) -> Resul } /// Verifies the BIP-322 simple from proper Rust types. -pub fn verify_simple(address: &Address, message: &[u8], signature: Witness) -> Result<()> { +pub fn verify_simple(address: &Address, message: &[u8], signature: Witness) -> BIP322Result<()> { verify_full( address, message, @@ -51,8 +53,9 @@ pub fn verify_simple(address: &Address, message: &[u8], signature: Witness) -> R } /// Verifies the BIP-322 full from proper Rust types. -pub fn verify_full(address: &Address, message: &[u8], to_sign: Transaction) -> Result<()> { +pub fn verify_full(address: &Address, message: &[u8], to_sign: Transaction) -> BIP322Result<()> { match address.to_address_data() { + // Handle P2TR (Taproot) addresses AddressData::Segwit { witness_program } if witness_program.version().to_num() == 1 && witness_program.program().len() == 32 => { @@ -61,6 +64,7 @@ pub fn verify_full(address: &Address, message: &[u8], to_sign: Transaction) -> R verify_full_p2tr(address, message, to_sign, pub_key) } + // Handle P2WPKH addresses AddressData::Segwit { witness_program } if witness_program.version().to_num() == 0 && witness_program.program().len() == 20 => { @@ -69,18 +73,45 @@ pub fn verify_full(address: &Address, message: &[u8], to_sign: Transaction) -> R verify_full_p2wpkh(address, message, to_sign, pub_key, false) } + // Handle P2SH-wrapped segwit addresses AddressData::P2sh { script_hash: _ } => { let pub_key = PublicKey::from_slice(&to_sign.input[0].witness[1]).map_err(|_| Error::InvalidPublicKey)?; verify_full_p2wpkh(address, message, to_sign, pub_key, true) } + // All other address types are unsupported _ => Err(Error::UnsupportedAddress { address: address.to_string(), }), } } +pub fn verify_message_bip322( + msg: &[u8], + pubkey: [u8; 32], + signature: [u8; 64], + uses_sighash_all: bool, + network: bitcoin::Network, +) -> BIP322Result<()> { + let mut signature = signature.to_vec(); + println!("signature: {:?}", signature); + if uses_sighash_all { + signature.push(1); + } + let mut witness = Witness::new(); + witness.push(&signature); + + let secp = Secp256k1::new(); + let xpubk = XOnlyPublicKey::from_slice(&pubkey).unwrap(); + let address = Address::p2tr(&secp, xpubk, None, network); + + println!("address: {:?}", address); + println!("msg: {:?}", msg); + + verify_simple(&address, msg, witness) +} + fn verify_full_p2wpkh( address: &Address, message: &[u8], @@ -171,7 +202,9 @@ fn verify_full_p2tr( message: &[u8], to_sign: Transaction, pub_key: XOnlyPublicKey, -) -> Result<()> { +) -> BIP322Result<()> { + use bitcoin::secp256k1::{schnorr::Signature, Message}; + let to_spend = create_to_spend(address, message)?; let to_sign = create_to_sign(&to_spend, Some(to_sign.input[0].witness.clone()))?; @@ -181,13 +214,11 @@ fn verify_full_p2tr( }; if to_spend_outpoint != to_sign.unsigned_tx.input[0].previous_output { - return Err(Error::ToSignInvalid); + return Err(error::Error::ToSignInvalid); } - let witness = if let Some(witness) = to_sign.inputs[0].final_script_witness.clone() { - witness - } else { - return Err(Error::WitnessEmpty); + let Some(witness) = to_sign.inputs[0].final_script_witness.clone() else { + return Err(error::Error::WitnessEmpty); }; let encoded_signature = witness.to_vec()[0].clone(); @@ -204,7 +235,7 @@ fn verify_full_p2tr( TapSighashType::Default, ), _ => { - return Err(Error::SignatureLength { + return Err(error::Error::SignatureLength { length: encoded_signature.len(), encoded_signature, }) @@ -212,7 +243,7 @@ fn verify_full_p2tr( }; if !(sighash_type == TapSighashType::All || sighash_type == TapSighashType::Default) { - return Err(Error::SigHashTypeUnsupported { + return Err(error::Error::SigHashTypeUnsupported { sighash_type: sighash_type.to_string(), }); } From 63dc56d374846ee5b42a3abb5e86cb025b3033f1 Mon Sep 17 00:00:00 2001 From: Brian Hoffman Date: Fri, 25 Oct 2024 12:54:14 -0400 Subject: [PATCH 2/5] Remove println! statements --- src/verify.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/verify.rs b/src/verify.rs index a134474a..70ba89c9 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -95,7 +95,6 @@ pub fn verify_message_bip322( network: bitcoin::Network, ) -> BIP322Result<()> { let mut signature = signature.to_vec(); - println!("signature: {:?}", signature); if uses_sighash_all { signature.push(1); } @@ -106,9 +105,6 @@ pub fn verify_message_bip322( let xpubk = XOnlyPublicKey::from_slice(&pubkey).unwrap(); let address = Address::p2tr(&secp, xpubk, None, network); - println!("address: {:?}", address); - println!("msg: {:?}", msg); - verify_simple(&address, msg, witness) } From 6b695b7ba3dadadf59c43f1215d8b46148143b38 Mon Sep 17 00:00:00 2001 From: 0xfinetuned <140081638+0xfinetuned@users.noreply.github.com> Date: Mon, 28 Oct 2024 08:52:59 -0400 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: raph --- src/lib.rs | 4 ++-- src/sign.rs | 6 +++--- src/verify.rs | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3f0bd14d..8319e508 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,11 +7,11 @@ use { blockdata::script, consensus::Decodable, consensus::Encodable, - key::{Keypair, Secp256k1, TapTweak, UntweakedKeypair}, + key::{Keypair TapTweak, UntweakedKeypair}, opcodes, psbt::Psbt, script::PushBytes, - secp256k1::{self, schnorr::Signature, Message, SecretKey, XOnlyPublicKey}, + secp256k1::{self, Secp256k1, schnorr::Signature, Message, SecretKey, XOnlyPublicKey}, sighash::{self, SighashCache, TapSighashType}, transaction::Version, Address, Amount, EcdsaSighashType, OutPoint, PrivateKey, PublicKey, ScriptBuf, Sequence, diff --git a/src/sign.rs b/src/sign.rs index bd899060..10473198 100644 --- a/src/sign.rs +++ b/src/sign.rs @@ -218,8 +218,8 @@ fn create_message_signature_taproot( .tap_tweak(&secp, to_sign.inputs[0].tap_merkle_root) .to_inner(); - let sig = secp.sign_schnorr( - &bitcoin::secp256k1::Message::from_digest_slice(sighash.as_ref()) + let signature = secp.sign_schnorr( + &secp256k1::Message::from_digest_slice(sighash.as_ref()) .expect("should be cryptographically secure hash"), &key_pair, ); @@ -230,7 +230,7 @@ fn create_message_signature_taproot( witness.push( bitcoin::taproot::Signature { - signature: sig, + signature, sighash_type, } .to_vec(), diff --git a/src/verify.rs b/src/verify.rs index 70ba89c9..a7902f30 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -231,7 +231,7 @@ fn verify_full_p2tr( TapSighashType::Default, ), _ => { - return Err(error::Error::SignatureLength { + return Err(Error::SignatureLength { length: encoded_signature.len(), encoded_signature, }) From 20b6d7df1ea18c07b6089c7b5338f1e940d0b785 Mon Sep 17 00:00:00 2001 From: Amine ElQaraoui Date: Mon, 28 Oct 2024 13:58:19 +0100 Subject: [PATCH 4/5] requested changes from code review --- src/lib.rs | 4 ++-- src/sign.rs | 43 ------------------------------------------- src/util.rs | 6 ++---- src/verify.rs | 29 +++-------------------------- 4 files changed, 7 insertions(+), 75 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8319e508..9f3bc864 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,11 +7,11 @@ use { blockdata::script, consensus::Decodable, consensus::Encodable, - key::{Keypair TapTweak, UntweakedKeypair}, + key::{Keypair, TapTweak}, opcodes, psbt::Psbt, script::PushBytes, - secp256k1::{self, Secp256k1, schnorr::Signature, Message, SecretKey, XOnlyPublicKey}, + secp256k1::{self, Secp256k1, Message, XOnlyPublicKey}, sighash::{self, SighashCache, TapSighashType}, transaction::Version, Address, Amount, EcdsaSighashType, OutPoint, PrivateKey, PublicKey, ScriptBuf, Sequence, diff --git a/src/sign.rs b/src/sign.rs index 10473198..297e8d31 100644 --- a/src/sign.rs +++ b/src/sign.rs @@ -95,49 +95,6 @@ pub fn sign_full( to_sign.extract_tx().context(error::TransactionExtract) } -pub fn sign_message_bip322( - keypair: &UntweakedKeypair, - msg: &[u8], - network: bitcoin::Network, -) -> [u8; 64] { - let secp = Secp256k1::new(); - let xpubk = XOnlyPublicKey::from_keypair(keypair).0; - let private_key = PrivateKey::new(SecretKey::from_keypair(keypair), network); - - let address = Address::p2tr(&secp, xpubk, None, network); - - let to_spend = create_to_spend(&address, msg).unwrap(); - let mut to_sign = create_to_sign(&to_spend, None).unwrap(); - - let witness = match address.witness_program() { - Some(witness_program) => { - let version = witness_program.version().to_num(); - let program_len = witness_program.program().len(); - - match version { - 1 => { - if program_len != 32 { - panic!("not key spend path"); - } - create_message_signature_taproot(&to_spend, &to_sign, private_key) - } - _ => { - panic!("unsuported address"); - } - } - } - None => { - panic!("unsuported address"); - } - }; - - to_sign.inputs[0].final_script_witness = Some(witness); - - let signature = to_sign.extract_tx().unwrap().input[0].witness.clone(); - - signature.to_vec()[0][..64].try_into().unwrap() -} - fn create_message_signature_p2wpkh( to_spend_tx: &Transaction, to_sign: &Psbt, diff --git a/src/util.rs b/src/util.rs index 64ee3099..362456f6 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,5 @@ use super::*; -pub type BIP322Result = std::result::Result; - const TAG: &str = "BIP0322-signed-message"; /// Create the tagged message hash. @@ -16,7 +14,7 @@ pub fn message_hash(message: &[u8]) -> Vec { } /// Create the `to_spend` transaction. -pub fn create_to_spend(address: &Address, message: &[u8]) -> BIP322Result { +pub fn create_to_spend(address: &Address, message: &[u8]) -> Result { Ok(Transaction { version: Version(0), lock_time: LockTime::ZERO, @@ -42,7 +40,7 @@ pub fn create_to_spend(address: &Address, message: &[u8]) -> BIP322Result) -> BIP322Result { +pub fn create_to_sign(to_spend: &Transaction, witness: Option) -> Result { let inputs = vec![TxIn { previous_output: OutPoint { txid: to_spend.compute_txid(), diff --git a/src/verify.rs b/src/verify.rs index a7902f30..3b886749 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -1,7 +1,5 @@ use super::*; -use crate::util::BIP322Result; - /// Verifies the BIP-322 simple from spec-compliant string encodings. pub fn verify_simple_encoded(address: &str, message: &str, signature: &str) -> Result<()> { let address = Address::from_str(address) @@ -42,7 +40,7 @@ pub fn verify_full_encoded(address: &str, message: &str, to_sign: &str) -> Resul } /// Verifies the BIP-322 simple from proper Rust types. -pub fn verify_simple(address: &Address, message: &[u8], signature: Witness) -> BIP322Result<()> { +pub fn verify_simple(address: &Address, message: &[u8], signature: Witness) -> Result<()> { verify_full( address, message, @@ -53,7 +51,7 @@ pub fn verify_simple(address: &Address, message: &[u8], signature: Witness) -> B } /// Verifies the BIP-322 full from proper Rust types. -pub fn verify_full(address: &Address, message: &[u8], to_sign: Transaction) -> BIP322Result<()> { +pub fn verify_full(address: &Address, message: &[u8], to_sign: Transaction) -> Result<()> { match address.to_address_data() { // Handle P2TR (Taproot) addresses AddressData::Segwit { witness_program } @@ -87,27 +85,6 @@ pub fn verify_full(address: &Address, message: &[u8], to_sign: Transaction) -> B } } -pub fn verify_message_bip322( - msg: &[u8], - pubkey: [u8; 32], - signature: [u8; 64], - uses_sighash_all: bool, - network: bitcoin::Network, -) -> BIP322Result<()> { - let mut signature = signature.to_vec(); - if uses_sighash_all { - signature.push(1); - } - let mut witness = Witness::new(); - witness.push(&signature); - - let secp = Secp256k1::new(); - let xpubk = XOnlyPublicKey::from_slice(&pubkey).unwrap(); - let address = Address::p2tr(&secp, xpubk, None, network); - - verify_simple(&address, msg, witness) -} - fn verify_full_p2wpkh( address: &Address, message: &[u8], @@ -198,7 +175,7 @@ fn verify_full_p2tr( message: &[u8], to_sign: Transaction, pub_key: XOnlyPublicKey, -) -> BIP322Result<()> { +) -> Result<()> { use bitcoin::secp256k1::{schnorr::Signature, Message}; let to_spend = create_to_spend(address, message)?; From 31ac30e92ad0851d13c092f1af248398eb7589f5 Mon Sep 17 00:00:00 2001 From: Amine ElQaraoui Date: Mon, 28 Oct 2024 14:03:33 +0100 Subject: [PATCH 5/5] requested changes from code review --- src/lib.rs | 2 +- src/verify.rs | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9f3bc864..d3e61b16 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ use { opcodes, psbt::Psbt, script::PushBytes, - secp256k1::{self, Secp256k1, Message, XOnlyPublicKey}, + secp256k1::{self, Secp256k1, Message, schnorr::Signature, XOnlyPublicKey}, sighash::{self, SighashCache, TapSighashType}, transaction::Version, Address, Amount, EcdsaSighashType, OutPoint, PrivateKey, PublicKey, ScriptBuf, Sequence, diff --git a/src/verify.rs b/src/verify.rs index 3b886749..1da20efe 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -176,8 +176,6 @@ fn verify_full_p2tr( to_sign: Transaction, pub_key: XOnlyPublicKey, ) -> Result<()> { - use bitcoin::secp256k1::{schnorr::Signature, Message}; - let to_spend = create_to_spend(address, message)?; let to_sign = create_to_sign(&to_spend, Some(to_sign.input[0].witness.clone()))?; @@ -187,11 +185,11 @@ fn verify_full_p2tr( }; if to_spend_outpoint != to_sign.unsigned_tx.input[0].previous_output { - return Err(error::Error::ToSignInvalid); + return Err(Error::ToSignInvalid); } let Some(witness) = to_sign.inputs[0].final_script_witness.clone() else { - return Err(error::Error::WitnessEmpty); + return Err(Error::WitnessEmpty); }; let encoded_signature = witness.to_vec()[0].clone(); @@ -216,7 +214,7 @@ fn verify_full_p2tr( }; if !(sighash_type == TapSighashType::All || sighash_type == TapSighashType::Default) { - return Err(error::Error::SigHashTypeUnsupported { + return Err(Error::SigHashTypeUnsupported { sighash_type: sighash_type.to_string(), }); }