diff --git a/common/src/lib.rs b/common/src/lib.rs index b7f45ab..01ae261 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -68,6 +68,8 @@ pub enum ExecutionFailureKind { DecodeError(String), #[error("Inputs amounts does not match outputs")] AmountMismatchError, + #[error("Accounts key not found")] + KeyNotFoundError, #[error("Sequencer client error: {0:?}")] SequencerClientError(#[from] SequencerClientError), #[error("Insufficient gas for operation")] diff --git a/integration_tests/configs/debug/wallet/wallet_config.json b/integration_tests/configs/debug/wallet/wallet_config.json index 6085a36..d888119 100644 --- a/integration_tests/configs/debug/wallet/wallet_config.json +++ b/integration_tests/configs/debug/wallet/wallet_config.json @@ -1,45 +1,113 @@ { - "home": "./node", - "override_rust_log": null, - "sequencer_addr": "http://127.0.0.1:3040", - "seq_poll_timeout_secs": 10, - "initial_accounts": [ - { - "address": "1b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f", - "balance": 10000, - "key_holder": { - "address": "1b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f", - "nullifer_public_key": "03A340BECA9FAAB444CED0140681D72EA1318B5C611704FEE017DA9836B17DB718", - "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], - "top_secret_key_holder": { - "secret_spending_key": "7BC46784DB1BC67825D8F029436846712BFDF9B5D79EA3AB11D39A52B9B229D4" + "home": "./node", + "override_rust_log": null, + "sequencer_addr": "http://127.0.0.1:3040", + "seq_poll_timeout_secs": 10, + "initial_accounts": [ + { + "address": "1b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f", + "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": 10000, + "nonce": 0, + "data": [] + } }, - "utxo_secret_key_holder": { - "nullifier_secret_key": "BB54A8D3C9C51B82C431082D1845A74677B0EF829A11B517E1D9885DE3139506", - "viewing_secret_key": "AD923E92F6A5683E30140CEAB2702AFB665330C1EE4EFA70FAF29767B6B52BAF" - }, - "viewing_public_key": "0361220C5D277E7A1709340FD31A52600C1432B9C45B9BCF88A43581D58824A8B6" - }, - "utxos": {} - }, - { - "address": "4d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d0766", - "balance": 20000, - "key_holder": { - "address": "4d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d0766", - "nullifer_public_key": "02172F50274DE67C4087C344F5D58E11DF761D90285B095060E0994FAA6BCDE271", - "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], - "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": {} - } - ] + { + "address": "4d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d0766", + "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": 20000, + "nonce": 0, + "data": [] + } + } + ] } diff --git a/integration_tests/src/lib.rs b/integration_tests/src/lib.rs index b5bb265..812e44f 100644 --- a/integration_tests/src/lib.rs +++ b/integration_tests/src/lib.rs @@ -156,7 +156,9 @@ pub async fn test_failure() { let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); - wallet::execute_subcommand(command).await.unwrap(); + let failed_send = wallet::execute_subcommand(command).await; + + assert!(failed_send.is_err()); info!("Waiting for next block creation"); tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; diff --git a/key_protocol/src/key_protocol_core/mod.rs b/key_protocol/src/key_protocol_core/mod.rs index cd9ab39..3b8092e 100644 --- a/key_protocol/src/key_protocol_core/mod.rs +++ b/key_protocol/src/key_protocol_core/mod.rs @@ -69,14 +69,26 @@ impl NSSAUserData { pub fn generate_new_account(&mut self) -> nssa::Address { let address = self.key_holder.generate_new_private_key(); - self.accounts.insert(address, nssa_core::account::Account::default()); + self.accounts + .insert(address, nssa_core::account::Account::default()); address } pub fn get_account_balance(&self, address: &nssa::Address) -> u128 { - self.accounts.get(address).map(|acc| acc.balance).unwrap_or(0) - } + self.accounts + .get(address) + .map(|acc| acc.balance) + .unwrap_or(0) + } + + pub fn get_account(&self, address: &nssa::Address) -> Option<&nssa_core::account::Account> { + self.accounts.get(address) + } + + pub fn get_account_signing_key(&self, address: &nssa::Address) -> Option<&nssa::PrivateKey> { + self.key_holder.get_pub_account_signing_key(address) + } pub fn encrypt_data( ephemeral_key_holder: &EphemeralKeyHolder, @@ -100,7 +112,7 @@ impl NSSAUserData { self.accounts .entry(address) .and_modify(|acc| acc.balance = new_balance) - .or_insert(nssa_core::account::Account::default()); + .or_default(); } //ToDo: Part of a private keys update diff --git a/wallet/Cargo.toml b/wallet/Cargo.toml index 13bea50..4a67791 100644 --- a/wallet/Cargo.toml +++ b/wallet/Cargo.toml @@ -22,6 +22,7 @@ risc0-zkvm = "2.3.1" hex.workspace = true actix-rt.workspace = true clap.workspace = true +nssa-core = { path = "../nssa/core" } [dependencies.key_protocol] path = "../key_protocol" diff --git a/wallet/src/chain_storage/accounts_store.rs b/wallet/src/chain_storage/accounts_store.rs deleted file mode 100644 index 243fe6a..0000000 --- a/wallet/src/chain_storage/accounts_store.rs +++ /dev/null @@ -1,113 +0,0 @@ -//TODO: NOT NSSA USER DATA, ACCOUNT -use key_protocol::key_protocol_core::NSSAUserData; -use nssa::Address; -use std::collections::HashMap; - -pub struct WalletAccountsStore { - pub accounts: HashMap
, -} - -impl WalletAccountsStore { - pub fn new() -> Self { - Self { - accounts: HashMap::new(), - } - } - - pub fn register_account(&mut self, account: NSSAUserData) { - self.accounts.insert(account.address, account); - } - - pub fn unregister_account(&mut self, account_addr: Address) { - self.accounts.remove(&account_addr); - } -} - -impl Default for WalletAccountsStore { - fn default() -> Self { - Self::new() - } -} - -#[cfg(test)] -mod tests { - use super::*; - use key_protocol::key_protocol_core::NSSAUserData; - /// Helper function to create a sample account - fn create_sample_account(balance: u64) -> NSSAUserData { - NSSAUserData::new_with_balance(balance) - } - - fn pad_to_32(slice: &[u8]) -> [u8; 32] { - let mut padded = [0u8; 32]; - let len = slice.len().min(32); - padded[..len].copy_from_slice(&slice[..len]); - padded - } - - #[test] - fn test_create_empty_store() { - let store = WalletAccountsStore::new(); - assert!(store.accounts.is_empty()); - } - - #[test] - fn test_register_account() { - let mut store = WalletAccountsStore::new(); - - let account = create_sample_account(100); - let account_addr = account.address; - - store.register_account(account); - - assert_eq!(store.accounts.len(), 1); - let stored_account = store.accounts.get(&account_addr).unwrap(); - assert_eq!(stored_account.balance, 100); - } - - #[test] - fn test_unregister_account() { - let mut store = WalletAccountsStore::new(); - - let account = create_sample_account(100); - let account_addr = account.address; - store.register_account(account); - - assert_eq!(store.accounts.len(), 1); - - store.unregister_account(account_addr); - assert!(store.accounts.is_empty()); - } - - #[test] - fn test_unregister_nonexistent_account() { - let mut store = WalletAccountsStore::new(); - - let account_addr: [u8; 32] = pad_to_32("nonexistent".to_string().as_bytes()); - store.unregister_account(Address::new(account_addr)); - - assert!(store.accounts.is_empty()); - } - - #[test] - fn test_register_multiple_accounts() { - let mut store = WalletAccountsStore::new(); - - let account1 = create_sample_account(100); - let account2 = create_sample_account(200); - - let address_1 = account1.address; - let address_2 = account2.address; - - store.register_account(account1); - store.register_account(account2); - - assert_eq!(store.accounts.len(), 2); - - let stored_account1 = store.accounts.get(&address_1).unwrap(); - let stored_account2 = store.accounts.get(&address_2).unwrap(); - - assert_eq!(stored_account1.balance, 100); - assert_eq!(stored_account2.balance, 200); - } -} diff --git a/wallet/src/chain_storage/mod.rs b/wallet/src/chain_storage/mod.rs index 80a14f8..40c9ddc 100644 --- a/wallet/src/chain_storage/mod.rs +++ b/wallet/src/chain_storage/mod.rs @@ -1,55 +1,37 @@ use std::collections::HashMap; -//TODO: NOT USER DATA, ACCOUNT use anyhow::Result; use common::merkle_tree_public::merkle_tree::UTXOCommitmentsMerkleTree; use key_protocol::key_protocol_core::NSSAUserData; -use nssa::Address; -use serde::{Deserialize, Serialize}; use crate::config::WalletConfig; -pub mod accounts_store; - -#[derive(Deserialize, Serialize)] -pub struct AccMap { - pub acc_map: HashMap