aum_core/
wallet.rs

1use thiserror::Error;
2
3/// A trait to manage wallets.
4pub trait WalletManager {
5    /// Associated type representing a wallet.
6    type Wallet: Wallet;
7    type Address: crate::address::Address;
8    type TransactionId: crate::transaction::TransactionId;
9
10    /// Creates a new wallet and returns a reference to it.
11    fn create_wallet(&mut self) -> &Self::Wallet;
12
13    /// Deletes the current wallet and transfers its balance to a specified target wallet.
14    /// Returns a reference to the target wallet on success.
15    fn delete_and_transfer(
16        &mut self,
17        target_wallet: &Self::Wallet,
18    ) -> Result<&Self::Wallet, WalletManagerError>;
19
20    /// Deletes the current wallet and distributes its balance across specified target wallets.
21    /// Returns references to the target wallets on success.
22    fn delete_and_distribute(
23        &mut self,
24        target_wallets: &[Self::Wallet],
25    ) -> Result<Vec<&Self::Wallet>, WalletManagerError>;
26
27    /// Scales the wallet system to a specified number of wallets.
28    /// Returns references to the newly created wallets on success.
29    fn scale_to(&mut self, count: u64) -> Result<Vec<&Self::Wallet>, WalletManagerError>;
30
31    /// Retrieve address of the best wallet.
32    fn retrieve_address(&self) -> Result<Self::Address, WalletManagerError>;
33
34    /// Send transaction to a specified address with a given amount.
35    fn send_transaction(
36        &self,
37        to: &Self::Address,
38        amount: u64,
39    ) -> Result<Self::TransactionId, WalletManagerError>;
40
41    /// Send transaction from a specified address to another with a given amount.
42    fn send_transaction_from(
43        &self,
44        from: &Self::Address,
45        to: &Self::Address,
46        amount: u64,
47    ) -> Result<Self::TransactionId, WalletManagerError>;
48
49    /// List all available wallets.
50    fn list_wallets(&self) -> Result<Vec<&Self::Wallet>, WalletManagerError>;
51
52    /// Retrieve the balance of a specified wallet.
53    fn retrieve_balance(&self, address: &Self::Address) -> Result<u64, WalletManagerError>;
54    /// Retrieve balances of all wallets.
55    fn retrieve_balances(&self) -> Result<Vec<(Self::Address, u64)>, WalletManagerError>;
56}
57
58/// An enumeration of possible errors that can occur during scaling operations.
59#[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    // Associated types for transactions, signed transactions, and addresses.
67    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    // Returns the wallet's address.
74    fn address(&self) -> &Self::Address;
75
76    // Returns the wallet's secret key.
77    fn secret_key(&self) -> &Self::SecretKey;
78
79    // Returns the wallet's public key.
80    fn pubkey(&self) -> &Self::PublicKey;
81
82    // Returns the wallet's balance.
83    fn balance(&self) -> u64;
84
85    // Signs a transaction and returns a signed transaction or an error.
86    fn sign_transaction(
87        &self,
88        transaction: &Self::Transaction,
89    ) -> Result<Self::SignedTransaction, WalletError>;
90
91    // Verifies the signature of a signed transaction.
92    fn verify_transaction_signature(
93        &self,
94        signed_transaction: &Self::SignedTransaction,
95    ) -> Result<bool, WalletError>;
96
97    // Transfers funds to another address, returning a transaction or an error.
98    fn transfer_funds(
99        &self,
100        to: &Self::Address,
101        amount: u64,
102    ) -> Result<Self::Transaction, WalletError>;
103
104    // Retrieves the transaction history for the wallet.
105    fn transaction_history(&self) -> Vec<Self::Transaction>;
106
107    // Checks if the wallet has sufficient balance for a given amount.
108    fn has_sufficient_balance(&self, amount: u64) -> bool {
109        self.balance() >= amount
110    }
111}
112
113// Define an error type for wallet-related operations.
114#[derive(Debug, Error)]
115pub enum WalletError {
116    // Error for insufficient balance in the wallet.
117    #[error("Insufficient balance")]
118    InsufficientBalance,
119
120    // Error for an invalid address.
121    #[error("Invalid address")]
122    InvalidAddress,
123
124    // Error for transaction-related issues, wrapping a TransactionError.
125    #[error("Transaction error: {0}")]
126    TransactionError(#[from] crate::transaction::TransactionError),
127}