diff --git a/key_protocol/src/key_management/mod.rs b/key_protocol/src/key_management/mod.rs index f22a99f..b642e13 100644 --- a/key_protocol/src/key_management/mod.rs +++ b/key_protocol/src/key_management/mod.rs @@ -39,6 +39,25 @@ impl KeyChain { } } + pub fn new_mnemonic(passphrase: String) -> Self { + //Currently dropping SeedHolder at the end of initialization. + //Now entirely sure if we need it in the future. + let seed_holder = SeedHolder::new_mnemonic(passphrase); + let secret_spending_key = seed_holder.produce_top_secret_key_holder(); + + let private_key_holder = secret_spending_key.produce_private_key_holder(); + + let nullifer_public_key = private_key_holder.generate_nullifier_public_key(); + let incoming_viewing_public_key = private_key_holder.generate_incoming_viewing_public_key(); + + Self { + secret_spending_key, + private_key_holder, + nullifer_public_key, + incoming_viewing_public_key, + } + } + pub fn calculate_shared_secret_receiver( &self, ephemeral_public_key_sender: EphemeralPublicKey, diff --git a/key_protocol/src/key_management/secret_holders.rs b/key_protocol/src/key_management/secret_holders.rs index 57dea90..f05a641 100644 --- a/key_protocol/src/key_management/secret_holders.rs +++ b/key_protocol/src/key_management/secret_holders.rs @@ -44,6 +44,18 @@ impl SeedHolder { } } + pub fn new_mnemonic(passphrase: String) -> Self { + let mut enthopy_bytes: [u8; 32] = [0; 32]; + OsRng.fill_bytes(&mut enthopy_bytes); + + let mnemonic = Mnemonic::from_entropy(&enthopy_bytes).unwrap(); + let seed_wide = mnemonic.to_seed(passphrase); + + Self { + seed: seed_wide.to_vec(), + } + } + pub fn generate_secret_spending_key_hash(&self) -> HashType { let mut hash = hmac_sha512::HMAC::mac(&self.seed, "NSSA_seed"); diff --git a/key_protocol/src/key_protocol_core/mod.rs b/key_protocol/src/key_protocol_core/mod.rs index b1ebe71..33a007a 100644 --- a/key_protocol/src/key_protocol_core/mod.rs +++ b/key_protocol/src/key_protocol_core/mod.rs @@ -14,6 +14,8 @@ pub struct NSSAUserData { pub pub_account_signing_keys: HashMap, ///Map for all user private accounts pub user_private_accounts: HashMap, + ///Mnemonic passphrase + pub password: String, } impl NSSAUserData { @@ -64,6 +66,31 @@ impl NSSAUserData { Ok(Self { pub_account_signing_keys: accounts_keys, user_private_accounts: accounts_key_chains, + password: "mnemonic".to_string(), + }) + } + + pub fn new_with_accounts_and_password( + accounts_keys: HashMap, + accounts_key_chains: HashMap, + password: String, + ) -> Result { + if !Self::valid_public_key_transaction_pairing_check(&accounts_keys) { + anyhow::bail!( + "Key transaction pairing check not satisfied, there is addresses, which is not derived from keys" + ); + } + + if !Self::valid_private_key_transaction_pairing_check(&accounts_key_chains) { + anyhow::bail!( + "Key transaction pairing check not satisfied, there is addresses, which is not derived from keys" + ); + } + + Ok(Self { + pub_account_signing_keys: accounts_keys, + user_private_accounts: accounts_key_chains, + password, }) } @@ -100,6 +127,21 @@ impl NSSAUserData { address } + /// Generated new private key for privacy preserving transactions + /// + /// Returns the address of new account + pub fn generate_new_privacy_preserving_transaction_key_chain_mnemonic( + &mut self, + ) -> nssa::Address { + let key_chain = KeyChain::new_mnemonic(self.password.clone()); + let address = nssa::Address::from(&key_chain.nullifer_public_key); + + self.user_private_accounts + .insert(address, (key_chain, nssa_core::account::Account::default())); + + address + } + /// Returns the signing key for public transaction signatures pub fn get_private_account( &self,