[][src]Trait bdk::keys::ToDescriptorKey

pub trait ToDescriptorKey<Ctx: ScriptContext>: Sized {
    pub fn to_descriptor_key(self) -> Result<DescriptorKey<Ctx>, KeyError>;
}

Trait for objects that can be turned into a public or secret DescriptorKey

The generic type Ctx is used to define the context in which the key is valid: some key formats, like the mnemonics used by Electrum wallets, encode internally whether the wallet is legacy or segwit. Thus, trying to turn a valid legacy mnemonic into a DescriptorKey that would become part of a segwit descriptor should fail.

For key types that do care about this, the ExtScriptContext trait provides some useful methods that can be used to check at runtime which Ctx is being used.

For key types that can do this check statically (because they can only work within a single Ctx), the "specialized" trait can be implemented to make the compiler handle the type checking.

Keys also have control over the networks they support: constructing the return object with DescriptorKey::from_public or DescriptorKey::from_secret allows to specify a set of ValidNetworks.

Examples

Key type valid in any context:

use bdk::bitcoin::PublicKey;

use bdk::keys::{DescriptorKey, KeyError, ScriptContext, ToDescriptorKey};

pub struct MyKeyType {
    pubkey: PublicKey,
}

impl<Ctx: ScriptContext> ToDescriptorKey<Ctx> for MyKeyType {
    fn to_descriptor_key(self) -> Result<DescriptorKey<Ctx>, KeyError> {
        self.pubkey.to_descriptor_key()
    }
}

Key type that is only valid on mainnet:

use bdk::bitcoin::PublicKey;

use bdk::keys::{
    mainnet_network, DescriptorKey, DescriptorPublicKey, DescriptorSinglePub, KeyError,
    ScriptContext, ToDescriptorKey,
};

pub struct MyKeyType {
    pubkey: PublicKey,
}

impl<Ctx: ScriptContext> ToDescriptorKey<Ctx> for MyKeyType {
    fn to_descriptor_key(self) -> Result<DescriptorKey<Ctx>, KeyError> {
        Ok(DescriptorKey::from_public(
            DescriptorPublicKey::SinglePub(DescriptorSinglePub {
                origin: None,
                key: self.pubkey,
            }),
            mainnet_network(),
        ))
    }
}

Key type that internally encodes in which context it's valid. The context is checked at runtime:

use bdk::bitcoin::PublicKey;

use bdk::keys::{DescriptorKey, ExtScriptContext, KeyError, ScriptContext, ToDescriptorKey};

pub struct MyKeyType {
    is_legacy: bool,
    pubkey: PublicKey,
}

impl<Ctx: ScriptContext + 'static> ToDescriptorKey<Ctx> for MyKeyType {
    fn to_descriptor_key(self) -> Result<DescriptorKey<Ctx>, KeyError> {
        if Ctx::is_legacy() == self.is_legacy {
            self.pubkey.to_descriptor_key()
        } else {
            Err(KeyError::InvalidScriptContext)
        }
    }
}

Key type that can only work within miniscript::Segwitv0 context. Only the specialized version of the trait is implemented.

This example deliberately fails to compile, to demonstrate how the compiler can catch when keys are misused. In this case, the "segwit-only" key is used to build a pkh() descriptor, which makes the compiler (correctly) fail.

This example deliberately fails to compile
use bdk::bitcoin::PublicKey;
use std::str::FromStr;

use bdk::keys::{DescriptorKey, KeyError, ToDescriptorKey};

pub struct MySegwitOnlyKeyType {
    pubkey: PublicKey,
}

impl ToDescriptorKey<bdk::miniscript::Segwitv0> for MySegwitOnlyKeyType {
    fn to_descriptor_key(self) -> Result<DescriptorKey<bdk::miniscript::Segwitv0>, KeyError> {
        self.pubkey.to_descriptor_key()
    }
}

let key = MySegwitOnlyKeyType {
    pubkey: PublicKey::from_str("...")?,
};
let (descriptor, _, _) = bdk::descriptor!(pkh(key))?;
//                                       ^^^^^ changing this to `wpkh` would make it compile

Required methods

pub fn to_descriptor_key(self) -> Result<DescriptorKey<Ctx>, KeyError>[src]

Turn the key into a DescriptorKey within the requested ScriptContext

Loading content...

Implementations on Foreign Types

impl<Ctx: ScriptContext, T: DerivableKey<Ctx>> ToDescriptorKey<Ctx> for (T, DerivationPath)[src]

impl<Ctx: ScriptContext, T: DerivableKey<Ctx>> ToDescriptorKey<Ctx> for (T, KeySource, DerivationPath)[src]

impl<Ctx: ScriptContext> ToDescriptorKey<Ctx> for PublicKey[src]

impl<Ctx: ScriptContext, '_> ToDescriptorKey<Ctx> for &'_ str[src]

impl<Ctx: ScriptContext> ToDescriptorKey<Ctx> for PrivateKey[src]

Loading content...

Implementors

impl<Ctx, K> ToDescriptorKey<Ctx> for GeneratedKey<K, Ctx> where
    Ctx: ScriptContext,
    K: ToDescriptorKey<Ctx>, 
[src]

impl<Ctx: ScriptContext> ToDescriptorKey<Ctx> for DescriptorKey<Ctx>[src]

The "identity" conversion is used internally by some bdk::fragments

impl<Ctx: ScriptContext> ToDescriptorKey<Ctx> for DescriptorPublicKey[src]

impl<Ctx: ScriptContext> ToDescriptorKey<Ctx> for DescriptorSecretKey[src]

Loading content...