2025-08-14 14:03:48 +03:00
|
|
|
use std::collections::HashMap;
|
2024-12-03 09:32:35 +02:00
|
|
|
|
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-18 16:15:25 +03:00
|
|
|
use key_protocol::key_protocol_core::NSSAUserData;
|
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
|
|
|
|
2025-08-11 08:55:08 +03:00
|
|
|
pub struct WalletChainStore {
|
2025-08-19 14:14:09 +03:00
|
|
|
pub user_data: NSSAUserData,
|
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-19 14:14:09 +03:00
|
|
|
let accounts_keys: HashMap<nssa::Address, nssa::PrivateKey> = config
|
|
|
|
|
.initial_accounts
|
|
|
|
|
.clone()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|init_acc_data| (init_acc_data.address, init_acc_data.pub_sign_key))
|
|
|
|
|
.collect();
|
|
|
|
|
|
2025-08-04 15:09:28 +03:00
|
|
|
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 {
|
2025-09-02 07:32:39 +03:00
|
|
|
user_data: NSSAUserData::new_with_accounts(accounts_keys)?,
|
2025-08-05 15:44:52 +03:00
|
|
|
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-06-11 01:23:12 -04:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2025-08-19 14:14:09 +03:00
|
|
|
use crate::config::InitialAccountData;
|
|
|
|
|
|
2025-06-11 01:23:12 -04:00
|
|
|
use super::*;
|
2025-06-11 01:25:47 -04:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use tempfile::tempdir;
|
2025-06-11 01:23:12 -04:00
|
|
|
|
2025-08-19 14:14:09 +03:00
|
|
|
fn create_initial_accounts() -> Vec<InitialAccountData> {
|
2025-07-29 14:20:03 +03:00
|
|
|
let initial_acc1 = serde_json::from_str(r#"{
|
2025-08-13 13:42:00 +03:00
|
|
|
"address": "1b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f",
|
2025-08-19 14:14:09 +03:00
|
|
|
"pub_sign_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],
|
|
|
|
|
"account": {
|
|
|
|
|
"program_owner": [0,0,0,0,0,0,0,0],
|
|
|
|
|
"balance": 100,
|
|
|
|
|
"nonce": 0,
|
|
|
|
|
"data": []
|
|
|
|
|
}
|
2025-07-29 14:20:03 +03:00
|
|
|
}"#).unwrap();
|
|
|
|
|
|
|
|
|
|
let initial_acc2 = serde_json::from_str(r#"{
|
2025-08-13 13:42:00 +03:00
|
|
|
"address": "4d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d0766",
|
2025-08-19 14:14:09 +03:00
|
|
|
"pub_sign_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],
|
|
|
|
|
"account": {
|
|
|
|
|
"program_owner": [0,0,0,0,0,0,0,0],
|
|
|
|
|
"balance": 100,
|
|
|
|
|
"nonce": 0,
|
|
|
|
|
"data": []
|
|
|
|
|
}
|
2025-07-29 14:20:03 +03:00
|
|
|
}"#).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_eq!(
|
|
|
|
|
store.utxo_commitments_store.get_root().unwrap_or([0; 32]),
|
|
|
|
|
[0; 32]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|