2025-08-11 08:55:08 +03:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
2024-12-03 09:32:35 +02:00
|
|
|
|
2025-08-13 13:42:00 +03:00
|
|
|
use accounts::account_core::Account;
|
2024-12-05 13:05:58 +02:00
|
|
|
use anyhow::Result;
|
2025-08-11 08:55:08 +03:00
|
|
|
use common::merkle_tree_public::merkle_tree::UTXOCommitmentsMerkleTree;
|
2025-08-13 13:42:00 +03:00
|
|
|
use nssa::Address;
|
2025-07-15 12:49:19 +03:00
|
|
|
use sc_core::public_context::PublicSCContext;
|
2025-06-11 01:22:48 -04:00
|
|
|
use serde::{Deserialize, Serialize};
|
2024-12-03 09:32:35 +02:00
|
|
|
|
2025-08-11 08:55:08 +03:00
|
|
|
use crate::config::WalletConfig;
|
2024-12-30 08:00:24 +02:00
|
|
|
|
2024-12-03 09:32:35 +02:00
|
|
|
pub mod accounts_store;
|
|
|
|
|
|
2025-06-11 01:22:48 -04:00
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
|
pub struct AccMap {
|
|
|
|
|
pub acc_map: HashMap<String, Account>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<HashMap<[u8; 32], Account>> for AccMap {
|
|
|
|
|
fn from(value: HashMap<[u8; 32], Account>) -> Self {
|
|
|
|
|
AccMap {
|
|
|
|
|
acc_map: value
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(key, val)| (hex::encode(key), val))
|
|
|
|
|
.collect(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<AccMap> for HashMap<[u8; 32], Account> {
|
|
|
|
|
fn from(value: AccMap) -> Self {
|
|
|
|
|
value
|
|
|
|
|
.acc_map
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(key, val)| (hex::decode(key).unwrap().try_into().unwrap(), val))
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 08:55:08 +03:00
|
|
|
pub struct WalletChainStore {
|
2025-08-13 13:42:00 +03:00
|
|
|
pub acc_map: HashMap<Address, Account>,
|
2024-12-03 09:32:35 +02:00
|
|
|
pub utxo_commitments_store: UTXOCommitmentsMerkleTree,
|
2025-08-11 08:55:08 +03:00
|
|
|
pub wallet_config: WalletConfig,
|
2024-12-03 09:32:35 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-11 08:55:08 +03:00
|
|
|
impl WalletChainStore {
|
|
|
|
|
pub fn new(config: WalletConfig) -> Result<Self> {
|
2025-08-04 15:09:28 +03:00
|
|
|
let acc_map = HashMap::new();
|
|
|
|
|
let utxo_commitments_store = UTXOCommitmentsMerkleTree::new(vec![]);
|
2025-06-11 01:22:48 -04:00
|
|
|
|
2025-08-05 15:44:52 +03:00
|
|
|
Ok(Self {
|
|
|
|
|
acc_map,
|
|
|
|
|
utxo_commitments_store,
|
2025-08-11 08:55:08 +03:00
|
|
|
wallet_config: config,
|
2025-08-05 15:44:52 +03:00
|
|
|
})
|
2024-12-03 09:32:35 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-13 13:42:00 +03:00
|
|
|
pub fn produce_context(&self, caller: Address) -> PublicSCContext {
|
2025-04-04 12:11:54 +03:00
|
|
|
let mut account_masks = BTreeMap::new();
|
|
|
|
|
|
|
|
|
|
for (acc_addr, acc) in &self.acc_map {
|
|
|
|
|
account_masks.insert(*acc_addr, acc.make_account_public_mask());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PublicSCContext {
|
|
|
|
|
caller_address: caller,
|
|
|
|
|
caller_balance: self.acc_map.get(&caller).unwrap().balance,
|
|
|
|
|
account_masks,
|
|
|
|
|
comitment_store_root: self.utxo_commitments_store.get_root().unwrap_or([0; 32]),
|
2025-07-15 12:49:19 +03:00
|
|
|
commitments_tree: self.utxo_commitments_store.clone(),
|
2025-04-04 12:11:54 +03:00
|
|
|
}
|
|
|
|
|
}
|
2024-12-03 09:32:35 +02:00
|
|
|
}
|
2025-06-11 01:23:12 -04:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use accounts::account_core::Account;
|
2025-06-11 01:25:47 -04:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use tempfile::tempdir;
|
2025-06-11 01:23:12 -04:00
|
|
|
|
2025-07-29 14:20:03 +03:00
|
|
|
fn create_initial_accounts() -> Vec<Account> {
|
|
|
|
|
let initial_acc1 = serde_json::from_str(r#"{
|
2025-08-13 13:42:00 +03:00
|
|
|
"address": "1b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f",
|
2025-07-29 14:20:03 +03:00
|
|
|
"balance": 100,
|
2025-08-11 14:01:13 +03:00
|
|
|
"nonce": 0,
|
2025-07-29 14:20:03 +03:00
|
|
|
"key_holder": {
|
|
|
|
|
"nullifer_public_key": "03A340BECA9FAAB444CED0140681D72EA1318B5C611704FEE017DA9836B17DB718",
|
2025-08-12 12:18:13 -03:00
|
|
|
"pub_account_signing_key": [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
|
2025-07-29 14:20:03 +03:00
|
|
|
"top_secret_key_holder": {
|
|
|
|
|
"secret_spending_key": "7BC46784DB1BC67825D8F029436846712BFDF9B5D79EA3AB11D39A52B9B229D4"
|
|
|
|
|
},
|
|
|
|
|
"utxo_secret_key_holder": {
|
|
|
|
|
"nullifier_secret_key": "BB54A8D3C9C51B82C431082D1845A74677B0EF829A11B517E1D9885DE3139506",
|
|
|
|
|
"viewing_secret_key": "AD923E92F6A5683E30140CEAB2702AFB665330C1EE4EFA70FAF29767B6B52BAF"
|
|
|
|
|
},
|
|
|
|
|
"viewing_public_key": "0361220C5D277E7A1709340FD31A52600C1432B9C45B9BCF88A43581D58824A8B6"
|
|
|
|
|
},
|
|
|
|
|
"utxos": {}
|
|
|
|
|
}"#).unwrap();
|
|
|
|
|
|
|
|
|
|
let initial_acc2 = serde_json::from_str(r#"{
|
2025-08-13 13:42:00 +03:00
|
|
|
"address": "4d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d0766",
|
2025-07-29 14:20:03 +03:00
|
|
|
"balance": 200,
|
2025-08-11 14:01:13 +03:00
|
|
|
"nonce": 0,
|
2025-07-29 14:20:03 +03:00
|
|
|
"key_holder": {
|
|
|
|
|
"nullifer_public_key": "02172F50274DE67C4087C344F5D58E11DF761D90285B095060E0994FAA6BCDE271",
|
2025-08-12 12:18:13 -03:00
|
|
|
"pub_account_signing_key": [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
|
2025-07-29 14:20:03 +03:00
|
|
|
"top_secret_key_holder": {
|
|
|
|
|
"secret_spending_key": "80A186737C8D38B4288A03F0F589957D9C040D79C19F3E0CC4BA80F8494E5179"
|
|
|
|
|
},
|
|
|
|
|
"utxo_secret_key_holder": {
|
|
|
|
|
"nullifier_secret_key": "746928E63F0984F6F4818933493CE9C067562D9CB932FDC06D82C86CDF6D7122",
|
|
|
|
|
"viewing_secret_key": "89176CF4BC9E673807643FD52110EF99D4894335AFB10D881AC0B5041FE1FCB7"
|
|
|
|
|
},
|
|
|
|
|
"viewing_public_key": "026072A8F83FEC3472E30CDD4767683F30B91661D25B1040AD9A5FC2E01D659F99"
|
|
|
|
|
},
|
|
|
|
|
"utxos": {}
|
|
|
|
|
}"#).unwrap();
|
|
|
|
|
|
|
|
|
|
let initial_accounts = vec![initial_acc1, initial_acc2];
|
|
|
|
|
|
|
|
|
|
initial_accounts
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 08:55:08 +03:00
|
|
|
fn create_sample_wallet_config(home: PathBuf) -> WalletConfig {
|
|
|
|
|
WalletConfig {
|
2025-06-11 01:23:12 -04:00
|
|
|
home,
|
|
|
|
|
override_rust_log: None,
|
|
|
|
|
sequencer_addr: "http://127.0.0.1".to_string(),
|
|
|
|
|
seq_poll_timeout_secs: 1,
|
2025-07-29 14:20:03 +03:00
|
|
|
initial_accounts: create_initial_accounts(),
|
2025-06-11 01:23:12 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_new_initializes_correctly() {
|
|
|
|
|
let temp_dir = tempdir().unwrap();
|
|
|
|
|
let path = temp_dir.path();
|
|
|
|
|
|
2025-08-11 08:55:08 +03:00
|
|
|
let config = create_sample_wallet_config(path.to_path_buf());
|
2025-06-11 01:23:12 -04:00
|
|
|
|
2025-08-11 08:55:08 +03:00
|
|
|
let store = WalletChainStore::new(config.clone()).unwrap();
|
2025-06-11 01:23:12 -04:00
|
|
|
|
|
|
|
|
assert!(store.acc_map.is_empty());
|
|
|
|
|
assert_eq!(
|
|
|
|
|
store.utxo_commitments_store.get_root().unwrap_or([0; 32]),
|
|
|
|
|
[0; 32]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|