diff --git a/Cargo.toml b/Cargo.toml index 6c75953da..e14402d7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "miniscript" -version = "12.0.0" +version = "13.0.0" authors = ["Andrew Poelstra , Sanket Kanjalkar "] license = "CC0-1.0" homepage = "https://github.com/rust-bitcoin/rust-miniscript/" @@ -12,25 +12,29 @@ edition = "2018" [features] default = ["std"] -std = ["bitcoin/std", "bitcoin/secp-recovery", "bech32/std"] -no-std = ["bech32/alloc"] +std = ["bip32/std", "bitcoin-address/std", "bitcoin-primitives/crypto-std", "bitcoin-primitives/bech32-std", "psbt-v0/std", "secp256k1/std", "secp256k1/recovery"] +no-std = [] compiler = [] trace = [] -serde = ["actual-serde", "bitcoin/serde"] -rand = ["bitcoin/rand"] -base64 = ["bitcoin/base64"] +serde = ["actual-serde", "bip32/serde", "bitcoin-address/serde", "bitcoin-primitives/crypto-serde", "psbt-v0/serde", "secp256k1/serde"] +rand = ["secp256k1/rand"] +base64 = ["psbt-v0/base64"] [dependencies] -bech32 = { version = "0.11.0", default-features = false } -bitcoin = { version = "0.32.0-rc1", default-features = false } +bitcoin-address = { version = "0.1.0", default-features = false } +bip32 = { version = "0.1.0", default-features = false } +bitcoin-primitives = { version = "0.1.0", default-features = false, features = ["crypto", "bech32"] } +psbt-v0 = { version = "0.1.0", default-features = false, features = [] } +secp256k1 = { version = "0.29.0", default-features = false, features = ["hashes", "alloc"] } # Do NOT use this as a feature! Use the `serde` feature instead. actual-serde = { package = "serde", version = "1.0.103", optional = true } [dev-dependencies] serde_test = "1.0.147" -bitcoin = { version = "0.32.0-rc1", features = ["base64"] } +bitcoin-primitives = { version = "0.1.0", features = ["crypto-std"] } +psbt-v0 = { version = "0.1.0", features = ["base64"] } secp256k1 = {version = "0.29.0", features = ["rand-std"]} [[example]] @@ -69,17 +73,52 @@ required-features = ["std", "base64", "compiler"] members = ["fuzz"] exclude = ["embedded"] +[patch.crates-io.base58ck] +path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/crate-smashing/base58" +# git = "https://github.com/tcharding/rust-bitcoin" +# branch = "crate-smashing" + +[patch.crates-io.bip32] +path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/crate-smashing/bip32" +# git = "https://github.com/tcharding/rust-bitcoin" +# branch = "crate-smashing" + [patch.crates-io.bitcoin] -git = "https://github.com/rust-bitcoin/rust-bitcoin" +path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/crate-smashing/bitcoin" +# git = "https://github.com/tcharding/rust-bitcoin" +# branch = "crate-smashing" + +[patch.crates-io.bitcoin-address] +path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/crate-smashing/address" +# git = "https://github.com/tcharding/rust-bitcoin" +# branch = "crate-smashing" [patch.crates-io.bitcoin_hashes] -git = "https://github.com/rust-bitcoin/rust-bitcoin" +path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/crate-smashing/hashes" +# git = "https://github.com/tcharding/rust-bitcoin" +# branch = "crate-smashing" [patch.crates-io.bitcoin-internals] -git = "https://github.com/rust-bitcoin/rust-bitcoin" +path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/crate-smashing/internals" +# git = "https://github.com/tcharding/rust-bitcoin" +# branch = "crate-smashing" [patch.crates-io.bitcoin-io] -git = "https://github.com/rust-bitcoin/rust-bitcoin" +path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/crate-smashing/io" +# git = "https://github.com/tcharding/rust-bitcoin" +# branch = "crate-smashing" + +[patch.crates-io.bitcoin-primitives] +path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/crate-smashing/primitives" +# git = "https://github.com/tcharding/rust-bitcoin" +# branch = "crate-smashing" + +[patch.crates-io.psbt-v0] +path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/crate-smashing/psbt" +# git = "https://github.com/tcharding/rust-bitcoin" +# branch = "crate-smashing" [patch.crates-io.bitcoin-units] -git = "https://github.com/rust-bitcoin/rust-bitcoin" +path = "/home/tobin/build/github.com/tcharding/rust-bitcoin/crate-smashing/units" +# git = "https://github.com/tcharding/rust-bitcoin" +# branch = "crate-smashing" diff --git a/embedded/src/main.rs b/embedded/src/main.rs index df91f4c17..3ad8cb9c6 100644 --- a/embedded/src/main.rs +++ b/embedded/src/main.rs @@ -34,11 +34,11 @@ fn main() -> ! { )))"; hprintln!("descriptor {}", descriptor).unwrap(); let desc = - miniscript::Descriptor::::from_str(descriptor).unwrap(); + miniscript::Descriptor::::from_str(descriptor).unwrap(); // Derive the P2SH address let p2sh_addr = desc - .address(miniscript::bitcoin::Network::Bitcoin) + .address(miniscript::bitcoin_primitives::Network::Bitcoin) .unwrap() .to_string(); hprintln!("p2sh address {}", p2sh_addr).unwrap(); diff --git a/examples/big.rs b/examples/big.rs index cf0bd099a..7f99e8cbf 100644 --- a/examples/big.rs +++ b/examples/big.rs @@ -12,7 +12,7 @@ use std::collections::HashMap; use std::str::FromStr; -use bitcoin::{ecdsa, XOnlyPublicKey}; +use bitcoin_primitives::{ecdsa, XOnlyPublicKey}; use miniscript::descriptor::Wsh; use miniscript::policy::{Concrete, Liftable}; use miniscript::psbt::PsbtExt; @@ -29,13 +29,13 @@ fn main() { let d = Descriptor::::from_str(&i).unwrap(); use_descriptor(d.clone()); use_descriptor(Descriptor::::from_str(&i).unwrap()); - use_descriptor(Descriptor::::from_str(&i).unwrap()); + use_descriptor(Descriptor::::from_str(&i).unwrap()); use_descriptor(Descriptor::::from_str(&i).unwrap()); let a = d .at_derivation_index(0) .unwrap() - .address(bitcoin::Network::Bitcoin) + .address(bitcoin_primitives::Network::Bitcoin) .unwrap(); println!("{}", a); @@ -44,20 +44,20 @@ fn main() { use_descriptor(d); println!("{:?}", m); - let p = Concrete::::from_str(&i).unwrap(); + let p = Concrete::::from_str(&i).unwrap(); let h = Wsh::new(p.compile().unwrap()).unwrap(); println!("{}", h); println!("{:?}", h.lift()); println!("{:?}", h.script_pubkey()); - println!("{:?}", h.address(bitcoin::Network::Bitcoin)); + println!("{:?}", h.address(bitcoin_primitives::Network::Bitcoin)); - let psbt: bitcoin::Psbt = i.parse().unwrap(); + let psbt: psbt_v0::Psbt = i.parse().unwrap(); let psbt = psbt.finalize(&secp).unwrap(); let mut tx = psbt.extract_tx().unwrap(); println!("{:?}", tx); - let d = miniscript::Descriptor::::from_str(&i).unwrap(); - let sigs = HashMap::::new(); + let d = miniscript::Descriptor::::from_str(&i).unwrap(); + let sigs = HashMap::::new(); d.satisfy(&mut tx.input[0], &sigs).unwrap(); let pol = Concrete::::from_str(&i).unwrap(); @@ -67,7 +67,9 @@ fn main() { let mut t = StrPkTranslator { pk_map }; let real_desc = desc.translate_pk(&mut t).unwrap(); println!("{}", real_desc); - let addr = real_desc.address(bitcoin::Network::Bitcoin).unwrap(); + let addr = real_desc + .address(bitcoin_primitives::Network::Bitcoin) + .unwrap(); println!("{}", addr); } diff --git a/examples/htlc.rs b/examples/htlc.rs index 8f864d639..af418528e 100644 --- a/examples/htlc.rs +++ b/examples/htlc.rs @@ -5,13 +5,13 @@ use std::str::FromStr; -use miniscript::bitcoin::Network; +use miniscript::bitcoin_primitives::Network; use miniscript::descriptor::Wsh; use miniscript::policy::{Concrete, Liftable}; fn main() { // HTLC policy with 10:1 odds for happy (co-operative) case compared to uncooperative case. - let htlc_policy = Concrete::::from_str(&format!("or(10@and(sha256({secret_hash}),pk({redeem_identity})),1@and(older({expiry}),pk({refund_identity})))", + let htlc_policy = Concrete::::from_str(&format!("or(10@and(sha256({secret_hash}),pk({redeem_identity})),1@and(older({expiry}),pk({refund_identity})))", secret_hash = "1111111111111111111111111111111111111111111111111111111111111111", redeem_identity = "022222222222222222222222222222222222222222222222222222222222222222", refund_identity = "020202020202020202020202020202020202020202020202020202020202020202", diff --git a/examples/parse.rs b/examples/parse.rs index 04c2fd837..28e7443cc 100644 --- a/examples/parse.rs +++ b/examples/parse.rs @@ -8,7 +8,7 @@ use miniscript::descriptor::DescriptorType; use miniscript::Descriptor; fn main() { - let desc = miniscript::Descriptor::::from_str( + let desc = miniscript::Descriptor::::from_str( "wsh(c:pk_k(020202020202020202020202020202020202020202020202020202020202020202))", ) .unwrap(); @@ -48,7 +48,7 @@ fn main() { ); // In a similar fashion we can parse a wrapped segwit script. - let desc = miniscript::Descriptor::::from_str( + let desc = miniscript::Descriptor::::from_str( "sh(wsh(c:pk_k(020202020202020202020202020202020202020202020202020202020202020202)))", ) .unwrap(); diff --git a/examples/psbt_sign_finalize.rs b/examples/psbt_sign_finalize.rs index b7c2a62f3..b4ac6ed4f 100644 --- a/examples/psbt_sign_finalize.rs +++ b/examples/psbt_sign_finalize.rs @@ -3,16 +3,16 @@ use std::collections::BTreeMap; use std::str::FromStr; -use miniscript::bitcoin::consensus::encode::deserialize; -use miniscript::bitcoin::hashes::hex::FromHex; -use miniscript::bitcoin::psbt::{self, Psbt}; -use miniscript::bitcoin::sighash::SighashCache; -//use miniscript::bitcoin::secp256k1; // https://github.com/rust-lang/rust/issues/121684 -use miniscript::bitcoin::{ +use miniscript::bitcoin_primitives::consensus::encode::deserialize; +use miniscript::bitcoin_primitives::hex::FromHex; +use miniscript::bitcoin_primitives::sighash::SighashCache; +//use miniscript::secp256k1; // https://github.com/rust-lang/rust/issues/121684 +use miniscript::bitcoin_primitives::{ transaction, Address, Amount, Network, OutPoint, PrivateKey, Script, Sequence, Transaction, TxIn, TxOut, }; use miniscript::psbt::{PsbtExt, PsbtInputExt}; +use miniscript::psbt_v0::{self, Psbt}; use miniscript::Descriptor; fn main() { @@ -20,7 +20,7 @@ fn main() { let s = "wsh(t:or_c(pk(027a3565454fe1b749bccaef22aff72843a9c3efefd7b16ac54537a0c23f0ec0de),v:thresh(1,pkh(032d672a1a91cc39d154d366cd231983661b0785c7f27bc338447565844f4a6813),a:pkh(03417129311ed34c242c012cd0a3e0b9bca0065f742d0dfb63c78083ea6a02d4d9),a:pkh(025a687659658baeabdfc415164528065be7bcaade19342241941e556557f01e28))))#7hut9ukn"; let bridge_descriptor = Descriptor::from_str(s).unwrap(); - //let bridge_descriptor = Descriptor::::from_str(&s).expect("parse descriptor string"); + //let bridge_descriptor = Descriptor::::from_str(&s).expect("parse descriptor string"); assert!(bridge_descriptor.sanity_check().is_ok()); println!("Bridge pubkey script: {}", bridge_descriptor.script_pubkey()); println!("Bridge address: {}", bridge_descriptor.address(Network::Regtest).unwrap()); @@ -54,7 +54,7 @@ fn main() { let spend_tx = Transaction { version: transaction::Version::TWO, - lock_time: bitcoin::absolute::LockTime::from_consensus(5000), + lock_time: bitcoin_primitives::absolute::LockTime::from_consensus(5000), input: vec![], output: vec![], }; @@ -100,14 +100,14 @@ fn main() { // Generating signatures & witness data - let mut input = psbt::Input::default(); + let mut input = psbt_v0::Input::default(); input .update_with_descriptor_unchecked(&bridge_descriptor) .unwrap(); input.witness_utxo = Some(witness_utxo.clone()); psbt.inputs.push(input); - psbt.outputs.push(psbt::Output::default()); + psbt.outputs.push(psbt_v0::Output::default()); let mut sighash_cache = SighashCache::new(&psbt.unsigned_tx); @@ -117,7 +117,7 @@ fn main() { .to_secp_msg(); // Fixme: Take a parameter - let hash_ty = bitcoin::sighash::EcdsaSighashType::All; + let hash_ty = bitcoin_primitives::sighash::EcdsaSighashType::All; let sk1 = backup1_private.inner; let sk2 = backup2_private.inner; @@ -132,9 +132,10 @@ fn main() { let pk2 = backup2_private.public_key(&secp256k1); assert!(secp256k1.verify_ecdsa(&msg, &sig2, &pk2.inner).is_ok()); - psbt.inputs[0] - .partial_sigs - .insert(pk1, bitcoin::ecdsa::Signature { signature: sig1, sighash_type: hash_ty }); + psbt.inputs[0].partial_sigs.insert( + pk1, + bitcoin_primitives::ecdsa::Signature { signature: sig1, sighash_type: hash_ty }, + ); println!("{:#?}", psbt); println!("{}", psbt); @@ -143,7 +144,7 @@ fn main() { println!("{:#?}", psbt); let tx = psbt.extract_tx().expect("failed to extract tx"); - println!("{}", bitcoin::consensus::encode::serialize_hex(&tx)); + println!("{}", bitcoin_primitives::consensus::encode::serialize_hex(&tx)); } // Find the Outpoint by spk diff --git a/examples/sign_multisig.rs b/examples/sign_multisig.rs index 6ce989bc5..8776e7934 100644 --- a/examples/sign_multisig.rs +++ b/examples/sign_multisig.rs @@ -5,8 +5,8 @@ use std::collections::HashMap; use std::str::FromStr; -use bitcoin::witness::Witness; -use bitcoin::{absolute, ecdsa, transaction, Amount, Sequence}; +use bitcoin_primitives::witness::Witness; +use bitcoin_primitives::{absolute, ecdsa, transaction, Amount, Sequence}; fn main() { let mut tx = spending_transaction(); @@ -15,7 +15,7 @@ fn main() { // Descriptor for the output being spent. let s = format!("wsh(multi(2,{},{},{}))", pks[0], pks[1], pks[2],); - let descriptor = miniscript::Descriptor::::from_str(&s).unwrap(); + let descriptor = miniscript::Descriptor::::from_str(&s).unwrap(); // Check weight for witness satisfaction cost ahead of time. // 106 (serialized witnessScript) @@ -23,7 +23,7 @@ fn main() { assert_eq!(descriptor.max_weight_to_satisfy().unwrap().to_wu(), 253); // Sometimes it is necessary to have additional information to get the - // `bitcoin::PublicKey` from the `MiniscriptKey` which can be supplied by + // `bitcoin_primitives::PublicKey` from the `MiniscriptKey` which can be supplied by // the `to_pk_ctx` parameter. For example, when calculating the script // pubkey of a descriptor with xpubs, the secp context and child information // maybe required. @@ -51,7 +51,7 @@ fn main() { // Attempt to satisfy at age 0, height 0. let original_txin = tx.input[0].clone(); - let mut sigs = HashMap::::new(); + let mut sigs = HashMap::::new(); // Doesn't work with no signatures. assert!(descriptor.satisfy(&mut tx.input[0], &sigs).is_err()); @@ -76,35 +76,35 @@ fn main() { } // Transaction which spends some output. -fn spending_transaction() -> bitcoin::Transaction { - bitcoin::Transaction { +fn spending_transaction() -> bitcoin_primitives::Transaction { + bitcoin_primitives::Transaction { version: transaction::Version::TWO, lock_time: absolute::LockTime::ZERO, - input: vec![bitcoin::TxIn { + input: vec![bitcoin_primitives::TxIn { previous_output: Default::default(), - script_sig: bitcoin::ScriptBuf::new(), + script_sig: bitcoin_primitives::ScriptBuf::new(), sequence: Sequence::MAX, witness: Witness::default(), }], - output: vec![bitcoin::TxOut { - script_pubkey: bitcoin::ScriptBuf::new(), + output: vec![bitcoin_primitives::TxOut { + script_pubkey: bitcoin_primitives::ScriptBuf::new(), value: Amount::from_sat(100_000_000), }], } } #[rustfmt::skip] -fn list_of_three_arbitrary_public_keys() -> Vec { +fn list_of_three_arbitrary_public_keys() -> Vec { vec![ - bitcoin::PublicKey::from_slice(&[2; 33]).expect("key 1"), - bitcoin::PublicKey::from_slice(&[ + bitcoin_primitives::PublicKey::from_slice(&[2; 33]).expect("key 1"), + bitcoin_primitives::PublicKey::from_slice(&[ 0x02, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ]).expect("key 2"), - bitcoin::PublicKey::from_slice(&[ + bitcoin_primitives::PublicKey::from_slice(&[ 0x03, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, @@ -126,6 +126,6 @@ fn random_signature_from_the_blockchain() -> ecdsa::Signature { 531d75c136272f127a5dc14acc0722301cbddc222262934151f140da345af177", ) .unwrap(), - sighash_type: bitcoin::sighash::EcdsaSighashType::All, + sighash_type: bitcoin_primitives::sighash::EcdsaSighashType::All, } } diff --git a/examples/taproot.rs b/examples/taproot.rs index 3ac20c4ec..0a7206a3c 100644 --- a/examples/taproot.rs +++ b/examples/taproot.rs @@ -3,11 +3,11 @@ use std::collections::HashMap; use std::str::FromStr; -use miniscript::bitcoin::key::{Keypair, XOnlyPublicKey}; -use miniscript::bitcoin::secp256k1::rand; -use miniscript::bitcoin::{Network, WitnessVersion}; +use miniscript::bitcoin_primitives::key::{Keypair, XOnlyPublicKey}; +use miniscript::bitcoin_primitives::{Network, WitnessVersion}; use miniscript::descriptor::DescriptorType; use miniscript::policy::Concrete; +use miniscript::secp256k1::rand; use miniscript::{translate_hash_fail, Descriptor, Miniscript, Tap, TranslatePk, Translator}; // Refer to https://github.com/sanket1729/adv_btc_workshop/blob/master/workshop.md#creating-a-taproot-descriptor @@ -108,7 +108,7 @@ fn main() { // Compute the bitcoin address and check if it matches let network = Network::Bitcoin; let addr = real_desc.address(network).unwrap(); - let expected_addr = bitcoin::Address::from_str( + let expected_addr = bitcoin_address::Address::from_str( "bc1p4l2xzq7js40965s5w0fknd287kdlmt2dljte37zsc5a34u0h9c4q85snyd", ) .unwrap() diff --git a/examples/verify_tx.rs b/examples/verify_tx.rs index f2a63f447..36787f927 100644 --- a/examples/verify_tx.rs +++ b/examples/verify_tx.rs @@ -4,10 +4,10 @@ use std::str::FromStr; -use miniscript::bitcoin::consensus::Decodable; -use miniscript::bitcoin::secp256k1::Secp256k1; -use miniscript::bitcoin::{absolute, sighash, Sequence}; +use miniscript::bitcoin_primitives::consensus::Decodable; +use miniscript::bitcoin_primitives::{absolute, sighash, Sequence}; use miniscript::interpreter::KeySigPair; +use miniscript::secp256k1::Secp256k1; fn main() { // @@ -31,7 +31,7 @@ fn main() { // To do sanity checks on the transaction using the interpreter parse the // descriptor with `from_str`. - let _ = miniscript::Descriptor::::from_str(&desc_string) + let _ = miniscript::Descriptor::::from_str(&desc_string) .expect("sanity checks to pass"); // Alternately, use `inferred_descriptor` which does sanity checks for us also. let _ = interpreter.inferred_descriptor().expect("same as from_str"); @@ -67,7 +67,7 @@ fn main() { // We can set prevouts to be empty list because this is a legacy transaction // and this information is not required for sighash computation. - let prevouts = sighash::Prevouts::All::(&[]); + let prevouts = sighash::Prevouts::All::(&[]); for elem in interpreter.iter(&secp, &tx, 0, &prevouts) { if let miniscript::interpreter::SatisfiedConstraint::PublicKey { key_sig } = @@ -87,7 +87,7 @@ fn main() { let iter = interpreter.iter_custom(Box::new(|key_sig: &KeySigPair| { let (pk, ecdsa_sig) = key_sig.as_ecdsa().expect("Ecdsa Sig"); - ecdsa_sig.sighash_type == bitcoin::sighash::EcdsaSighashType::All + ecdsa_sig.sighash_type == bitcoin_primitives::sighash::EcdsaSighashType::All && secp .verify_ecdsa(&message, &ecdsa_sig.signature, &pk.inner) .is_ok() @@ -103,7 +103,7 @@ fn main() { /// Returns an arbitrary transaction. #[rustfmt::skip] -fn hard_coded_transaction() -> bitcoin::Transaction { +fn hard_coded_transaction() -> bitcoin_primitives::Transaction { // tx `f27eba163c38ad3f34971198687a3f1882b7ec818599ffe469a8440d82261c98` let tx_bytes = vec![ 0x01, 0x00, 0x00, 0x00, 0x02, 0xc5, 0x11, 0x1d, 0xb7, 0x93, 0x50, 0xc1, @@ -158,11 +158,11 @@ fn hard_coded_transaction() -> bitcoin::Transaction { 0xe7, 0x87, 0x09, 0x5d, 0x07, 0x00, ]; - bitcoin::Transaction::consensus_decode(&mut &tx_bytes[..]).expect("decode transaction") + bitcoin_primitives::Transaction::consensus_decode(&mut &tx_bytes[..]).expect("decode transaction") } -fn hard_coded_script_pubkey() -> bitcoin::ScriptBuf { - bitcoin::ScriptBuf::from(vec![ +fn hard_coded_script_pubkey() -> bitcoin_primitives::ScriptBuf { + bitcoin_primitives::ScriptBuf::from(vec![ 0xa9, 0x14, 0x92, 0x09, 0xa8, 0xf9, 0x0c, 0x58, 0x4b, 0xb5, 0x97, 0x4d, 0x58, 0x68, 0x72, 0x49, 0xe5, 0x32, 0xde, 0x59, 0xf4, 0xbc, 0x87, ]) diff --git a/examples/xpub_descriptors.rs b/examples/xpub_descriptors.rs index 90b0458f3..0412665e6 100644 --- a/examples/xpub_descriptors.rs +++ b/examples/xpub_descriptors.rs @@ -4,8 +4,9 @@ use std::str::FromStr; -use miniscript::bitcoin::secp256k1::{Secp256k1, Verification}; -use miniscript::bitcoin::{Address, Network}; +use miniscript::bitcoin_address::Address; +use miniscript::bitcoin_primitives::Network; +use miniscript::secp256k1::{Secp256k1, Verification}; use miniscript::{DefiniteDescriptorKey, Descriptor, DescriptorPublicKey}; const XPUB_1: &str = "xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB"; @@ -35,7 +36,7 @@ fn p2wsh(secp: &Secp256k1) -> Address { .address(Network::Bitcoin) .unwrap(); - let expected = bitcoin::Address::from_str( + let expected = bitcoin_address::Address::from_str( "bc1qpq2cfgz5lktxzr5zqv7nrzz46hsvq3492ump9pz8rzcl8wqtwqcspx5y6a", ) .unwrap() diff --git a/fuzz/fuzz_targets/roundtrip_miniscript_script.rs b/fuzz/fuzz_targets/roundtrip_miniscript_script.rs index b1cd9b90e..2adf8299c 100644 --- a/fuzz/fuzz_targets/roundtrip_miniscript_script.rs +++ b/fuzz/fuzz_targets/roundtrip_miniscript_script.rs @@ -1,14 +1,15 @@ #![allow(unexpected_cfgs)] use honggfuzz::fuzz; -use miniscript::bitcoin::script; +use miniscript::bitcoin_primitives::script; use miniscript::{Miniscript, Segwitv0}; fn do_test(data: &[u8]) { // Try round-tripping as a script let script = script::Script::from_bytes(data); - if let Ok(pt) = Miniscript::::parse(script) { + if let Ok(pt) = Miniscript::::parse(script) + { let output = pt.encode(); assert_eq!(pt.script_size(), output.len()); assert_eq!(&output, script); diff --git a/src/descriptor/bare.rs b/src/descriptor/bare.rs index 99571479d..7db46cf1a 100644 --- a/src/descriptor/bare.rs +++ b/src/descriptor/bare.rs @@ -9,8 +9,9 @@ use core::fmt; -use bitcoin::script::{self, PushBytes}; -use bitcoin::{Address, Network, ScriptBuf, Weight}; +use bitcoin_address::Address; +use bitcoin_primitives::script::{self, PushBytes}; +use bitcoin_primitives::{Network, ScriptBuf, Weight}; use super::checksum::verify_checksum; use crate::descriptor::{write_descriptor, DefiniteDescriptorKey}; diff --git a/src/descriptor/checksum.rs b/src/descriptor/checksum.rs index ec286f7e5..a9d04e512 100644 --- a/src/descriptor/checksum.rs +++ b/src/descriptor/checksum.rs @@ -11,8 +11,8 @@ use core::convert::TryFrom; use core::fmt; use core::iter::FromIterator; -use bech32::primitives::checksum::PackedFe32; -use bech32::{Checksum, Fe32}; +use bitcoin_primitives::bech32::primitives::checksum::PackedFe32; +use bitcoin_primitives::bech32::{self, Checksum, Fe32}; pub use crate::expression::VALID_CHARS; use crate::prelude::*; diff --git a/src/descriptor/key.rs b/src/descriptor/key.rs index 3f09fb415..7dfb97c54 100644 --- a/src/descriptor/key.rs +++ b/src/descriptor/key.rs @@ -6,10 +6,10 @@ use core::str::FromStr; #[cfg(feature = "std")] use std::error; -use bitcoin::bip32::{self, XKeyIdentifier}; -use bitcoin::hashes::{hash160, ripemd160, sha256, HashEngine}; -use bitcoin::key::XOnlyPublicKey; -use bitcoin::secp256k1::{Secp256k1, Signing, Verification}; +use bip32::XKeyIdentifier; +use bitcoin_primitives::hashes::{hash160, ripemd160, sha256, HashEngine}; +use bitcoin_primitives::XOnlyPublicKey; +use secp256k1::{Secp256k1, Signing, Verification}; use crate::prelude::*; #[cfg(feature = "serde")] @@ -47,13 +47,13 @@ pub struct SinglePub { pub key: SinglePubKey, } -/// A descriptor [`bitcoin::PrivateKey`] with optional origin information. +/// A descriptor [`bitcoin_primitives::PrivateKey`] with optional origin information. #[derive(Debug, Eq, PartialEq, Clone)] pub struct SinglePriv { /// Origin information (fingerprint and derivation path). pub origin: Option<(bip32::Fingerprint, bip32::DerivationPath)>, /// The private key. - pub key: bitcoin::PrivateKey, + pub key: bitcoin_primitives::PrivateKey, } /// An extended key with origin, derivation path, and wildcard. @@ -107,7 +107,7 @@ pub struct DescriptorMultiXKey { #[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)] pub enum SinglePubKey { /// A bitcoin public key (compressed or uncompressed). - FullKey(bitcoin::PublicKey), + FullKey(bitcoin_primitives::PublicKey), /// An xonly public key. XOnly(XOnlyPublicKey), } @@ -454,7 +454,7 @@ impl FromStr for DescriptorPublicKey { "Only publickeys with prefixes 02/03/04 are allowed", )); } - let key = bitcoin::PublicKey::from_str(key_part).map_err(|_| { + let key = bitcoin_primitives::PublicKey::from_str(key_part).map_err(|_| { DescriptorKeyParseError("Error while parsing simple public key") })?; SinglePubKey::FullKey(key) @@ -697,7 +697,7 @@ impl FromStr for DescriptorSecretKey { let (key_part, origin) = parse_key_origin(s)?; if key_part.len() <= 52 { - let sk = bitcoin::PrivateKey::from_str(key_part) + let sk = bitcoin_primitives::PrivateKey::from_str(key_part) .map_err(|_| DescriptorKeyParseError("Error while parsing a WIF private key"))?; Ok(DescriptorSecretKey::Single(SinglePriv { key: sk, origin: None })) } else { @@ -888,10 +888,9 @@ impl DescriptorXKey { /// ``` /// # use std::str::FromStr; /// # fn body() -> Result<(), ()> { - /// use miniscript::bitcoin::bip32; /// use miniscript::descriptor::DescriptorPublicKey; /// - /// let ctx = miniscript::bitcoin::secp256k1::Secp256k1::signing_only(); + /// let ctx = miniscript::secp256k1::Secp256k1::signing_only(); /// /// let key = DescriptorPublicKey::from_str("[d34db33f/44'/0'/0']xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/1/*").or(Err(()))?; /// let xpub = match key { @@ -996,7 +995,7 @@ impl MiniscriptKey for DescriptorPublicKey { impl DefiniteDescriptorKey { /// Computes the public key corresponding to this descriptor key. /// When deriving from an XOnlyPublicKey, it adds the default 0x02 y-coordinate - /// and returns the obtained full [`bitcoin::PublicKey`]. All BIP32 derivations + /// and returns the obtained full [`bitcoin_primitives::PublicKey`]. All BIP32 derivations /// always return a compressed key /// /// Will return an error if the descriptor key has any hardened derivation steps in its path. To @@ -1006,7 +1005,7 @@ impl DefiniteDescriptorKey { pub fn derive_public_key( &self, secp: &Secp256k1, - ) -> Result { + ) -> Result { match self.0 { DescriptorPublicKey::Single(ref pk) => match pk.key { SinglePubKey::FullKey(pk) => Ok(pk), @@ -1017,7 +1016,7 @@ impl DefiniteDescriptorKey { unreachable!("we've excluded this error case") } Wildcard::None => match xpk.xkey.derive_pub(secp, &xpk.derivation_path.as_ref()) { - Ok(xpub) => Ok(bitcoin::PublicKey::new(xpub.public_key)), + Ok(xpub) => Ok(bitcoin_primitives::PublicKey::new(xpub.public_key)), Err(bip32::Error::CannotDeriveFromHardenedKey) => { Err(ConversionError::HardenedChild) } @@ -1091,7 +1090,7 @@ impl MiniscriptKey for DefiniteDescriptorKey { } impl ToPublicKey for DefiniteDescriptorKey { - fn to_public_key(&self) -> bitcoin::PublicKey { + fn to_public_key(&self) -> bitcoin_primitives::PublicKey { let secp = Secp256k1::verification_only(); self.derive_public_key(&secp).unwrap() } @@ -1138,7 +1137,6 @@ impl Serialize for DescriptorPublicKey { mod test { use core::str::FromStr; - use bitcoin::bip32; #[cfg(feature = "serde")] use serde_test::{assert_tokens, Token}; diff --git a/src/descriptor/mod.rs b/src/descriptor/mod.rs index d74309fcf..2836fd09b 100644 --- a/src/descriptor/mod.rs +++ b/src/descriptor/mod.rs @@ -15,9 +15,10 @@ use core::fmt; use core::ops::Range; use core::str::{self, FromStr}; -use bitcoin::hashes::{hash160, ripemd160, sha256}; -use bitcoin::{ - secp256k1, Address, Network, Script, ScriptBuf, TxIn, Weight, Witness, WitnessVersion, +use bitcoin_address::Address; +use bitcoin_primitives::hashes::{hash160, ripemd160, sha256}; +use bitcoin_primitives::{ + secp256k1, Network, Script, ScriptBuf, TxIn, Weight, Witness, WitnessVersion, }; use sync::Arc; @@ -627,14 +628,14 @@ impl Descriptor { self.at_derivation_index(index) } - /// Convert all the public keys in the descriptor to [`bitcoin::PublicKey`] by deriving them or - /// otherwise converting them. All [`bitcoin::secp256k1::XOnlyPublicKey`]s are converted to by adding a + /// Convert all the public keys in the descriptor to [`bitcoin_primitives::PublicKey`] by deriving them or + /// otherwise converting them. All [`secp256k1::XOnlyPublicKey`]s are converted to by adding a /// default(0x02) y-coordinate. /// /// This is a shorthand for: /// /// ``` - /// # use miniscript::{Descriptor, DescriptorPublicKey, bitcoin::secp256k1::Secp256k1}; + /// # use miniscript::{Descriptor, DescriptorPublicKey, secp256k1::Secp256k1}; /// # use core::str::FromStr; /// # let descriptor = Descriptor::::from_str("tr(xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*)") /// .expect("Valid ranged descriptor"); @@ -657,7 +658,7 @@ impl Descriptor { &self, secp: &secp256k1::Secp256k1, index: u32, - ) -> Result, ConversionError> { + ) -> Result, ConversionError> { self.at_derivation_index(index)?.derived_descriptor(secp) } @@ -793,7 +794,7 @@ impl Descriptor { secp: &secp256k1::Secp256k1, script_pubkey: &Script, range: Range, - ) -> Result)>, ConversionError> { + ) -> Result)>, ConversionError> { let range = if self.has_wildcard() { range } else { 0..1 }; for i in range { @@ -871,15 +872,15 @@ impl Descriptor { } impl Descriptor { - /// Convert all the public keys in the descriptor to [`bitcoin::PublicKey`] by deriving them or - /// otherwise converting them. All [`bitcoin::secp256k1::XOnlyPublicKey`]s are converted to by adding a + /// Convert all the public keys in the descriptor to [`bitcoin_primitives::PublicKey`] by deriving them or + /// otherwise converting them. All [`secp256k1::XOnlyPublicKey`]s are converted to by adding a /// default(0x02) y-coordinate. /// /// # Examples /// /// ``` /// use miniscript::descriptor::{Descriptor, DescriptorPublicKey}; - /// use miniscript::bitcoin::secp256k1; + /// use miniscript::secp256k1; /// use std::str::FromStr; /// /// // test from bip 86 @@ -896,21 +897,25 @@ impl Descriptor { pub fn derived_descriptor( &self, secp: &secp256k1::Secp256k1, - ) -> Result, ConversionError> { + ) -> Result, ConversionError> { struct Derivator<'a, C: secp256k1::Verification>(&'a secp256k1::Secp256k1); impl<'a, C: secp256k1::Verification> - Translator + Translator for Derivator<'a, C> { fn pk( &mut self, pk: &DefiniteDescriptorKey, - ) -> Result { + ) -> Result { pk.derive_public_key(self.0) } - translate_hash_clone!(DefiniteDescriptorKey, bitcoin::PublicKey, ConversionError); + translate_hash_clone!( + DefiniteDescriptorKey, + bitcoin_primitives::PublicKey, + ConversionError + ); } let derived = self.translate_pk(&mut Derivator(secp)); @@ -1000,11 +1005,11 @@ pub(crate) use write_descriptor; mod tests { use core::convert::TryFrom; - use bitcoin::hashes::hex::FromHex; - use bitcoin::opcodes::all::{OP_CLTV, OP_CSV}; - use bitcoin::script::{Instruction, PushBytes}; - use bitcoin::sighash::EcdsaSighashType; - use bitcoin::{bip32, opcodes, script, PublicKey, Sequence}; + use bitcoin_primitives::hex::FromHex; + use bitcoin_primitives::opcodes::all::{OP_CLTV, OP_CSV}; + use bitcoin_primitives::script::{Instruction, PushBytes}; + use bitcoin_primitives::sighash::EcdsaSighashType; + use bitcoin_primitives::{opcodes, script, PublicKey, Sequence}; use super::checksum::desc_checksum; use super::*; @@ -1245,7 +1250,8 @@ mod tests { let secp = secp256k1::Secp256k1::new(); let sk = secp256k1::SecretKey::from_slice(&b"sally was a secret key, she said"[..]).unwrap(); - let pk = bitcoin::PublicKey::new(secp256k1::PublicKey::from_secret_key(&secp, &sk)); + let pk = + bitcoin_primitives::PublicKey::new(secp256k1::PublicKey::from_secret_key(&secp, &sk)); let msg = secp256k1::Message::from_digest_slice(&b"michael was a message, amusingly"[..]) .expect("32 bytes"); let sig = secp.sign_ecdsa(&msg, &sk); @@ -1254,18 +1260,18 @@ mod tests { struct SimpleSat { sig: secp256k1::ecdsa::Signature, - pk: bitcoin::PublicKey, + pk: bitcoin_primitives::PublicKey, } - impl Satisfier for SimpleSat { + impl Satisfier for SimpleSat { fn lookup_ecdsa_sig( &self, - pk: &bitcoin::PublicKey, - ) -> Option { + pk: &bitcoin_primitives::PublicKey, + ) -> Option { if *pk == self.pk { - Some(bitcoin::ecdsa::Signature { + Some(bitcoin_primitives::ecdsa::Signature { signature: self.sig, - sighash_type: bitcoin::sighash::EcdsaSighashType::All, + sighash_type: bitcoin_primitives::sighash::EcdsaSighashType::All, }) } else { None @@ -1276,9 +1282,9 @@ mod tests { let satisfier = SimpleSat { sig, pk }; let ms = ms_str!("c:pk_k({})", pk); - let mut txin = bitcoin::TxIn { - previous_output: bitcoin::OutPoint::default(), - script_sig: bitcoin::ScriptBuf::new(), + let mut txin = bitcoin_primitives::TxIn { + previous_output: bitcoin_primitives::OutPoint::default(), + script_sig: bitcoin_primitives::ScriptBuf::new(), sequence: Sequence::from_height(100), witness: Witness::default(), }; @@ -1287,8 +1293,8 @@ mod tests { bare.satisfy(&mut txin, &satisfier).expect("satisfaction"); assert_eq!( txin, - bitcoin::TxIn { - previous_output: bitcoin::OutPoint::default(), + bitcoin_primitives::TxIn { + previous_output: bitcoin_primitives::OutPoint::default(), script_sig: script::Builder::new() .push_slice(<&PushBytes>::try_from(sigser.as_slice()).unwrap()) .into_script(), @@ -1296,14 +1302,14 @@ mod tests { witness: Witness::default(), } ); - assert_eq!(bare.unsigned_script_sig(), bitcoin::ScriptBuf::new()); + assert_eq!(bare.unsigned_script_sig(), bitcoin_primitives::ScriptBuf::new()); let pkh = Descriptor::new_pkh(pk).unwrap(); pkh.satisfy(&mut txin, &satisfier).expect("satisfaction"); assert_eq!( txin, - bitcoin::TxIn { - previous_output: bitcoin::OutPoint::default(), + bitcoin_primitives::TxIn { + previous_output: bitcoin_primitives::OutPoint::default(), script_sig: script::Builder::new() .push_slice(<&PushBytes>::try_from(sigser.as_slice()).unwrap()) .push_key(pk) @@ -1312,20 +1318,20 @@ mod tests { witness: Witness::default(), } ); - assert_eq!(pkh.unsigned_script_sig(), bitcoin::ScriptBuf::new()); + assert_eq!(pkh.unsigned_script_sig(), bitcoin_primitives::ScriptBuf::new()); let wpkh = Descriptor::new_wpkh(pk).unwrap(); wpkh.satisfy(&mut txin, &satisfier).expect("satisfaction"); assert_eq!( txin, - bitcoin::TxIn { - previous_output: bitcoin::OutPoint::default(), - script_sig: bitcoin::ScriptBuf::new(), + bitcoin_primitives::TxIn { + previous_output: bitcoin_primitives::OutPoint::default(), + script_sig: bitcoin_primitives::ScriptBuf::new(), sequence: Sequence::from_height(100), witness: Witness::from_slice(&[sigser.clone(), pk.to_bytes()]), } ); - assert_eq!(wpkh.unsigned_script_sig(), bitcoin::ScriptBuf::new()); + assert_eq!(wpkh.unsigned_script_sig(), bitcoin_primitives::ScriptBuf::new()); let shwpkh = Descriptor::new_sh_wpkh(pk).unwrap(); shwpkh.satisfy(&mut txin, &satisfier).expect("satisfaction"); @@ -1339,8 +1345,8 @@ mod tests { .into_script(); assert_eq!( txin, - bitcoin::TxIn { - previous_output: bitcoin::OutPoint::default(), + bitcoin_primitives::TxIn { + previous_output: bitcoin_primitives::OutPoint::default(), script_sig: script::Builder::new() .push_slice(<&PushBytes>::try_from(redeem_script.as_bytes()).unwrap()) .into_script(), @@ -1360,8 +1366,8 @@ mod tests { sh.satisfy(&mut txin, &satisfier).expect("satisfaction"); assert_eq!( txin, - bitcoin::TxIn { - previous_output: bitcoin::OutPoint::default(), + bitcoin_primitives::TxIn { + previous_output: bitcoin_primitives::OutPoint::default(), script_sig: script::Builder::new() .push_slice(<&PushBytes>::try_from(sigser.as_slice()).unwrap()) .push_slice(<&PushBytes>::try_from(ms.encode().as_bytes()).unwrap()) @@ -1370,7 +1376,7 @@ mod tests { witness: Witness::default(), } ); - assert_eq!(sh.unsigned_script_sig(), bitcoin::ScriptBuf::new()); + assert_eq!(sh.unsigned_script_sig(), bitcoin_primitives::ScriptBuf::new()); let ms = ms_str!("c:pk_k({})", pk); @@ -1378,21 +1384,21 @@ mod tests { wsh.satisfy(&mut txin, &satisfier).expect("satisfaction"); assert_eq!( txin, - bitcoin::TxIn { - previous_output: bitcoin::OutPoint::default(), - script_sig: bitcoin::ScriptBuf::new(), + bitcoin_primitives::TxIn { + previous_output: bitcoin_primitives::OutPoint::default(), + script_sig: bitcoin_primitives::ScriptBuf::new(), sequence: Sequence::from_height(100), witness: Witness::from_slice(&[sigser.clone(), ms.encode().into_bytes()]), } ); - assert_eq!(wsh.unsigned_script_sig(), bitcoin::ScriptBuf::new()); + assert_eq!(wsh.unsigned_script_sig(), bitcoin_primitives::ScriptBuf::new()); let shwsh = Descriptor::new_sh_wsh(ms.clone()).unwrap(); shwsh.satisfy(&mut txin, &satisfier).expect("satisfaction"); assert_eq!( txin, - bitcoin::TxIn { - previous_output: bitcoin::OutPoint::default(), + bitcoin_primitives::TxIn { + previous_output: bitcoin_primitives::OutPoint::default(), script_sig: script::Builder::new() .push_slice( <&PushBytes>::try_from( @@ -1426,7 +1432,8 @@ mod tests { #[test] fn after_is_cltv() { - let descriptor = Descriptor::::from_str("wsh(after(1000))").unwrap(); + let descriptor = + Descriptor::::from_str("wsh(after(1000))").unwrap(); let script = descriptor.explicit_script().unwrap(); let actual_instructions: Vec<_> = script.instructions().collect(); @@ -1437,7 +1444,8 @@ mod tests { #[test] fn older_is_csv() { - let descriptor = Descriptor::::from_str("wsh(older(1000))").unwrap(); + let descriptor = + Descriptor::::from_str("wsh(older(1000))").unwrap(); let script = descriptor.explicit_script().unwrap(); let actual_instructions: Vec<_> = script.instructions().collect(); @@ -1492,7 +1500,7 @@ mod tests { #[test] fn tr_script_pubkey() { - let key = Descriptor::::from_str( + let key = Descriptor::::from_str( "tr(02e20e746af365e86647826397ba1c0e0d5cb685752976fe2f326ab76bdc4d6ee9)", ) .unwrap(); @@ -1504,41 +1512,41 @@ mod tests { #[test] fn roundtrip_tests() { - let descriptor = Descriptor::::from_str("multi"); + let descriptor = Descriptor::::from_str("multi"); assert_eq!(descriptor.unwrap_err().to_string(), "expected threshold, found terminal",); } #[test] fn empty_thresh() { - let descriptor = Descriptor::::from_str("thresh"); + let descriptor = Descriptor::::from_str("thresh"); assert_eq!(descriptor.unwrap_err().to_string(), "expected threshold, found terminal"); } #[test] fn witness_stack_for_andv_is_arranged_in_correct_order() { // arrange - let a = bitcoin::PublicKey::from_str( + let a = bitcoin_primitives::PublicKey::from_str( "02937402303919b3a2ee5edd5009f4236f069bf75667b8e6ecf8e5464e20116a0e", ) .unwrap(); let sig_a = secp256k1::ecdsa::Signature::from_str("3045022100a7acc3719e9559a59d60d7b2837f9842df30e7edcd754e63227e6168cec72c5d022066c2feba4671c3d99ea75d9976b4da6c86968dbf3bab47b1061e7a1966b1778c").unwrap(); - let b = bitcoin::PublicKey::from_str( + let b = bitcoin_primitives::PublicKey::from_str( "02eb64639a17f7334bb5a1a3aad857d6fec65faef439db3de72f85c88bc2906ad3", ) .unwrap(); let sig_b = secp256k1::ecdsa::Signature::from_str("3044022075b7b65a7e6cd386132c5883c9db15f9a849a0f32bc680e9986398879a57c276022056d94d12255a4424f51c700ac75122cb354895c9f2f88f0cbb47ba05c9c589ba").unwrap(); - let descriptor = Descriptor::::from_str(&format!( + let descriptor = Descriptor::::from_str(&format!( "wsh(and_v(v:pk({A}),pk({B})))", A = a, B = b )) .unwrap(); - let mut txin = bitcoin::TxIn { - previous_output: bitcoin::OutPoint::default(), - script_sig: bitcoin::ScriptBuf::new(), + let mut txin = bitcoin_primitives::TxIn { + previous_output: bitcoin_primitives::OutPoint::default(), + script_sig: bitcoin_primitives::ScriptBuf::new(), sequence: Sequence::ZERO, witness: Witness::default(), }; @@ -1547,11 +1555,17 @@ mod tests { satisfier.insert( a, - bitcoin::ecdsa::Signature { signature: sig_a, sighash_type: EcdsaSighashType::All }, + bitcoin_primitives::ecdsa::Signature { + signature: sig_a, + sighash_type: EcdsaSighashType::All, + }, ); satisfier.insert( b, - bitcoin::ecdsa::Signature { signature: sig_b, sighash_type: EcdsaSighashType::All }, + bitcoin_primitives::ecdsa::Signature { + signature: sig_b, + sighash_type: EcdsaSighashType::All, + }, ); satisfier @@ -1679,7 +1693,7 @@ mod tests { let key = "03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8"; let expected = DescriptorPublicKey::Single(SinglePub { key: SinglePubKey::FullKey( - bitcoin::PublicKey::from_str( + bitcoin_primitives::PublicKey::from_str( "03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8", ) .unwrap(), @@ -1692,7 +1706,7 @@ mod tests { // Raw (uncompressed) pubkey let key = "04f5eeb2b10c944c6b9fbcfff94c35bdeecd93df977882babc7f3a2cf7f5c81d3b09a68db7f0e04f21de5d4230e75e6dbe7ad16eefe0d4325a62067dc6f369446a"; let expected = DescriptorPublicKey::Single(SinglePub { - key: SinglePubKey::FullKey(bitcoin::PublicKey::from_str( + key: SinglePubKey::FullKey(bitcoin_primitives::PublicKey::from_str( "04f5eeb2b10c944c6b9fbcfff94c35bdeecd93df977882babc7f3a2cf7f5c81d3b09a68db7f0e04f21de5d4230e75e6dbe7ad16eefe0d4325a62067dc6f369446a", ) .unwrap()), @@ -1706,7 +1720,7 @@ mod tests { "[78412e3a/0'/42/0']0231c7d3fc85c148717848033ce276ae2b464a4e2c367ed33886cc428b8af48ff8"; let expected = DescriptorPublicKey::Single(SinglePub { key: SinglePubKey::FullKey( - bitcoin::PublicKey::from_str( + bitcoin_primitives::PublicKey::from_str( "0231c7d3fc85c148717848033ce276ae2b464a4e2c367ed33886cc428b8af48ff8", ) .unwrap(), @@ -1745,16 +1759,16 @@ mod tests { .unwrap() .derived_descriptor(&secp_ctx) .unwrap() - .address(bitcoin::Network::Bitcoin) + .address(bitcoin_primitives::Network::Bitcoin) .unwrap(); let addr_two = desc_two .at_derivation_index(index) .unwrap() .derived_descriptor(&secp_ctx) .unwrap() - .address(bitcoin::Network::Bitcoin) + .address(bitcoin_primitives::Network::Bitcoin) .unwrap(); - let addr_expected = bitcoin::Address::from_str(raw_addr_expected) + let addr_expected = bitcoin_address::Address::from_str(raw_addr_expected) .unwrap() .assume_checked(); assert_eq!(addr_one, addr_expected); @@ -2018,15 +2032,15 @@ pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))"; #[test] fn test_context_pks() { - let comp_key = bitcoin::PublicKey::from_str( + let comp_key = bitcoin_primitives::PublicKey::from_str( "02015e4cb53458bf813db8c79968e76e10d13ed6426a23fa71c2f41ba021c2a7ab", ) .unwrap(); - let x_only_key = bitcoin::key::XOnlyPublicKey::from_str( + let x_only_key = bitcoin_primitives::key::XOnlyPublicKey::from_str( "015e4cb53458bf813db8c79968e76e10d13ed6426a23fa71c2f41ba021c2a7ab", ) .unwrap(); - let uncomp_key = bitcoin::PublicKey::from_str("04015e4cb53458bf813db8c79968e76e10d13ed6426a23fa71c2f41ba021c2a7ab0d46021e9e69ef061eb25eab41ae206187b2b05e829559df59d78319bd9267b4").unwrap(); + let uncomp_key = bitcoin_primitives::PublicKey::from_str("04015e4cb53458bf813db8c79968e76e10d13ed6426a23fa71c2f41ba021c2a7ab0d46021e9e69ef061eb25eab41ae206187b2b05e829559df59d78319bd9267b4").unwrap(); type Desc = Descriptor; diff --git a/src/descriptor/segwitv0.rs b/src/descriptor/segwitv0.rs index 35b7bb8e9..255df0ac7 100644 --- a/src/descriptor/segwitv0.rs +++ b/src/descriptor/segwitv0.rs @@ -8,8 +8,9 @@ use core::convert::TryFrom; use core::fmt; -use bitcoin::key::CompressedPublicKey; -use bitcoin::{Address, Network, ScriptBuf, Weight}; +use bitcoin_address::Address; +use bitcoin_primitives::key::CompressedPublicKey; +use bitcoin_primitives::{Network, ScriptBuf, Weight}; use super::checksum::verify_checksum; use super::SortedMultiVec; diff --git a/src/descriptor/sh.rs b/src/descriptor/sh.rs index 5a3686b85..cbdf08dbf 100644 --- a/src/descriptor/sh.rs +++ b/src/descriptor/sh.rs @@ -10,8 +10,9 @@ use core::convert::TryFrom; use core::fmt; -use bitcoin::script::PushBytes; -use bitcoin::{script, Address, Network, ScriptBuf, Weight}; +use bitcoin_address::Address; +use bitcoin_primitives::script::PushBytes; +use bitcoin_primitives::{script, Network, ScriptBuf, Weight}; use super::checksum::verify_checksum; use super::{SortedMultiVec, Wpkh, Wsh}; diff --git a/src/descriptor/sortedmulti.rs b/src/descriptor/sortedmulti.rs index 5b7079292..1af5bd461 100644 --- a/src/descriptor/sortedmulti.rs +++ b/src/descriptor/sortedmulti.rs @@ -9,7 +9,7 @@ use core::fmt; use core::marker::PhantomData; use core::str::FromStr; -use bitcoin::script; +use bitcoin_primitives::script; use crate::miniscript::context::ScriptContext; use crate::miniscript::decode::Terminal; @@ -227,7 +227,7 @@ impl fmt::Display for SortedMultiVec Tr { /// Obtains the corresponding script pubkey for this descriptor. pub fn script_pubkey(&self) -> ScriptBuf { let output_key = self.spend_info().output_key(); - let builder = bitcoin::script::Builder::new(); + let builder = bitcoin_primitives::script::Builder::new(); builder .push_opcode(opcodes::all::OP_PUSHNUM_1) .push_slice(output_key.serialize()) diff --git a/src/interpreter/error.rs b/src/interpreter/error.rs index db924b6aa..9c8a5fd30 100644 --- a/src/interpreter/error.rs +++ b/src/interpreter/error.rs @@ -5,11 +5,11 @@ use core::fmt; #[cfg(feature = "std")] use std::error; -use bitcoin::hashes::hash160; -use bitcoin::hex::DisplayHex; +use bitcoin_primitives::hashes::hash160; +use bitcoin_primitives::hex::DisplayHex; +use bitcoin_primitives::{absolute, relative, taproot}; #[cfg(not(test))] // https://github.com/rust-lang/rust/issues/121684 -use bitcoin::secp256k1; -use bitcoin::{absolute, relative, taproot}; +use secp256k1; use super::BitcoinKey; use crate::prelude::*; @@ -27,13 +27,13 @@ pub enum Error { /// not supported in descriptor spec CannotInferTrDescriptors, /// Error parsing taproot control block - ControlBlockParse(taproot::TaprootError), + ControlBlockParse(taproot::DecodeError), /// Tap control block(merkle proofs + tweak) verification error ControlBlockVerificationError, /// General Interpreter error. CouldNotEvaluate, /// ECDSA Signature related error - EcdsaSig(bitcoin::ecdsa::Error), + EcdsaSig(bitcoin_primitives::ecdsa::Error), /// We expected a push (including a `OP_1` but no other numeric pushes) ExpectedPush, /// The preimage to the hash function must be exactly 32 bytes. @@ -51,9 +51,9 @@ pub enum Error { /// Invalid Sighash type InvalidSchnorrSighashType(Vec), /// ecdsa Signature failed to verify - InvalidEcdsaSignature(bitcoin::PublicKey), + InvalidEcdsaSignature(bitcoin_primitives::PublicKey), /// Signature failed to verify - InvalidSchnorrSignature(bitcoin::key::XOnlyPublicKey), + InvalidSchnorrSignature(bitcoin_primitives::key::XOnlyPublicKey), /// Last byte of this signature isn't a standard sighash type NonStandardSighash(Vec), /// Miniscript error @@ -93,7 +93,7 @@ pub enum Error { /// Miniscript requires the entire top level script to be satisfied. ScriptSatisfactionError, /// Schnorr Signature error - SchnorrSig(bitcoin::taproot::SigFromSliceError), + SchnorrSig(bitcoin_primitives::taproot::SigFromSliceError), /// Taproot Annex Unsupported TapAnnexUnsupported, /// An uncompressed public key was encountered in a context where it is @@ -238,13 +238,13 @@ impl From for Error { } #[doc(hidden)] -impl From for Error { - fn from(e: bitcoin::ecdsa::Error) -> Error { Error::EcdsaSig(e) } +impl From for Error { + fn from(e: bitcoin_primitives::ecdsa::Error) -> Error { Error::EcdsaSig(e) } } #[doc(hidden)] -impl From for Error { - fn from(e: bitcoin::taproot::SigFromSliceError) -> Error { Error::SchnorrSig(e) } +impl From for Error { + fn from(e: bitcoin_primitives::taproot::SigFromSliceError) -> Error { Error::SchnorrSig(e) } } #[doc(hidden)] @@ -257,9 +257,9 @@ impl From for Error { #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum PkEvalErrInner { /// Full Key - FullKey(bitcoin::PublicKey), + FullKey(bitcoin_primitives::PublicKey), /// XOnly Key - XOnlyKey(bitcoin::key::XOnlyPublicKey), + XOnlyKey(bitcoin_primitives::key::XOnlyPublicKey), } impl From for PkEvalErrInner { diff --git a/src/interpreter/inner.rs b/src/interpreter/inner.rs index d7aff1573..9fb707ce8 100644 --- a/src/interpreter/inner.rs +++ b/src/interpreter/inner.rs @@ -1,9 +1,9 @@ // Written in 2019 by Sanket Kanjular and Andrew Poelstra // SPDX-License-Identifier: CC0-1.0 -use bitcoin::hashes::{hash160, sha256}; -use bitcoin::taproot::{ControlBlock, TAPROOT_ANNEX_PREFIX}; -use bitcoin::Witness; +use bitcoin_primitives::hashes::{hash160, sha256}; +use bitcoin_primitives::taproot::{ControlBlock, TAPROOT_ANNEX_PREFIX}; +use bitcoin_primitives::Witness; use super::{stack, BitcoinKey, Error, Stack}; use crate::miniscript::context::{NoChecks, ScriptContext, SigType}; @@ -12,8 +12,11 @@ use crate::{BareCtx, ExtParams, Legacy, Miniscript, Segwitv0, Tap, ToPublicKey, /// Attempts to parse a slice as a Bitcoin public key, checking compressedness /// if asked to, but otherwise dropping it -fn pk_from_slice(slice: &[u8], require_compressed: bool) -> Result { - if let Ok(pk) = bitcoin::PublicKey::from_slice(slice) { +fn pk_from_slice( + slice: &[u8], + require_compressed: bool, +) -> Result { + if let Ok(pk) = bitcoin_primitives::PublicKey::from_slice(slice) { if require_compressed && !pk.compressed { Err(Error::UncompressedPubkey) } else { @@ -27,7 +30,7 @@ fn pk_from_slice(slice: &[u8], require_compressed: bool) -> Result, require_compressed: bool, -) -> Result { +) -> Result { let slice = if let stack::Element::Push(slice) = *elem { slice } else { @@ -42,10 +45,11 @@ fn script_from_stack_elem( elem: &stack::Element<'_>, ) -> Result, Error> { match *elem { - stack::Element::Push(sl) => { - Miniscript::parse_with_ext(bitcoin::Script::from_bytes(sl), &ExtParams::allow_all()) - .map_err(Error::from) - } + stack::Element::Push(sl) => Miniscript::parse_with_ext( + bitcoin_primitives::Script::from_bytes(sl), + &ExtParams::allow_all(), + ) + .map_err(Error::from), stack::Element::Satisfied => Ok(Miniscript::TRUE), stack::Element::Dissatisfied => Ok(Miniscript::FALSE), } @@ -92,10 +96,10 @@ pub(super) enum Inner { /// Tr outputs don't have script code and return None. #[allow(clippy::collapsible_else_if)] pub(super) fn from_txdata<'txin>( - spk: &bitcoin::Script, - script_sig: &'txin bitcoin::Script, + spk: &bitcoin_primitives::Script, + script_sig: &'txin bitcoin_primitives::Script, witness: &'txin Witness, -) -> Result<(Inner, Stack<'txin>, Option), Error> { +) -> Result<(Inner, Stack<'txin>, Option), Error> { let mut ssig_stack: Stack = script_sig .instructions_minimal() .map(stack::Element::from_instruction) @@ -130,7 +134,9 @@ pub(super) fn from_txdata<'txin>( Some(elem) => { let pk = pk_from_stack_elem(&elem, false)?; if *spk - == bitcoin::ScriptBuf::new_p2pkh(&pk.to_pubkeyhash(SigType::Ecdsa).into()) + == bitcoin_primitives::ScriptBuf::new_p2pkh( + &pk.to_pubkeyhash(SigType::Ecdsa).into(), + ) { Ok(( Inner::PublicKey(pk.into(), PubkeyType::Pkh), @@ -153,11 +159,11 @@ pub(super) fn from_txdata<'txin>( Some(elem) => { let pk = pk_from_stack_elem(&elem, true)?; let hash160 = pk.to_pubkeyhash(SigType::Ecdsa); - if *spk == bitcoin::ScriptBuf::new_p2wpkh(&hash160.into()) { + if *spk == bitcoin_primitives::ScriptBuf::new_p2wpkh(&hash160.into()) { Ok(( Inner::PublicKey(pk.into(), PubkeyType::Wpkh), wit_stack, - Some(bitcoin::ScriptBuf::new_p2pkh(&hash160.into())), // bip143, why.. + Some(bitcoin_primitives::ScriptBuf::new_p2pkh(&hash160.into())), // bip143, why.. )) } else { Err(Error::IncorrectWPubkeyHash) @@ -177,7 +183,7 @@ pub(super) fn from_txdata<'txin>( let script = miniscript.encode(); let miniscript = miniscript.to_no_checks_ms(); let scripthash = sha256::Hash::hash(script.as_bytes()); - if *spk == bitcoin::ScriptBuf::new_p2wsh(&scripthash.into()) { + if *spk == bitcoin_primitives::ScriptBuf::new_p2wsh(&scripthash.into()) { Ok((Inner::Script(miniscript, ScriptType::Wsh), wit_stack, Some(script))) } else { Err(Error::IncorrectWScriptHash) @@ -191,8 +197,9 @@ pub(super) fn from_txdata<'txin>( if !ssig_stack.is_empty() { Err(Error::NonEmptyScriptSig) } else { - let output_key = bitcoin::key::XOnlyPublicKey::from_slice(spk[2..].as_bytes()) - .map_err(|_| Error::XOnlyPublicKeyParseError)?; + let output_key = + bitcoin_primitives::key::XOnlyPublicKey::from_slice(spk[2..].as_bytes()) + .map_err(|_| Error::XOnlyPublicKeyParseError)?; let has_annex = wit_stack .last() .and_then(|x| x.as_push().ok()) @@ -222,7 +229,7 @@ pub(super) fn from_txdata<'txin>( let tap_script = script_from_stack_elem::(&tap_script)?; let ms = tap_script.to_no_checks_ms(); // Creating new contexts is cheap - let secp = bitcoin::secp256k1::Secp256k1::verification_only(); + let secp = secp256k1::Secp256k1::verification_only(); let tap_script = tap_script.encode(); if ctrl_blk.verify_taproot_commitment(&secp, output_key, &tap_script) { Ok(( @@ -248,7 +255,7 @@ pub(super) fn from_txdata<'txin>( Some(elem) => { if let stack::Element::Push(slice) = elem { let scripthash = hash160::Hash::hash(slice); - if *spk != bitcoin::ScriptBuf::new_p2sh(&scripthash.into()) { + if *spk != bitcoin_primitives::ScriptBuf::new_p2sh(&scripthash.into()) { return Err(Error::IncorrectScriptHash); } // ** p2sh-wrapped wpkh ** @@ -261,13 +268,17 @@ pub(super) fn from_txdata<'txin>( let pk = pk_from_stack_elem(&elem, true)?; let hash160 = pk.to_pubkeyhash(SigType::Ecdsa); if slice - == bitcoin::ScriptBuf::new_p2wpkh(&hash160.into()) - .as_bytes() + == bitcoin_primitives::ScriptBuf::new_p2wpkh( + &hash160.into(), + ) + .as_bytes() { Ok(( Inner::PublicKey(pk.into(), PubkeyType::ShWpkh), wit_stack, - Some(bitcoin::ScriptBuf::new_p2pkh(&hash160.into())), // bip143, why.. + Some(bitcoin_primitives::ScriptBuf::new_p2pkh( + &hash160.into(), + )), // bip143, why.. )) } else { Err(Error::IncorrectWScriptHash) @@ -289,8 +300,10 @@ pub(super) fn from_txdata<'txin>( let miniscript = miniscript.to_no_checks_ms(); let scripthash = sha256::Hash::hash(script.as_bytes()); if slice - == bitcoin::ScriptBuf::new_p2wsh(&scripthash.into()) - .as_bytes() + == bitcoin_primitives::ScriptBuf::new_p2wsh( + &scripthash.into(), + ) + .as_bytes() { Ok(( Inner::Script(miniscript, ScriptType::ShWsh), @@ -312,7 +325,7 @@ pub(super) fn from_txdata<'txin>( let miniscript = miniscript.to_no_checks_ms(); if wit_stack.is_empty() { let scripthash = hash160::Hash::hash(script.as_bytes()); - if *spk == bitcoin::ScriptBuf::new_p2sh(&scripthash.into()) { + if *spk == bitcoin_primitives::ScriptBuf::new_p2sh(&scripthash.into()) { Ok((Inner::Script(miniscript, ScriptType::Sh), ssig_stack, Some(script))) } else { Err(Error::IncorrectScriptHash) @@ -327,7 +340,7 @@ pub(super) fn from_txdata<'txin>( } else { if wit_stack.is_empty() { // Bare script parsed in BareCtx - let miniscript = Miniscript::::parse_with_ext( + let miniscript = Miniscript::::parse_with_ext( spk, &ExtParams::allow_all(), )?; @@ -352,16 +365,16 @@ pub(super) trait ToNoChecks { fn to_no_checks_ms(&self) -> Miniscript; } -impl ToNoChecks for Miniscript { +impl ToNoChecks for Miniscript { fn to_no_checks_ms(&self) -> Miniscript { struct TranslateFullPk; - impl Translator for TranslateFullPk { - fn pk(&mut self, pk: &bitcoin::PublicKey) -> Result { + impl Translator for TranslateFullPk { + fn pk(&mut self, pk: &bitcoin_primitives::PublicKey) -> Result { Ok(BitcoinKey::Fullkey(*pk)) } - translate_hash_clone!(bitcoin::PublicKey, BitcoinKey, ()); + translate_hash_clone!(bitcoin_primitives::PublicKey, BitcoinKey, ()); } self.translate_pk_ctx(&mut TranslateFullPk) @@ -369,17 +382,20 @@ impl ToNoChecks for Miniscript { } } -impl ToNoChecks for Miniscript { +impl ToNoChecks for Miniscript { fn to_no_checks_ms(&self) -> Miniscript { // specify the () error type as this cannot error struct TranslateXOnlyPk; - impl Translator for TranslateXOnlyPk { - fn pk(&mut self, pk: &bitcoin::key::XOnlyPublicKey) -> Result { + impl Translator for TranslateXOnlyPk { + fn pk( + &mut self, + pk: &bitcoin_primitives::key::XOnlyPublicKey, + ) -> Result { Ok(BitcoinKey::XOnlyPublicKey(*pk)) } - translate_hash_clone!(bitcoin::key::XOnlyPublicKey, BitcoinKey, ()); + translate_hash_clone!(bitcoin_primitives::key::XOnlyPublicKey, BitcoinKey, ()); } self.translate_pk_ctx(&mut TranslateXOnlyPk) .expect("Translation should succeed") @@ -392,29 +408,29 @@ mod tests { use core::convert::TryFrom; use core::str::FromStr; - use bitcoin::hashes::hex::FromHex; - use bitcoin::script::PushBytes; - use bitcoin::{script, ScriptBuf}; + use bitcoin_primitives::hex::FromHex; + use bitcoin_primitives::script::PushBytes; + use bitcoin_primitives::{script, ScriptBuf}; use super::*; struct KeyTestData { - pk_spk: bitcoin::ScriptBuf, - pk_sig: bitcoin::ScriptBuf, - pkh_spk: bitcoin::ScriptBuf, - pkh_sig: bitcoin::ScriptBuf, - pkh_sig_justkey: bitcoin::ScriptBuf, - wpkh_spk: bitcoin::ScriptBuf, + pk_spk: bitcoin_primitives::ScriptBuf, + pk_sig: bitcoin_primitives::ScriptBuf, + pkh_spk: bitcoin_primitives::ScriptBuf, + pkh_sig: bitcoin_primitives::ScriptBuf, + pkh_sig_justkey: bitcoin_primitives::ScriptBuf, + wpkh_spk: bitcoin_primitives::ScriptBuf, wpkh_stack: Witness, wpkh_stack_justkey: Witness, - sh_wpkh_spk: bitcoin::ScriptBuf, - sh_wpkh_sig: bitcoin::ScriptBuf, + sh_wpkh_spk: bitcoin_primitives::ScriptBuf, + sh_wpkh_sig: bitcoin_primitives::ScriptBuf, sh_wpkh_stack: Witness, sh_wpkh_stack_justkey: Witness, } impl KeyTestData { - fn from_key(key: bitcoin::PublicKey) -> KeyTestData { + fn from_key(key: bitcoin_primitives::PublicKey) -> KeyTestData { // what a funny looking signature.. let dummy_sig_vec = Vec::from_hex( "\ @@ -428,12 +444,12 @@ mod tests { let pkhash = key.to_pubkeyhash(SigType::Ecdsa).into(); let wpkhash = key.to_pubkeyhash(SigType::Ecdsa).into(); - let wpkh_spk = bitcoin::ScriptBuf::new_p2wpkh(&wpkhash); + let wpkh_spk = bitcoin_primitives::ScriptBuf::new_p2wpkh(&wpkhash); let wpkh_scripthash = hash160::Hash::hash(wpkh_spk.as_bytes()).into(); KeyTestData { - pk_spk: bitcoin::ScriptBuf::new_p2pk(key), - pkh_spk: bitcoin::ScriptBuf::new_p2pkh(&pkhash), + pk_spk: bitcoin_primitives::ScriptBuf::new_p2pk(key), + pkh_spk: bitcoin_primitives::ScriptBuf::new_p2pkh(&pkhash), pk_sig: script::Builder::new().push_slice(dummy_sig).into_script(), pkh_sig: script::Builder::new() .push_slice(dummy_sig) @@ -443,7 +459,7 @@ mod tests { wpkh_spk: wpkh_spk.clone(), wpkh_stack: Witness::from_slice(&[dummy_sig_vec.clone(), key.to_bytes()]), wpkh_stack_justkey: Witness::from_slice(&[key.to_bytes()]), - sh_wpkh_spk: bitcoin::ScriptBuf::new_p2sh(&wpkh_scripthash), + sh_wpkh_spk: bitcoin_primitives::ScriptBuf::new_p2sh(&wpkh_scripthash), sh_wpkh_sig: script::Builder::new() .push_slice(<&PushBytes>::try_from(wpkh_spk[..].as_bytes()).unwrap()) .into_script(), @@ -454,19 +470,19 @@ mod tests { } struct FixedTestData { - pk_comp: bitcoin::PublicKey, - pk_uncomp: bitcoin::PublicKey, + pk_comp: bitcoin_primitives::PublicKey, + pk_uncomp: bitcoin_primitives::PublicKey, } fn fixed_test_data() -> FixedTestData { FixedTestData { - pk_comp: bitcoin::PublicKey::from_str( + pk_comp: bitcoin_primitives::PublicKey::from_str( "\ 025edd5cc23c51e87a497ca815d5dce0f8ab52554f849ed8995de64c5f34ce7143\ ", ) .unwrap(), - pk_uncomp: bitcoin::PublicKey::from_str( + pk_uncomp: bitcoin_primitives::PublicKey::from_str( "\ 045edd5cc23c51e87a497ca815d5dce0f8ab52554f849ed8995de64c5f34ce7143\ efae9c8dbc14130661e8cec030c89ad0c13c66c0d17a2905cdc706ab7399a868\ @@ -481,7 +497,7 @@ mod tests { let fixed = fixed_test_data(); let comp = KeyTestData::from_key(fixed.pk_comp); let uncomp = KeyTestData::from_key(fixed.pk_uncomp); - let blank_script = bitcoin::ScriptBuf::new(); + let blank_script = bitcoin_primitives::ScriptBuf::new(); let empty_wit = Witness::default(); // Compressed pk, empty scriptsig @@ -515,15 +531,15 @@ mod tests { // Scriptpubkey has invalid key let mut spk = comp.pk_spk.to_bytes(); spk[1] = 5; - let spk = bitcoin::ScriptBuf::from(spk); - let err = from_txdata(&spk, &bitcoin::ScriptBuf::new(), &empty_wit).unwrap_err(); + let spk = bitcoin_primitives::ScriptBuf::from(spk); + let err = from_txdata(&spk, &bitcoin_primitives::ScriptBuf::new(), &empty_wit).unwrap_err(); assert_eq!(err.to_string(), "could not parse pubkey"); // Scriptpubkey has invalid script let mut spk = comp.pk_spk.to_bytes(); spk[0] = 100; - let spk = bitcoin::ScriptBuf::from(spk); - let err = from_txdata(&spk, &bitcoin::ScriptBuf::new(), &empty_wit).unwrap_err(); + let spk = bitcoin_primitives::ScriptBuf::from(spk); + let err = from_txdata(&spk, &bitcoin_primitives::ScriptBuf::new(), &empty_wit).unwrap_err(); assert_eq!(&err.to_string()[0..12], "parse error:"); // Witness is nonempty @@ -540,7 +556,8 @@ mod tests { let empty_wit = Witness::default(); // pkh, empty scriptsig; this time it errors out - let err = from_txdata(&comp.pkh_spk, &bitcoin::ScriptBuf::new(), &empty_wit).unwrap_err(); + let err = from_txdata(&comp.pkh_spk, &bitcoin_primitives::ScriptBuf::new(), &empty_wit) + .unwrap_err(); assert_eq!(err.to_string(), "unexpected end of stack"); // pkh, wrong pubkey @@ -586,7 +603,7 @@ mod tests { let fixed = fixed_test_data(); let comp = KeyTestData::from_key(fixed.pk_comp); let uncomp = KeyTestData::from_key(fixed.pk_uncomp); - let blank_script = bitcoin::ScriptBuf::new(); + let blank_script = bitcoin_primitives::ScriptBuf::new(); // wpkh, empty witness; this time it errors out let err = from_txdata(&comp.wpkh_spk, &blank_script, &Witness::default()).unwrap_err(); @@ -627,7 +644,7 @@ mod tests { let fixed = fixed_test_data(); let comp = KeyTestData::from_key(fixed.pk_comp); let uncomp = KeyTestData::from_key(fixed.pk_uncomp); - let blank_script = bitcoin::ScriptBuf::new(); + let blank_script = bitcoin_primitives::ScriptBuf::new(); // sh_wpkh, missing witness or scriptsig let err = from_txdata(&comp.sh_wpkh_spk, &blank_script, &Witness::default()).unwrap_err(); @@ -672,9 +689,14 @@ mod tests { assert_eq!(script_code, Some(comp.pkh_spk.clone())); } - fn ms_inner_script(ms: &str) -> (Miniscript, bitcoin::ScriptBuf) { - let ms = Miniscript::::from_str_ext(ms, &ExtParams::insane()) - .unwrap(); + fn ms_inner_script( + ms: &str, + ) -> (Miniscript, bitcoin_primitives::ScriptBuf) { + let ms = Miniscript::::from_str_ext( + ms, + &ExtParams::insane(), + ) + .unwrap(); let spk = ms.encode(); let miniscript = ms.to_no_checks_ms(); (miniscript, spk) @@ -684,7 +706,7 @@ mod tests { fn script_bare() { let preimage = b"12345678----____12345678----____"; let hash = hash160::Hash::hash(&preimage[..]); - let blank_script = bitcoin::ScriptBuf::new(); + let blank_script = bitcoin_primitives::ScriptBuf::new(); let empty_wit = Witness::default(); let (miniscript, spk) = ms_inner_script(&format!("hash160({})", hash)); @@ -716,7 +738,7 @@ mod tests { let script_sig = script::Builder::new() .push_slice(<&PushBytes>::try_from(redeem_script.as_bytes()).unwrap()) .into_script(); - let blank_script = bitcoin::ScriptBuf::new(); + let blank_script = bitcoin_primitives::ScriptBuf::new(); let empty_wit = Witness::default(); // sh without scriptsig @@ -749,7 +771,7 @@ mod tests { let wit_stack = Witness::from_slice(&[witness_script.to_bytes()]); let spk = ScriptBuf::new_p2wsh(&wit_hash); - let blank_script = bitcoin::ScriptBuf::new(); + let blank_script = bitcoin_primitives::ScriptBuf::new(); // wsh without witness let err = from_txdata(&spk, &blank_script, &Witness::default()).unwrap_err(); @@ -787,7 +809,7 @@ mod tests { let script_sig = script::Builder::new() .push_slice(<&PushBytes>::try_from(redeem_script.as_bytes()).unwrap()) .into_script(); - let blank_script = bitcoin::ScriptBuf::new(); + let blank_script = bitcoin_primitives::ScriptBuf::new(); let rs_hash = hash160::Hash::hash(redeem_script.as_bytes()).into(); let spk = ScriptBuf::new_p2sh(&rs_hash); diff --git a/src/interpreter/mod.rs b/src/interpreter/mod.rs index 9fc666587..1cc752237 100644 --- a/src/interpreter/mod.rs +++ b/src/interpreter/mod.rs @@ -11,8 +11,10 @@ use core::fmt; use core::str::FromStr; -use bitcoin::hashes::{hash160, ripemd160, sha256}; -use bitcoin::{absolute, relative, secp256k1, sighash, taproot, Sequence, TxOut, Witness}; +use bitcoin_primitives::hashes::{hash160, ripemd160, sha256}; +use bitcoin_primitives::{ + absolute, relative, secp256k1, sighash, taproot, Sequence, TxOut, Witness, +}; use crate::miniscript::context::{NoChecks, SigType}; use crate::miniscript::ScriptContext; @@ -34,7 +36,7 @@ pub struct Interpreter<'txin> { stack: Stack<'txin>, /// For non-Taproot spends, the scriptCode; for Taproot script-spends, this /// is the leaf script; for key-spends it is `None`. - script_code: Option, + script_code: Option, sequence: Sequence, lock_time: absolute::LockTime, } @@ -46,24 +48,27 @@ pub struct Interpreter<'txin> { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum KeySigPair { /// A Full public key and corresponding Ecdsa signature - Ecdsa(bitcoin::PublicKey, bitcoin::ecdsa::Signature), + Ecdsa(bitcoin_primitives::PublicKey, bitcoin_primitives::ecdsa::Signature), /// A x-only key and corresponding Schnorr signature - Schnorr(bitcoin::key::XOnlyPublicKey, bitcoin::taproot::Signature), + Schnorr(bitcoin_primitives::key::XOnlyPublicKey, bitcoin_primitives::taproot::Signature), } impl KeySigPair { - /// Obtain a pair of ([`bitcoin::PublicKey`], [`bitcoin::ecdsa::Signature`]) from [`KeySigPair`] - pub fn as_ecdsa(&self) -> Option<(bitcoin::PublicKey, bitcoin::ecdsa::Signature)> { + /// Obtain a pair of ([`bitcoin_primitives::PublicKey`], [`bitcoin_primitives::ecdsa::Signature`]) from [`KeySigPair`] + pub fn as_ecdsa( + &self, + ) -> Option<(bitcoin_primitives::PublicKey, bitcoin_primitives::ecdsa::Signature)> { match self { KeySigPair::Ecdsa(pk, sig) => Some((*pk, *sig)), KeySigPair::Schnorr(_, _) => None, } } - /// Obtain a pair of ([`bitcoin::secp256k1::XOnlyPublicKey`], [`bitcoin::taproot::Signature`]) from [`KeySigPair`] + /// Obtain a pair of ([`secp256k1::XOnlyPublicKey`], [`bitcoin_primitives::taproot::Signature`]) from [`KeySigPair`] pub fn as_schnorr( &self, - ) -> Option<(bitcoin::key::XOnlyPublicKey, bitcoin::taproot::Signature)> { + ) -> Option<(bitcoin_primitives::key::XOnlyPublicKey, bitcoin_primitives::taproot::Signature)> + { match self { KeySigPair::Ecdsa(_, _) => None, KeySigPair::Schnorr(pk, sig) => Some((*pk, *sig)), @@ -86,9 +91,9 @@ impl KeySigPair { #[derive(Hash, Eq, Ord, PartialEq, PartialOrd, Clone, Copy, Debug)] enum BitcoinKey { // Full key - Fullkey(bitcoin::PublicKey), + Fullkey(bitcoin_primitives::PublicKey), // Xonly key - XOnlyPublicKey(bitcoin::key::XOnlyPublicKey), + XOnlyPublicKey(bitcoin_primitives::key::XOnlyPublicKey), } impl BitcoinKey { @@ -110,12 +115,14 @@ impl fmt::Display for BitcoinKey { } } -impl From for BitcoinKey { - fn from(pk: bitcoin::PublicKey) -> Self { BitcoinKey::Fullkey(pk) } +impl From for BitcoinKey { + fn from(pk: bitcoin_primitives::PublicKey) -> Self { BitcoinKey::Fullkey(pk) } } -impl From for BitcoinKey { - fn from(xpk: bitcoin::key::XOnlyPublicKey) -> Self { BitcoinKey::XOnlyPublicKey(xpk) } +impl From for BitcoinKey { + fn from(xpk: bitcoin_primitives::key::XOnlyPublicKey) -> Self { + BitcoinKey::XOnlyPublicKey(xpk) + } } impl MiniscriptKey for BitcoinKey { @@ -140,8 +147,8 @@ impl<'txin> Interpreter<'txin> { /// function; otherwise, it should be a closure containing a sighash and /// secp context, which can actually verify a given signature. pub fn from_txdata( - spk: &bitcoin::ScriptBuf, - script_sig: &'txin bitcoin::Script, + spk: &bitcoin_primitives::ScriptBuf, + script_sig: &'txin bitcoin_primitives::Script, witness: &'txin Witness, sequence: Sequence, // CSV, relative lock time. lock_time: absolute::LockTime, // CLTV, absolute lock time. @@ -193,7 +200,7 @@ impl<'txin> Interpreter<'txin> { pub fn verify_sig>( &self, secp: &secp256k1::Secp256k1, - tx: &bitcoin::Transaction, + tx: &bitcoin_primitives::Transaction, input_idx: usize, prevouts: &sighash::Prevouts, sig: &KeySigPair, @@ -213,7 +220,7 @@ impl<'txin> Interpreter<'txin> { sighash::Prevouts::All(prevouts) => prevouts.get(input_index), } } - let mut cache = bitcoin::sighash::SighashCache::new(tx); + let mut cache = bitcoin_primitives::sighash::SighashCache::new(tx); match sig { KeySigPair::Ecdsa(key, ecdsa_sig) => { let script_pubkey = self.script_code.as_ref().expect("Legacy have script code"); @@ -303,7 +310,7 @@ impl<'txin> Interpreter<'txin> { pub fn iter<'iter, C: secp256k1::Verification, T: Borrow>( &'iter self, secp: &'iter secp256k1::Secp256k1, - tx: &'txin bitcoin::Transaction, + tx: &'txin bitcoin_primitives::Transaction, input_idx: usize, prevouts: &'iter sighash::Prevouts, // actually a 'prevouts, but 'prevouts: 'iter ) -> Iter<'txin, 'iter> { @@ -438,8 +445,10 @@ impl<'txin> Interpreter<'txin> { /// This may not represent the original descriptor used to produce the transaction, /// since it cannot distinguish between sorted and unsorted multisigs (and anyway /// it can only see the final keys, keyorigin info is lost in serializing to Bitcoin). - /// x-only keys are translated to [`bitcoin::PublicKey`] with 0x02 prefix. - pub fn inferred_descriptor(&self) -> Result, crate::Error> { + /// x-only keys are translated to [`bitcoin_primitives::PublicKey`] with 0x02 prefix. + pub fn inferred_descriptor( + &self, + ) -> Result, crate::Error> { Descriptor::from_str(&self.inferred_descriptor_string()) } } @@ -1028,7 +1037,7 @@ fn verify_sersig<'txin>( ) -> Result { match pk { BitcoinKey::Fullkey(pk) => { - let ecdsa_sig = bitcoin::ecdsa::Signature::from_slice(sigser)?; + let ecdsa_sig = bitcoin_primitives::ecdsa::Signature::from_slice(sigser)?; let key_sig_pair = KeySigPair::Ecdsa(*pk, ecdsa_sig); if verify_sig(&key_sig_pair) { Ok(key_sig_pair) @@ -1037,7 +1046,7 @@ fn verify_sersig<'txin>( } } BitcoinKey::XOnlyPublicKey(x_only_pk) => { - let schnorr_sig = bitcoin::taproot::Signature::from_slice(sigser)?; + let schnorr_sig = bitcoin_primitives::taproot::Signature::from_slice(sigser)?; let key_sig_pair = KeySigPair::Schnorr(*x_only_pk, schnorr_sig); if verify_sig(&key_sig_pair) { Ok(key_sig_pair) @@ -1051,7 +1060,7 @@ fn verify_sersig<'txin>( #[cfg(test)] mod tests { - use bitcoin::secp256k1::Secp256k1; + use secp256k1::Secp256k1; use super::inner::ToNoChecks; use super::*; @@ -1061,13 +1070,13 @@ mod tests { fn setup_keys_sigs( n: usize, ) -> ( - Vec, + Vec, Vec>, - Vec, + Vec, secp256k1::Message, Secp256k1, - Vec, - Vec, + Vec, + Vec, Vec>, ) { let secp = secp256k1::Secp256k1::new(); @@ -1086,27 +1095,28 @@ mod tests { sk[2] = (i >> 16) as u8; let sk = secp256k1::SecretKey::from_slice(&sk[..]).expect("secret key"); - let pk = bitcoin::PublicKey { + let pk = bitcoin_primitives::PublicKey { inner: secp256k1::PublicKey::from_secret_key(&secp, &sk), compressed: true, }; let signature = secp.sign_ecdsa(&msg, &sk); - ecdsa_sigs.push(bitcoin::ecdsa::Signature { + ecdsa_sigs.push(bitcoin_primitives::ecdsa::Signature { signature, - sighash_type: bitcoin::sighash::EcdsaSighashType::All, + sighash_type: bitcoin_primitives::sighash::EcdsaSighashType::All, }); let mut sigser = signature.serialize_der().to_vec(); sigser.push(0x01); // sighash_all pks.push(pk); der_sigs.push(sigser); - let keypair = bitcoin::key::Keypair::from_secret_key(&secp, &sk); - let (x_only_pk, _parity) = bitcoin::key::XOnlyPublicKey::from_keypair(&keypair); + let keypair = bitcoin_primitives::key::Keypair::from_secret_key(&secp, &sk); + let (x_only_pk, _parity) = + bitcoin_primitives::key::XOnlyPublicKey::from_keypair(&keypair); x_only_pks.push(x_only_pk); let schnorr_sig = secp.sign_schnorr_with_aux_rand(&msg, &keypair, &[0u8; 32]); - let schnorr_sig = bitcoin::taproot::Signature { + let schnorr_sig = bitcoin_primitives::taproot::Signature { signature: schnorr_sig, - sighash_type: bitcoin::sighash::TapSighashType::Default, + sighash_type: bitcoin_primitives::sighash::TapSighashType::Default, }; ser_schnorr_sigs.push(schnorr_sig.to_vec()); schnorr_sigs.push(schnorr_sig); @@ -1558,13 +1568,13 @@ mod tests { // because it does not implement FromStr fn no_checks_ms(ms: &str) -> Miniscript { // Parsing should allow raw hashes in the interpreter - let elem: Miniscript = + let elem: Miniscript = Miniscript::from_str_ext(ms, &ExtParams::allow_all()).unwrap(); elem.to_no_checks_ms() } fn x_only_no_checks_ms(ms: &str) -> Miniscript { - let elem: Miniscript = + let elem: Miniscript = Miniscript::from_str_ext(ms, &ExtParams::allow_all()).unwrap(); elem.to_no_checks_ms() } diff --git a/src/interpreter/stack.rs b/src/interpreter/stack.rs index 71fc77d29..a98867178 100644 --- a/src/interpreter/stack.rs +++ b/src/interpreter/stack.rs @@ -3,8 +3,8 @@ //! Interpreter stack -use bitcoin::hashes::{hash160, ripemd160, sha256}; -use bitcoin::{absolute, opcodes, relative, script, Sequence}; +use bitcoin_primitives::hashes::{hash160, ripemd160, sha256}; +use bitcoin_primitives::{absolute, opcodes, relative, script, Sequence}; use super::error::PkEvalErrInner; use super::{verify_sersig, BitcoinKey, Error, HashLockType, KeySigPair, SatisfiedConstraint}; @@ -49,7 +49,7 @@ impl<'txin> Element<'txin> { /// /// Supports `OP_1` but no other numbers since these are not used by Miniscript pub fn from_instruction( - ins: Result, bitcoin::script::Error>, + ins: Result, bitcoin_primitives::script::Error>, ) -> Result { match ins { //Also covers the dissatisfied case as PushBytes0 @@ -151,8 +151,10 @@ impl<'txin> Stack<'txin> { // We don't really store information about which key error. fn bitcoin_key_from_slice(sl: &[u8], sig_type: SigType) -> Option { let key: BitcoinKey = match sig_type { - SigType::Schnorr => bitcoin::key::XOnlyPublicKey::from_slice(sl).ok()?.into(), - SigType::Ecdsa => bitcoin::PublicKey::from_slice(sl).ok()?.into(), + SigType::Schnorr => bitcoin_primitives::key::XOnlyPublicKey::from_slice(sl) + .ok()? + .into(), + SigType::Ecdsa => bitcoin_primitives::PublicKey::from_slice(sl).ok()?.into(), }; Some(key) } diff --git a/src/lib.rs b/src/lib.rs index e80166307..a50086411 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,7 +43,7 @@ //! ```rust //! use std::str::FromStr; //! -//! let desc = miniscript::Descriptor::::from_str("\ +//! let desc = miniscript::Descriptor::::from_str("\ //! sh(wsh(or_d(\ //! c:pk_k(020e0338c96a8870479f2396c373cc7696ba124e8635d41b0ea581112b67817261),\ //! c:pk_k(0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352)\ @@ -52,7 +52,7 @@ //! //! // Derive the P2SH address. //! assert_eq!( -//! desc.address(bitcoin::Network::Bitcoin).unwrap().to_string(), +//! desc.address(bitcoin_primitives::Network::Bitcoin).unwrap().to_string(), //! "3CJxbQBfWAe1ZkKiGQNEYrioV73ZwvBWns" //! ); //! @@ -98,7 +98,21 @@ compile_error!( #[cfg(not(any(feature = "std", feature = "no-std")))] compile_error!("at least one of the `std` or `no-std` features must be enabled"); -pub use bitcoin; +/// Re-expot the `bip32` crate. +pub extern crate bip32; + +/// Re-export the `bitcoin-address` crate. +pub extern crate bitcoin_address; + +/// Re-export the `bitcoin-primitives` crate. +pub extern crate bitcoin_primitives; + +/// Re-export the `psbt-v0` crate. +pub extern crate psbt_v0; + +/// Rust wrapper library for Pieter Wuille's libsecp256k1. Implements ECDSA and BIP 340 signatures +/// for the SECG elliptic curve group secp256k1 and related utilities. +pub extern crate secp256k1; #[cfg(not(feature = "std"))] #[macro_use] @@ -138,9 +152,9 @@ use core::{fmt, hash, str}; #[cfg(feature = "std")] use std::error; -use bitcoin::hashes::{hash160, ripemd160, sha256}; -use bitcoin::hex::DisplayHex; -use bitcoin::{script, Opcode}; +use bitcoin_primitives::hashes::{hash160, ripemd160, sha256}; +use bitcoin_primitives::hex::DisplayHex; +use bitcoin_primitives::{script, Opcode}; pub use crate::blanket_traits::FromStrKey; pub use crate::descriptor::{DefiniteDescriptorKey, Descriptor, DescriptorPublicKey}; @@ -170,7 +184,7 @@ pub trait MiniscriptKey: Clone + Eq + Ord + fmt::Debug + fmt::Display + hash::Ha /// in BIP389 multipath descriptors. fn num_der_paths(&self) -> usize { 0 } - /// The associated [`bitcoin::hashes::sha256::Hash`] for this [`MiniscriptKey`], used in the + /// The associated [`bitcoin_primitives::hashes::sha256::Hash`] for this [`MiniscriptKey`], used in the /// sha256 fragment. type Sha256: Clone + Eq + Ord + fmt::Display + fmt::Debug + hash::Hash; @@ -178,23 +192,23 @@ pub trait MiniscriptKey: Clone + Eq + Ord + fmt::Debug + fmt::Display + hash::Ha /// hash256 fragment. type Hash256: Clone + Eq + Ord + fmt::Display + fmt::Debug + hash::Hash; - /// The associated [`bitcoin::hashes::ripemd160::Hash`] for this [`MiniscriptKey`] type, used + /// The associated [`bitcoin_primitives::hashes::ripemd160::Hash`] for this [`MiniscriptKey`] type, used /// in the ripemd160 fragment. type Ripemd160: Clone + Eq + Ord + fmt::Display + fmt::Debug + hash::Hash; - /// The associated [`bitcoin::hashes::hash160::Hash`] for this [`MiniscriptKey`] type, used in + /// The associated [`bitcoin_primitives::hashes::hash160::Hash`] for this [`MiniscriptKey`] type, used in /// the hash160 fragment. type Hash160: Clone + Eq + Ord + fmt::Display + fmt::Debug + hash::Hash; } -impl MiniscriptKey for bitcoin::secp256k1::PublicKey { +impl MiniscriptKey for secp256k1::PublicKey { type Sha256 = sha256::Hash; type Hash256 = hash256::Hash; type Ripemd160 = ripemd160::Hash; type Hash160 = hash160::Hash; } -impl MiniscriptKey for bitcoin::PublicKey { +impl MiniscriptKey for bitcoin_primitives::PublicKey { /// Returns the compressed-ness of the underlying secp256k1 key. fn is_uncompressed(&self) -> bool { !self.compressed } @@ -204,7 +218,7 @@ impl MiniscriptKey for bitcoin::PublicKey { type Hash160 = hash160::Hash; } -impl MiniscriptKey for bitcoin::secp256k1::XOnlyPublicKey { +impl MiniscriptKey for secp256k1::XOnlyPublicKey { type Sha256 = sha256::Hash; type Hash256 = hash256::Hash; type Ripemd160 = ripemd160::Hash; @@ -223,12 +237,12 @@ impl MiniscriptKey for String { /// Trait describing public key types which can be converted to bitcoin pubkeys pub trait ToPublicKey: MiniscriptKey { /// Converts an object to a public key - fn to_public_key(&self) -> bitcoin::PublicKey; + fn to_public_key(&self) -> bitcoin_primitives::PublicKey; /// Convert an object to x-only pubkey - fn to_x_only_pubkey(&self) -> bitcoin::secp256k1::XOnlyPublicKey { + fn to_x_only_pubkey(&self) -> secp256k1::XOnlyPublicKey { let pk = self.to_public_key(); - bitcoin::secp256k1::XOnlyPublicKey::from(pk.inner) + secp256k1::XOnlyPublicKey::from(pk.inner) } /// Obtain the public key hash for this MiniscriptKey @@ -255,8 +269,8 @@ pub trait ToPublicKey: MiniscriptKey { fn to_hash160(hash: &::Hash160) -> hash160::Hash; } -impl ToPublicKey for bitcoin::PublicKey { - fn to_public_key(&self) -> bitcoin::PublicKey { *self } +impl ToPublicKey for bitcoin_primitives::PublicKey { + fn to_public_key(&self) -> bitcoin_primitives::PublicKey { *self } fn to_sha256(hash: &sha256::Hash) -> sha256::Hash { *hash } @@ -267,8 +281,10 @@ impl ToPublicKey for bitcoin::PublicKey { fn to_hash160(hash: &hash160::Hash) -> hash160::Hash { *hash } } -impl ToPublicKey for bitcoin::secp256k1::PublicKey { - fn to_public_key(&self) -> bitcoin::PublicKey { bitcoin::PublicKey::new(*self) } +impl ToPublicKey for secp256k1::PublicKey { + fn to_public_key(&self) -> bitcoin_primitives::PublicKey { + bitcoin_primitives::PublicKey::new(*self) + } fn to_sha256(hash: &sha256::Hash) -> sha256::Hash { *hash } @@ -279,17 +295,17 @@ impl ToPublicKey for bitcoin::secp256k1::PublicKey { fn to_hash160(hash: &hash160::Hash) -> hash160::Hash { *hash } } -impl ToPublicKey for bitcoin::secp256k1::XOnlyPublicKey { - fn to_public_key(&self) -> bitcoin::PublicKey { +impl ToPublicKey for secp256k1::XOnlyPublicKey { + fn to_public_key(&self) -> bitcoin_primitives::PublicKey { // This code should never be used. // But is implemented for completeness let mut data: Vec = vec![0x02]; data.extend(self.serialize().iter()); - bitcoin::PublicKey::from_slice(&data) + bitcoin_primitives::PublicKey::from_slice(&data) .expect("Failed to construct 33 Publickey from 0x02 appended x-only key") } - fn to_x_only_pubkey(&self) -> bitcoin::secp256k1::XOnlyPublicKey { *self } + fn to_x_only_pubkey(&self) -> secp256k1::XOnlyPublicKey { *self } fn to_sha256(hash: &sha256::Hash) -> sha256::Hash { *hash } @@ -431,7 +447,7 @@ pub enum Error { /// rust-bitcoin script error Script(script::Error), /// rust-bitcoin address error - AddrError(bitcoin::address::ParseError), + AddrError(bitcoin_address::ParseError), /// A `CHECKMULTISIG` opcode was preceded by a number > 20 CmsTooManyKeys(u32), /// A tapscript multi_a cannot support more than Weight::MAX_BLOCK/32 keys @@ -455,7 +471,7 @@ pub enum Error { /// Parsed a miniscript but there were more script opcodes after it Trailing(String), /// Could not satisfy a script (fragment) because of a missing signature - MissingSig(bitcoin::PublicKey), + MissingSig(bitcoin_primitives::PublicKey), /// General failure to satisfy CouldNotSatisfy, /// Typechecking failed @@ -463,7 +479,7 @@ pub enum Error { /// General error in creating descriptor BadDescriptor(String), /// Forward-secp related errors - Secp(bitcoin::secp256k1::Error), + Secp(secp256k1::Error), #[cfg(feature = "compiler")] /// Compiler related errors CompilerError(crate::policy::compiler::CompilerError), @@ -631,13 +647,13 @@ impl From for Error { } #[doc(hidden)] -impl From for Error { - fn from(e: bitcoin::secp256k1::Error) -> Error { Error::Secp(e) } +impl From for Error { + fn from(e: secp256k1::Error) -> Error { Error::Secp(e) } } #[doc(hidden)] -impl From for Error { - fn from(e: bitcoin::address::ParseError) -> Error { Error::AddrError(e) } +impl From for Error { + fn from(e: bitcoin_address::ParseError) -> Error { Error::AddrError(e) } } #[doc(hidden)] @@ -684,9 +700,9 @@ fn push_opcode_size(script_size: usize) -> usize { /// Helper function used by tests #[cfg(test)] -fn hex_script(s: &str) -> bitcoin::ScriptBuf { - let v: Vec = bitcoin::hashes::hex::FromHex::from_hex(s).unwrap(); - bitcoin::ScriptBuf::from(v) +fn hex_script(s: &str) -> bitcoin_primitives::ScriptBuf { + let v: Vec = bitcoin_primitives::hex::FromHex::from_hex(s).unwrap(); + bitcoin_primitives::ScriptBuf::from(v) } #[cfg(test)] @@ -697,7 +713,7 @@ mod tests { #[test] fn regression_bitcoin_key_hash() { - use bitcoin::PublicKey; + use bitcoin_primitives::PublicKey; // Uncompressed key. let pk = PublicKey::from_str( @@ -711,7 +727,7 @@ mod tests { #[test] fn regression_secp256k1_key_hash() { - use bitcoin::secp256k1::PublicKey; + use secp256k1::PublicKey; // Compressed key. let pk = PublicKey::from_str( @@ -726,7 +742,7 @@ mod tests { #[test] fn regression_xonly_key_hash() { - use bitcoin::secp256k1::XOnlyPublicKey; + use secp256k1::XOnlyPublicKey; let pk = XOnlyPublicKey::from_str( "cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115", diff --git a/src/miniscript/astelem.rs b/src/miniscript/astelem.rs index 5e9293469..c6e4b23b0 100644 --- a/src/miniscript/astelem.rs +++ b/src/miniscript/astelem.rs @@ -10,8 +10,8 @@ use core::fmt; use core::str::FromStr; -use bitcoin::hashes::hash160; -use bitcoin::{absolute, opcodes, script}; +use bitcoin_primitives::hashes::hash160; +use bitcoin_primitives::{absolute, opcodes, script}; use sync::Arc; use crate::miniscript::context::SigType; diff --git a/src/miniscript/context.rs b/src/miniscript/context.rs index 584ff4adf..2d09a436b 100644 --- a/src/miniscript/context.rs +++ b/src/miniscript/context.rs @@ -5,8 +5,8 @@ use core::{fmt, hash}; #[cfg(feature = "std")] use std::error; -use bitcoin::hashes::{hash160, ripemd160, sha256}; -use bitcoin::Weight; +use bitcoin_primitives::hashes::{hash160, ripemd160, sha256}; +use bitcoin_primitives::Weight; use super::decode::ParseableKey; use crate::miniscript::limits::{ @@ -201,7 +201,7 @@ where /// Or some of the script resource limits may have been exceeded. /// These miniscripts would never be accepted by the Bitcoin network and hence /// it is safe to discard them - /// For example, in Segwit Context with MiniscriptKey as bitcoin::PublicKey + /// For example, in Segwit Context with MiniscriptKey as bitcoin_primitives::PublicKey /// uncompressed public keys are non-standard and thus invalid. /// In LegacyP2SH context, scripts above 520 bytes are invalid. /// Post Tapscript upgrade, this would have to consider other nodes. @@ -216,7 +216,7 @@ where /// may have been exceeded under the current bitcoin core policy rules /// These miniscripts would never be accepted by the Bitcoin network and hence /// it is safe to discard them. (unless explicitly disabled by non-standard flag) - /// For example, in Segwit Context with MiniscriptKey as bitcoin::PublicKey + /// For example, in Segwit Context with MiniscriptKey as bitcoin_primitives::PublicKey /// scripts over 3600 bytes are invalid. /// Post Tapscript upgrade, this would have to consider other nodes. /// This does *NOT* recursively check the miniscript fragments. @@ -358,7 +358,7 @@ pub enum SigType { pub enum Legacy {} impl ScriptContext for Legacy { - type Key = bitcoin::PublicKey; + type Key = bitcoin_primitives::PublicKey; fn check_terminal_non_malleable( frag: &Terminal, ) -> Result<(), ScriptContextError> { @@ -459,7 +459,7 @@ impl ScriptContext for Legacy { pub enum Segwitv0 {} impl ScriptContext for Segwitv0 { - type Key = bitcoin::PublicKey; + type Key = bitcoin_primitives::PublicKey; fn check_terminal_non_malleable( _frag: &Terminal, ) -> Result<(), ScriptContextError> { @@ -564,7 +564,7 @@ impl ScriptContext for Segwitv0 { pub enum Tap {} impl ScriptContext for Tap { - type Key = bitcoin::secp256k1::XOnlyPublicKey; + type Key = secp256k1::XOnlyPublicKey; fn check_terminal_non_malleable( _frag: &Terminal, ) -> Result<(), ScriptContextError> { @@ -675,7 +675,7 @@ impl ScriptContext for Tap { pub enum BareCtx {} impl ScriptContext for BareCtx { - type Key = bitcoin::PublicKey; + type Key = bitcoin_primitives::PublicKey; fn check_terminal_non_malleable( _frag: &Terminal, ) -> Result<(), ScriptContextError> { @@ -765,7 +765,7 @@ impl ScriptContext for BareCtx { pub enum NoChecks {} impl ScriptContext for NoChecks { // todo: When adding support for interpreter, we need a enum with all supported keys here - type Key = bitcoin::PublicKey; + type Key = bitcoin_primitives::PublicKey; fn check_terminal_non_malleable( _frag: &Terminal, ) -> Result<(), ScriptContextError> { diff --git a/src/miniscript/decode.rs b/src/miniscript/decode.rs index 36bb22515..f59ca0aac 100644 --- a/src/miniscript/decode.rs +++ b/src/miniscript/decode.rs @@ -10,7 +10,7 @@ use core::marker::PhantomData; #[cfg(feature = "std")] use std::error; -use bitcoin::hashes::{hash160, ripemd160, sha256}; +use bitcoin_primitives::hashes::{hash160, ripemd160, sha256}; use sync::Arc; use crate::miniscript::lex::{Token as Tk, TokenIter}; @@ -31,16 +31,15 @@ pub trait ParseableKey: Sized + ToPublicKey + private::Sealed { fn from_slice(sl: &[u8]) -> Result; } -impl ParseableKey for bitcoin::PublicKey { +impl ParseableKey for bitcoin_primitives::PublicKey { fn from_slice(sl: &[u8]) -> Result { - bitcoin::PublicKey::from_slice(sl).map_err(KeyParseError::FullKeyParseError) + bitcoin_primitives::PublicKey::from_slice(sl).map_err(KeyParseError::FullKeyParseError) } } -impl ParseableKey for bitcoin::secp256k1::XOnlyPublicKey { +impl ParseableKey for secp256k1::XOnlyPublicKey { fn from_slice(sl: &[u8]) -> Result { - bitcoin::secp256k1::XOnlyPublicKey::from_slice(sl) - .map_err(KeyParseError::XonlyKeyParseError) + secp256k1::XOnlyPublicKey::from_slice(sl).map_err(KeyParseError::XonlyKeyParseError) } } @@ -48,9 +47,9 @@ impl ParseableKey for bitcoin::secp256k1::XOnlyPublicKey { #[derive(Debug, Clone, PartialEq, Eq)] pub enum KeyParseError { /// Bitcoin PublicKey parse error - FullKeyParseError(bitcoin::key::FromSliceError), + FullKeyParseError(bitcoin_primitives::key::FromSliceError), /// Xonly key parse Error - XonlyKeyParseError(bitcoin::secp256k1::Error), + XonlyKeyParseError(secp256k1::Error), } impl fmt::Display for KeyParseError { @@ -78,8 +77,8 @@ mod private { pub trait Sealed {} // Implement for those same types, but no others. - impl Sealed for bitcoin::PublicKey {} - impl Sealed for bitcoin::secp256k1::XOnlyPublicKey {} + impl Sealed for bitcoin_primitives::PublicKey {} + impl Sealed for secp256k1::XOnlyPublicKey {} } #[derive(Copy, Clone, Debug)] diff --git a/src/miniscript/iter.rs b/src/miniscript/iter.rs index 53cea7647..0340c7d71 100644 --- a/src/miniscript/iter.rs +++ b/src/miniscript/iter.rs @@ -203,14 +203,14 @@ impl<'a, Pk: MiniscriptKey, Ctx: ScriptContext> Iterator for PkIter<'a, Pk, Ctx> // dependent libraries for their own tasts based on Miniscript AST #[cfg(test)] pub mod test { - use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d}; + use bitcoin_primitives::hashes::{hash160, ripemd160, sha256, sha256d}; use super::Miniscript; use crate::miniscript::context::Segwitv0; pub type TestData = ( - Miniscript, - Vec, + Miniscript, + Vec, Vec, bool, // Indicates that the top-level contains public key or hashes ); @@ -233,10 +233,10 @@ pub mod test { ret } - pub fn gen_bitcoin_pubkeys(n: usize, compressed: bool) -> Vec { + pub fn gen_bitcoin_pubkeys(n: usize, compressed: bool) -> Vec { gen_secp_pubkeys(n) .into_iter() - .map(|inner| bitcoin::PublicKey { inner, compressed }) + .map(|inner| bitcoin_primitives::PublicKey { inner, compressed }) .collect() } @@ -338,7 +338,7 @@ pub mod test { #[test] fn find_keys() { gen_testcases().into_iter().for_each(|(ms, k, _, _)| { - assert_eq!(ms.iter_pk().collect::>(), k); + assert_eq!(ms.iter_pk().collect::>(), k); }) } } diff --git a/src/miniscript/lex.rs b/src/miniscript/lex.rs index d9febba88..36a582ef9 100644 --- a/src/miniscript/lex.rs +++ b/src/miniscript/lex.rs @@ -7,7 +7,7 @@ use core::fmt; -use bitcoin::{opcodes, script}; +use bitcoin_primitives::{opcodes, script}; use super::Error; use crate::prelude::*; diff --git a/src/miniscript/limits.rs b/src/miniscript/limits.rs index abd111f96..aee73f761 100644 --- a/src/miniscript/limits.rs +++ b/src/miniscript/limits.rs @@ -34,4 +34,5 @@ pub const MAX_BLOCK_WEIGHT: usize = 4000000; // https://github.com/bitcoin/bitcoin/blob/6acda4b00b3fc1bfac02f5de590e1a5386cbc779/src/script/script.h#L30 pub const MAX_PUBKEYS_PER_MULTISIG: usize = 20; /// Maximum pubkeys in a CHECKSIGADD construction. -pub const MAX_PUBKEYS_IN_CHECKSIGADD: usize = (bitcoin::Weight::MAX_BLOCK.to_wu() / 32) as usize; +pub const MAX_PUBKEYS_IN_CHECKSIGADD: usize = + (bitcoin_primitives::Weight::MAX_BLOCK.to_wu() / 32) as usize; diff --git a/src/miniscript/mod.rs b/src/miniscript/mod.rs index 15de1174b..885f6beb1 100644 --- a/src/miniscript/mod.rs +++ b/src/miniscript/mod.rs @@ -15,9 +15,9 @@ use core::marker::PhantomData; use core::{fmt, hash, str}; -use bitcoin::hashes::hash160; -use bitcoin::script; -use bitcoin::taproot::{LeafVersion, TapLeafHash}; +use bitcoin_primitives::hashes::hash160; +use bitcoin_primitives::script; +use bitcoin_primitives::taproot::{LeafVersion, TapLeafHash}; use self::analyzable::ExtParams; pub use self::context::{BareCtx, Legacy, Segwitv0, Tap}; @@ -328,24 +328,24 @@ impl Miniscript { /// /// ```rust /// use miniscript::{Miniscript, Segwitv0, Tap}; - /// use miniscript::bitcoin::secp256k1::XOnlyPublicKey; - /// use miniscript::bitcoin::hashes::hex::FromHex; + /// use miniscript::secp256k1::XOnlyPublicKey; + /// use miniscript::bitcoin_primitives::hex::FromHex; /// - /// type Segwitv0Script = Miniscript; + /// type Segwitv0Script = Miniscript; /// type TapScript = Miniscript; /// /// // parse x-only miniscript in Taproot context - /// let tapscript_ms = TapScript::parse(&bitcoin::ScriptBuf::from_hex( + /// let tapscript_ms = TapScript::parse(&bitcoin_primitives::ScriptBuf::from_hex( /// "202788ee41e76f4f3af603da5bc8fa22997bc0344bb0f95666ba6aaff0242baa99ac", /// ).expect("Even length hex")) /// .expect("Xonly keys are valid only in taproot context"); /// // tapscript fails decoding when we use them with compressed keys - /// let err = TapScript::parse(&bitcoin::ScriptBuf::from_hex( + /// let err = TapScript::parse(&bitcoin_primitives::ScriptBuf::from_hex( /// "21022788ee41e76f4f3af603da5bc8fa22997bc0344bb0f95666ba6aaff0242baa99ac", /// ).expect("Even length hex")) /// .expect_err("Compressed keys cannot be used in Taproot context"); /// // Segwitv0 succeeds decoding with full keys. - /// Segwitv0Script::parse(&bitcoin::ScriptBuf::from_hex( + /// Segwitv0Script::parse(&bitcoin_primitives::ScriptBuf::from_hex( /// "21022788ee41e76f4f3af603da5bc8fa22997bc0344bb0f95666ba6aaff0242baa99ac", /// ).expect("Even length hex")) /// .expect("Compressed keys are allowed in Segwit context"); @@ -681,7 +681,7 @@ serde_string_impl_pk!(Miniscript, "a miniscript", Ctx; ScriptContext); /// Provides a Double SHA256 `Hash` type that displays forwards. pub mod hash256 { - use bitcoin::hashes::{hash_newtype, sha256d}; + use bitcoin_primitives::hashes::{hash_newtype, sha256d}; hash_newtype! { /// A hash256 of preimage. @@ -697,9 +697,9 @@ mod tests { use core::str; use core::str::FromStr; - use bitcoin::hashes::{hash160, sha256}; - use bitcoin::secp256k1::XOnlyPublicKey; - use bitcoin::taproot::TapLeafHash; + use bitcoin_primitives::hashes::{hash160, sha256}; + use bitcoin_primitives::taproot::TapLeafHash; + use secp256k1::XOnlyPublicKey; use sync::Arc; use super::{Miniscript, ScriptContext, Segwitv0, Tap}; @@ -710,10 +710,10 @@ mod tests { use crate::test_utils::{StrKeyTranslator, StrXOnlyKeyTranslator}; use crate::{hex_script, ExtParams, RelLockTime, Satisfier, ToPublicKey, TranslatePk}; - type Segwitv0Script = Miniscript; - type Tapscript = Miniscript; + type Segwitv0Script = Miniscript; + type Tapscript = Miniscript; - fn pubkeys(n: usize) -> Vec { + fn pubkeys(n: usize) -> Vec { let mut ret = Vec::with_capacity(n); let secp = secp256k1::Secp256k1::new(); let mut sk = [0; 32]; @@ -722,7 +722,7 @@ mod tests { sk[1] = (i >> 8) as u8; sk[2] = (i >> 16) as u8; - let pk = bitcoin::PublicKey { + let pk = bitcoin_primitives::PublicKey { inner: secp256k1::PublicKey::from_secret_key( &secp, &secp256k1::SecretKey::from_slice(&sk[..]).expect("secret key"), @@ -735,7 +735,7 @@ mod tests { } fn string_rtt( - script: Miniscript, + script: Miniscript, expected_debug: &str, expected_display: &str, ) { @@ -753,7 +753,7 @@ mod tests { } fn string_display_debug_test( - script: Miniscript, + script: Miniscript, expected_debug: &str, expected_display: &str, ) { @@ -881,7 +881,7 @@ mod tests { #[test] fn basic() { - let pk = bitcoin::PublicKey::from_str( + let pk = bitcoin_primitives::PublicKey::from_str( "\ 020202020202020202020202020202020202020202020202020202020202020202\ ", @@ -1206,14 +1206,14 @@ mod tests { ) .unwrap(); - // Now test that bitcoin::PublicKey works with Taproot context - Miniscript::::from_str_insane( + // Now test that bitcoin_primitives::PublicKey works with Taproot context + Miniscript::::from_str_insane( "pk(022788ee41e76f4f3af603da5bc8fa22997bc0344bb0f95666ba6aaff0242baa99)", ) .unwrap(); // uncompressed keys should not be allowed - Miniscript::::from_str_insane( + Miniscript::::from_str_insane( "pk(04eed24a081bf1b1e49e3300df4bebe04208ac7e516b6f3ea8eb6e094584267c13483f89dcf194132e12238cc5a34b6b286fc7990d68ed1db86b69ebd826c63b29)" ) .unwrap_err(); @@ -1286,10 +1286,10 @@ mod tests { &self, _pk: &Pk, _h: &TapLeafHash, - ) -> Option { - Some(bitcoin::taproot::Signature { + ) -> Option { + Some(bitcoin_primitives::taproot::Signature { signature: self.0, - sighash_type: bitcoin::sighash::TapSighashType::Default, + sighash_type: bitcoin_primitives::sighash::TapSighashType::Default, }) } } @@ -1312,7 +1312,7 @@ mod tests { .unwrap(); let ms_trans = ms.translate_pk(&mut StrKeyTranslator::new()).unwrap(); let enc = ms_trans.encode(); - let ms = Miniscript::::parse_insane(&enc).unwrap(); + let ms = Miniscript::::parse_insane(&enc).unwrap(); assert_eq!(ms_trans.encode(), ms.encode()); } @@ -1321,7 +1321,7 @@ mod tests { // test that parsing raw hash160 does not work with let hash160_str = "e9f171df53e04b270fa6271b42f66b0f4a99c5a2"; let ms_str = &format!("c:expr_raw_pkh({})", hash160_str); - type SegwitMs = Miniscript; + type SegwitMs = Miniscript; // Test that parsing raw hash160 from string does not work without extra features SegwitMs::from_str(ms_str).unwrap_err(); @@ -1349,7 +1349,7 @@ mod tests { fn translate_tests() { let ms = Miniscript::::from_str("pk(A)").unwrap(); let mut t = StrKeyTranslator::new(); - let uncompressed = bitcoin::PublicKey::from_str("0400232a2acfc9b43fa89f1b4f608fde335d330d7114f70ea42bfb4a41db368a3e3be6934a4097dd25728438ef73debb1f2ffdb07fec0f18049df13bdc5285dc5b").unwrap(); + let uncompressed = bitcoin_primitives::PublicKey::from_str("0400232a2acfc9b43fa89f1b4f608fde335d330d7114f70ea42bfb4a41db368a3e3be6934a4097dd25728438ef73debb1f2ffdb07fec0f18049df13bdc5285dc5b").unwrap(); t.pk_map.insert(String::from("A"), uncompressed); ms.translate_pk(&mut t).unwrap_err(); } @@ -1357,11 +1357,11 @@ mod tests { #[test] fn template_timelocks() { use crate::{AbsLockTime, RelLockTime}; - let key_present = bitcoin::PublicKey::from_str( + let key_present = bitcoin_primitives::PublicKey::from_str( "0327a6ed0e71b451c79327aa9e4a6bb26ffb1c0056abc02c25e783f6096b79bb4f", ) .unwrap(); - let key_missing = bitcoin::PublicKey::from_str( + let key_missing = bitcoin_primitives::PublicKey::from_str( "03e4d788718644a59030b1d234d8bb8fff28314720b9a1a237874b74b089c638da", ) .unwrap(); @@ -1408,35 +1408,35 @@ mod tests { ]; // Test satisfaction code - struct SimpleSatisfier(secp256k1::schnorr::Signature, bitcoin::PublicKey); + struct SimpleSatisfier(secp256k1::schnorr::Signature, bitcoin_primitives::PublicKey); // a simple satisfier that always outputs the same signature - impl Satisfier for SimpleSatisfier { + impl Satisfier for SimpleSatisfier { fn lookup_tap_leaf_script_sig( &self, - pk: &bitcoin::PublicKey, + pk: &bitcoin_primitives::PublicKey, _h: &TapLeafHash, - ) -> Option { + ) -> Option { if pk == &self.1 { - Some(bitcoin::taproot::Signature { + Some(bitcoin_primitives::taproot::Signature { signature: self.0, - sighash_type: bitcoin::sighash::TapSighashType::Default, + sighash_type: bitcoin_primitives::sighash::TapSighashType::Default, }) } else { None } } - fn check_older(&self, _: bitcoin::relative::LockTime) -> bool { true } + fn check_older(&self, _: bitcoin_primitives::relative::LockTime) -> bool { true } - fn check_after(&self, _: bitcoin::absolute::LockTime) -> bool { true } + fn check_after(&self, _: bitcoin_primitives::absolute::LockTime) -> bool { true } } let schnorr_sig = secp256k1::schnorr::Signature::from_str("84526253c27c7aef56c7b71a5cd25bebb66dddda437826defc5b2568bde81f0784526253c27c7aef56c7b71a5cd25bebb66dddda437826defc5b2568bde81f07").unwrap(); let s = SimpleSatisfier(schnorr_sig, key_present); for (ms_str, absolute_timelock, relative_timelock) in test_cases { - let ms = Miniscript::::from_str(&ms_str).unwrap(); + let ms = Miniscript::::from_str(&ms_str).unwrap(); let template = ms.build_template(&s); match template.stack { crate::miniscript::satisfy::Witness::Stack(_) => {} diff --git a/src/miniscript/satisfy.rs b/src/miniscript/satisfy.rs index fcd5cf3aa..29ea53b31 100644 --- a/src/miniscript/satisfy.rs +++ b/src/miniscript/satisfy.rs @@ -8,10 +8,10 @@ use core::{cmp, fmt, mem}; -use bitcoin::hashes::hash160; -use bitcoin::key::XOnlyPublicKey; -use bitcoin::taproot::{ControlBlock, LeafVersion, TapLeafHash, TapNodeHash}; -use bitcoin::{absolute, relative, ScriptBuf, Sequence}; +use bitcoin_primitives::hashes::hash160; +use bitcoin_primitives::key::XOnlyPublicKey; +use bitcoin_primitives::taproot::{ControlBlock, LeafVersion, TapLeafHash, TapNodeHash}; +use bitcoin_primitives::{absolute, relative, ScriptBuf, Sequence}; use sync::Arc; use super::context::SigType; @@ -31,31 +31,31 @@ pub type Preimage32 = [u8; 32]; /// have data for. pub trait Satisfier { /// Given a public key, look up an ECDSA signature with that key - fn lookup_ecdsa_sig(&self, _: &Pk) -> Option { None } + fn lookup_ecdsa_sig(&self, _: &Pk) -> Option { None } /// Lookup the tap key spend sig - fn lookup_tap_key_spend_sig(&self) -> Option { None } + fn lookup_tap_key_spend_sig(&self) -> Option { None } /// Given a public key and a associated leaf hash, look up an schnorr signature with that key fn lookup_tap_leaf_script_sig( &self, _: &Pk, _: &TapLeafHash, - ) -> Option { + ) -> Option { None } /// Obtain a reference to the control block for a ver and script fn lookup_tap_control_block_map( &self, - ) -> Option<&BTreeMap> { + ) -> Option<&BTreeMap> { None } - /// Given a raw `Pkh`, lookup corresponding [`bitcoin::PublicKey`] - fn lookup_raw_pkh_pk(&self, _: &hash160::Hash) -> Option { None } + /// Given a raw `Pkh`, lookup corresponding [`bitcoin_primitives::PublicKey`] + fn lookup_raw_pkh_pk(&self, _: &hash160::Hash) -> Option { None } - /// Given a raw `Pkh`, lookup corresponding [`bitcoin::secp256k1::XOnlyPublicKey`] + /// Given a raw `Pkh`, lookup corresponding [`secp256k1::XOnlyPublicKey`] fn lookup_raw_pkh_x_only_pk(&self, _: &hash160::Hash) -> Option { None } /// Given a keyhash, look up the EC signature and the associated key @@ -65,7 +65,7 @@ pub trait Satisfier { fn lookup_raw_pkh_ecdsa_sig( &self, _: &hash160::Hash, - ) -> Option<(bitcoin::PublicKey, bitcoin::ecdsa::Signature)> { + ) -> Option<(bitcoin_primitives::PublicKey, bitcoin_primitives::ecdsa::Signature)> { None } @@ -76,7 +76,7 @@ pub trait Satisfier { fn lookup_raw_pkh_tap_leaf_script_sig( &self, _: &(hash160::Hash, TapLeafHash), - ) -> Option<(XOnlyPublicKey, bitcoin::taproot::Signature)> { + ) -> Option<(XOnlyPublicKey, bitcoin_primitives::taproot::Signature)> { None } @@ -138,9 +138,9 @@ macro_rules! impl_satisfier_for_map_key_to_ecdsa_sig { ($(#[$($attr:meta)*])* impl Satisfier for $map:ident<$key:ty, $val:ty>) => { $(#[$($attr)*])* impl Satisfier - for $map + for $map { - fn lookup_ecdsa_sig(&self, key: &Pk) -> Option { + fn lookup_ecdsa_sig(&self, key: &Pk) -> Option { self.get(key).copied() } } @@ -148,25 +148,25 @@ macro_rules! impl_satisfier_for_map_key_to_ecdsa_sig { } impl_satisfier_for_map_key_to_ecdsa_sig! { - impl Satisfier for BTreeMap + impl Satisfier for BTreeMap } impl_satisfier_for_map_key_to_ecdsa_sig! { #[cfg(feature = "std")] - impl Satisfier for HashMap + impl Satisfier for HashMap } macro_rules! impl_satisfier_for_map_key_hash_to_taproot_sig { ($(#[$($attr:meta)*])* impl Satisfier for $map:ident<$key:ty, $val:ty>) => { $(#[$($attr)*])* impl Satisfier - for $map<(Pk, TapLeafHash), bitcoin::taproot::Signature> + for $map<(Pk, TapLeafHash), bitcoin_primitives::taproot::Signature> { fn lookup_tap_leaf_script_sig( &self, key: &Pk, h: &TapLeafHash, - ) -> Option { + ) -> Option { // Unfortunately, there is no way to get a &(a, b) from &a and &b without allocating // If we change the signature the of lookup_tap_leaf_script_sig to accept a tuple. We would // face the same problem while satisfying PkK. @@ -178,34 +178,34 @@ macro_rules! impl_satisfier_for_map_key_hash_to_taproot_sig { } impl_satisfier_for_map_key_hash_to_taproot_sig! { - impl Satisfier for BTreeMap<(Pk, TapLeafHash), bitcoin::taproot::Signature> + impl Satisfier for BTreeMap<(Pk, TapLeafHash), bitcoin_primitives::taproot::Signature> } impl_satisfier_for_map_key_hash_to_taproot_sig! { #[cfg(feature = "std")] - impl Satisfier for HashMap<(Pk, TapLeafHash), bitcoin::taproot::Signature> + impl Satisfier for HashMap<(Pk, TapLeafHash), bitcoin_primitives::taproot::Signature> } macro_rules! impl_satisfier_for_map_hash_to_key_ecdsa_sig { ($(#[$($attr:meta)*])* impl Satisfier for $map:ident<$key:ty, $val:ty>) => { $(#[$($attr)*])* impl Satisfier - for $map + for $map where Pk: MiniscriptKey + ToPublicKey, { - fn lookup_ecdsa_sig(&self, key: &Pk) -> Option { + fn lookup_ecdsa_sig(&self, key: &Pk) -> Option { self.get(&key.to_pubkeyhash(SigType::Ecdsa)).map(|x| x.1) } - fn lookup_raw_pkh_pk(&self, pk_hash: &hash160::Hash) -> Option { + fn lookup_raw_pkh_pk(&self, pk_hash: &hash160::Hash) -> Option { self.get(pk_hash).map(|x| x.0.to_public_key()) } fn lookup_raw_pkh_ecdsa_sig( &self, pk_hash: &hash160::Hash, - ) -> Option<(bitcoin::PublicKey, bitcoin::ecdsa::Signature)> { + ) -> Option<(bitcoin_primitives::PublicKey, bitcoin_primitives::ecdsa::Signature)> { self.get(pk_hash) .map(|&(ref pk, sig)| (pk.to_public_key(), sig)) } @@ -214,19 +214,19 @@ macro_rules! impl_satisfier_for_map_hash_to_key_ecdsa_sig { } impl_satisfier_for_map_hash_to_key_ecdsa_sig! { - impl Satisfier for BTreeMap + impl Satisfier for BTreeMap } impl_satisfier_for_map_hash_to_key_ecdsa_sig! { #[cfg(feature = "std")] - impl Satisfier for HashMap + impl Satisfier for HashMap } macro_rules! impl_satisfier_for_map_hash_tapleafhash_to_key_taproot_sig { ($(#[$($attr:meta)*])* impl Satisfier for $map:ident<$key:ty, $val:ty>) => { $(#[$($attr)*])* impl Satisfier - for $map<(hash160::Hash, TapLeafHash), (Pk, bitcoin::taproot::Signature)> + for $map<(hash160::Hash, TapLeafHash), (Pk, bitcoin_primitives::taproot::Signature)> where Pk: MiniscriptKey + ToPublicKey, { @@ -234,7 +234,7 @@ macro_rules! impl_satisfier_for_map_hash_tapleafhash_to_key_taproot_sig { &self, key: &Pk, h: &TapLeafHash, - ) -> Option { + ) -> Option { self.get(&(key.to_pubkeyhash(SigType::Schnorr), *h)) .map(|x| x.1) } @@ -242,7 +242,7 @@ macro_rules! impl_satisfier_for_map_hash_tapleafhash_to_key_taproot_sig { fn lookup_raw_pkh_tap_leaf_script_sig( &self, pk_hash: &(hash160::Hash, TapLeafHash), - ) -> Option<(XOnlyPublicKey, bitcoin::taproot::Signature)> { + ) -> Option<(XOnlyPublicKey, bitcoin_primitives::taproot::Signature)> { self.get(pk_hash) .map(|&(ref pk, sig)| (pk.to_x_only_pubkey(), sig)) } @@ -251,16 +251,16 @@ macro_rules! impl_satisfier_for_map_hash_tapleafhash_to_key_taproot_sig { } impl_satisfier_for_map_hash_tapleafhash_to_key_taproot_sig! { - impl Satisfier for BTreeMap<(hash160::Hash, TapLeafHash), (Pk, bitcoin::taproot::Signature)> + impl Satisfier for BTreeMap<(hash160::Hash, TapLeafHash), (Pk, bitcoin_primitives::taproot::Signature)> } impl_satisfier_for_map_hash_tapleafhash_to_key_taproot_sig! { #[cfg(feature = "std")] - impl Satisfier for HashMap<(hash160::Hash, TapLeafHash), (Pk, bitcoin::taproot::Signature)> + impl Satisfier for HashMap<(hash160::Hash, TapLeafHash), (Pk, bitcoin_primitives::taproot::Signature)> } impl<'a, Pk: MiniscriptKey + ToPublicKey, S: Satisfier> Satisfier for &'a S { - fn lookup_ecdsa_sig(&self, p: &Pk) -> Option { + fn lookup_ecdsa_sig(&self, p: &Pk) -> Option { (**self).lookup_ecdsa_sig(p) } @@ -268,11 +268,11 @@ impl<'a, Pk: MiniscriptKey + ToPublicKey, S: Satisfier> Satisfier for &' &self, p: &Pk, h: &TapLeafHash, - ) -> Option { + ) -> Option { (**self).lookup_tap_leaf_script_sig(p, h) } - fn lookup_raw_pkh_pk(&self, pkh: &hash160::Hash) -> Option { + fn lookup_raw_pkh_pk(&self, pkh: &hash160::Hash) -> Option { (**self).lookup_raw_pkh_pk(pkh) } @@ -283,24 +283,24 @@ impl<'a, Pk: MiniscriptKey + ToPublicKey, S: Satisfier> Satisfier for &' fn lookup_raw_pkh_ecdsa_sig( &self, pkh: &hash160::Hash, - ) -> Option<(bitcoin::PublicKey, bitcoin::ecdsa::Signature)> { + ) -> Option<(bitcoin_primitives::PublicKey, bitcoin_primitives::ecdsa::Signature)> { (**self).lookup_raw_pkh_ecdsa_sig(pkh) } - fn lookup_tap_key_spend_sig(&self) -> Option { + fn lookup_tap_key_spend_sig(&self) -> Option { (**self).lookup_tap_key_spend_sig() } fn lookup_raw_pkh_tap_leaf_script_sig( &self, pkh: &(hash160::Hash, TapLeafHash), - ) -> Option<(XOnlyPublicKey, bitcoin::taproot::Signature)> { + ) -> Option<(XOnlyPublicKey, bitcoin_primitives::taproot::Signature)> { (**self).lookup_raw_pkh_tap_leaf_script_sig(pkh) } fn lookup_tap_control_block_map( &self, - ) -> Option<&BTreeMap> { + ) -> Option<&BTreeMap> { (**self).lookup_tap_control_block_map() } @@ -320,7 +320,7 @@ impl<'a, Pk: MiniscriptKey + ToPublicKey, S: Satisfier> Satisfier for &' } impl<'a, Pk: MiniscriptKey + ToPublicKey, S: Satisfier> Satisfier for &'a mut S { - fn lookup_ecdsa_sig(&self, p: &Pk) -> Option { + fn lookup_ecdsa_sig(&self, p: &Pk) -> Option { (**self).lookup_ecdsa_sig(p) } @@ -328,15 +328,15 @@ impl<'a, Pk: MiniscriptKey + ToPublicKey, S: Satisfier> Satisfier for &' &self, p: &Pk, h: &TapLeafHash, - ) -> Option { + ) -> Option { (**self).lookup_tap_leaf_script_sig(p, h) } - fn lookup_tap_key_spend_sig(&self) -> Option { + fn lookup_tap_key_spend_sig(&self) -> Option { (**self).lookup_tap_key_spend_sig() } - fn lookup_raw_pkh_pk(&self, pkh: &hash160::Hash) -> Option { + fn lookup_raw_pkh_pk(&self, pkh: &hash160::Hash) -> Option { (**self).lookup_raw_pkh_pk(pkh) } @@ -347,20 +347,20 @@ impl<'a, Pk: MiniscriptKey + ToPublicKey, S: Satisfier> Satisfier for &' fn lookup_raw_pkh_ecdsa_sig( &self, pkh: &hash160::Hash, - ) -> Option<(bitcoin::PublicKey, bitcoin::ecdsa::Signature)> { + ) -> Option<(bitcoin_primitives::PublicKey, bitcoin_primitives::ecdsa::Signature)> { (**self).lookup_raw_pkh_ecdsa_sig(pkh) } fn lookup_raw_pkh_tap_leaf_script_sig( &self, pkh: &(hash160::Hash, TapLeafHash), - ) -> Option<(XOnlyPublicKey, bitcoin::taproot::Signature)> { + ) -> Option<(XOnlyPublicKey, bitcoin_primitives::taproot::Signature)> { (**self).lookup_raw_pkh_tap_leaf_script_sig(pkh) } fn lookup_tap_control_block_map( &self, - ) -> Option<&BTreeMap> { + ) -> Option<&BTreeMap> { (**self).lookup_tap_control_block_map() } @@ -387,7 +387,7 @@ macro_rules! impl_tuple_satisfier { Pk: MiniscriptKey + ToPublicKey, $($ty: Satisfier< Pk>,)* { - fn lookup_ecdsa_sig(&self, key: &Pk) -> Option { + fn lookup_ecdsa_sig(&self, key: &Pk) -> Option { let &($(ref $ty,)*) = self; $( if let Some(result) = $ty.lookup_ecdsa_sig(key) { @@ -397,7 +397,7 @@ macro_rules! impl_tuple_satisfier { None } - fn lookup_tap_key_spend_sig(&self) -> Option { + fn lookup_tap_key_spend_sig(&self) -> Option { let &($(ref $ty,)*) = self; $( if let Some(result) = $ty.lookup_tap_key_spend_sig() { @@ -407,7 +407,7 @@ macro_rules! impl_tuple_satisfier { None } - fn lookup_tap_leaf_script_sig(&self, key: &Pk, h: &TapLeafHash) -> Option { + fn lookup_tap_leaf_script_sig(&self, key: &Pk, h: &TapLeafHash) -> Option { let &($(ref $ty,)*) = self; $( if let Some(result) = $ty.lookup_tap_leaf_script_sig(key, h) { @@ -420,7 +420,7 @@ macro_rules! impl_tuple_satisfier { fn lookup_raw_pkh_ecdsa_sig( &self, key_hash: &hash160::Hash, - ) -> Option<(bitcoin::PublicKey, bitcoin::ecdsa::Signature)> { + ) -> Option<(bitcoin_primitives::PublicKey, bitcoin_primitives::ecdsa::Signature)> { let &($(ref $ty,)*) = self; $( if let Some(result) = $ty.lookup_raw_pkh_ecdsa_sig(key_hash) { @@ -433,7 +433,7 @@ macro_rules! impl_tuple_satisfier { fn lookup_raw_pkh_tap_leaf_script_sig( &self, key_hash: &(hash160::Hash, TapLeafHash), - ) -> Option<(XOnlyPublicKey, bitcoin::taproot::Signature)> { + ) -> Option<(XOnlyPublicKey, bitcoin_primitives::taproot::Signature)> { let &($(ref $ty,)*) = self; $( if let Some(result) = $ty.lookup_raw_pkh_tap_leaf_script_sig(key_hash) { @@ -446,7 +446,7 @@ macro_rules! impl_tuple_satisfier { fn lookup_raw_pkh_pk( &self, key_hash: &hash160::Hash, - ) -> Option { + ) -> Option { let &($(ref $ty,)*) = self; $( if let Some(result) = $ty.lookup_raw_pkh_pk(key_hash) { @@ -470,7 +470,7 @@ macro_rules! impl_tuple_satisfier { fn lookup_tap_control_block_map( &self, - ) -> Option<&BTreeMap> { + ) -> Option<&BTreeMap> { let &($(ref $ty,)*) = self; $( if let Some(result) = $ty.lookup_tap_control_block_map() { @@ -633,7 +633,7 @@ impl fmt::Display for Placeholder { TapControlBlock(control_block) => write!( f, "TapControlBlock(control_block: {})", - bitcoin::consensus::encode::serialize_hex(&control_block.serialize()) + bitcoin_primitives::consensus::encode::serialize_hex(&control_block.serialize()) ), } } diff --git a/src/plan.rs b/src/plan.rs index 468b6016b..af4dbff36 100644 --- a/src/plan.rs +++ b/src/plan.rs @@ -16,11 +16,11 @@ use core::iter::FromIterator; -use bitcoin::hashes::{hash160, ripemd160, sha256}; -use bitcoin::key::XOnlyPublicKey; -use bitcoin::script::PushBytesBuf; -use bitcoin::taproot::{ControlBlock, LeafVersion, TapLeafHash}; -use bitcoin::{absolute, bip32, psbt, relative, ScriptBuf, WitnessVersion}; +use bitcoin_primitives::hashes::{hash160, ripemd160, sha256}; +use bitcoin_primitives::key::XOnlyPublicKey; +use bitcoin_primitives::script::PushBytesBuf; +use bitcoin_primitives::taproot::{ControlBlock, LeafVersion, TapLeafHash}; +use bitcoin_primitives::{absolute, relative, ScriptBuf, WitnessVersion}; use crate::descriptor::{self, Descriptor, DescriptorType, KeyMap}; use crate::miniscript::hash256; @@ -54,14 +54,19 @@ pub trait AssetProvider { /// Obtain a reference to the control block for a ver and script fn provider_lookup_tap_control_block_map( &self, - ) -> Option<&BTreeMap> { + ) -> Option<&BTreeMap> { None } - /// Given a raw `Pkh`, lookup corresponding [`bitcoin::PublicKey`] - fn provider_lookup_raw_pkh_pk(&self, _: &hash160::Hash) -> Option { None } + /// Given a raw `Pkh`, lookup corresponding [`bitcoin_primitives::PublicKey`] + fn provider_lookup_raw_pkh_pk( + &self, + _: &hash160::Hash, + ) -> Option { + None + } - /// Given a raw `Pkh`, lookup corresponding [`bitcoin::secp256k1::XOnlyPublicKey`] + /// Given a raw `Pkh`, lookup corresponding [`secp256k1::XOnlyPublicKey`] fn provider_lookup_raw_pkh_x_only_pk(&self, _: &hash160::Hash) -> Option { None } @@ -71,7 +76,10 @@ pub trait AssetProvider { /// Even if signatures for public key Hashes are not available, the users /// can use this map to provide pkh -> pk mapping which can be useful /// for dissatisfying pkh. - fn provider_lookup_raw_pkh_ecdsa_sig(&self, _: &hash160::Hash) -> Option { + fn provider_lookup_raw_pkh_ecdsa_sig( + &self, + _: &hash160::Hash, + ) -> Option { None } @@ -127,10 +135,10 @@ impl AssetProvider for LoggerAssetProvider { impl_log_method!(provider_lookup_ecdsa_sig, pk: &DefiniteDescriptorKey, -> bool); impl_log_method!(provider_lookup_tap_key_spend_sig, pk: &DefiniteDescriptorKey, -> Option); impl_log_method!(provider_lookup_tap_leaf_script_sig, pk: &DefiniteDescriptorKey, leaf_hash: &TapLeafHash, -> Option); - impl_log_method!(provider_lookup_tap_control_block_map, -> Option<&BTreeMap>); - impl_log_method!(provider_lookup_raw_pkh_pk, hash: &hash160::Hash, -> Option); + impl_log_method!(provider_lookup_tap_control_block_map, -> Option<&BTreeMap>); + impl_log_method!(provider_lookup_raw_pkh_pk, hash: &hash160::Hash, -> Option); impl_log_method!(provider_lookup_raw_pkh_x_only_pk, hash: &hash160::Hash, -> Option); - impl_log_method!(provider_lookup_raw_pkh_ecdsa_sig, hash: &hash160::Hash, -> Option); + impl_log_method!(provider_lookup_raw_pkh_ecdsa_sig, hash: &hash160::Hash, -> Option); impl_log_method!(provider_lookup_raw_pkh_tap_leaf_script_sig, hash: &(hash160::Hash, TapLeafHash), -> Option<(XOnlyPublicKey, usize)>); impl_log_method!(provider_lookup_sha256, hash: &sha256::Hash, -> bool); impl_log_method!(provider_lookup_hash256, hash: &hash256::Hash, -> bool); @@ -163,11 +171,14 @@ where fn provider_lookup_tap_control_block_map( &self, - ) -> Option<&BTreeMap> { + ) -> Option<&BTreeMap> { Satisfier::lookup_tap_control_block_map(self) } - fn provider_lookup_raw_pkh_pk(&self, hash: &hash160::Hash) -> Option { + fn provider_lookup_raw_pkh_pk( + &self, + hash: &hash160::Hash, + ) -> Option { Satisfier::lookup_raw_pkh_pk(self, hash) } @@ -178,7 +189,7 @@ where fn provider_lookup_raw_pkh_ecdsa_sig( &self, hash: &hash160::Hash, - ) -> Option { + ) -> Option { Satisfier::lookup_raw_pkh_ecdsa_sig(self, hash).map(|(pk, _)| pk) } @@ -271,7 +282,7 @@ impl Plan { &self, stfr: &Sat, ) -> Result<(Vec>, ScriptBuf), Error> { - use bitcoin::script::Builder; + use bitcoin_primitives::script::Builder; let stack = self .template @@ -311,7 +322,7 @@ impl Plan { /// This will only add the metadata for items required to complete this plan. For example, if /// there are multiple keys present in the descriptor, only the few used by this plan will be /// added to the PSBT. - pub fn update_psbt_input(&self, input: &mut psbt::Input) { + pub fn update_psbt_input(&self, input: &mut psbt_v0::Input) { if let Descriptor::Tr(tr) = &self.descriptor { enum SpendType { KeySpend { internal_key: XOnlyPublicKey }, @@ -734,7 +745,7 @@ impl Assets { mod test { use std::str::FromStr; - use bitcoin::bip32::Xpub; + use bip32::Xpub; use super::*; use crate::*; @@ -866,7 +877,7 @@ mod test { #[test] fn test_thresh() { // relative::LockTime has no constructors except by going through Sequence - use bitcoin::Sequence; + use bitcoin_primitives::Sequence; let keys = vec![ DescriptorPublicKey::from_str( "02c2fd50ceae468857bb7eb32ae9cd4083e6c7e42fbbec179d81134b3e3830586c", @@ -1100,7 +1111,7 @@ mod test { let first_branch = DescriptorPublicKey::from_str(&format!("{}/0/1", xpub)).unwrap(); let second_branch = DescriptorPublicKey::from_str(&format!("{}/1/*", xpub)).unwrap(); // Note this is a wildcard key, so it can sign for the whole multi_a - let mut psbt_input = bitcoin::psbt::Input::default(); + let mut psbt_input = psbt_v0::Input::default(); let assets = Assets::new().add(internal_key); desc.clone() .plan(&assets) @@ -1111,7 +1122,7 @@ mod test { assert_eq!(psbt_input.tap_key_origins.len(), 1, "Unexpected number of tap_key_origins"); assert_eq!(psbt_input.tap_scripts.len(), 0, "Unexpected number of tap_scripts"); - let mut psbt_input = bitcoin::psbt::Input::default(); + let mut psbt_input = psbt_v0::Input::default(); let assets = Assets::new().add(first_branch); desc.clone() .plan(&assets) @@ -1122,7 +1133,7 @@ mod test { assert_eq!(psbt_input.tap_key_origins.len(), 1, "Unexpected number of tap_key_origins"); assert_eq!(psbt_input.tap_scripts.len(), 1, "Unexpected number of tap_scripts"); - let mut psbt_input = bitcoin::psbt::Input::default(); + let mut psbt_input = psbt_v0::Input::default(); let assets = Assets::new().add(second_branch); desc.plan(&assets) .unwrap() @@ -1145,7 +1156,7 @@ mod test { let asset_key = DescriptorPublicKey::from_str(&format!("{}/1/*", xpub)).unwrap(); // Note this is a wildcard key, so it can sign for the whole multi - let mut psbt_input = bitcoin::psbt::Input::default(); + let mut psbt_input = psbt_v0::Input::default(); let assets = Assets::new().add(asset_key); desc.plan(&assets) .unwrap() diff --git a/src/policy/compiler.rs b/src/policy/compiler.rs index db54a297c..179795b2e 100644 --- a/src/policy/compiler.rs +++ b/src/policy/compiler.rs @@ -1200,7 +1200,7 @@ where mod tests { use core::str::FromStr; - use bitcoin::{hashes, opcodes, script}; + use bitcoin_primitives::{hashes, opcodes, script}; use super::*; use crate::miniscript::{Legacy, Segwitv0, Tap}; @@ -1208,11 +1208,13 @@ mod tests { use crate::{script_num_size, AbsLockTime, RelLockTime, Threshold, ToPublicKey}; type SPolicy = Concrete; - type BPolicy = Concrete; + type BPolicy = Concrete; type TapAstElemExt = policy::compiler::AstElemExt; - type SegwitMiniScript = Miniscript; + type SegwitMiniScript = Miniscript; - fn pubkeys_and_a_sig(n: usize) -> (Vec, secp256k1::ecdsa::Signature) { + fn pubkeys_and_a_sig( + n: usize, + ) -> (Vec, secp256k1::ecdsa::Signature) { let mut ret = Vec::with_capacity(n); let secp = secp256k1::Secp256k1::new(); let mut sk = [0; 32]; @@ -1221,7 +1223,7 @@ mod tests { sk[1] = (i >> 8) as u8; sk[2] = (i >> 16) as u8; - let pk = bitcoin::PublicKey { + let pk = bitcoin_primitives::PublicKey { inner: secp256k1::PublicKey::from_secret_key( &secp, &secp256k1::SecretKey::from_slice(&sk[..]).expect("sk"), @@ -1369,7 +1371,7 @@ mod tests { let ms: SegwitMiniScript = policy.compile().unwrap(); - let ms_comp_res: Miniscript = ms_str!( + let ms_comp_res: Miniscript = ms_str!( "or_d(multi(3,{},{},{},{},{}),\ and_v(v:thresh(2,c:pk_h({}),\ ac:pk_h({}),ac:pk_h({})),older(10000)))", @@ -1398,17 +1400,19 @@ mod tests { assert_eq!(abs.n_keys(), 5); assert_eq!(abs.minimum_n_keys(), Some(3)); - let bitcoinsig = bitcoin::ecdsa::Signature { + let bitcoinsig = bitcoin_primitives::ecdsa::Signature { signature, - sighash_type: bitcoin::sighash::EcdsaSighashType::All, + sighash_type: bitcoin_primitives::sighash::EcdsaSighashType::All, }; let sigvec = bitcoinsig.to_vec(); - let no_sat = BTreeMap::::new(); - let mut left_sat = BTreeMap::::new(); + let no_sat = + BTreeMap::::new(); + let mut left_sat = + BTreeMap::::new(); let mut right_sat = BTreeMap::< hashes::hash160::Hash, - (bitcoin::PublicKey, bitcoin::ecdsa::Signature), + (bitcoin_primitives::PublicKey, bitcoin_primitives::ecdsa::Signature), >::new(); for i in 0..5 { @@ -1484,11 +1488,12 @@ mod tests { // and to a ms thresh otherwise. // k = 1 (or 2) does not compile, see https://github.com/rust-bitcoin/rust-miniscript/issues/114 for k in &[10, 15, 21] { - let thresh: Threshold>, 0> = Threshold::from_iter( - *k, - keys.iter().map(|pubkey| Arc::new(Concrete::Key(*pubkey))), - ) - .unwrap(); + let thresh: Threshold>, 0> = + Threshold::from_iter( + *k, + keys.iter().map(|pubkey| Arc::new(Concrete::Key(*pubkey))), + ) + .unwrap(); let big_thresh = Concrete::Thresh(thresh); let big_thresh_ms: SegwitMiniScript = big_thresh.compile().unwrap(); if *k == 21 { @@ -1515,11 +1520,11 @@ mod tests { // or(thresh(52, [pubkey; 52]), thresh(52, [pubkey; 52])) results in a 3642-bytes long // witness script with only 54 stack elements let (keys, _) = pubkeys_and_a_sig(104); - let keys_a: Vec>> = keys[..keys.len() / 2] + let keys_a: Vec>> = keys[..keys.len() / 2] .iter() .map(|pubkey| Arc::new(Concrete::Key(*pubkey))) .collect(); - let keys_b: Vec>> = keys[keys.len() / 2..] + let keys_b: Vec>> = keys[keys.len() / 2..] .iter() .map(|pubkey| Arc::new(Concrete::Key(*pubkey))) .collect(); @@ -1539,7 +1544,7 @@ mod tests { // Hit the maximum witness stack elements limit let (keys, _) = pubkeys_and_a_sig(100); - let keys: Vec>> = keys + let keys: Vec>> = keys .iter() .map(|pubkey| Arc::new(Concrete::Key(*pubkey))) .collect(); diff --git a/src/policy/concrete.rs b/src/policy/concrete.rs index a07bf262a..bf2b39073 100644 --- a/src/policy/concrete.rs +++ b/src/policy/concrete.rs @@ -7,7 +7,7 @@ use core::{fmt, str}; #[cfg(feature = "std")] use std::error; -use bitcoin::absolute; +use bitcoin_primitives::absolute; #[cfg(feature = "compiler")] use { crate::descriptor::TapTree, diff --git a/src/policy/mod.rs b/src/policy/mod.rs index 5c2f83448..1abceef04 100644 --- a/src/policy/mod.rs +++ b/src/policy/mod.rs @@ -343,16 +343,16 @@ mod tests { #[test] fn lift_andor() { - let key_a: bitcoin::PublicKey = + let key_a: bitcoin_primitives::PublicKey = "02d7924d4f7d43ea965a465ae3095ff41131e5946f3c85f79e44adbcf8e27e080e" .parse() .unwrap(); - let key_b: bitcoin::PublicKey = + let key_b: bitcoin_primitives::PublicKey = "03b506a1dbe57b4bf48c95e0c7d417b87dd3b4349d290d2e7e9ba72c912652d80a" .parse() .unwrap(); - let ms_str: Miniscript = + let ms_str: Miniscript = format!("andor(multi(1,{}),older(42),c:pk_k({}))", key_a.inner, key_b.inner) .parse() .unwrap(); diff --git a/src/policy/semantic.rs b/src/policy/semantic.rs index cd7d61002..3da33c168 100644 --- a/src/policy/semantic.rs +++ b/src/policy/semantic.rs @@ -8,7 +8,7 @@ use core::str::FromStr; use core::{fmt, str}; -use bitcoin::{absolute, relative}; +use bitcoin_primitives::{absolute, relative}; use super::concrete::PolicyError; use super::ENTAILMENT_MAX_TERMINALS; @@ -67,33 +67,33 @@ impl Policy { /// ``` /// use std::collections::HashMap; /// use std::str::FromStr; - /// use miniscript::bitcoin::{hashes::hash160, PublicKey}; + /// use miniscript::bitcoin_primitives::{hashes::hash160, PublicKey}; /// use miniscript::{translate_hash_fail, policy::semantic::Policy, Translator}; /// let alice_pk = "02c79ef3ede6d14f72a00d0e49b4becfb152197b64c0707425c4f231df29500ee7"; /// let bob_pk = "03d008a849fbf474bd17e9d2c1a827077a468150e58221582ec3410ab309f5afe4"; /// let placeholder_policy = Policy::::from_str("and(pk(alice_pk),pk(bob_pk))").unwrap(); /// - /// // Information to translate abstract string type keys to concrete `bitcoin::PublicKey`s. + /// // Information to translate abstract string type keys to concrete `bitcoin_primitives::PublicKey`s. /// // In practice, wallets would map from string key names to BIP32 keys. /// struct StrPkTranslator { - /// pk_map: HashMap + /// pk_map: HashMap /// } /// /// // If we also wanted to provide mapping of other associated types (sha256, older etc), /// // we would use the general [`Translator`] trait. - /// impl Translator for StrPkTranslator { - /// fn pk(&mut self, pk: &String) -> Result { + /// impl Translator for StrPkTranslator { + /// fn pk(&mut self, pk: &String) -> Result { /// self.pk_map.get(pk).copied().ok_or(()) // Dummy Err /// } /// /// // Handy macro for failing if we encounter any other fragment. /// // See also [`translate_hash_clone!`] for cloning instead of failing. - /// translate_hash_fail!(String, bitcoin::PublicKey, ()); + /// translate_hash_fail!(String, bitcoin_primitives::PublicKey, ()); /// } /// /// let mut pk_map = HashMap::new(); - /// pk_map.insert(String::from("alice_pk"), bitcoin::PublicKey::from_str(alice_pk).unwrap()); - /// pk_map.insert(String::from("bob_pk"), bitcoin::PublicKey::from_str(bob_pk).unwrap()); + /// pk_map.insert(String::from("alice_pk"), bitcoin_primitives::PublicKey::from_str(alice_pk).unwrap()); + /// pk_map.insert(String::from("bob_pk"), bitcoin_primitives::PublicKey::from_str(bob_pk).unwrap()); /// let mut t = StrPkTranslator { pk_map }; /// /// let real_policy = placeholder_policy.translate_pk(&mut t).unwrap(); @@ -645,7 +645,7 @@ impl TreeLike for Arc> { #[cfg(test)] mod tests { - use bitcoin::PublicKey; + use bitcoin_primitives::PublicKey; use super::*; diff --git a/src/primitives/absolute_locktime.rs b/src/primitives/absolute_locktime.rs index e2373d781..7a265d079 100644 --- a/src/primitives/absolute_locktime.rs +++ b/src/primitives/absolute_locktime.rs @@ -4,7 +4,7 @@ use core::{cmp, fmt}; -use bitcoin::absolute; +use bitcoin_primitives::absolute; /// Maximum allowed absolute locktime value. pub const MAX_ABSOLUTE_LOCKTIME: u32 = 0x8000_0000; diff --git a/src/primitives/relative_locktime.rs b/src/primitives/relative_locktime.rs index 05239a498..3700e19ff 100644 --- a/src/primitives/relative_locktime.rs +++ b/src/primitives/relative_locktime.rs @@ -4,7 +4,7 @@ use core::{cmp, convert, fmt}; -use bitcoin::{relative, Sequence}; +use bitcoin_primitives::{relative, Sequence}; /// Error parsing an absolute locktime. #[derive(Debug, PartialEq)] diff --git a/src/psbt/finalizer.rs b/src/psbt/finalizer.rs index 465b8b572..759fa3da7 100644 --- a/src/psbt/finalizer.rs +++ b/src/psbt/finalizer.rs @@ -11,14 +11,14 @@ use core::convert::TryFrom; use core::mem; -use bitcoin::hashes::hash160; -use bitcoin::key::{CompressedPublicKey, XOnlyPublicKey}; +use bitcoin_primitives::hashes::hash160; +use bitcoin_primitives::key::{CompressedPublicKey, XOnlyPublicKey}; +use bitcoin_primitives::sighash::Prevouts; +use bitcoin_primitives::taproot::LeafVersion; +use bitcoin_primitives::{PublicKey, Script, ScriptBuf, TxOut, Witness}; #[cfg(not(test))] // https://github.com/rust-lang/rust/issues/121684 -use bitcoin::secp256k1; -use bitcoin::secp256k1::Secp256k1; -use bitcoin::sighash::Prevouts; -use bitcoin::taproot::LeafVersion; -use bitcoin::{PublicKey, Script, ScriptBuf, TxOut, Witness}; +use secp256k1; +use secp256k1::Secp256k1; use super::{sanity_check, Error, InputError, Psbt, PsbtInputSatisfier}; use crate::prelude::*; @@ -39,7 +39,7 @@ fn construct_tap_witness( ) -> Result>, InputError> { // When miniscript tries to finalize the PSBT, it doesn't have the full descriptor (which contained a pkh() fragment) // and instead resorts to parsing the raw script sig, which is translated into a "expr_raw_pkh" internally. - let mut map: BTreeMap = BTreeMap::new(); + let mut map: BTreeMap = BTreeMap::new(); let psbt_inputs = &sat.psbt.inputs; for psbt_input in psbt_inputs { // We need to satisfy or dissatisfy any given key. `tap_key_origin` is the only field of PSBT Input which consist of @@ -111,7 +111,10 @@ pub(super) fn get_scriptpubkey(psbt: &Psbt, index: usize) -> Result Result<&bitcoin::TxOut, InputError> { +pub(super) fn get_utxo( + psbt: &Psbt, + index: usize, +) -> Result<&bitcoin_primitives::TxOut, InputError> { let inp = &psbt.inputs[index]; let utxo = if let Some(ref witness_utxo) = inp.witness_utxo { witness_utxo @@ -125,7 +128,7 @@ pub(super) fn get_utxo(psbt: &Psbt, index: usize) -> Result<&bitcoin::TxOut, Inp } /// Get the Prevouts for the psbt -pub(super) fn prevouts(psbt: &Psbt) -> Result, super::Error> { +pub(super) fn prevouts(psbt: &Psbt) -> Result, super::Error> { let mut utxos = vec![]; for i in 0..psbt.inputs.len() { let utxo_ref = get_utxo(psbt, i).map_err(|e| Error::InputError(e, i))?; @@ -149,7 +152,7 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result, In // Use BIP32 Derviation to get set of all possible keys. let public_keys = psbt_input.bip32_derivation.keys(); for key in public_keys { - let bitcoin_key = bitcoin::PublicKey::new(*key); + let bitcoin_key = bitcoin_primitives::PublicKey::new(*key); let hash = bitcoin_key.pubkey_hash().to_raw_hash(); map.insert(hash, bitcoin_key); } @@ -162,7 +165,7 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result, In if script_pubkey.is_p2pk() { let script_pubkey_len = script_pubkey.len(); let pk_bytes = &script_pubkey.to_bytes(); - match bitcoin::PublicKey::from_slice(&pk_bytes[1..script_pubkey_len - 1]) { + match bitcoin_primitives::PublicKey::from_slice(&pk_bytes[1..script_pubkey_len - 1]) { Ok(pk) => Ok(Descriptor::new_pk(pk)), Err(e) => Err(InputError::from(e)), } @@ -175,7 +178,7 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result, In // Partial sigs loses the compressed flag that is necessary // TODO: See https://github.com/rust-bitcoin/rust-bitcoin/pull/836 // The type checker will fail again after we update to 0.28 and this can be removed - let addr = bitcoin::Address::p2pkh(pk, bitcoin::Network::Bitcoin); + let addr = bitcoin_address::Address::p2pkh(pk, bitcoin_primitives::Network::Bitcoin); *script_pubkey == addr.script_pubkey() }); match partial_sig_contains_pk { @@ -189,7 +192,8 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result, In // Indirect way to check the equivalence of pubkey-hashes. // Create a pubkey hash and check if they are the same. - let addr = bitcoin::Address::p2wpkh(pk, bitcoin::Network::Bitcoin); + let addr = + bitcoin_address::Address::p2wpkh(pk, bitcoin_primitives::Network::Bitcoin); *script_pubkey == addr.script_pubkey() }); match partial_sig_contains_pk { @@ -212,7 +216,7 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result, In p2wsh_expected: script_pubkey.clone(), }); } - let ms = Miniscript::::parse_with_ext( + let ms = Miniscript::::parse_with_ext( witness_script, &ExtParams::allow_all(), )?; @@ -247,10 +251,11 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result, In p2wsh_expected: redeem_script.clone(), }); } - let ms = Miniscript::::parse_with_ext( - witness_script, - &ExtParams::allow_all(), - )?; + let ms = + Miniscript::::parse_with_ext( + witness_script, + &ExtParams::allow_all(), + )?; Ok(Descriptor::new_sh_wsh(ms.substitute_raw_pkh(&map))?) } else { Err(InputError::MissingWitnessScript) @@ -260,7 +265,10 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result, In let partial_sig_contains_pk = inp.partial_sigs.iter().find(|&(&pk, _sig)| { let pk = CompressedPublicKey::try_from(pk).expect("compressed key for p2wpkh"); - let addr = bitcoin::Address::p2wpkh(pk, bitcoin::Network::Bitcoin); + let addr = bitcoin_address::Address::p2wpkh( + pk, + bitcoin_primitives::Network::Bitcoin, + ); *redeem_script == addr.script_pubkey() }); match partial_sig_contains_pk { @@ -273,10 +281,11 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result, In return Err(InputError::NonEmptyWitnessScript); } if let Some(ref redeem_script) = inp.redeem_script { - let ms = Miniscript::::parse_with_ext( - redeem_script, - &ExtParams::allow_all(), - )?; + let ms = + Miniscript::::parse_with_ext( + redeem_script, + &ExtParams::allow_all(), + )?; Ok(Descriptor::new_sh(ms)?) } else { Err(InputError::MissingWitnessScript) @@ -292,7 +301,7 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result, In if inp.redeem_script.is_some() { return Err(InputError::NonEmptyRedeemScript); } - let ms = Miniscript::::parse_with_ext( + let ms = Miniscript::::parse_with_ext( &script_pubkey, &ExtParams::allow_all(), )?; @@ -426,7 +435,7 @@ fn finalize_input_helper( } }; - let witness = bitcoin::Witness::from_slice(&witness); + let witness = bitcoin_primitives::Witness::from_slice(&witness); let utxos = prevouts(psbt)?; let utxos = &Prevouts::All(&utxos); interpreter_inp_check(psbt, secp, index, utxos, &witness, &script_sig)?; @@ -466,7 +475,7 @@ pub(super) fn finalize_input( #[cfg(test)] mod tests { - use bitcoin::hashes::hex::FromHex; + use bitcoin_primitives::hex::FromHex; use super::*; use crate::psbt::PsbtExt; diff --git a/src/psbt/mod.rs b/src/psbt/mod.rs index 58b4dd353..a61ed5f26 100644 --- a/src/psbt/mod.rs +++ b/src/psbt/mod.rs @@ -12,14 +12,14 @@ use core::fmt; #[cfg(feature = "std")] use std::error; -use bitcoin::hashes::{hash160, sha256d}; -use bitcoin::psbt::{self, Psbt}; +use bitcoin_primitives::hashes::{hash160, sha256d}; +use bitcoin_primitives::sighash::{self, SighashCache}; +use bitcoin_primitives::taproot::{self, ControlBlock, LeafVersion, TapLeafHash}; +use bitcoin_primitives::{absolute, relative, transaction, Script, ScriptBuf}; +use psbt_v0::Psbt; #[cfg(not(test))] // https://github.com/rust-lang/rust/issues/121684 -use bitcoin::secp256k1; -use bitcoin::secp256k1::{Secp256k1, VerifyOnly}; -use bitcoin::sighash::{self, SighashCache}; -use bitcoin::taproot::{self, ControlBlock, LeafVersion, TapLeafHash}; -use bitcoin::{absolute, bip32, relative, transaction, Script, ScriptBuf}; +use secp256k1; +use secp256k1::{Secp256k1, VerifyOnly}; use crate::miniscript::context::SigType; use crate::prelude::*; @@ -86,9 +86,9 @@ impl error::Error for Error { #[derive(Debug)] pub enum InputError { /// Get the secp Errors directly - SecpErr(bitcoin::secp256k1::Error), + SecpErr(secp256k1::Error), /// Key errors - KeyErr(bitcoin::key::FromSliceError), + KeyErr(bitcoin_primitives::key::FromSliceError), /// Could not satisfy taproot descriptor /// This error is returned when both script path and key paths could not be /// satisfied. We cannot return a detailed error because we try all miniscripts @@ -113,7 +113,7 @@ pub enum InputError { /// Invalid sig InvalidSignature { /// The bitcoin public key - pubkey: bitcoin::PublicKey, + pubkey: bitcoin_primitives::PublicKey, /// The (incorrect) signature sig: Vec, }, @@ -142,7 +142,7 @@ pub enum InputError { /// the sighash type we got got: sighash::EcdsaSighashType, /// the corresponding publickey - pubkey: bitcoin::PublicKey, + pubkey: bitcoin_primitives::PublicKey, }, } @@ -226,13 +226,13 @@ impl From for InputError { } #[doc(hidden)] -impl From for InputError { - fn from(e: bitcoin::secp256k1::Error) -> InputError { InputError::SecpErr(e) } +impl From for InputError { + fn from(e: secp256k1::Error) -> InputError { InputError::SecpErr(e) } } #[doc(hidden)] -impl From for InputError { - fn from(e: bitcoin::key::FromSliceError) -> InputError { InputError::KeyErr(e) } +impl From for InputError { + fn from(e: bitcoin_primitives::key::FromSliceError) -> InputError { InputError::KeyErr(e) } } /// Psbt satisfier for at inputs at a particular index @@ -254,7 +254,7 @@ impl<'psbt> PsbtInputSatisfier<'psbt> { } impl<'psbt, Pk: MiniscriptKey + ToPublicKey> Satisfier for PsbtInputSatisfier<'psbt> { - fn lookup_tap_key_spend_sig(&self) -> Option { + fn lookup_tap_key_spend_sig(&self) -> Option { self.psbt.inputs[self.index].tap_key_sig } @@ -262,31 +262,31 @@ impl<'psbt, Pk: MiniscriptKey + ToPublicKey> Satisfier for PsbtInputSatisfie &self, pk: &Pk, lh: &TapLeafHash, - ) -> Option { + ) -> Option { self.psbt.inputs[self.index] .tap_script_sigs .get(&(pk.to_x_only_pubkey(), *lh)) .copied() } - fn lookup_raw_pkh_pk(&self, pkh: &hash160::Hash) -> Option { + fn lookup_raw_pkh_pk(&self, pkh: &hash160::Hash) -> Option { self.psbt.inputs[self.index] .bip32_derivation .iter() .find(|&(pubkey, _)| pubkey.to_pubkeyhash(SigType::Ecdsa) == *pkh) - .map(|(pubkey, _)| bitcoin::PublicKey::new(*pubkey)) + .map(|(pubkey, _)| bitcoin_primitives::PublicKey::new(*pubkey)) } fn lookup_tap_control_block_map( &self, - ) -> Option<&BTreeMap> { + ) -> Option<&BTreeMap> { Some(&self.psbt.inputs[self.index].tap_scripts) } fn lookup_raw_pkh_tap_leaf_script_sig( &self, pkh: &(hash160::Hash, TapLeafHash), - ) -> Option<(bitcoin::secp256k1::XOnlyPublicKey, bitcoin::taproot::Signature)> { + ) -> Option<(secp256k1::XOnlyPublicKey, bitcoin_primitives::taproot::Signature)> { self.psbt.inputs[self.index] .tap_script_sigs .iter() @@ -296,7 +296,7 @@ impl<'psbt, Pk: MiniscriptKey + ToPublicKey> Satisfier for PsbtInputSatisfie .map(|((x_only_pk, _leaf_hash), sig)| (*x_only_pk, *sig)) } - fn lookup_ecdsa_sig(&self, pk: &Pk) -> Option { + fn lookup_ecdsa_sig(&self, pk: &Pk) -> Option { self.psbt.inputs[self.index] .partial_sigs .get(&pk.to_public_key()) @@ -306,7 +306,7 @@ impl<'psbt, Pk: MiniscriptKey + ToPublicKey> Satisfier for PsbtInputSatisfie fn lookup_raw_pkh_ecdsa_sig( &self, pkh: &hash160::Hash, - ) -> Option<(bitcoin::PublicKey, bitcoin::ecdsa::Signature)> { + ) -> Option<(bitcoin_primitives::PublicKey, bitcoin_primitives::ecdsa::Signature)> { self.psbt.inputs[self.index] .partial_sigs .iter() @@ -510,14 +510,14 @@ pub trait PsbtExt { ) -> Result; /// Psbt extractor as defined in BIP174 that takes in a psbt reference - /// and outputs a extracted bitcoin::Transaction + /// and outputs a extracted bitcoin_primitives::Transaction /// Also does the interpreter sanity check /// Will error if the final ScriptSig or final Witness are missing /// or the interpreter check fails. fn extract( &self, secp: &Secp256k1, - ) -> Result; + ) -> Result; /// Update PSBT input with a descriptor and check consistency of `*_utxo` fields. /// @@ -563,7 +563,7 @@ pub trait PsbtExt { /// Based on the sighash /// flag specified in the [`Psbt`] sighash field. If the input sighash flag psbt field is `None` /// the [`sighash::TapSighashType::Default`] is chosen - /// for for taproot spends, otherwise [`EcdsaSighashType::All`](bitcoin::sighash::EcdsaSighashType::All) is chosen. + /// for for taproot spends, otherwise [`EcdsaSighashType::All`](bitcoin_primitives::sighash::EcdsaSighashType::All) is chosen. /// If the utxo at `idx` is a taproot output, returns a [`PsbtSighashMsg::TapSighash`] variant. /// If the utxo at `idx` is a pre-taproot segwit output, returns a [`PsbtSighashMsg::SegwitV0Sighash`] variant. /// For legacy outputs, returns a [`PsbtSighashMsg::LegacySighash`] variant. @@ -578,8 +578,8 @@ pub trait PsbtExt { /// * `cache`: The [`SighashCache`] for used to cache/read previously cached computations /// * `tapleaf_hash`: If the output is taproot, compute the sighash for this particular leaf. /// - /// [`SighashCache`]: bitcoin::sighash::SighashCache - fn sighash_msg>( + /// [`SighashCache`]: bitcoin_primitives::sighash::SighashCache + fn sighash_msg>( &self, idx: usize, cache: &mut SighashCache, @@ -696,7 +696,7 @@ impl PsbtExt for Psbt { fn extract( &self, secp: &Secp256k1, - ) -> Result { + ) -> Result { sanity_check(self)?; let mut ret = self.unsigned_tx.clone(); @@ -809,7 +809,7 @@ impl PsbtExt for Psbt { Ok(()) } - fn sighash_msg>( + fn sighash_msg>( &self, idx: usize, cache: &mut SighashCache, @@ -823,7 +823,7 @@ impl PsbtExt for Psbt { let prevouts = finalizer::prevouts(self).map_err(|_e| SighashError::MissingSpendUtxos)?; // Note that as per Psbt spec we should have access to spent_utxos for the transaction // Even if the transaction does not require SighashAll, we create `Prevouts::All` for code simplicity - let prevouts = bitcoin::sighash::Prevouts::All(&prevouts); + let prevouts = bitcoin_primitives::sighash::Prevouts::All(&prevouts); let inp_spk = finalizer::get_scriptpubkey(self, idx).map_err(|_e| SighashError::MissingInputUtxo)?; if inp_spk.is_p2tr() { @@ -919,14 +919,14 @@ pub trait PsbtInputExt { fn update_with_descriptor_unchecked( &mut self, descriptor: &Descriptor, - ) -> Result, descriptor::ConversionError>; + ) -> Result, descriptor::ConversionError>; } -impl PsbtInputExt for psbt::Input { +impl PsbtInputExt for psbt_v0::Input { fn update_with_descriptor_unchecked( &mut self, descriptor: &Descriptor, - ) -> Result, descriptor::ConversionError> { + ) -> Result, descriptor::ConversionError> { let (derived, _) = update_item_with_descriptor_helper(self, descriptor, None)?; Ok(derived) } @@ -953,14 +953,14 @@ pub trait PsbtOutputExt { fn update_with_descriptor_unchecked( &mut self, descriptor: &Descriptor, - ) -> Result, descriptor::ConversionError>; + ) -> Result, descriptor::ConversionError>; } -impl PsbtOutputExt for psbt::Output { +impl PsbtOutputExt for psbt_v0::Output { fn update_with_descriptor_unchecked( &mut self, descriptor: &Descriptor, - ) -> Result, descriptor::ConversionError> { + ) -> Result, descriptor::ConversionError> { let (derived, _) = update_item_with_descriptor_helper(self, descriptor, None)?; Ok(derived) } @@ -973,13 +973,13 @@ struct KeySourceLookUp( pub secp256k1::Secp256k1, ); -impl Translator +impl Translator for KeySourceLookUp { fn pk( &mut self, xpk: &DefiniteDescriptorKey, - ) -> Result { + ) -> Result { let derived = xpk.derive_public_key(&self.1)?; self.0.insert( derived.to_public_key().inner, @@ -992,7 +992,11 @@ impl Translator &mut Option; fn witness_script(&mut self) -> &mut Option; fn bip32_derivation(&mut self) -> &mut BTreeMap; - fn tap_internal_key(&mut self) -> &mut Option; + fn tap_internal_key(&mut self) -> &mut Option; fn tap_key_origins( &mut self, - ) -> &mut BTreeMap, bip32::KeySource)>; + ) -> &mut BTreeMap, bip32::KeySource)>; #[allow(dead_code)] - fn proprietary(&mut self) -> &mut BTreeMap>; + fn proprietary(&mut self) -> &mut BTreeMap>; #[allow(dead_code)] - fn unknown(&mut self) -> &mut BTreeMap>; + fn unknown(&mut self) -> &mut BTreeMap>; - // `tap_tree` only appears in psbt::Output, so it's returned as an option of a mutable ref + // `tap_tree` only appears in psbt_v0::Output, so it's returned as an option of a mutable ref fn tap_tree(&mut self) -> Option<&mut Option> { None } - // `tap_scripts` and `tap_merkle_root` only appear in psbt::Input + // `tap_scripts` and `tap_merkle_root` only appear in psbt_v0::Input fn tap_scripts(&mut self) -> Option<&mut BTreeMap> { None } fn tap_merkle_root(&mut self) -> Option<&mut Option> { None } } -impl PsbtFields for psbt::Input { +impl PsbtFields for psbt_v0::Input { fn redeem_script(&mut self) -> &mut Option { &mut self.redeem_script } fn witness_script(&mut self) -> &mut Option { &mut self.witness_script } fn bip32_derivation(&mut self) -> &mut BTreeMap { &mut self.bip32_derivation } - fn tap_internal_key(&mut self) -> &mut Option { + fn tap_internal_key(&mut self) -> &mut Option { &mut self.tap_internal_key } fn tap_key_origins( &mut self, - ) -> &mut BTreeMap, bip32::KeySource)> { + ) -> &mut BTreeMap, bip32::KeySource)> + { &mut self.tap_key_origins } #[allow(dead_code)] - fn proprietary(&mut self) -> &mut BTreeMap> { + fn proprietary(&mut self) -> &mut BTreeMap> { &mut self.proprietary } #[allow(dead_code)] - fn unknown(&mut self) -> &mut BTreeMap> { &mut self.unknown } + fn unknown(&mut self) -> &mut BTreeMap> { &mut self.unknown } fn tap_scripts(&mut self) -> Option<&mut BTreeMap> { Some(&mut self.tap_scripts) @@ -1049,26 +1054,27 @@ impl PsbtFields for psbt::Input { } } -impl PsbtFields for psbt::Output { +impl PsbtFields for psbt_v0::Output { fn redeem_script(&mut self) -> &mut Option { &mut self.redeem_script } fn witness_script(&mut self) -> &mut Option { &mut self.witness_script } fn bip32_derivation(&mut self) -> &mut BTreeMap { &mut self.bip32_derivation } - fn tap_internal_key(&mut self) -> &mut Option { + fn tap_internal_key(&mut self) -> &mut Option { &mut self.tap_internal_key } fn tap_key_origins( &mut self, - ) -> &mut BTreeMap, bip32::KeySource)> { + ) -> &mut BTreeMap, bip32::KeySource)> + { &mut self.tap_key_origins } #[allow(dead_code)] - fn proprietary(&mut self) -> &mut BTreeMap> { + fn proprietary(&mut self) -> &mut BTreeMap> { &mut self.proprietary } #[allow(dead_code)] - fn unknown(&mut self) -> &mut BTreeMap> { &mut self.unknown } + fn unknown(&mut self) -> &mut BTreeMap> { &mut self.unknown } fn tap_tree(&mut self) -> Option<&mut Option> { Some(&mut self.tap_tree) } } @@ -1080,7 +1086,7 @@ fn update_item_with_descriptor_helper( // the return value is a tuple here since the two internal calls to it require different info. // One needs the derived descriptor and the other needs to know whether the script_pubkey check // failed. -) -> Result<(Descriptor, bool), descriptor::ConversionError> { +) -> Result<(Descriptor, bool), descriptor::ConversionError> { let secp = secp256k1::Secp256k1::verification_only(); let derived = if let Descriptor::Tr(_) = &descriptor { @@ -1409,22 +1415,21 @@ impl PsbtSighashMsg { mod tests { use std::str::FromStr; - use bitcoin::bip32::{DerivationPath, Xpub}; - use bitcoin::consensus::encode::deserialize; - use bitcoin::hashes::hex::FromHex; - use bitcoin::key::XOnlyPublicKey; - use bitcoin::secp256k1::PublicKey; - use bitcoin::{Amount, OutPoint, TxIn, TxOut}; + use bip32::{DerivationPath, Xpub}; + use bitcoin_primitives::consensus::encode::deserialize; + use bitcoin_primitives::hex::FromHex; + use bitcoin_primitives::{Amount, OutPoint, TxIn, TxOut, XOnlyPublicKey}; + use secp256k1::PublicKey; use super::*; use crate::Miniscript; #[test] fn test_extract_bip174() { - let psbt = bitcoin::Psbt::deserialize(&Vec::::from_hex("70736274ff01009a020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f00000000000100bb0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f6187650000000107da00473044022074018ad4180097b873323c0015720b3684cc8123891048e7dbcd9b55ad679c99022073d369b740e3eb53dcefa33823c8070514ca55a7dd9544f157c167913261118c01483045022100f61038b308dc1da865a34852746f015772934208c6d24454393cd99bdf2217770220056e675a675a6d0a02b85b14e5e29074d8a25a9b5760bea2816f661910a006ea01475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae0001012000c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e8870107232200208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b20289030108da0400473044022062eb7a556107a7c73f45ac4ab5a1dddf6f7075fb1275969a7f383efff784bcb202200c05dbb7470dbf2f08557dd356c7325c1ed30913e996cd3840945db12228da5f01473044022065f45ba5998b59a27ffe1a7bed016af1f1f90d54b3aa8f7450aa5f56a25103bd02207f724703ad1edb96680b284b56d4ffcb88f7fb759eabbe08aa30f29b851383d20147522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae00220203a9a4c37f5996d3aa25dbac6b570af0650394492942460b354753ed9eeca5877110d90c6a4f000000800000008004000080002202027f6399757d2eff55a136ad02c684b1838b6556e5f1b6b34282a94b6b5005109610d90c6a4f00000080000000800500008000").unwrap()).unwrap(); + let psbt = Psbt::deserialize(&Vec::::from_hex("70736274ff01009a020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f00000000000100bb0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f6187650000000107da00473044022074018ad4180097b873323c0015720b3684cc8123891048e7dbcd9b55ad679c99022073d369b740e3eb53dcefa33823c8070514ca55a7dd9544f157c167913261118c01483045022100f61038b308dc1da865a34852746f015772934208c6d24454393cd99bdf2217770220056e675a675a6d0a02b85b14e5e29074d8a25a9b5760bea2816f661910a006ea01475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae0001012000c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e8870107232200208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b20289030108da0400473044022062eb7a556107a7c73f45ac4ab5a1dddf6f7075fb1275969a7f383efff784bcb202200c05dbb7470dbf2f08557dd356c7325c1ed30913e996cd3840945db12228da5f01473044022065f45ba5998b59a27ffe1a7bed016af1f1f90d54b3aa8f7450aa5f56a25103bd02207f724703ad1edb96680b284b56d4ffcb88f7fb759eabbe08aa30f29b851383d20147522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae00220203a9a4c37f5996d3aa25dbac6b570af0650394492942460b354753ed9eeca5877110d90c6a4f000000800000008004000080002202027f6399757d2eff55a136ad02c684b1838b6556e5f1b6b34282a94b6b5005109610d90c6a4f00000080000000800500008000").unwrap()).unwrap(); let secp = Secp256k1::verification_only(); let tx = psbt.extract(&secp).unwrap(); - let expected: bitcoin::Transaction = deserialize(&Vec::::from_hex("0200000000010258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd7500000000da00473044022074018ad4180097b873323c0015720b3684cc8123891048e7dbcd9b55ad679c99022073d369b740e3eb53dcefa33823c8070514ca55a7dd9544f157c167913261118c01483045022100f61038b308dc1da865a34852746f015772934208c6d24454393cd99bdf2217770220056e675a675a6d0a02b85b14e5e29074d8a25a9b5760bea2816f661910a006ea01475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752aeffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d01000000232200208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f000400473044022062eb7a556107a7c73f45ac4ab5a1dddf6f7075fb1275969a7f383efff784bcb202200c05dbb7470dbf2f08557dd356c7325c1ed30913e996cd3840945db12228da5f01473044022065f45ba5998b59a27ffe1a7bed016af1f1f90d54b3aa8f7450aa5f56a25103bd02207f724703ad1edb96680b284b56d4ffcb88f7fb759eabbe08aa30f29b851383d20147522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae00000000").unwrap()).unwrap(); + let expected: bitcoin_primitives::Transaction = deserialize(&Vec::::from_hex("0200000000010258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd7500000000da00473044022074018ad4180097b873323c0015720b3684cc8123891048e7dbcd9b55ad679c99022073d369b740e3eb53dcefa33823c8070514ca55a7dd9544f157c167913261118c01483045022100f61038b308dc1da865a34852746f015772934208c6d24454393cd99bdf2217770220056e675a675a6d0a02b85b14e5e29074d8a25a9b5760bea2816f661910a006ea01475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752aeffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d01000000232200208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f000400473044022062eb7a556107a7c73f45ac4ab5a1dddf6f7075fb1275969a7f383efff784bcb202200c05dbb7470dbf2f08557dd356c7325c1ed30913e996cd3840945db12228da5f01473044022065f45ba5998b59a27ffe1a7bed016af1f1f90d54b3aa8f7450aa5f56a25103bd02207f724703ad1edb96680b284b56d4ffcb88f7fb759eabbe08aa30f29b851383d20147522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae00000000").unwrap()).unwrap(); assert_eq!(tx, expected); } @@ -1435,9 +1440,9 @@ mod tests { let fingerprint = root_xpub.fingerprint(); let desc = format!("tr([{}/86'/0'/0']xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/0)", fingerprint); let desc = Descriptor::from_str(&desc).unwrap(); - let mut psbt_input = psbt::Input::default(); + let mut psbt_input = psbt_v0::Input::default(); psbt_input.update_with_descriptor_unchecked(&desc).unwrap(); - let mut psbt_output = psbt::Output::default(); + let mut psbt_output = psbt_v0::Output::default(); psbt_output.update_with_descriptor_unchecked(&desc).unwrap(); let internal_key = XOnlyPublicKey::from_str( "cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115", @@ -1472,9 +1477,9 @@ mod tests { "cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115", ) .unwrap(); - let mut psbt_input = psbt::Input::default(); + let mut psbt_input = psbt_v0::Input::default(); psbt_input.update_with_descriptor_unchecked(&desc).unwrap(); - let mut psbt_output = psbt::Output::default(); + let mut psbt_output = psbt_v0::Output::default(); psbt_output.update_with_descriptor_unchecked(&desc).unwrap(); assert_eq!(psbt_input.tap_internal_key, Some(internal_key)); assert_eq!( @@ -1560,12 +1565,12 @@ mod tests { let desc = format!("wsh(multi(2,{}/0/0,{}/0/1,{}/1/0))", xpub, xpub, xpub); let desc = Descriptor::from_str(&desc).unwrap(); let derived = format!("wsh(multi(2,{}))", pubkeys.join(",")); - let derived = Descriptor::::from_str(&derived).unwrap(); + let derived = Descriptor::::from_str(&derived).unwrap(); - let mut psbt_input = psbt::Input::default(); + let mut psbt_input = psbt_v0::Input::default(); psbt_input.update_with_descriptor_unchecked(&desc).unwrap(); - let mut psbt_output = psbt::Output::default(); + let mut psbt_output = psbt_v0::Output::default(); psbt_output.update_with_descriptor_unchecked(&desc).unwrap(); assert_eq!(expected_bip32, psbt_input.bip32_derivation); @@ -1580,12 +1585,12 @@ mod tests { let desc = format!("sh(multi(2,{}/0/0,{}/0/1,{}/1/0))", xpub, xpub, xpub); let desc = Descriptor::from_str(&desc).unwrap(); let derived = format!("sh(multi(2,{}))", pubkeys.join(",")); - let derived = Descriptor::::from_str(&derived).unwrap(); + let derived = Descriptor::::from_str(&derived).unwrap(); - let mut psbt_input = psbt::Input::default(); + let mut psbt_input = psbt_v0::Input::default(); psbt_input.update_with_descriptor_unchecked(&desc).unwrap(); - let mut psbt_output = psbt::Output::default(); + let mut psbt_output = psbt_v0::Output::default(); psbt_output.update_with_descriptor_unchecked(&desc).unwrap(); assert_eq!(psbt_input.bip32_derivation, expected_bip32); @@ -1603,7 +1608,7 @@ mod tests { let desc = "tr([73c5da0a/86'/0'/0']xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/0)"; let desc = Descriptor::::from_str(desc).unwrap(); - let mut non_witness_utxo = bitcoin::Transaction { + let mut non_witness_utxo = bitcoin_primitives::Transaction { version: transaction::Version::ONE, lock_time: absolute::LockTime::ZERO, input: vec![], @@ -1616,7 +1621,7 @@ mod tests { }], }; - let tx = bitcoin::Transaction { + let tx = bitcoin_primitives::Transaction { version: transaction::Version::ONE, lock_time: absolute::LockTime::ZERO, input: vec![TxIn { @@ -1665,7 +1670,7 @@ mod tests { let desc = "tr([73c5da0a/86'/0'/0']xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/0)"; let desc = Descriptor::::from_str(desc).unwrap(); - let tx = bitcoin::Transaction { + let tx = bitcoin_primitives::Transaction { version: transaction::Version::ONE, lock_time: absolute::LockTime::ZERO, input: vec![], diff --git a/src/pub_macros.rs b/src/pub_macros.rs index bdb0d59b4..efa29688c 100644 --- a/src/pub_macros.rs +++ b/src/pub_macros.rs @@ -10,36 +10,36 @@ /// This macro is handy when dealing with scripts that are only contain keys. /// See also [`crate::translate_hash_clone`] /// ```rust -/// use miniscript::{bitcoin::PublicKey, policy::concrete::Policy, Translator, hash256}; +/// use miniscript::{bitcoin_primitives::PublicKey, policy::concrete::Policy, Translator, hash256}; /// use std::str::FromStr; /// use miniscript::translate_hash_fail; /// use std::collections::HashMap; -/// use miniscript::bitcoin::hashes::{sha256, hash160, ripemd160}; +/// use miniscript::bitcoin_primitives::hashes::{sha256, hash160, ripemd160}; /// let alice_key = "0270cf3c71f65a3d93d285d9149fddeeb638f87a2d4d8cf16c525f71c417439777"; /// let bob_key = "02f43b15c50a436f5335dbea8a64dd3b4e63e34c3b50c42598acb5f4f336b5d2fb"; /// let placeholder_policy = Policy::::from_str("and(pk(alice_key),pk(bob_key))").unwrap(); /// -/// // Information to translator abstract String type keys to concrete bitcoin::PublicKey. +/// // Information to translator abstract String type keys to concrete bitcoin_primitives::PublicKey. /// // In practice, wallets would map from String key names to BIP32 keys /// struct StrPkTranslator { -/// pk_map: HashMap +/// pk_map: HashMap /// } /// /// // If we also wanted to provide mapping of other associated types(sha256, older etc), /// // we would use the general Translator Trait. -/// impl Translator for StrPkTranslator { +/// impl Translator for StrPkTranslator { /// // Provides the translation public keys P -> Q -/// fn pk(&mut self, pk: &String) -> Result { +/// fn pk(&mut self, pk: &String) -> Result { /// self.pk_map.get(pk).copied().ok_or(()) // Dummy Err /// } /// /// // Fail for hash types -/// translate_hash_fail!(String, bitcoin::PublicKey, ()); +/// translate_hash_fail!(String, bitcoin_primitives::PublicKey, ()); /// } /// /// let mut pk_map = HashMap::new(); -/// pk_map.insert(String::from("alice_key"), bitcoin::PublicKey::from_str(alice_key).unwrap()); -/// pk_map.insert(String::from("bob_key"), bitcoin::PublicKey::from_str(bob_key).unwrap()); +/// pk_map.insert(String::from("alice_key"), bitcoin_primitives::PublicKey::from_str(alice_key).unwrap()); +/// pk_map.insert(String::from("bob_key"), bitcoin_primitives::PublicKey::from_str(bob_key).unwrap()); /// let mut t = StrPkTranslator { pk_map: pk_map }; /// ``` #[macro_export] diff --git a/src/test_utils.rs b/src/test_utils.rs index 170e3c7f6..f102c0c5f 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -5,30 +5,28 @@ use std::collections::HashMap; use std::str::FromStr; -use bitcoin::hashes::{hash160, ripemd160, sha256}; -use bitcoin::key::XOnlyPublicKey; -#[cfg(not(test))] // https://github.com/rust-lang/rust/issues/121684 -use bitcoin::secp256k1; +use bitcoin_primitives::hashes::{hash160, ripemd160, sha256}; +use bitcoin_primitives::XOnlyPublicKey; use crate::miniscript::context::SigType; use crate::{hash256, ToPublicKey, Translator}; -/// Translate from a String MiniscriptKey type to bitcoin::PublicKey +/// Translate from a String MiniscriptKey type to bitcoin_primitives::PublicKey /// If the hashmap is populated, this will lookup for keys in HashMap /// Otherwise, this will return a translation to a random key #[derive(Debug, PartialEq, Eq, Clone)] pub struct StrKeyTranslator { - pub pk_map: HashMap, + pub pk_map: HashMap, pub pkh_map: HashMap, pub sha256_map: HashMap, pub ripemd160_map: HashMap, pub hash160_map: HashMap, } -impl Translator for StrKeyTranslator { - fn pk(&mut self, pk: &String) -> Result { +impl Translator for StrKeyTranslator { + fn pk(&mut self, pk: &String) -> Result { let key = self.pk_map.get(pk).copied().unwrap_or_else(|| { - bitcoin::PublicKey::from_str( + bitcoin_primitives::PublicKey::from_str( "02c2122e30e73f7fe37986e3f81ded00158e94b7ad472369b83bbdd28a9a198a39", ) .unwrap() @@ -64,7 +62,7 @@ impl Translator for StrKeyTranslator { } } -/// Same as [`StrKeyTranslator`], but for [`bitcoin::XOnlyPublicKey`] +/// Same as [`StrKeyTranslator`], but for [`bitcoin_primitives::XOnlyPublicKey`] #[derive(Debug, PartialEq, Eq, Clone)] pub struct StrXOnlyKeyTranslator { pub pk_map: HashMap, @@ -134,7 +132,9 @@ impl StrKeyTranslator { let sks = random_sks(26); let pks: Vec<_> = sks .iter() - .map(|sk| bitcoin::PublicKey::new(secp256k1::PublicKey::from_secret_key(&secp, sk))) + .map(|sk| { + bitcoin_primitives::PublicKey::new(secp256k1::PublicKey::from_secret_key(&secp, sk)) + }) .collect(); let mut pk_map = HashMap::new(); let mut pkh_map = HashMap::new(); diff --git a/src/util.rs b/src/util.rs index 5d6985dde..cb9c3e139 100644 --- a/src/util.rs +++ b/src/util.rs @@ -2,14 +2,14 @@ use core::convert::TryFrom; -use bitcoin::script::{self, PushBytes, ScriptBuf}; -use bitcoin::PubkeyHash; +use bitcoin_primitives::script::{self, PushBytes, ScriptBuf}; +use bitcoin_primitives::PubkeyHash; use crate::miniscript::context; use crate::miniscript::satisfy::Placeholder; use crate::prelude::*; use crate::{MiniscriptKey, ScriptContext, ToPublicKey}; -pub(crate) fn varint_len(n: usize) -> usize { bitcoin::VarInt(n as u64).size() } +pub(crate) fn varint_len(n: usize) -> usize { bitcoin_primitives::VarInt(n as u64).size() } pub(crate) trait ItemSize { fn size(&self) -> usize; diff --git a/tests/bip-174.rs b/tests/bip-174.rs index a8b7e22f4..88304f3e0 100644 --- a/tests/bip-174.rs +++ b/tests/bip-174.rs @@ -1,9 +1,9 @@ // SPDX-License-Identifier: CC0-1.0 -use bitcoin::consensus::encode::deserialize; -use bitcoin::hashes::hex::FromHex; -use bitcoin::psbt::Psbt; +use bitcoin_primitives::consensus::encode::deserialize; +use bitcoin_primitives::hex::FromHex; use miniscript::psbt::PsbtExt; +use psbt_v0::Psbt; fn main() { // Test vectors from BIP 174 @@ -14,7 +14,7 @@ fn main() { let expected_finalized_psbt = Psbt::deserialize(&Vec::::from_hex("70736274ff01009a020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f00000000000100bb0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f6187650000000107da00473044022074018ad4180097b873323c0015720b3684cc8123891048e7dbcd9b55ad679c99022073d369b740e3eb53dcefa33823c8070514ca55a7dd9544f157c167913261118c01483045022100f61038b308dc1da865a34852746f015772934208c6d24454393cd99bdf2217770220056e675a675a6d0a02b85b14e5e29074d8a25a9b5760bea2816f661910a006ea01475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae0001012000c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e8870107232200208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b20289030108da0400473044022062eb7a556107a7c73f45ac4ab5a1dddf6f7075fb1275969a7f383efff784bcb202200c05dbb7470dbf2f08557dd356c7325c1ed30913e996cd3840945db12228da5f01473044022065f45ba5998b59a27ffe1a7bed016af1f1f90d54b3aa8f7450aa5f56a25103bd02207f724703ad1edb96680b284b56d4ffcb88f7fb759eabbe08aa30f29b851383d20147522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae00220203a9a4c37f5996d3aa25dbac6b570af0650394492942460b354753ed9eeca5877110d90c6a4f000000800000008004000080002202027f6399757d2eff55a136ad02c684b1838b6556e5f1b6b34282a94b6b5005109610d90c6a4f00000080000000800500008000").unwrap()).unwrap(); // Construct a secp context for transaction signature verification - let secp = bitcoin::secp256k1::Secp256k1::verification_only(); + let secp = secp256k1::Secp256k1::verification_only(); // Assuming all partial sigs are filled in. // Construct a generic finalizer psbt.finalize_mut(&secp).unwrap(); @@ -25,7 +25,7 @@ fn main() { // Extract the transaction from the psbt let tx = psbt.extract(&secp).unwrap(); - let expected: bitcoin::Transaction = deserialize(&Vec::::from_hex("0200000000010258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd7500000000da00473044022074018ad4180097b873323c0015720b3684cc8123891048e7dbcd9b55ad679c99022073d369b740e3eb53dcefa33823c8070514ca55a7dd9544f157c167913261118c01483045022100f61038b308dc1da865a34852746f015772934208c6d24454393cd99bdf2217770220056e675a675a6d0a02b85b14e5e29074d8a25a9b5760bea2816f661910a006ea01475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752aeffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d01000000232200208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f000400473044022062eb7a556107a7c73f45ac4ab5a1dddf6f7075fb1275969a7f383efff784bcb202200c05dbb7470dbf2f08557dd356c7325c1ed30913e996cd3840945db12228da5f01473044022065f45ba5998b59a27ffe1a7bed016af1f1f90d54b3aa8f7450aa5f56a25103bd02207f724703ad1edb96680b284b56d4ffcb88f7fb759eabbe08aa30f29b851383d20147522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae00000000").unwrap()).unwrap(); + let expected: bitcoin_primitives::Transaction = deserialize(&Vec::::from_hex("0200000000010258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd7500000000da00473044022074018ad4180097b873323c0015720b3684cc8123891048e7dbcd9b55ad679c99022073d369b740e3eb53dcefa33823c8070514ca55a7dd9544f157c167913261118c01483045022100f61038b308dc1da865a34852746f015772934208c6d24454393cd99bdf2217770220056e675a675a6d0a02b85b14e5e29074d8a25a9b5760bea2816f661910a006ea01475221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752aeffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d01000000232200208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f000400473044022062eb7a556107a7c73f45ac4ab5a1dddf6f7075fb1275969a7f383efff784bcb202200c05dbb7470dbf2f08557dd356c7325c1ed30913e996cd3840945db12228da5f01473044022065f45ba5998b59a27ffe1a7bed016af1f1f90d54b3aa8f7450aa5f56a25103bd02207f724703ad1edb96680b284b56d4ffcb88f7fb759eabbe08aa30f29b851383d20147522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e7352ae00000000").unwrap()).unwrap(); // println!("{:?}", tx); assert_eq!(tx, expected); }