diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c2e63ec..3cf22f85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Keys #### Changed - Renamed `DerivableKey::add_metadata()` to `DerivableKey::into_descriptor_key()` +- Renamed `ToDescriptorKey::to_descriptor_key()` to `IntoDescriptorKey::into_descriptor_key()` #### Added - Added an `ExtendedKey` type that is an enum of `bip32::ExtendedPubKey` and `bip32::ExtendedPrivKey` - Added `DerivableKey::into_extended_key()` as the only method that needs to be implemented @@ -27,9 +28,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Wallet #### Changed - Removed the explicit `id` argument from `Wallet::add_signer()` since that's now part of `Signer` itself -- Renamed `ToWalletDescriptor::to_wallet_descriptor` to `IntoWalletDescriptor::into_wallet_descriptor` +- Renamed `ToWalletDescriptor::to_wallet_descriptor()` to `IntoWalletDescriptor::into_wallet_descriptor()` -### Policy +### Policy #### Changed - Removed unneeded `Result<(), PolicyError>` return type for `Satisfaction::finalize()` diff --git a/src/descriptor/dsl.rs b/src/descriptor/dsl.rs index 75d2d147..37e9a1a4 100644 --- a/src/descriptor/dsl.rs +++ b/src/descriptor/dsl.rs @@ -76,7 +76,7 @@ macro_rules! impl_top_level_pk { use $crate::miniscript::descriptor::$inner_type; #[allow(unused_imports)] - use $crate::keys::{DescriptorKey, ToDescriptorKey}; + use $crate::keys::{DescriptorKey, IntoDescriptorKey}; let secp = $crate::bitcoin::secp256k1::Secp256k1::new(); $key.into_descriptor_key() @@ -225,7 +225,7 @@ macro_rules! impl_sortedmulti { $crate::keys::make_sortedmulti($thresh, $keys, $build_desc, &secp) }); ( $build_desc:expr, sortedmulti ( $thresh:expr $(, $key:expr )+ ) ) => ({ - use $crate::keys::ToDescriptorKey; + use $crate::keys::IntoDescriptorKey; let secp = $crate::bitcoin::secp256k1::Secp256k1::new(); let mut keys = vec![]; @@ -326,11 +326,11 @@ macro_rules! apply_modifier { /// broken up to `s:d:v:older(144)`. /// /// The `pk()`, `pk_k()` and `pk_h()` operands can take as argument any type that implements -/// [`ToDescriptorKey`]. This means that keys can also be written inline as strings, but in that +/// [`IntoDescriptorKey`]. This means that keys can also be written inline as strings, but in that /// case they must be wrapped in quotes, which is another difference compared to the standard /// descriptor syntax. /// -/// [`ToDescriptorKey`]: crate::keys::ToDescriptorKey +/// [`IntoDescriptorKey`]: crate::keys::IntoDescriptorKey /// /// ## Example /// @@ -653,7 +653,7 @@ macro_rules! fragment { $crate::keys::make_multi($thresh, $keys) }); ( multi ( $thresh:expr $(, $key:expr )+ ) ) => ({ - use $crate::keys::ToDescriptorKey; + use $crate::keys::IntoDescriptorKey; let secp = $crate::bitcoin::secp256k1::Secp256k1::new(); let mut keys = vec![]; @@ -685,7 +685,7 @@ mod test { use std::str::FromStr; use crate::descriptor::{DescriptorError, DescriptorMeta}; - use crate::keys::{DescriptorKey, ToDescriptorKey, ValidNetworks}; + use crate::keys::{DescriptorKey, IntoDescriptorKey, ValidNetworks}; use bitcoin::network::constants::Network::{Bitcoin, Regtest, Signet, Testnet}; use bitcoin::util::bip32; use bitcoin::PrivateKey; @@ -724,7 +724,7 @@ mod test { } // - at least one of each "type" of operator; ie. one modifier, one leaf_opcode, one leaf_opcode_value, etc. - // - mixing up key types that implement ToDescriptorKey in multi() or thresh() + // - mixing up key types that implement IntoDescriptorKey in multi() or thresh() // expected script for pk and bare manually created // expected addresses created with `bitcoin-cli getdescriptorinfo` (for hash) and `bitcoin-cli deriveaddresses` @@ -1020,7 +1020,7 @@ mod test { assert_eq!(key_map.get(&key3).unwrap().to_string(), "tprv8ZgxMBicQKsPdZXrcHNLf5JAJWFAoJ2TrstMRdSKtEggz6PddbuSkvHKM9oKJyFgZV1B7rw8oChspxyYbtmEXYyg1AjfWbL3ho3XHDpHRZf/10/20/30/40/*"); } - // - verify the ScriptContext is correctly validated (i.e. passing a type that only impl ToDescriptorKey to a pkh() descriptor should throw a compilation error + // - verify the ScriptContext is correctly validated (i.e. passing a type that only impl IntoDescriptorKey to a pkh() descriptor should throw a compilation error #[test] fn test_script_context_validation() { // this compiles diff --git a/src/descriptor/mod.rs b/src/descriptor/mod.rs index 09c60e9f..10000786 100644 --- a/src/descriptor/mod.rs +++ b/src/descriptor/mod.rs @@ -54,7 +54,7 @@ pub use self::derived::DerivedDescriptorKey; pub use self::error::Error as DescriptorError; pub use self::policy::Policy; use self::template::DescriptorTemplateOut; -use crate::keys::{KeyError, ToDescriptorKey}; +use crate::keys::{IntoDescriptorKey, KeyError}; use crate::wallet::signer::SignersContainer; use crate::wallet::utils::SecpCtx; @@ -614,7 +614,7 @@ mod test { #[test] fn test_to_wallet_descriptor_fixup_networks() { - use crate::keys::{any_network, ToDescriptorKey}; + use crate::keys::{any_network, IntoDescriptorKey}; let secp = Secp256k1::new(); diff --git a/src/descriptor/policy.rs b/src/descriptor/policy.rs index f138dfdb..54ba808b 100644 --- a/src/descriptor/policy.rs +++ b/src/descriptor/policy.rs @@ -892,7 +892,7 @@ mod test { use super::*; use crate::descriptor::policy::SatisfiableItem::{Multisig, Signature, Thresh}; - use crate::keys::{DescriptorKey, ToDescriptorKey}; + use crate::keys::{DescriptorKey, IntoDescriptorKey}; use crate::wallet::signer::SignersContainer; use bitcoin::secp256k1::{All, Secp256k1}; use bitcoin::util::bip32; diff --git a/src/descriptor/template.rs b/src/descriptor/template.rs index 98103dc6..14c3aef7 100644 --- a/src/descriptor/template.rs +++ b/src/descriptor/template.rs @@ -34,7 +34,7 @@ use miniscript::{Legacy, Segwitv0}; use super::{ExtendedDescriptor, IntoWalletDescriptor, KeyMap}; use crate::descriptor::DescriptorError; -use crate::keys::{DerivableKey, ToDescriptorKey, ValidNetworks}; +use crate::keys::{DerivableKey, IntoDescriptorKey, ValidNetworks}; use crate::wallet::utils::SecpCtx; use crate::{descriptor, KeychainKind}; @@ -50,13 +50,13 @@ pub type DescriptorTemplateOut = (ExtendedDescriptor, KeyMap, ValidNetworks); /// /// ``` /// use bdk::descriptor::error::Error as DescriptorError; -/// use bdk::keys::{KeyError, ToDescriptorKey}; +/// use bdk::keys::{KeyError, IntoDescriptorKey}; /// use bdk::miniscript::Legacy; /// use bdk::template::{DescriptorTemplate, DescriptorTemplateOut}; /// -/// struct MyP2PKH>(K); +/// struct MyP2PKH>(K); /// -/// impl> DescriptorTemplate for MyP2PKH { +/// impl> DescriptorTemplate for MyP2PKH { /// fn build(self) -> Result { /// Ok(bdk::descriptor!(pkh(self.0))?) /// } @@ -104,9 +104,9 @@ impl IntoWalletDescriptor for T { /// ); /// # Ok::<_, Box>(()) /// ``` -pub struct P2PKH>(pub K); +pub struct P2PKH>(pub K); -impl> DescriptorTemplate for P2PKH { +impl> DescriptorTemplate for P2PKH { fn build(self) -> Result { Ok(descriptor!(pkh(self.0))?) } @@ -138,9 +138,9 @@ impl> DescriptorTemplate for P2PKH { /// # Ok::<_, Box>(()) /// ``` #[allow(non_camel_case_types)] -pub struct P2WPKH_P2SH>(pub K); +pub struct P2WPKH_P2SH>(pub K); -impl> DescriptorTemplate for P2WPKH_P2SH { +impl> DescriptorTemplate for P2WPKH_P2SH { fn build(self) -> Result { Ok(descriptor!(sh(wpkh(self.0)))?) } @@ -171,9 +171,9 @@ impl> DescriptorTemplate for P2WPKH_P2SH { /// ); /// # Ok::<_, Box>(()) /// ``` -pub struct P2WPKH>(pub K); +pub struct P2WPKH>(pub K); -impl> DescriptorTemplate for P2WPKH { +impl> DescriptorTemplate for P2WPKH { fn build(self) -> Result { Ok(descriptor!(wpkh(self.0))?) } @@ -410,7 +410,7 @@ macro_rules! expand_make_bipxx { bip: u32, key: K, keychain: KeychainKind, - ) -> Result, DescriptorError> { + ) -> Result, DescriptorError> { let mut derivation_path = Vec::with_capacity(4); derivation_path.push(bip32::ChildNumber::from_hardened_idx(bip)?); derivation_path.push(bip32::ChildNumber::from_hardened_idx(0)?); @@ -434,7 +434,7 @@ macro_rules! expand_make_bipxx { key: K, parent_fingerprint: bip32::Fingerprint, keychain: KeychainKind, - ) -> Result, DescriptorError> { + ) -> Result, DescriptorError> { let derivation_path: bip32::DerivationPath = match keychain { KeychainKind::External => vec![bip32::ChildNumber::from_normal_idx(0)?].into(), KeychainKind::Internal => vec![bip32::ChildNumber::from_normal_idx(1)?].into(), diff --git a/src/keys/mod.rs b/src/keys/mod.rs index 3f6bfe03..0ce5d128 100644 --- a/src/keys/mod.rs +++ b/src/keys/mod.rs @@ -205,13 +205,13 @@ impl ExtScriptContext for Ctx { /// ``` /// use bdk::bitcoin::PublicKey; /// -/// use bdk::keys::{DescriptorKey, KeyError, ScriptContext, ToDescriptorKey}; +/// use bdk::keys::{DescriptorKey, KeyError, ScriptContext, IntoDescriptorKey}; /// /// pub struct MyKeyType { /// pubkey: PublicKey, /// } /// -/// impl ToDescriptorKey for MyKeyType { +/// impl IntoDescriptorKey for MyKeyType { /// fn into_descriptor_key(self) -> Result, KeyError> { /// self.pubkey.into_descriptor_key() /// } @@ -225,14 +225,14 @@ impl ExtScriptContext for Ctx { /// /// use bdk::keys::{ /// mainnet_network, DescriptorKey, DescriptorPublicKey, DescriptorSinglePub, KeyError, -/// ScriptContext, ToDescriptorKey, +/// ScriptContext, IntoDescriptorKey, /// }; /// /// pub struct MyKeyType { /// pubkey: PublicKey, /// } /// -/// impl ToDescriptorKey for MyKeyType { +/// impl IntoDescriptorKey for MyKeyType { /// fn into_descriptor_key(self) -> Result, KeyError> { /// Ok(DescriptorKey::from_public( /// DescriptorPublicKey::SinglePub(DescriptorSinglePub { @@ -250,14 +250,14 @@ impl ExtScriptContext for Ctx { /// ``` /// use bdk::bitcoin::PublicKey; /// -/// use bdk::keys::{DescriptorKey, ExtScriptContext, KeyError, ScriptContext, ToDescriptorKey}; +/// use bdk::keys::{DescriptorKey, ExtScriptContext, KeyError, ScriptContext, IntoDescriptorKey}; /// /// pub struct MyKeyType { /// is_legacy: bool, /// pubkey: PublicKey, /// } /// -/// impl ToDescriptorKey for MyKeyType { +/// impl IntoDescriptorKey for MyKeyType { /// fn into_descriptor_key(self) -> Result, KeyError> { /// if Ctx::is_legacy() == self.is_legacy { /// self.pubkey.into_descriptor_key() @@ -279,13 +279,13 @@ impl ExtScriptContext for Ctx { /// use bdk::bitcoin::PublicKey; /// use std::str::FromStr; /// -/// use bdk::keys::{DescriptorKey, KeyError, ToDescriptorKey}; +/// use bdk::keys::{DescriptorKey, KeyError, IntoDescriptorKey}; /// /// pub struct MySegwitOnlyKeyType { /// pubkey: PublicKey, /// } /// -/// impl ToDescriptorKey for MySegwitOnlyKeyType { +/// impl IntoDescriptorKey for MySegwitOnlyKeyType { /// fn into_descriptor_key(self) -> Result, KeyError> { /// self.pubkey.into_descriptor_key() /// } @@ -299,7 +299,7 @@ impl ExtScriptContext for Ctx { /// /// # Ok::<_, Box>(()) /// ``` -pub trait ToDescriptorKey: Sized { +pub trait IntoDescriptorKey: Sized { /// Turn the key into a [`DescriptorKey`] within the requested [`ScriptContext`] fn into_descriptor_key(self) -> Result, KeyError>; } @@ -370,12 +370,12 @@ impl From for ExtendedKey { /// Trait for keys that can be derived. /// /// When extra metadata are provided, a [`DerivableKey`] can be transofrmed into a -/// [`DescriptorKey`]: the trait [`ToDescriptorKey`] is automatically implemented +/// [`DescriptorKey`]: the trait [`IntoDescriptorKey`] is automatically implemented /// for `(DerivableKey, DerivationPath)` and /// `(DerivableKey, KeySource, DerivationPath)` tuples. /// /// For key types that don't encode any indication about the path to use (like bip39), it's -/// generally recommended to implemented this trait instead of [`ToDescriptorKey`]. The same +/// generally recommended to implemented this trait instead of [`IntoDescriptorKey`]. The same /// rules regarding script context and valid networks apply. /// /// ## Examples @@ -583,10 +583,10 @@ where // Make generated keys directly usable in descriptors, and make sure they get assigned the right // `valid_networks`. -impl ToDescriptorKey for GeneratedKey +impl IntoDescriptorKey for GeneratedKey where Ctx: ScriptContext, - K: ToDescriptorKey, + K: IntoDescriptorKey, { fn into_descriptor_key(self) -> Result, KeyError> { let desc_key = self.key.into_descriptor_key()?; @@ -596,12 +596,12 @@ where /// Trait for keys that can be generated /// -/// The same rules about [`ScriptContext`] and [`ValidNetworks`] from [`ToDescriptorKey`] apply. +/// The same rules about [`ScriptContext`] and [`ValidNetworks`] from [`IntoDescriptorKey`] apply. /// /// This trait is particularly useful when combined with [`DerivableKey`]: if `Self` /// implements it, the returned [`GeneratedKey`] will also implement it. The same is true for -/// [`ToDescriptorKey`]: the generated keys can be directly used in descriptors if `Self` is also -/// [`ToDescriptorKey`]. +/// [`IntoDescriptorKey`]: the generated keys can be directly used in descriptors if `Self` is also +/// [`IntoDescriptorKey`]. pub trait GeneratableKey: Sized { /// Type specifying the amount of entropy required e.g. `[u8;32]` type Entropy: AsMut<[u8]> + Default; @@ -711,13 +711,15 @@ impl GeneratableKey for PrivateKey { } } -impl> ToDescriptorKey for (T, bip32::DerivationPath) { +impl> IntoDescriptorKey + for (T, bip32::DerivationPath) +{ fn into_descriptor_key(self) -> Result, KeyError> { self.0.into_descriptor_key(None, self.1) } } -impl> ToDescriptorKey +impl> IntoDescriptorKey for (T, bip32::KeySource, bip32::DerivationPath) { fn into_descriptor_key(self) -> Result, KeyError> { @@ -725,7 +727,7 @@ impl> ToDescriptorKey } } -fn expand_multi_keys, Ctx: ScriptContext>( +fn expand_multi_keys, Ctx: ScriptContext>( pks: Vec, secp: &SecpCtx, ) -> Result<(Vec, KeyMap, ValidNetworks), KeyError> { @@ -752,7 +754,7 @@ fn expand_multi_keys, Ctx: ScriptContext>( // Used internally by `bdk::fragment!` to build `pk_k()` fragments #[doc(hidden)] -pub fn make_pk, Ctx: ScriptContext>( +pub fn make_pk, Ctx: ScriptContext>( descriptor_key: Pk, secp: &SecpCtx, ) -> Result<(Miniscript, KeyMap, ValidNetworks), DescriptorError> { @@ -766,7 +768,7 @@ pub fn make_pk, Ctx: ScriptContext>( // Used internally by `bdk::fragment!` to build `multi()` fragments #[doc(hidden)] -pub fn make_multi, Ctx: ScriptContext>( +pub fn make_multi, Ctx: ScriptContext>( thresh: usize, pks: Vec, secp: &SecpCtx, @@ -788,7 +790,7 @@ pub fn make_sortedmulti( secp: &SecpCtx, ) -> Result<(Descriptor, KeyMap, ValidNetworks), DescriptorError> where - Pk: ToDescriptorKey, + Pk: IntoDescriptorKey, Ctx: ScriptContext, F: Fn( usize, @@ -802,13 +804,13 @@ where } /// The "identity" conversion is used internally by some `bdk::fragment`s -impl ToDescriptorKey for DescriptorKey { +impl IntoDescriptorKey for DescriptorKey { fn into_descriptor_key(self) -> Result, KeyError> { Ok(self) } } -impl ToDescriptorKey for DescriptorPublicKey { +impl IntoDescriptorKey for DescriptorPublicKey { fn into_descriptor_key(self) -> Result, KeyError> { let networks = match self { DescriptorPublicKey::SinglePub(_) => any_network(), @@ -824,7 +826,7 @@ impl ToDescriptorKey for DescriptorPublicKey { } } -impl ToDescriptorKey for PublicKey { +impl IntoDescriptorKey for PublicKey { fn into_descriptor_key(self) -> Result, KeyError> { DescriptorPublicKey::SinglePub(DescriptorSinglePub { key: self, @@ -834,7 +836,7 @@ impl ToDescriptorKey for PublicKey { } } -impl ToDescriptorKey for DescriptorSecretKey { +impl IntoDescriptorKey for DescriptorSecretKey { fn into_descriptor_key(self) -> Result, KeyError> { let networks = match &self { DescriptorSecretKey::SinglePriv(sk) if sk.key.network == Network::Bitcoin => { @@ -852,7 +854,7 @@ impl ToDescriptorKey for DescriptorSecretKey { } } -impl ToDescriptorKey for &'_ str { +impl IntoDescriptorKey for &'_ str { fn into_descriptor_key(self) -> Result, KeyError> { DescriptorSecretKey::from_str(self) .map_err(|e| KeyError::Message(e.to_string()))? @@ -860,7 +862,7 @@ impl ToDescriptorKey for &'_ str { } } -impl ToDescriptorKey for PrivateKey { +impl IntoDescriptorKey for PrivateKey { fn into_descriptor_key(self) -> Result, KeyError> { DescriptorSecretKey::SinglePriv(DescriptorSinglePriv { key: self, diff --git a/src/wallet/signer.rs b/src/wallet/signer.rs index aad07a20..83888f50 100644 --- a/src/wallet/signer.rs +++ b/src/wallet/signer.rs @@ -561,7 +561,7 @@ mod signers_container_tests { use super::*; use crate::descriptor; use crate::descriptor::IntoWalletDescriptor; - use crate::keys::{DescriptorKey, ToDescriptorKey}; + use crate::keys::{DescriptorKey, IntoDescriptorKey}; use bitcoin::secp256k1::{All, Secp256k1}; use bitcoin::util::bip32; use bitcoin::util::psbt::PartiallySignedTransaction;