Trait bdk::keys::DerivableKey [−][src]
Trait for keys that can be derived.
When extra metadata are provided, a DerivableKey
can be transofrmed into a
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 IntoDescriptorKey
. The same
rules regarding script context and valid networks apply.
Examples
Key types that can be directly converted into an ExtendedPrivKey
or
an ExtendedPubKey
can implement only the required into_extended_key()
method.
use bdk::bitcoin; use bdk::bitcoin::util::bip32; use bdk::keys::{DerivableKey, ExtendedKey, KeyError, ScriptContext}; struct MyCustomKeyType { key_data: bitcoin::PrivateKey, chain_code: Vec<u8>, network: bitcoin::Network, } impl<Ctx: ScriptContext> DerivableKey<Ctx> for MyCustomKeyType { fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError> { let xprv = bip32::ExtendedPrivKey { network: self.network, depth: 0, parent_fingerprint: bip32::Fingerprint::default(), private_key: self.key_data, chain_code: bip32::ChainCode::from(self.chain_code.as_ref()), child_number: bip32::ChildNumber::Normal { index: 0 }, }; xprv.into_extended_key() } }
Types that don’t internally encode the Network
in which they are valid need some extra
steps to override the set of valid networks, otherwise only the network specified in the
ExtendedPrivKey
or ExtendedPubKey
will be considered valid.
use bdk::bitcoin; use bdk::bitcoin::util::bip32; use bdk::keys::{ any_network, DerivableKey, DescriptorKey, ExtendedKey, KeyError, ScriptContext, }; struct MyCustomKeyType { key_data: bitcoin::PrivateKey, chain_code: Vec<u8>, } impl<Ctx: ScriptContext> DerivableKey<Ctx> for MyCustomKeyType { fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError> { let xprv = bip32::ExtendedPrivKey { network: bitcoin::Network::Bitcoin, // pick an arbitrary network here depth: 0, parent_fingerprint: bip32::Fingerprint::default(), private_key: self.key_data, chain_code: bip32::ChainCode::from(self.chain_code.as_ref()), child_number: bip32::ChildNumber::Normal { index: 0 }, }; xprv.into_extended_key() } fn into_descriptor_key( self, source: Option<bip32::KeySource>, derivation_path: bip32::DerivationPath, ) -> Result<DescriptorKey<Ctx>, KeyError> { let descriptor_key = self .into_extended_key()? .into_descriptor_key(source, derivation_path)?; // Override the set of valid networks here Ok(descriptor_key.override_valid_networks(any_network())) } }
Required methods
fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>
[src]
Consume self
and turn it into an ExtendedKey
This can be used to get direct access to xprv
s and xpub
s for types that implement this trait,
like Mnemonic
when the keys-bip39
feature is enabled.
use bdk::bitcoin::Network; use bdk::keys::{DerivableKey, ExtendedKey}; use bdk::keys::bip39::{Mnemonic, Language}; let xkey: ExtendedKey = Mnemonic::from_phrase( "jelly crash boy whisper mouse ecology tuna soccer memory million news short", Language::English )? .into_extended_key()?; let xprv = xkey.into_xprv(Network::Bitcoin).unwrap();
Provided methods
fn into_descriptor_key(
self,
origin: Option<KeySource>,
derivation_path: DerivationPath
) -> Result<DescriptorKey<Ctx>, KeyError>
[src]
self,
origin: Option<KeySource>,
derivation_path: DerivationPath
) -> Result<DescriptorKey<Ctx>, KeyError>
Consume self
and turn it into a DescriptorKey
by adding the extra metadata, such as
key origin and derivation path
Implementations on Foreign Types
impl<Ctx: ScriptContext> DerivableKey<Ctx> for ExtendedPubKey
[src]
fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>
[src]
impl<Ctx: ScriptContext> DerivableKey<Ctx> for ExtendedPrivKey
[src]
fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>
[src]
Implementors
impl<Ctx, K> DerivableKey<Ctx> for GeneratedKey<K, Ctx> where
Ctx: ScriptContext,
K: DerivableKey<Ctx>,
[src]
Ctx: ScriptContext,
K: DerivableKey<Ctx>,
fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>
[src]
fn into_descriptor_key(
self,
origin: Option<KeySource>,
derivation_path: DerivationPath
) -> Result<DescriptorKey<Ctx>, KeyError>
[src]
self,
origin: Option<KeySource>,
derivation_path: DerivationPath
) -> Result<DescriptorKey<Ctx>, KeyError>
impl<Ctx: ScriptContext> DerivableKey<Ctx> for ExtendedKey<Ctx>
[src]
Identity conversion
fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>
[src]
impl<Ctx: ScriptContext> DerivableKey<Ctx> for Mnemonic
[src]
keys-bip39
only.fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>
[src]
fn into_descriptor_key(
self,
source: Option<KeySource>,
derivation_path: DerivationPath
) -> Result<DescriptorKey<Ctx>, KeyError>
[src]
self,
source: Option<KeySource>,
derivation_path: DerivationPath
) -> Result<DescriptorKey<Ctx>, KeyError>
impl<Ctx: ScriptContext> DerivableKey<Ctx> for Seed
[src]
keys-bip39
only.fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>
[src]
fn into_descriptor_key(
self,
source: Option<KeySource>,
derivation_path: DerivationPath
) -> Result<DescriptorKey<Ctx>, KeyError>
[src]
self,
source: Option<KeySource>,
derivation_path: DerivationPath
) -> Result<DescriptorKey<Ctx>, KeyError>
impl<Ctx: ScriptContext> DerivableKey<Ctx> for MnemonicWithPassphrase
[src]
keys-bip39
only.fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>
[src]
fn into_descriptor_key(
self,
source: Option<KeySource>,
derivation_path: DerivationPath
) -> Result<DescriptorKey<Ctx>, KeyError>
[src]
self,
source: Option<KeySource>,
derivation_path: DerivationPath
) -> Result<DescriptorKey<Ctx>, KeyError>