Skip to main content

KeyProvider

Trait KeyProvider 

Source
pub trait KeyProvider: Send + Sync {
    // Required methods
    fn get_next_keypair(
        &self,
        keypair_index: KeypairIndex,
    ) -> Result<Keypair, Error>;
    fn get_keypair_for_path(&self, path: &[u32]) -> Result<Keypair, Error>;
    fn get_keypair_for_pk(&self, pk: &XOnlyPublicKey) -> Result<Keypair, Error>;
    fn get_cached_pks(&self) -> Result<Vec<XOnlyPublicKey>, Error>;
}
Expand description

Provides keypairs for signing operations

This trait allows different key management strategies:

  • Static keypair (single key)
  • BIP32 HD wallet (hierarchical deterministic)
  • Hardware wallets (future)
  • Custom key derivation schemes

Required Methods§

Source

fn get_next_keypair( &self, keypair_index: KeypairIndex, ) -> Result<Keypair, Error>

Get a keypair for receiving funds

For static key providers, this always returns the same keypair regardless of the index. For HD wallets, behavior depends on the keypair_index parameter.

§Arguments
  • keypair_index - Controls which keypair to return:
    • KeypairIndex::New: Increments the internal index and returns a new keypair
    • KeypairIndex::LastUnused: Returns the last unused keypair without incrementing
§Returns

A keypair to use for receiving funds

Source

fn get_keypair_for_path(&self, path: &[u32]) -> Result<Keypair, Error>

Get a keypair for a specific BIP32 derivation path

§Arguments
  • path - BIP32 derivation path as an array of child indexes
§Returns

A keypair derived at the specified path, or an error if derivation is not supported

Source

fn get_keypair_for_pk(&self, pk: &XOnlyPublicKey) -> Result<Keypair, Error>

Get a keypair for a specific public key

This is essential for HD wallets where you need to find the correct keypair for signing with a previously generated public key.

§Arguments
  • pk - The X-only public key to find the keypair for
§Returns

The keypair corresponding to the public key, or an error if not found

Source

fn get_cached_pks(&self) -> Result<Vec<XOnlyPublicKey>, Error>

Get all public keys that this provider currently knows about

For static key providers, this returns the single keypair’s public key. For HD wallets, this returns all public keys that have been derived and cached (i.e., keys generated via get_next_keypair).

This is useful for determining which keys are available for signing operations without having to search or derive new keys.

§Returns

A vector of X-only public keys known to this provider

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: KeyProvider + ?Sized> KeyProvider for Arc<T>

Implementors§