neo3/neo_wallets/wallet_trait.rs
1use crate::{neo_protocol::AccountTrait, ScryptParamsDef};
2use primitive_types::H160;
3
4/// Represents the core functionalities of a cryptocurrency wallet.
5///
6/// This trait defines the common operations that a cryptocurrency wallet should support,
7/// including access to account details, wallet metadata (like name and version), and
8/// scrypt parameters for key derivation. It also provides methods for account management,
9/// such as adding, removing, and setting a default account.
10///
11/// # Type Parameters
12///
13/// - `Account`: The specific type of account managed by the wallet, constrained by
14/// the `AccountTrait` to ensure it adheres to the expected interface for accounts.
15///
16/// # Required Methods
17///
18/// - `name`, `version`: Accessors for the wallet's metadata.
19/// - `scrypt_params`: Access to the wallet's key derivation parameters.
20/// - `accounts`: Lists all accounts stored within the wallet.
21/// - `default_account`: Retrieves the wallet's default account.
22/// - `set_name`, `set_version`, `set_scrypt_params`, `set_default_account`: Mutators for
23/// the wallet's properties.
24/// - `add_account`, `remove_account`: Methods for account management within the wallet.
25///
26/// # Example
27///
28/// Implementing the `WalletTrait` for a simple wallet:
29///
30/// ```ignore
31/// struct SimpleWallet {
32/// name: String,
33/// version: String,
34/// scrypt_params: ScryptParamsDef,
35/// accounts: Vec<Account>,
36/// default_account: H160,
37/// }
38///
39/// impl WalletTrait for SimpleWallet {
40/// type Account = Account;
41///
42/// fn name(&self) -> &String {
43/// &self.name
44/// }
45///
46/// // Implementations for other methods follow...
47/// }
48/// ```
49///
50/// This trait allows for the abstraction over different wallet implementations,
51/// facilitating the use of different key storage mechanisms, account management strategies,
52/// and cryptographic algorithms.
53pub trait WalletTrait {
54 /// The type of account managed by the wallet.
55 type Account: AccountTrait;
56
57 /// Returns the name of the wallet.
58 fn name(&self) -> &String;
59
60 /// Returns the version of the wallet.
61 fn version(&self) -> &String;
62
63 /// Returns the scrypt parameters used for key derivation.
64 fn scrypt_params(&self) -> &ScryptParamsDef;
65
66 /// Returns a list of accounts stored in the wallet.
67 fn accounts(&self) -> Vec<Self::Account>;
68
69 /// Returns a reference to the default account of the wallet.
70 fn default_account(&self) -> &Self::Account;
71
72 /// Sets the name of the wallet.
73 fn set_name(&mut self, name: String);
74
75 /// Sets the version of the wallet.
76 fn set_version(&mut self, version: String);
77
78 /// Sets the scrypt parameters for the wallet.
79 fn set_scrypt_params(&mut self, params: ScryptParamsDef);
80
81 /// Sets the default account of the wallet.
82 fn set_default_account(&mut self, default_account: H160);
83
84 /// Adds a new account to the wallet.
85 fn add_account(&mut self, account: Self::Account);
86
87 /// Removes an account from the wallet by its hash.
88 ///
89 /// Returns the removed account if it existed, or `None` otherwise.
90 fn remove_account(&mut self, hash: &H160) -> Option<Self::Account>;
91}