120 lines
4.1 KiB
Rust
Raw Normal View History

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-18 16:15:25 +03:00
use key_protocol::key_protocol_core::NSSAUserData;
use nssa::program::Program;
2024-12-03 09:32:35 +02:00
2025-09-11 18:32:46 +03:00
use crate::config::{InitialAccountData, PersistentAccountData, 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,
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-09-11 18:32:46 +03:00
let mut public_init_acc_map = HashMap::new();
let mut private_init_acc_map = HashMap::new();
for init_acc_data in config.initial_accounts.clone() {
match init_acc_data {
InitialAccountData::Public(data) => {
2025-09-30 16:45:25 -03:00
public_init_acc_map.insert(data.address.parse()?, data.pub_sign_key);
2025-09-11 18:32:46 +03:00
}
InitialAccountData::Private(data) => {
let mut account = data.account;
// TODO: Program owner is only known after code is compiled and can't be set in
// the config. Therefore we overwrite it here on startup. Fix this when program
// id can be fetched from the node and queried from the wallet.
account.program_owner = Program::authenticated_transfer_program().id();
private_init_acc_map.insert(data.address.parse()?, (data.key_chain, account));
2025-09-11 18:32:46 +03:00
}
}
}
2025-08-19 14:14:09 +03:00
2025-08-05 15:44:52 +03:00
Ok(Self {
2025-09-11 18:32:46 +03:00
user_data: NSSAUserData::new_with_accounts(public_init_acc_map, private_init_acc_map)?,
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-22 15:58:43 +03:00
2025-09-23 14:47:18 +03:00
pub fn insert_private_account_data(
&mut self,
addr: nssa::Address,
account: nssa_core::account::Account,
) {
2025-10-03 15:59:27 -03:00
println!("inserting at addres {}, this account {:?}", addr, account);
2025-09-23 14:47:18 +03:00
self.user_data
.user_private_accounts
.entry(addr)
.and_modify(|(_, acc)| *acc = account);
}
2025-09-02 09:01:33 +03:00
pub(crate) fn insert_account_data(&mut self, acc_data: PersistentAccountData) {
2025-09-11 18:32:46 +03:00
match acc_data {
PersistentAccountData::Public(acc_data) => {
self.user_data
.pub_account_signing_keys
.insert(acc_data.address, acc_data.pub_sign_key);
}
PersistentAccountData::Private(acc_data) => {
self.user_data
.user_private_accounts
.insert(acc_data.address, (acc_data.key_chain, acc_data.account));
}
}
2025-08-22 15:58:43 +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-09-17 13:57:31 +03:00
"Public": {
"address": "d07ad2e84b27fa00c262f0a1eea0ff35ca0973547e6a106f72f193c2dc838b44",
2025-09-17 13:57:31 +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]
2025-08-19 14:14:09 +03:00
}
2025-07-29 14:20:03 +03:00
}"#).unwrap();
let initial_acc2 = serde_json::from_str(r#"{
2025-09-17 13:57:31 +03:00
"Public": {
"address": "e7ae77c5ef1a05999344af499fc78a1705398d62ed06cf2e1479f6def89a39bc",
2025-09-17 13:57:31 +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]
2025-08-19 14:14:09 +03:00
}
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(),
2025-08-21 15:58:31 +03:00
seq_poll_timeout_millis: 12000,
seq_poll_max_blocks: 5,
seq_poll_max_retries: 10,
seq_poll_retry_delay_millis: 500,
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-09-03 10:29:51 +03:00
let _ = WalletChainStore::new(config.clone()).unwrap();
2025-06-11 01:23:12 -04:00
}
}