Trait bdk::keys::DerivableKey

source ·
pub trait DerivableKey<Ctx: ScriptContext = Legacy>: Sized {
    // Required method
    fn into_extended_key(self) -> Result<ExtendedKey<Ctx>, KeyError>;

    // Provided method
    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 implement 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::bip32;
use bdk::keys::{DerivableKey, ExtendedKey, KeyError, ScriptContext};

struct MyCustomKeyType {
    key_data: bitcoin::PrivateKey,
    chain_code: [u8; 32],
    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),
            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::bip32;
use bdk::keys::{
    any_network, DerivableKey, DescriptorKey, ExtendedKey, KeyError, ScriptContext,
};

struct MyCustomKeyType {
    key_data: bitcoin::PrivateKey,
    chain_code: [u8; 32],
}

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),
            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§

source

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 xprvs and xpubs 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§

source

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

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<Ctx: ScriptContext> DerivableKey<Ctx> for (GeneratedKey<Mnemonic, Ctx>, Option<String>)

Available on crate feature keys-bip39 only.
source§

impl<Ctx: ScriptContext> DerivableKey<Ctx> for ExtendedPrivKey

source§

impl<Ctx: ScriptContext> DerivableKey<Ctx> for ExtendedPubKey

source§

impl<Ctx: ScriptContext> DerivableKey<Ctx> for [u8; 64]

Available on crate feature keys-bip39 only.

Implementors§

source§

impl<Ctx, K> DerivableKey<Ctx> for GeneratedKey<K, Ctx>where Ctx: ScriptContext, K: DerivableKey<Ctx>,

source§

impl<Ctx: ScriptContext> DerivableKey<Ctx> for ExtendedKey<Ctx>

Identity conversion

source§

impl<Ctx: ScriptContext> DerivableKey<Ctx> for Mnemonic

Available on crate feature keys-bip39 only.
source§

impl<Ctx: ScriptContext> DerivableKey<Ctx> for MnemonicWithPassphrase

Available on crate feature keys-bip39 only.