neo3/neo_wallets/wallet/
nep6wallet.rs

1use std::collections::HashMap;
2
3use crate::{neo_wallets::NEP6Account, ScryptParamsDef};
4use getset::{CopyGetters, Getters};
5use serde::{Deserialize, Serialize};
6
7/// Represents a NEP-6 wallet.
8#[derive(Serialize, Deserialize, Clone, Getters, CopyGetters)]
9#[getset(get = "pub", set = "pub")]
10pub struct Nep6Wallet {
11	/// The name of the wallet.
12	#[serde(rename = "name")]
13	pub(crate) name: String,
14	/// The version of the wallet.
15	#[serde(rename = "version")]
16	pub(crate) version: String,
17	/// The scrypt parameters used for encryption.
18	#[serde(rename = "scrypt")]
19	pub(crate) scrypt: ScryptParamsDef,
20	/// The accounts associated with the wallet.
21	#[serde(rename = "accounts")]
22	pub(crate) accounts: Vec<NEP6Account>,
23	/// Additional data associated with the wallet.
24	#[serde(rename = "extra")]
25	#[serde(skip_serializing_if = "Option::is_none")]
26	pub(crate) extra: Option<HashMap<String, String>>,
27}
28
29impl Nep6Wallet {
30	/// Creates a new NEP-6 wallet with the given parameters.
31	///
32	/// # Arguments
33	///
34	/// * `name` - The name of the wallet.
35	/// * `version` - The version of the wallet.
36	/// * `scrypt` - The scrypt parameters used for encryption.
37	/// * `accounts` - The accounts associated with the wallet.
38	/// * `extra` - Additional data associated with the wallet.
39	///
40	/// # Example
41	///
42	/// ```
43	/// use std::collections::HashMap;
44	/// use neo3::prelude::*;
45	/// use neo3::ScryptParamsDef;
46	///
47	/// let name = "MyWallet".to_string();
48	/// let version = "1.0".to_string();
49	/// let scrypt = ScryptParamsDef::default();
50	/// let accounts = vec![];
51	/// let extra = Some(HashMap::new());
52	///
53	/// let wallet = wallets::Nep6Wallet::new(name, version, scrypt, accounts, extra);
54	/// ```
55	pub fn new(
56		name: String,
57		version: String,
58		scrypt: ScryptParamsDef,
59		accounts: Vec<NEP6Account>,
60		extra: Option<HashMap<String, String>>,
61	) -> Self {
62		Self { name, version, scrypt, accounts, extra }
63	}
64}
65
66#[cfg(test)]
67mod tests {
68	use crate::{neo_wallets::Nep6Wallet, ScryptParamsDef};
69	use neo3::prelude::ContractParameterType;
70
71	#[test]
72	fn test_read_wallet() {
73		let data = include_str!("../../../test_resources/wallet/wallet.json");
74		let wallet: Nep6Wallet = serde_json::from_str(data)
75			.expect("Should be able to deserialize valid Nep6Wallet JSON in test");
76
77		assert_eq!(wallet.clone().name, "Wallet");
78		assert_eq!(wallet.clone().version, "1.0");
79		assert_eq!(wallet.clone().scrypt, ScryptParamsDef::default());
80		assert_eq!(wallet.clone().accounts.len(), 2);
81
82		let account1 = &wallet.accounts[0];
83
84		assert_eq!(account1.address, "NLnyLtep7jwyq1qhNPkwXbJpurC4jUT8ke");
85		assert_eq!(account1.label, Some("Account1".to_string()));
86		assert!(account1.is_default);
87		assert!(!account1.lock);
88		assert_eq!(
89			account1.key,
90			Some("6PYVEi6ZGdsLoCYbbGWqoYef7VWMbKwcew86m5fpxnZRUD8tEjainBgQW1".to_string())
91		);
92		assert!(account1.extra.is_none());
93
94		let contract1 =
95			account1.contract.as_ref().expect("Contract should be present in test account1");
96
97		assert_eq!(
98			contract1.script,
99			Some("DCECJJQloGtaH45hM/x5r6LCuEML+TJyl/F2dh33no2JKcULQZVEDXg=".to_string())
100		);
101		assert!(!contract1.is_deployed);
102
103		let parameter1 = &contract1.nep6_parameters[0];
104		assert_eq!(parameter1.param_name, "signature".to_string());
105		assert_eq!(parameter1.param_type, ContractParameterType::Signature);
106
107		let account2 = &wallet.accounts[1];
108
109		assert_eq!(account2.address, "NWcx4EfYdfqn5jNjDz8AHE6hWtWdUGDdmy".to_string());
110		assert_eq!(account2.label, Some("Account2".to_string()));
111		assert!(!account2.is_default);
112		assert!(!account2.lock);
113		assert_eq!(
114			account2.key,
115			Some("6PYSQWBqZE5oEFdMGCJ3xR7bz6ezz814oKE7GqwB9i5uhtUzkshe9B6YGB".to_string())
116		);
117		assert!(account2.extra.is_none());
118
119		let contract2 =
120			account2.contract.as_ref().expect("Contract should be present in test account2");
121
122		assert_eq!(
123			contract2.script,
124			Some("DCEDHMqqRt98SU9EJpjIwXwJMR42FcLcBCy9Ov6rpg+kB0ALQZVEDXg=".to_string())
125		);
126		assert!(!contract2.is_deployed);
127
128		let parameter2 = &contract2.nep6_parameters[0];
129		assert_eq!(parameter2.param_name, "signature".to_string());
130		assert_eq!(parameter2.param_type, ContractParameterType::Signature);
131	}
132}