Skip to main content

hyper_keyring/
lib.rs

1pub mod controller;
2pub mod hd;
3pub mod simple;
4pub mod vault;
5
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct KeyringAccount {
10    pub address: String,
11    pub label: Option<String>,
12}
13
14#[derive(Debug, thiserror::Error)]
15pub enum KeyringError {
16    #[error("Account not found: {0}")]
17    AccountNotFound(String),
18    #[error("Duplicate account: {0}")]
19    DuplicateAccount(String),
20    #[error("Invalid private key: {0}")]
21    InvalidKey(String),
22    #[error("Serialization error: {0}")]
23    SerializationError(String),
24    #[error("Signing error: {0}")]
25    SigningError(String),
26}
27
28pub trait Keyring: Send + Sync {
29    /// Keyring type identifier (e.g., "simple", "hd")
30    fn keyring_type(&self) -> &str;
31
32    /// Serialize keyring state (for encrypted storage)
33    fn serialize(&self) -> Result<Vec<u8>, KeyringError>;
34
35    /// Deserialize keyring state
36    fn deserialize(data: &[u8]) -> Result<Self, KeyringError>
37    where
38        Self: Sized;
39
40    /// Add accounts (for SimpleKeyring: import private keys)
41    fn add_accounts(
42        &mut self,
43        private_keys: &[String],
44    ) -> Result<Vec<KeyringAccount>, KeyringError>;
45
46    /// Get all accounts managed by this keyring
47    fn get_accounts(&self) -> Vec<KeyringAccount>;
48
49    /// Export a private key for a given address
50    fn export_account(&self, address: &str) -> Result<String, KeyringError>;
51
52    /// Remove an account by address
53    fn remove_account(&mut self, address: &str) -> Result<(), KeyringError>;
54
55    /// Sign an arbitrary message hash (32 bytes) with the key for the given address
56    fn sign_hash(&self, address: &str, hash: &[u8; 32]) -> Result<[u8; 65], KeyringError>;
57}
58
59// ---------------------------------------------------------------------------
60// Bridge: implement hyper_exchange::Signer for keyring types
61// ---------------------------------------------------------------------------
62
63macro_rules! impl_signer_for_keyring {
64    ($ty:ty) => {
65        impl hyper_exchange::Signer for $ty {
66            fn sign_hash(
67                &self,
68                address: &str,
69                hash: &[u8; 32],
70            ) -> Result<[u8; 65], hyper_exchange::ExchangeError> {
71                Keyring::sign_hash(self, address, hash)
72                    .map_err(|e| hyper_exchange::ExchangeError::SigningError(e.to_string()))
73            }
74        }
75    };
76}
77
78impl_signer_for_keyring!(controller::WalletController);
79impl_signer_for_keyring!(simple::SimpleKeyring);