From 42375e426ce704189ae0fdf17ba8437e21cb05f6 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Tue, 1 Oct 2024 15:18:52 +0200 Subject: [PATCH 1/4] bump MSRV 1.58 -> 1.63 --- .github/workflows/rust.yml | 4 ++-- README.md | 4 ++-- clippy.toml | 2 +- contrib/test.sh | 8 +++----- fuzz/generate-files.sh | 2 +- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 693ca198..4508b893 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -19,7 +19,7 @@ jobs: uses: actions-rs/toolchain@v1 with: profile: minimal - toolchain: 1.58.0 + toolchain: 1.63.0 override: true - name: Running fuzzer env: @@ -69,7 +69,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: [stable, beta, nightly, 1.58.0] + rust: [stable, beta, nightly, 1.63.0] steps: - name: Checkout Crate uses: actions/checkout@v2 diff --git a/README.md b/README.md index f84d6f4e..d8962f24 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ![Build](https://github.com/ElementsProject/elements-miniscript/workflows/Continuous%20integration/badge.svg) -**Minimum Supported Rust Version:** 1.58.0 +**Minimum Supported Rust Version:** 1.63.0 *This crate uses "2018" edition @@ -40,7 +40,7 @@ Enabling the `no-std` feature does not disable `std`. To disable the `std` featu To run the benchmarks run `RUSTFLAGS=--cfg=miniscript_bench cargo +nightly bench --all-features`. ## Minimum Supported Rust Version (MSRV) -This library should always compile with any combination of features on **Rust 1.58.0**. +This library should always compile with any combination of features on **Rust 1.63.0**. Some dependencies do not play nicely with our MSRV, if you are running the tests diff --git a/clippy.toml b/clippy.toml index 48204101..b3c3a24c 100644 --- a/clippy.toml +++ b/clippy.toml @@ -1 +1 @@ -msrv = "1.58.0" +msrv = "1.63.0" diff --git a/contrib/test.sh b/contrib/test.sh index 9faa2840..b5fbc5d9 100755 --- a/contrib/test.sh +++ b/contrib/test.sh @@ -7,11 +7,9 @@ FEATURES="compiler serde rand base64 simplicity" cargo --version rustc --version -# Pin dependencies required to build with Rust 1.58 -if cargo --version | grep "1\.58"; then - cargo update -p byteorder --precise 1.4.3 - cargo update -p cc --precise 1.0.94 - cargo update -p ppv-lite86 --precise 0.2.17 +# Pin dependencies required to build with Rust 1.63 +if cargo --version | grep "1\.63"; then + cargo update -p regex --precise 1.8.4 fi # Format if told to diff --git a/fuzz/generate-files.sh b/fuzz/generate-files.sh index 829d878c..232480a8 100755 --- a/fuzz/generate-files.sh +++ b/fuzz/generate-files.sh @@ -71,7 +71,7 @@ $(for name in $(listTargetNames); do echo "$name,"; done) key: cache-\${{ matrix.target }}-\${{ hashFiles('**/Cargo.toml','**/Cargo.lock') }} - uses: actions-rs/toolchain@v1 with: - toolchain: 1.58 + toolchain: 1.63 override: true profile: minimal - name: fuzz From b6ffda36237a15797cfb83f9eac3d47d29fc373d Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Thu, 26 Sep 2024 10:13:45 +0200 Subject: [PATCH 2/4] ci: let other toolchain finish if one is failing --- .github/workflows/rust.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 4508b893..940f282f 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -68,6 +68,7 @@ jobs: name: Tests runs-on: ubuntu-latest strategy: + fail-fast: false matrix: rust: [stable, beta, nightly, 1.63.0] steps: From 79f31099f9ae6f1f39cf10606571eb3406b6b034 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Wed, 25 Sep 2024 22:21:47 +0200 Subject: [PATCH 3/4] bump elements and related dep --- Cargo.toml | 12 ++-- bitcoind-tests/Cargo.toml | 2 +- bitcoind-tests/tests/test_arith.rs | 2 +- src/confidential/slip77.rs | 11 +--- src/descriptor/key.rs | 16 ++--- src/descriptor/pegin/dynafed_pegin.rs | 5 +- src/descriptor/pegin/legacy_pegin.rs | 89 +++++++++++++-------------- src/descriptor/tr.rs | 6 +- src/lib.rs | 29 ++++++--- src/macros.rs | 63 ++----------------- src/miniscript/decode.rs | 2 +- src/miniscript/mod.rs | 2 +- src/policy/mod.rs | 13 ++-- src/psbt/mod.rs | 10 +-- src/util.rs | 2 +- 15 files changed, 109 insertions(+), 155 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ceb46a2f..605d13b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,10 +19,10 @@ rand = ["bitcoin/rand"] base64 = ["bitcoin/base64", "elements/base64"] [dependencies] -bitcoin = "0.31.0" -elements = "0.24.0" -bitcoin-miniscript = { package = "miniscript", version = "11.0" } -simplicity = { package = "simplicity-lang", version = "0.2.0", optional = true } +bitcoin = "0.32.0" +elements = "0.25.0" +bitcoin-miniscript = { package = "miniscript", version = "12.0" } +simplicity = { package = "simplicity-lang", version = "0.3.0", optional = true } # Do NOT use this as a feature! Use the `serde` feature instead. actual-serde = { package = "serde", version = "1.0", optional = true } @@ -31,8 +31,8 @@ actual-serde = { package = "serde", version = "1.0", optional = true } serde_json = "1.0" actual-rand = { package = "rand", version = "0.8.4"} serde_test = "1.0.147" -bitcoin = { version = "0.31.0", features = ["base64"] } -secp256k1 = {version = "0.28.0", features = ["rand-std"]} +bitcoin = { version = "0.32.0", features = ["base64"] } +secp256k1 = { version = "0.29.0", features = ["rand-std"] } actual-base64 = { package = "base64", version = "0.13.0" } [lints.rust] diff --git a/bitcoind-tests/Cargo.toml b/bitcoind-tests/Cargo.toml index 50451e0c..ea67a4dc 100644 --- a/bitcoind-tests/Cargo.toml +++ b/bitcoind-tests/Cargo.toml @@ -10,4 +10,4 @@ edition = "2018" elements-miniscript = { path = "../" } elementsd = { version = "0.9.0" } actual-rand = { package = "rand", version = "0.8.4" } -secp256k1 = { version = "0.28.1", features = ["rand-std"] } +secp256k1 = { version = "0.29.0", features = ["rand-std"] } diff --git a/bitcoind-tests/tests/test_arith.rs b/bitcoind-tests/tests/test_arith.rs index b2196c86..9a11c916 100644 --- a/bitcoind-tests/tests/test_arith.rs +++ b/bitcoind-tests/tests/test_arith.rs @@ -125,7 +125,7 @@ pub fn test_desc_satisfy(cl: &ElementsD, testdata: &TestData, desc: &str) -> Vec testdata.pubdata.genesis_hash, ) .unwrap(); - let msg = secp256k1::Message::from_slice(&sighash_msg[..]).unwrap(); + let msg = secp256k1::Message::from_digest_slice(&sighash_msg[..]).unwrap(); let mut aux_rand = [0u8; 32]; rand::thread_rng().fill_bytes(&mut aux_rand); let sig = secp.sign_schnorr_with_aux_rand(&msg, &keypair, &aux_rand); diff --git a/src/confidential/slip77.rs b/src/confidential/slip77.rs index ee25a0ba..198a59b2 100644 --- a/src/confidential/slip77.rs +++ b/src/confidential/slip77.rs @@ -92,15 +92,10 @@ impl MasterBlindingKey { } impl hex::FromHex for MasterBlindingKey { - type Err = hex::HexToArrayError; + type Error = hex::HexToArrayError; - fn from_byte_iter(iter: I) -> Result - where - I: Iterator> - + ExactSizeIterator - + DoubleEndedIterator, - { - Ok(MasterBlindingKey(<[u8; 32]>::from_byte_iter(iter)?)) + fn from_hex(s: &str) -> Result { + Ok(MasterBlindingKey(<[u8; 32]>::from_hex(s)?)) } } diff --git a/src/descriptor/key.rs b/src/descriptor/key.rs index 3d325943..b17b1fde 100644 --- a/src/descriptor/key.rs +++ b/src/descriptor/key.rs @@ -1286,7 +1286,7 @@ mod test { assert_eq!(public_key.master_fingerprint().to_string(), "abcdef00"); assert_eq!( public_key.full_derivation_path().unwrap().to_string(), - "m/0'/1'/2" + "0'/1'/2" ); assert!(!public_key.has_wildcard()); @@ -1294,7 +1294,7 @@ mod test { assert_eq!(public_key.master_fingerprint().to_string(), "abcdef00"); assert_eq!( public_key.full_derivation_path().unwrap().to_string(), - "m/0'/1'" + "0'/1'" ); assert!(public_key.has_wildcard()); @@ -1302,7 +1302,7 @@ mod test { assert_eq!(public_key.master_fingerprint().to_string(), "abcdef00"); assert_eq!( public_key.full_derivation_path().unwrap().to_string(), - "m/0'/1'" + "0'/1'" ); assert!(public_key.has_wildcard()); } @@ -1317,7 +1317,7 @@ mod test { assert_eq!(public_key.master_fingerprint().to_string(), "2cbe2a6d"); assert_eq!( public_key.full_derivation_path().unwrap().to_string(), - "m/0'/1'/2" + "0'/1'/2" ); assert!(!public_key.has_wildcard()); @@ -1327,7 +1327,7 @@ mod test { assert_eq!(public_key.master_fingerprint().to_string(), "2cbe2a6d"); assert_eq!( public_key.full_derivation_path().unwrap().to_string(), - "m/0'/1'/2'" + "0'/1'/2'" ); let secret_key = DescriptorSecretKey::from_str("tprv8ZgxMBicQKsPcwcD4gSnMti126ZiETsuX7qwrtMypr6FBwAP65puFn4v6c3jrN9VwtMRMph6nyT63NrfUL4C3nBzPcduzVSuHD7zbX2JKVc/0/1/2").unwrap(); @@ -1336,7 +1336,7 @@ mod test { assert_eq!(public_key.master_fingerprint().to_string(), "2cbe2a6d"); assert_eq!( public_key.full_derivation_path().unwrap().to_string(), - "m/0/1/2" + "0/1/2" ); let secret_key = DescriptorSecretKey::from_str("[aabbccdd]tprv8ZgxMBicQKsPcwcD4gSnMti126ZiETsuX7qwrtMypr6FBwAP65puFn4v6c3jrN9VwtMRMph6nyT63NrfUL4C3nBzPcduzVSuHD7zbX2JKVc/0/1/2").unwrap(); @@ -1345,7 +1345,7 @@ mod test { assert_eq!(public_key.master_fingerprint().to_string(), "aabbccdd"); assert_eq!( public_key.full_derivation_path().unwrap().to_string(), - "m/0/1/2" + "0/1/2" ); let secret_key = DescriptorSecretKey::from_str("[aabbccdd/90']tprv8ZgxMBicQKsPcwcD4gSnMti126ZiETsuX7qwrtMypr6FBwAP65puFn4v6c3jrN9VwtMRMph6nyT63NrfUL4C3nBzPcduzVSuHD7zbX2JKVc/0'/1'/2").unwrap(); @@ -1354,7 +1354,7 @@ mod test { assert_eq!(public_key.master_fingerprint().to_string(), "aabbccdd"); assert_eq!( public_key.full_derivation_path().unwrap().to_string(), - "m/90'/0'/1'/2" + "90'/0'/1'/2" ); } diff --git a/src/descriptor/pegin/dynafed_pegin.rs b/src/descriptor/pegin/dynafed_pegin.rs index 4928e49c..c79eca1d 100644 --- a/src/descriptor/pegin/dynafed_pegin.rs +++ b/src/descriptor/pegin/dynafed_pegin.rs @@ -24,7 +24,7 @@ use std::convert::TryFrom; use std::fmt; use bitcoin::blockdata::script::{self, PushBytes}; -use bitcoin::{self, ScriptBuf as BtcScript}; +use bitcoin::{self, ScriptBuf as BtcScript, Weight}; use elements::secp256k1_zkp; use crate::descriptor::checksum::{self, verify_checksum}; @@ -100,6 +100,7 @@ impl_from_tree!( // // TODO: Confirm with Andrew about the descriptor type for dynafed // Assuming sh(wsh) for now. + let fed_desc = BtcDescriptor::::from_tree(&ms_expr)?; let elem_desc = Descriptor::>::from_tree(&top.args[1])?; Ok(Pegin::new(fed_desc, elem_desc)) @@ -242,7 +243,7 @@ impl Pegin { /// and sighash suffix. Includes the weight of the VarInts encoding the /// scriptSig and witness stack length. // FIXME: the ToPublicKey bound here should not needed. Fix after upstream - pub fn max_satisfaction_weight(&self) -> Result + pub fn max_satisfaction_weight(&self) -> Result where Pk: ToPublicKey, { diff --git a/src/descriptor/pegin/legacy_pegin.rs b/src/descriptor/pegin/legacy_pegin.rs index dc87d23f..00c2610a 100644 --- a/src/descriptor/pegin/legacy_pegin.rs +++ b/src/descriptor/pegin/legacy_pegin.rs @@ -31,6 +31,7 @@ use bitcoin::blockdata::script::PushBytes; use bitcoin::blockdata::{opcodes, script}; use bitcoin::hashes::{hash160, ripemd160, sha256, Hash}; use bitcoin::{self, hashes, ScriptBuf as BtcScript}; +use bitcoin_miniscript::miniscript::limits::MAX_PUBKEYS_PER_MULTISIG; use bitcoin_miniscript::TranslatePk as BtcTranslatePk; use elements::secp256k1_zkp; @@ -114,16 +115,15 @@ impl MiniscriptKey for LegacyPeginKey { /// Legacy Pegin Descriptor #[derive(Clone, Ord, PartialOrd, Eq, PartialEq)] pub struct LegacyPegin { - /// The federation pks - pub fed_pks: Vec, /// The federation threshold - pub fed_k: usize, - /// The emergency pks - pub emer_pks: Vec, + fed: bitcoin_miniscript::Threshold, + /// The emergency threshold - pub emer_k: usize, + emer: bitcoin_miniscript::Threshold, + /// csv timelock - pub timelock: bitcoin::Sequence, + timelock: bitcoin_miniscript::RelLockTime, + /// The elements descriptor required to redeem /// /// TODO: Allow extension user descriptors when claiming pegins @@ -136,30 +136,29 @@ pub struct LegacyPegin { impl LegacyPegin { /// Create a new LegacyPegin descriptor pub fn new( - fed_pks: Vec, - fed_k: usize, - emer_pks: Vec, - emer_k: usize, - timelock: bitcoin::Sequence, + fed: bitcoin_miniscript::Threshold, + emer: bitcoin_miniscript::Threshold, + timelock: bitcoin_miniscript::RelLockTime, desc: Descriptor>, ) -> Self { - let fed_ms = BtcMiniscript::from_ast(BtcTerminal::Multi(fed_k, fed_pks.clone())) + let fed_ms = BtcMiniscript::from_ast(BtcTerminal::Multi(fed.clone())) .expect("Multi type check can't fail"); let csv = BtcMiniscript::from_ast(BtcTerminal::Verify(Arc::new( - BtcMiniscript::from_ast(BtcTerminal::Older(timelock)).unwrap(), + BtcMiniscript::from_ast(BtcTerminal::Older( + bitcoin_miniscript::RelLockTime::try_from(timelock).expect("TODO"), + )) + .unwrap(), ))) .unwrap(); - let emer_ms = BtcMiniscript::from_ast(BtcTerminal::Multi(emer_k, emer_pks.clone())) + let emer_ms = BtcMiniscript::from_ast(BtcTerminal::Multi(emer.clone())) .expect("Multi type check can't fail"); let emer_ms = BtcMiniscript::from_ast(BtcTerminal::AndV(Arc::new(csv), Arc::new(emer_ms))).unwrap(); let ms = BtcMiniscript::from_ast(BtcTerminal::OrD(Arc::new(fed_ms), Arc::new(emer_ms))) .expect("Type check"); Self { - fed_pks, - fed_k, - emer_pks, - emer_k, + fed, + emer, timelock, desc, ms, @@ -175,21 +174,19 @@ impl LegacyPegin { // Miniscript is a bunch of Arc's. So, cloning is not as bad. // Can we avoid this without NLL? let ms_clone = ms.clone(); - let (fed_pks, fed_k, right) = if let BtcTerminal::OrD(ref a, ref b) = ms_clone.node { - if let (BtcTerminal::Multi(fed_k, fed_pks), right) = (&a.node, &b.node) { - (fed_pks, *fed_k, right) + let (fed, right) = if let BtcTerminal::OrD(ref a, ref b) = ms_clone.node { + if let (BtcTerminal::Multi(t), right) = (&a.node, &b.node) { + (t.clone(), right) } else { unreachable!("Only valid pegin miniscripts"); } } else { unreachable!("Only valid pegin miniscripts"); }; - let (timelock, emer_pks, emer_k) = if let BtcTerminal::AndV(l, r) = right { - if let (BtcTerminal::Verify(csv), BtcTerminal::Multi(emer_k, emer_pks)) = - (&l.node, &r.node) - { + let (timelock, emer) = if let BtcTerminal::AndV(l, r) = right { + if let (BtcTerminal::Verify(csv), BtcTerminal::Multi(t)) = (&l.node, &r.node) { if let BtcTerminal::Older(timelock) = csv.node { - (timelock, emer_pks, *emer_k) + (timelock, t.clone()) } else { unreachable!("Only valid pegin miniscripts"); } @@ -200,10 +197,8 @@ impl LegacyPegin { unreachable!("Only valid pegin miniscripts"); }; Self { - fed_pks: fed_pks.to_vec(), - fed_k, - emer_pks: emer_pks.to_vec(), - emer_k, + fed, + emer, timelock, desc, ms, @@ -222,18 +217,18 @@ impl LegacyPegin { // Hopefully, we never have to use this and dynafed is deployed let mut builder = script::Builder::new() .push_opcode(opcodes::all::OP_DEPTH) - .push_int(self.fed_k as i64 + 1) + .push_int(self.fed.k() as i64 + 1) .push_opcode(opcodes::all::OP_EQUAL) .push_opcode(opcodes::all::OP_IF) // manually serialize the left CMS branch, without the OP_CMS - .push_int(self.fed_k as i64); + .push_int(self.fed.k() as i64); - for key in &self.fed_pks { + for key in self.fed.iter() { let tweaked_pk = tweak_key(key.as_untweaked(), secp, tweak.as_byte_array()); builder = builder.push_key(&tweaked_pk); } let mut nearly_done = builder - .push_int(self.fed_pks.len() as i64) + .push_int(self.fed.n() as i64) .push_opcode(opcodes::all::OP_ELSE) .into_script() .to_bytes(); @@ -298,6 +293,8 @@ impl LegacyPegin { .map(|pk| LegacyPeginKey::Functionary(bitcoin::PublicKey::from_str(pk).unwrap())) .collect(); + let fed = bitcoin_miniscript::Threshold::new(22, fed_pks).expect("statically defined"); + let emer_pks = " 03aab896d53a8e7d6433137bbba940f9c521e085dd07e60994579b64a6d992cf79, 0291b7d0b1b692f8f524516ed950872e5da10fb1b808b5a526dedc6fed1cf29807, @@ -307,12 +304,12 @@ impl LegacyPegin { .map(|pk| LegacyPeginKey::Functionary(bitcoin::PublicKey::from_str(pk).unwrap())) .collect(); + let emer = bitcoin_miniscript::Threshold::new(2, emer_pks).expect("statically defined"); + Self::new( - fed_pks, - 11, - emer_pks, - 2, - bitcoin::Sequence::from_consensus(4032), + fed, + emer, + bitcoin_miniscript::RelLockTime::from_consensus(4032).expect("statically defined"), user_desc, ) } @@ -473,28 +470,28 @@ impl LegacyPegin { let tweak = hashes::sha256::Hash::hash(&tweak_vec); let unsigned_script_sig = self.bitcoin_unsigned_script_sig(secp); let mut sigs = vec![]; - for key in &self.fed_pks { + for key in self.fed.iter() { let tweaked_pk = tweak_key(key.as_untweaked(), secp, tweak.as_byte_array()); if let Some(sig) = satisfier.lookup_ecdsa_sig(&tweaked_pk) { sigs.push(sig.to_vec()); } } sigs.sort_by_key(|a| a.len()); - if sigs.len() >= self.fed_k { + if sigs.len() >= self.fed.k() { // Prefer using federation keys over emergency paths - let mut sigs: Vec> = sigs.into_iter().take(self.fed_k).collect(); + let mut sigs: Vec> = sigs.into_iter().take(self.fed.k()).collect(); sigs.push(vec![0]); // CMS extra value Ok((sigs, unsigned_script_sig)) } else { let mut emer_sigs = vec![]; - for emer_key in &self.emer_pks { + for emer_key in self.emer.iter() { if let Some(sig) = satisfier.lookup_ecdsa_sig(emer_key.as_untweaked()) { emer_sigs.push(sig.to_vec()); } } emer_sigs.sort_by_key(|a| a.len()); - if emer_sigs.len() >= self.emer_k { - let mut sigs: Vec> = emer_sigs.into_iter().take(self.emer_k).collect(); + if emer_sigs.len() >= self.emer.k() { + let mut sigs: Vec> = emer_sigs.into_iter().take(self.emer.k()).collect(); sigs.push(vec![0]); // CMS extra value Ok((sigs, unsigned_script_sig)) } else { diff --git a/src/descriptor/tr.rs b/src/descriptor/tr.rs index 0ff19190..69bf3e88 100644 --- a/src/descriptor/tr.rs +++ b/src/descriptor/tr.rs @@ -553,9 +553,9 @@ impl<'a, Pk: ToPublicKey, Ext: ParseableExt> TapLeafScript<'a, Pk, Ext> { TapLeafScript::Simplicity(sim) => { let satisfier = crate::simplicity::SatisfierWrapper::new(satisfier); let program = sim.satisfy(&satisfier).map_err(|_| Error::CouldNotSatisfy)?; - let program_and_witness_bytes = program.encode_to_vec(); - Ok(vec![program_and_witness_bytes]) - }, + let (program_bytes, witness_bytes) = program.encode_to_vec(); + Ok(vec![program_bytes, witness_bytes]) + } } } } diff --git a/src/lib.rs b/src/lib.rs index e931c09c..6ed2da2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -134,11 +134,11 @@ pub mod miniscript; pub mod policy; pub mod psbt; +#[cfg(feature = "simplicity")] +mod simplicity; #[cfg(test)] mod test_utils; mod util; -#[cfg(feature = "simplicity")] -mod simplicity; use std::{cmp, error, fmt, str}; @@ -146,8 +146,8 @@ use elements::hashes::sha256; use elements::secp256k1_zkp::Secp256k1; use elements::{locktime, opcodes, script, secp256k1_zkp}; -pub use crate::confidential::Descriptor as ConfidentialDescriptor; pub use crate::confidential::slip77; +pub use crate::confidential::Descriptor as ConfidentialDescriptor; pub use crate::descriptor::{DefiniteDescriptorKey, Descriptor, DescriptorPublicKey}; pub use crate::extensions::{CovenantExt, Extension, NoExt, TxEnv}; pub use crate::interpreter::Interpreter; @@ -291,7 +291,7 @@ pub enum Error { /// rust-bitcoin script error Script(script::Error), /// rust-bitcoin address error - AddrError(bitcoin::address::Error), + AddrError(bitcoin::address::ParseError), /// A `CHECKMULTISIG` opcode was preceded by a number > 20 CmsTooManyKeys(u32), /// A tapscript multi_a cannot support more than MAX_BLOCK_WEIGHT/32 keys @@ -317,7 +317,9 @@ pub enum Error { /// Parsed a miniscript but there were more script opcodes after it Trailing(String), /// Failed to parse a push as a public key - BadPubkey(bitcoin::key::Error), + BadPubkey(bitcoin::key::ParsePublicKeyError), + /// Failed to parse a slice as public key + BadPubkeySlice(bitcoin::key::FromSliceError), /// Could not satisfy a script (fragment) because of a missing hash preimage MissingHash(sha256::Hash), /// Could not satisfy a script (fragment) because of a missing signature @@ -428,14 +430,21 @@ impl From for Error { } #[doc(hidden)] -impl From for Error { - fn from(e: bitcoin::key::Error) -> Error { +impl From for Error { + fn from(e: bitcoin::key::ParsePublicKeyError) -> Error { Error::BadPubkey(e) } } -impl From for Error { - fn from(e: bitcoin::address::Error) -> Error { +#[doc(hidden)] +impl From for Error { + fn from(e: bitcoin::key::FromSliceError) -> Error { + Error::BadPubkeySlice(e) + } +} + +impl From for Error { + fn from(e: bitcoin::address::ParseError) -> Error { Error::AddrError(e) } } @@ -483,6 +492,7 @@ impl fmt::Display for Error { ), Error::CouldNotSatisfy => f.write_str("could not satisfy"), Error::BadPubkey(ref e) => fmt::Display::fmt(e, f), + Error::BadPubkeySlice(ref e) => fmt::Display::fmt(e, f), Error::TypeCheck(ref e) => write!(f, "typecheck: {}", e), Error::BadDescriptor(ref e) => write!(f, "Invalid descriptor: {}", e), Error::Secp(ref e) => fmt::Display::fmt(e, f), @@ -565,6 +575,7 @@ impl error::Error for Error { Script(_e) => None, // should be Some(e), but requires changes upstream AddrError(e) => Some(e), BadPubkey(e) => Some(e), + BadPubkeySlice(e) => Some(e), Secp(e) => Some(e), #[cfg(feature = "compiler")] CompilerError(e) => Some(e), diff --git a/src/macros.rs b/src/macros.rs index 6e8be78e..53f1b457 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -30,16 +30,7 @@ macro_rules! impl_from_tree { ) => { impl $crate::expression::FromTree for $name where - Pk: MiniscriptKey + core::str::FromStr, - Pk::Sha256: core::str::FromStr, - Pk::Hash256: core::str::FromStr, - Pk::Ripemd160: core::str::FromStr, - Pk::Hash160: core::str::FromStr, - ::Err: std::string::ToString, - <::Sha256 as core::str::FromStr>::Err: std::string::ToString, - <::Hash256 as core::str::FromStr>::Err: std::string::ToString, - <::Ripemd160 as core::str::FromStr>::Err: std::string::ToString, - <::Hash160 as core::str::FromStr>::Err: std::string::ToString, + Pk: bitcoin_miniscript::FromStrKey, $($gen : $gen_con,)* $($ext: $trt)? { @@ -64,16 +55,7 @@ macro_rules! impl_from_str { ) => { impl core::str::FromStr for $name where - Pk: MiniscriptKey + core::str::FromStr, - Pk::Sha256: core::str::FromStr, - Pk::Hash256: core::str::FromStr, - Pk::Ripemd160: core::str::FromStr, - Pk::Hash160: core::str::FromStr, - ::Err: std::string::ToString, - <::Sha256 as core::str::FromStr>::Err: std::string::ToString, - <::Hash256 as core::str::FromStr>::Err: std::string::ToString, - <::Ripemd160 as core::str::FromStr>::Err: std::string::ToString, - <::Hash160 as core::str::FromStr>::Err: std::string::ToString, + Pk: bitcoin_miniscript::FromStrKey, $($gen : $gen_con,)* $($ext: $trt)? { @@ -98,16 +80,7 @@ macro_rules! impl_block_str { ) => { impl $name where - Pk: MiniscriptKey + core::str::FromStr, - Pk::Sha256: core::str::FromStr, - Pk::Hash256: core::str::FromStr, - Pk::Ripemd160: core::str::FromStr, - Pk::Hash160: core::str::FromStr, - ::Err: std::string::ToString, - <::Sha256 as core::str::FromStr>::Err: std::string::ToString, - <::Hash256 as core::str::FromStr>::Err: std::string::ToString, - <::Ripemd160 as core::str::FromStr>::Err: std::string::ToString, - <::Hash160 as core::str::FromStr>::Err: std::string::ToString, + Pk: bitcoin_miniscript::FromStrKey, $($gen : $gen_con,)* $($ext: $trt)? { @@ -126,20 +99,7 @@ macro_rules! serde_string_impl_pk { #[cfg(feature = "serde")] impl<'de, Pk $(, $gen)* $(, $ext)*> $crate::serde::Deserialize<'de> for $name where - Pk: $crate::MiniscriptKey + core::str::FromStr, - Pk::Sha256: core::str::FromStr, - Pk::Hash256: core::str::FromStr, - Pk::Ripemd160: core::str::FromStr, - Pk::Hash160: core::str::FromStr, - ::Err: core::fmt::Display, - <::Sha256 as core::str::FromStr>::Err: - core::fmt::Display, - <::Hash256 as core::str::FromStr>::Err: - core::fmt::Display, - <::Ripemd160 as core::str::FromStr>::Err: - core::fmt::Display, - <::Hash160 as core::str::FromStr>::Err: - core::fmt::Display, + Pk: bitcoin_miniscript::FromStrKey, $($gen : $gen_con,)* $($ext : $ext_bound,)* { @@ -155,20 +115,7 @@ macro_rules! serde_string_impl_pk { struct Visitor(PhantomData<(Pk $(, $gen)* $(, $ext)*)>); impl<'de, Pk $(, $gen)* $(, $ext)*> $crate::serde::de::Visitor<'de> for Visitor where - Pk: $crate::MiniscriptKey + core::str::FromStr, - Pk::Sha256: core::str::FromStr, - Pk::Hash256: core::str::FromStr, - Pk::Ripemd160: core::str::FromStr, - Pk::Hash160: core::str::FromStr, - ::Err: core::fmt::Display, - <::Sha256 as core::str::FromStr>::Err: - core::fmt::Display, - <::Hash256 as core::str::FromStr>::Err: - core::fmt::Display, - <::Ripemd160 as core::str::FromStr>::Err: - core::fmt::Display, - <::Hash160 as core::str::FromStr>::Err: - core::fmt::Display, + Pk: bitcoin_miniscript::FromStrKey, $($gen: $gen_con,)* $($ext : $ext_bound,)* { diff --git a/src/miniscript/decode.rs b/src/miniscript/decode.rs index 2020cb39..ea2a5052 100644 --- a/src/miniscript/decode.rs +++ b/src/miniscript/decode.rs @@ -48,7 +48,7 @@ impl ParseableKey for bitcoin::key::XOnlyPublicKey { #[derive(Debug, PartialEq, Eq)] pub enum KeyParseError { /// Bitcoin PublicKey parse error - FullKeyParseError(bitcoin::key::Error), + FullKeyParseError(bitcoin::key::FromSliceError), /// Xonly key parse Error XonlyKeyParseError(bitcoin::secp256k1::Error), } diff --git a/src/miniscript/mod.rs b/src/miniscript/mod.rs index 09fd0d02..8d4e2cae 100644 --- a/src/miniscript/mod.rs +++ b/src/miniscript/mod.rs @@ -1088,7 +1088,7 @@ mod tests { ); assert_eq!( ms.unwrap_err().to_string(), - "unexpected «pubkey hex should be 66 or 130 digits long, got: 64»", + "unexpected «pubkey string should be 66 or 130 digits long, got: 64»", ); Tapscript::from_str_insane( "pk(2788ee41e76f4f3af603da5bc8fa22997bc0344bb0f95666ba6aaff0242baa99)", diff --git a/src/policy/mod.rs b/src/policy/mod.rs index 298b50b9..9a2d85fe 100644 --- a/src/policy/mod.rs +++ b/src/policy/mod.rs @@ -229,7 +229,7 @@ impl Liftable for Concrete { // Implement lifting from bitcoin policy to elements one impl Liftable for BtcPolicy { fn lift(&self) -> Result, Error> { - match *self { + match self { BtcPolicy::Unsatisfiable => Ok(Semantic::Unsatisfiable), BtcPolicy::Trivial => Ok(Semantic::Trivial), BtcPolicy::Key(ref pkh) => Ok(Semantic::Key(pkh.clone())), @@ -241,10 +241,13 @@ impl Liftable for BtcPolicy { n.to_consensus_u32(), ))), BtcPolicy::Older(n) => Ok(Semantic::Older(Sequence(n.to_consensus_u32()))), - BtcPolicy::Threshold(k, ref subs) => { - let new_subs: Result>, _> = - subs.iter().map(|sub| Liftable::lift(sub)).collect(); - Ok(Semantic::Threshold(k, new_subs?)) + BtcPolicy::Thresh(t) => { + let new_subs: Result>, _> = t + .data() + .iter() + .map(|sub| Liftable::lift(sub.as_ref())) + .collect(); + Ok(Semantic::Threshold(t.k(), new_subs?)) } } } diff --git a/src/psbt/mod.rs b/src/psbt/mod.rs index 06630816..18230ca3 100644 --- a/src/psbt/mod.rs +++ b/src/psbt/mod.rs @@ -106,7 +106,7 @@ pub enum InputError { /// Get the secp Errors directly SecpErr(elements::secp256k1_zkp::Error), /// Key errors - KeyErr(bitcoin::key::Error), + KeyErr(bitcoin::key::FromSliceError), /// Error doing an interpreter-check on a finalized psbt Interpreter(interpreter::Error), /// Redeem script does not match the p2sh hash @@ -257,8 +257,8 @@ impl From for InputError { } #[doc(hidden)] -impl From for InputError { - fn from(e: bitcoin::key::Error) -> InputError { +impl From for InputError { + fn from(e: bitcoin::key::FromSliceError) -> InputError { InputError::KeyErr(e) } } @@ -1664,7 +1664,7 @@ mod tests { let (leaf_hashes, (key_fingerprint, deriv_path)) = psbt_input.tap_key_origins.get(&key_0_1).unwrap(); assert_eq!(key_fingerprint, &fingerprint); - assert_eq!(&deriv_path.to_string(), "m/86'/0'/0'/0/1"); + assert_eq!(&deriv_path.to_string(), "86'/0'/0'/0/1"); assert_eq!(leaf_hashes.len(), 2); assert!(leaf_hashes.contains(&first_leaf_hash)); } @@ -1678,7 +1678,7 @@ mod tests { let (leaf_hashes, (key_fingerprint, deriv_path)) = psbt_input.tap_key_origins.get(&key_1_0).unwrap(); assert_eq!(key_fingerprint, &fingerprint); - assert_eq!(&deriv_path.to_string(), "m/86'/0'/0'/1/0"); + assert_eq!(&deriv_path.to_string(), "86'/0'/0'/1/0"); assert_eq!(leaf_hashes.len(), 1); assert!(!leaf_hashes.contains(&first_leaf_hash)); } diff --git a/src/util.rs b/src/util.rs index 3b5d40b1..968a3291 100644 --- a/src/util.rs +++ b/src/util.rs @@ -6,7 +6,7 @@ use crate::miniscript::context; use crate::{ScriptContext, ToPublicKey}; pub(crate) fn varint_len(n: usize) -> usize { - elements::VarInt(n as u64).size() + bitcoin::VarInt(n as u64).size() } // Helper function to calculate witness size From 6ed3bbbe979b0f11fd44086b65456b064aff86ec Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Tue, 8 Oct 2024 09:12:42 +0200 Subject: [PATCH 4/4] avoid useless conversion of the same type --- src/descriptor/pegin/legacy_pegin.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/descriptor/pegin/legacy_pegin.rs b/src/descriptor/pegin/legacy_pegin.rs index 00c2610a..64fb9d5d 100644 --- a/src/descriptor/pegin/legacy_pegin.rs +++ b/src/descriptor/pegin/legacy_pegin.rs @@ -144,10 +144,7 @@ impl LegacyPegin { let fed_ms = BtcMiniscript::from_ast(BtcTerminal::Multi(fed.clone())) .expect("Multi type check can't fail"); let csv = BtcMiniscript::from_ast(BtcTerminal::Verify(Arc::new( - BtcMiniscript::from_ast(BtcTerminal::Older( - bitcoin_miniscript::RelLockTime::try_from(timelock).expect("TODO"), - )) - .unwrap(), + BtcMiniscript::from_ast(BtcTerminal::Older(timelock)).unwrap(), ))) .unwrap(); let emer_ms = BtcMiniscript::from_ast(BtcTerminal::Multi(emer.clone()))