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>;
// Provided methods
fn supports_discovery(&self) -> bool { ... }
fn get_derivation_index_for_pk(&self, _pk: &XOnlyPublicKey) -> Option<u32> { ... }
fn derive_at_discovery_index(
&self,
_index: u32,
) -> Result<Option<Keypair>, Error> { ... }
fn cache_discovered_keypair(
&self,
_index: u32,
_kp: Keypair,
) -> Result<(), Error> { ... }
fn mark_as_used(&self, _pk: &XOnlyPublicKey) -> Result<(), 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§
Sourcefn get_next_keypair(
&self,
keypair_index: KeypairIndex,
) -> Result<Keypair, Error>
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 keypairKeypairIndex::LastUnused: Returns the last unused keypair without incrementing
§Returns
A keypair to use for receiving funds
Sourcefn get_keypair_for_pk(&self, pk: &XOnlyPublicKey) -> Result<Keypair, Error>
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
Sourcefn get_cached_pks(&self) -> Result<Vec<XOnlyPublicKey>, Error>
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
Provided Methods§
Sourcefn supports_discovery(&self) -> bool
fn supports_discovery(&self) -> bool
Returns true if this provider supports key discovery
HD wallets return true since they can derive and discover previously used keys. Static key providers return false (single key, nothing to discover).
Sourcefn get_derivation_index_for_pk(&self, _pk: &XOnlyPublicKey) -> Option<u32>
fn get_derivation_index_for_pk(&self, _pk: &XOnlyPublicKey) -> Option<u32>
Get the derivation index for a cached public key.
Returns None if the provider doesn’t support index-based derivation
or if the key is not in the cache.
Sourcefn derive_at_discovery_index(
&self,
_index: u32,
) -> Result<Option<Keypair>, Error>
fn derive_at_discovery_index( &self, _index: u32, ) -> Result<Option<Keypair>, Error>
Derive a keypair at a specific index without caching
This is used during discovery to check keys without affecting the provider’s state.
Returns None if the provider doesn’t support index-based derivation.
§Arguments
index- The derivation index (appended to base path for HD wallets)
Sourcefn cache_discovered_keypair(
&self,
_index: u32,
_kp: Keypair,
) -> Result<(), Error>
fn cache_discovered_keypair( &self, _index: u32, _kp: Keypair, ) -> Result<(), Error>
Cache a discovered keypair at the given index
This is called after discovery determines a key is “used” (has VTXOs). Also updates next_index if index >= current next_index to avoid collisions.
No-op for providers that don’t support discovery.
§Arguments
index- The derivation indexkp- The keypair to cache
fn mark_as_used(&self, _pk: &XOnlyPublicKey) -> Result<(), Error>
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".