From 5e3fd34d5dc3051057ea3d3d1dd2126a83c7c5ae Mon Sep 17 00:00:00 2001 From: Hunter Trujillo Date: Sun, 14 Jan 2024 10:38:02 -0700 Subject: [PATCH] Add segwit key derivation as a feature --- Cargo.lock | 2 +- Cargo.toml | 1 + src/bitcoin/keys.rs | 12 +++++++++++- src/constants.rs | 7 +++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f9597f0f..b3ab1932 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -659,7 +659,7 @@ dependencies = [ [[package]] name = "bitmask-core" -version = "0.7.0-beta.10" +version = "0.7.0-beta.11" dependencies = [ "amplify", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 94596af3..18bb118b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ required-features = ["server"] all = [] default = [] web = [] +segwit = [] server = ["tokio/full", "tower-http/cors"] [dependencies] diff --git a/src/bitcoin/keys.rs b/src/bitcoin/keys.rs index 3eafcb61..c7c1ec8d 100644 --- a/src/bitcoin/keys.rs +++ b/src/bitcoin/keys.rs @@ -1,12 +1,16 @@ use std::str::FromStr; +#[cfg(feature = "segwit")] +use bdk::miniscript::Segwitv0; +#[cfg(not(feature = "segwit"))] +use bdk::miniscript::Tap; use bdk::{ bitcoin::{ secp256k1::Secp256k1, util::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey, KeySource}, }, keys::{DerivableKey, DescriptorKey, DescriptorKey::Secret as SecretDesc, DescriptorSecretKey}, - miniscript::{descriptor::DescriptorKeyParseError, Tap}, + miniscript::descriptor::DescriptorKeyParseError, }; use bip39::{Language, Mnemonic}; use bitcoin::{KeyPair, Network}; @@ -70,10 +74,16 @@ fn get_descriptor( let deriv_descriptor = DerivationPath::from_str(path)?; let derived_xprv = &xprv.derive_priv(&secp, &deriv_descriptor)?; let origin: KeySource = (xprv.fingerprint(&secp), deriv_descriptor); + #[cfg(not(feature = "segwit"))] let derived_xprv_desc_key: DescriptorKey = derived_xprv.into_descriptor_key( Some(origin), DerivationPath::default().child(ChildNumber::from_normal_idx(change)?), )?; + #[cfg(feature = "segwit")] + let derived_xprv_desc_key: DescriptorKey = derived_xprv.into_descriptor_key( + Some(origin), + DerivationPath::default().child(ChildNumber::from_normal_idx(change)?), + )?; if let SecretDesc(desc_seckey, _, _) = derived_xprv_desc_key { Ok(desc_seckey) diff --git a/src/constants.rs b/src/constants.rs index c9e593be..03b6b4a3 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -90,8 +90,15 @@ pub async fn get_udas_utxo() -> String { } // Descriptor strings +#[cfg(not(feature = "segwit"))] pub const BTC_MAINNET_PATH: &str = "m/86h/0h/0h"; +#[cfg(not(feature = "segwit"))] pub const BTC_TESTNET_PATH: &str = "m/86h/1h/0h"; +#[cfg(feature = "segwit")] +pub const BTC_MAINNET_PATH: &str = "m/84h/0h/0h"; +#[cfg(feature = "segwit")] +pub const BTC_TESTNET_PATH: &str = "m/84h/1h/0h"; + pub static BTC_PATH: Lazy> = Lazy::new(|| { RwLock::new(if dot_env("BITCOIN_NETWORK") == "bitcoin" { BTC_MAINNET_PATH.to_owned()