diff --git a/key_protocol/src/key_protocol_core/mod.rs b/key_protocol/src/key_protocol_core/mod.rs index ab5840c..eb946f9 100644 --- a/key_protocol/src/key_protocol_core/mod.rs +++ b/key_protocol/src/key_protocol_core/mod.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; +use anyhow::Result; use k256::AffinePoint; use serde::{Deserialize, Serialize}; @@ -23,16 +24,32 @@ impl NSSAUserData { } } + fn valid_key_transaction_pairing_check( + accounts_keys_map: &HashMap, + ) -> bool { + let mut check_res = true; + for (addr, key) in accounts_keys_map { + if &nssa::Address::from(&nssa::PublicKey::new_from_private_key(&key)) != addr { + check_res = false; + } + } + check_res + } + pub fn new_with_accounts( accounts_keys: HashMap, accounts: HashMap, - ) -> Self { + ) -> Result { + if !Self::valid_key_transaction_pairing_check(&accounts_keys) { + anyhow::bail!("Key transaction pairing check not satisfied, there is addresses, which is not derived from keys"); + } + let key_holder = KeyChain::new_os_random_with_accounts(accounts_keys); - Self { + Ok(Self { key_holder, accounts, - } + }) } pub fn generate_new_account(&mut self) -> nssa::Address { diff --git a/wallet/src/chain_storage/mod.rs b/wallet/src/chain_storage/mod.rs index 40c9ddc..dae0dc1 100644 --- a/wallet/src/chain_storage/mod.rs +++ b/wallet/src/chain_storage/mod.rs @@ -31,7 +31,7 @@ impl WalletChainStore { let utxo_commitments_store = UTXOCommitmentsMerkleTree::new(vec![]); Ok(Self { - user_data: NSSAUserData::new_with_accounts(accounts_keys, accounts), + user_data: NSSAUserData::new_with_accounts(accounts_keys, accounts)?, utxo_commitments_store, wallet_config: config, })