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 fn keyring_type(&self) -> &str;
31
32 fn serialize(&self) -> Result<Vec<u8>, KeyringError>;
34
35 fn deserialize(data: &[u8]) -> Result<Self, KeyringError>
37 where
38 Self: Sized;
39
40 fn add_accounts(
42 &mut self,
43 private_keys: &[String],
44 ) -> Result<Vec<KeyringAccount>, KeyringError>;
45
46 fn get_accounts(&self) -> Vec<KeyringAccount>;
48
49 fn export_account(&self, address: &str) -> Result<String, KeyringError>;
51
52 fn remove_account(&mut self, address: &str) -> Result<(), KeyringError>;
54
55 fn sign_hash(&self, address: &str, hash: &[u8; 32]) -> Result<[u8; 65], KeyringError>;
57}
58
59macro_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);