use alloc::boxed::Box;
use alloc::collections::BTreeSet;
use alloc::string::String;
use alloc::vec::Vec;
use miden_protocol::account::AccountId;
use miden_protocol::account::auth::{AuthSecretKey, PublicKeyCommitment};
use miden_tx::auth::TransactionAuthenticator;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum KeyStoreError {
#[error("storage error: {0}")]
StorageError(String),
#[error("decoding error: {0}")]
DecodingError(String),
}
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
pub trait Keystore: TransactionAuthenticator {
async fn add_key(
&self,
key: &AuthSecretKey,
account_id: AccountId,
) -> Result<(), KeyStoreError>;
async fn remove_key(&self, pub_key: PublicKeyCommitment) -> Result<(), KeyStoreError>;
async fn get_key(
&self,
pub_key: PublicKeyCommitment,
) -> Result<Option<AuthSecretKey>, KeyStoreError>;
async fn get_account_key_commitments(
&self,
account_id: &AccountId,
) -> Result<BTreeSet<PublicKeyCommitment>, KeyStoreError>;
async fn get_account_id_by_key_commitment(
&self,
pub_key_commitment: PublicKeyCommitment,
) -> Result<Option<AccountId>, KeyStoreError>;
async fn get_keys_for_account(
&self,
account_id: &AccountId,
) -> Result<Vec<AuthSecretKey>, KeyStoreError> {
let commitments = self.get_account_key_commitments(account_id).await?;
let mut keys = Vec::with_capacity(commitments.len());
for commitment in commitments {
if let Some(key) = self.get_key(commitment).await? {
keys.push(key);
}
}
Ok(keys)
}
}
#[cfg(feature = "std")]
mod fs_keystore;
#[cfg(feature = "std")]
pub use fs_keystore::FilesystemKeyStore;