Trait bdk::keys::DerivableKey
source · pub trait DerivableKey<Ctx: ScriptContext = Legacy>: Sized {
fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>;
fn into_descriptor_key(
self,
origin: Option<KeySource>,
derivation_path: DerivationPath
) -> Result<DescriptorKey<Ctx>, KeyError> { ... }
}
Expand description
Trait for keys that can be derived.
When extra metadata are provided, a DerivableKey
can be transformed 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.inner,
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.inner,
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
sourcefn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>
fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>
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::parse_in(
Language::English,
"jelly crash boy whisper mouse ecology tuna soccer memory million news short",
)?
.into_extended_key()?;
let xprv = xkey.into_xprv(Network::Bitcoin).unwrap();
Provided Methods
sourcefn into_descriptor_key(
self,
origin: Option<KeySource>,
derivation_path: DerivationPath
) -> Result<DescriptorKey<Ctx>, KeyError>
fn into_descriptor_key(
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
sourceimpl<Ctx: ScriptContext> DerivableKey<Ctx> for [u8; 64]
Available on crate feature keys-bip39
only.
impl<Ctx: ScriptContext> DerivableKey<Ctx> for [u8; 64]
keys-bip39
only.fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>
fn into_descriptor_key(
self,
source: Option<KeySource>,
derivation_path: DerivationPath
) -> Result<DescriptorKey<Ctx>, KeyError>
sourceimpl<Ctx: ScriptContext> DerivableKey<Ctx> for (GeneratedKey<Mnemonic, Ctx>, Option<String>)
Available on crate feature keys-bip39
only.
impl<Ctx: ScriptContext> DerivableKey<Ctx> for (GeneratedKey<Mnemonic, Ctx>, Option<String>)
keys-bip39
only.fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>
fn into_descriptor_key(
self,
source: Option<KeySource>,
derivation_path: DerivationPath
) -> Result<DescriptorKey<Ctx>, KeyError>
sourceimpl<Ctx: ScriptContext> DerivableKey<Ctx> for ExtendedPubKey
impl<Ctx: ScriptContext> DerivableKey<Ctx> for ExtendedPubKey
fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>
sourceimpl<Ctx: ScriptContext> DerivableKey<Ctx> for ExtendedPrivKey
impl<Ctx: ScriptContext> DerivableKey<Ctx> for ExtendedPrivKey
fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>
Implementors
impl<Ctx, K> DerivableKey<Ctx> for GeneratedKey<K, Ctx>where
Ctx: ScriptContext,
K: DerivableKey<Ctx>,
impl<Ctx: ScriptContext> DerivableKey<Ctx> for ExtendedKey<Ctx>
Identity conversion
impl<Ctx: ScriptContext> DerivableKey<Ctx> for Mnemonic
keys-bip39
only.impl<Ctx: ScriptContext> DerivableKey<Ctx> for MnemonicWithPassphrase
keys-bip39
only.