From c3ae7ab0a2e0083686e1e142189764fdb64c911a Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Fri, 26 Sep 2025 09:50:09 +0300 Subject: [PATCH] fix: account id merge fix --- .../key_management/ephemeral_key_holder.rs | 12 +++++++++++ key_protocol/src/key_management/mod.rs | 12 +++++++++++ wallet/src/token_transfers/deshielded.rs | 2 ++ wallet/src/token_transfers/private.rs | 5 +++++ wallet/src/token_transfers/shielded.rs | 20 ++++++++----------- 5 files changed, 39 insertions(+), 12 deletions(-) diff --git a/key_protocol/src/key_management/ephemeral_key_holder.rs b/key_protocol/src/key_management/ephemeral_key_holder.rs index b4835ff..63ae2b3 100644 --- a/key_protocol/src/key_management/ephemeral_key_holder.rs +++ b/key_protocol/src/key_management/ephemeral_key_holder.rs @@ -3,6 +3,7 @@ use nssa_core::{ NullifierPublicKey, SharedSecretKey, encryption::{EphemeralPublicKey, EphemeralSecretKey, IncomingViewingPublicKey}, }; +use rand::{RngCore, rngs::OsRng}; use sha2::Digest; use crate::key_management::secret_holders::OutgoingViewingSecretKey; @@ -13,6 +14,17 @@ pub struct EphemeralKeyHolder { ephemeral_secret_key: EphemeralSecretKey, } +pub fn produce_one_sided_shared_secret_receiver( + ipk: &IncomingViewingPublicKey, +) -> (SharedSecretKey, EphemeralPublicKey) { + let mut esk = [0; 32]; + OsRng.fill_bytes(&mut esk); + ( + SharedSecretKey::new(&esk, ipk), + EphemeralPublicKey::from_scalar(esk), + ) +} + impl EphemeralKeyHolder { pub fn new( receiver_nullifier_public_key: NullifierPublicKey, diff --git a/key_protocol/src/key_management/mod.rs b/key_protocol/src/key_management/mod.rs index 5f5f2aa..4154095 100644 --- a/key_protocol/src/key_management/mod.rs +++ b/key_protocol/src/key_management/mod.rs @@ -22,6 +22,18 @@ pub struct KeyChain { pub incoming_viewing_public_key: IncomingViewingPublicKey, } +pub fn produce_user_address_foreign_account( + npk: &NullifierPublicKey, + ipk: &IncomingViewingPublicKey, +) -> [u8; 32] { + let mut hasher = sha2::Sha256::new(); + + hasher.update(npk); + hasher.update(ipk.to_bytes()); + + ::from(hasher.finalize_fixed()) +} + impl KeyChain { pub fn new_os_random() -> Self { //Currently dropping SeedHolder at the end of initialization. diff --git a/wallet/src/token_transfers/deshielded.rs b/wallet/src/token_transfers/deshielded.rs index 7243146..adf0614 100644 --- a/wallet/src/token_transfers/deshielded.rs +++ b/wallet/src/token_transfers/deshielded.rs @@ -34,10 +34,12 @@ impl WalletCore { let sender_pre = nssa_core::account::AccountWithMetadata { account: from_acc.clone(), is_authorized: true, + account_id: (&from_keys.nullifer_public_key).into(), }; let recipient_pre = nssa_core::account::AccountWithMetadata { account: to_acc.clone(), is_authorized: false, + account_id: (&to).into(), }; //Move into different function diff --git a/wallet/src/token_transfers/private.rs b/wallet/src/token_transfers/private.rs index 919055c..11ae6dc 100644 --- a/wallet/src/token_transfers/private.rs +++ b/wallet/src/token_transfers/private.rs @@ -31,10 +31,13 @@ impl WalletCore { let sender_pre = nssa_core::account::AccountWithMetadata { account: from_acc.clone(), is_authorized: true, + account_id: (&from_keys.nullifer_public_key).into(), }; + let recipient_pre = nssa_core::account::AccountWithMetadata { account: to_acc.clone(), is_authorized: false, + account_id: (&to_npk).into(), }; let eph_holder = EphemeralKeyHolder::new( @@ -141,10 +144,12 @@ impl WalletCore { let sender_pre = nssa_core::account::AccountWithMetadata { account: from_acc.clone(), is_authorized: true, + account_id: (&from_keys.nullifer_public_key).into(), }; let recipient_pre = nssa_core::account::AccountWithMetadata { account: to_acc.clone(), is_authorized: true, + account_id: (&to_npk).into(), }; let eph_holder = EphemeralKeyHolder::new( diff --git a/wallet/src/token_transfers/shielded.rs b/wallet/src/token_transfers/shielded.rs index e0108c7..859c465 100644 --- a/wallet/src/token_transfers/shielded.rs +++ b/wallet/src/token_transfers/shielded.rs @@ -1,7 +1,6 @@ use common::{ExecutionFailureKind, sequencer_client::json::SendTxResponse}; -use k256::elliptic_curve::rand_core::{OsRng, RngCore}; +use key_protocol::key_management::ephemeral_key_holder::produce_one_sided_shared_secret_receiver; use nssa::Address; -use nssa_core::{SharedSecretKey, encryption::EphemeralPublicKey}; use crate::WalletCore; @@ -37,17 +36,15 @@ impl WalletCore { let sender_pre = nssa_core::account::AccountWithMetadata { account: from_acc.clone(), is_authorized: true, + account_id: (&from).into(), }; let recipient_pre = nssa_core::account::AccountWithMetadata { account: to_acc.clone(), is_authorized: true, + account_id: (&to_npk).into(), }; - //Move into different function - let mut esk = [0; 32]; - OsRng.fill_bytes(&mut esk); - let shared_secret = SharedSecretKey::new(&esk, &to_keys.incoming_viewing_public_key); - let epk = EphemeralPublicKey::from_scalar(esk); + let (shared_secret, epk) = produce_one_sided_shared_secret_receiver(&to_ipk); let (output, proof) = nssa::privacy_preserving_transaction::circuit::execute_and_prove( &[sender_pre, recipient_pre], @@ -124,17 +121,16 @@ impl WalletCore { let sender_pre = nssa_core::account::AccountWithMetadata { account: from_acc.clone(), is_authorized: true, + account_id: (&from).into(), }; + let recipient_pre = nssa_core::account::AccountWithMetadata { account: to_acc.clone(), is_authorized: false, + account_id: (&to_npk).into(), }; - //Move into different function - let mut esk = [0; 32]; - OsRng.fill_bytes(&mut esk); - let shared_secret = SharedSecretKey::new(&esk, &to_ipk); - let epk = EphemeralPublicKey::from_scalar(esk); + let (shared_secret, epk) = produce_one_sided_shared_secret_receiver(&to_ipk); let (output, proof) = nssa::privacy_preserving_transaction::circuit::execute_and_prove( &[sender_pre, recipient_pre],