1use thiserror::Error;
2
3pub trait WalletManager {
5 type Wallet: Wallet;
7 type Address: crate::address::Address;
8 type TransactionId: crate::transaction::TransactionId;
9
10 fn create_wallet(&mut self) -> &Self::Wallet;
12
13 fn delete_and_transfer(
16 &mut self,
17 target_wallet: &Self::Wallet,
18 ) -> Result<&Self::Wallet, WalletManagerError>;
19
20 fn delete_and_distribute(
23 &mut self,
24 target_wallets: &[Self::Wallet],
25 ) -> Result<Vec<&Self::Wallet>, WalletManagerError>;
26
27 fn scale_to(&mut self, count: u64) -> Result<Vec<&Self::Wallet>, WalletManagerError>;
30
31 fn retrieve_address(&self) -> Result<Self::Address, WalletManagerError>;
33
34 fn send_transaction(
36 &self,
37 to: &Self::Address,
38 amount: u64,
39 ) -> Result<Self::TransactionId, WalletManagerError>;
40
41 fn send_transaction_from(
43 &self,
44 from: &Self::Address,
45 to: &Self::Address,
46 amount: u64,
47 ) -> Result<Self::TransactionId, WalletManagerError>;
48
49 fn list_wallets(&self) -> Result<Vec<&Self::Wallet>, WalletManagerError>;
51
52 fn retrieve_balance(&self, address: &Self::Address) -> Result<u64, WalletManagerError>;
54 fn retrieve_balances(&self) -> Result<Vec<(Self::Address, u64)>, WalletManagerError>;
56}
57
58#[derive(Debug, Error)]
60pub enum WalletManagerError {
61 #[error("Wallet error while scale: {0}")]
62 WalletError(#[from] crate::wallet::WalletError),
63}
64
65pub trait Wallet {
66 type Transaction: crate::transaction::Transaction;
68 type SignedTransaction: crate::transaction::SignedTransaction;
69 type Address: crate::address::Address;
70 type PublicKey: crate::keypair::PublicKey;
71 type SecretKey: crate::keypair::SecretKey;
72
73 fn address(&self) -> &Self::Address;
75
76 fn secret_key(&self) -> &Self::SecretKey;
78
79 fn pubkey(&self) -> &Self::PublicKey;
81
82 fn balance(&self) -> u64;
84
85 fn sign_transaction(
87 &self,
88 transaction: &Self::Transaction,
89 ) -> Result<Self::SignedTransaction, WalletError>;
90
91 fn verify_transaction_signature(
93 &self,
94 signed_transaction: &Self::SignedTransaction,
95 ) -> Result<bool, WalletError>;
96
97 fn transfer_funds(
99 &self,
100 to: &Self::Address,
101 amount: u64,
102 ) -> Result<Self::Transaction, WalletError>;
103
104 fn transaction_history(&self) -> Vec<Self::Transaction>;
106
107 fn has_sufficient_balance(&self, amount: u64) -> bool {
109 self.balance() >= amount
110 }
111}
112
113#[derive(Debug, Error)]
115pub enum WalletError {
116 #[error("Insufficient balance")]
118 InsufficientBalance,
119
120 #[error("Invalid address")]
122 InvalidAddress,
123
124 #[error("Transaction error: {0}")]
126 TransactionError(#[from] crate::transaction::TransactionError),
127}