From e0f5ac9e7cd731c9de50b03d9affa8c1ddf370e2 Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Tue, 23 Sep 2025 09:54:33 +0300 Subject: [PATCH] fix: all execution types tests added --- integration_tests/src/lib.rs | 217 +++++++++++++++++++++-- wallet/src/lib.rs | 149 +++++++++++++++- wallet/src/token_transfers/deshielded.rs | 118 ++---------- wallet/src/token_transfers/shielded.rs | 66 ++++--- 4 files changed, 399 insertions(+), 151 deletions(-) diff --git a/integration_tests/src/lib.rs b/integration_tests/src/lib.rs index 7f29eaa..696997c 100644 --- a/integration_tests/src/lib.rs +++ b/integration_tests/src/lib.rs @@ -6,6 +6,7 @@ use clap::Parser; use common::sequencer_client::SequencerClient; use log::{info, warn}; use nssa::program::Program; +use nssa_core::{NullifierPublicKey, encryption::shared_key_derivation::Secp256k1Point}; use sequencer_core::config::SequencerConfig; use sequencer_runner::startup_sequencer; use tempfile::TempDir; @@ -373,14 +374,18 @@ pub async fn test_success_private_transfer_to_another_owned_account() { } pub async fn test_success_private_transfer_to_another_foreign_account() { - let command = Command::SendNativeTokenTransferPrivate { + let to_npk_orig = NullifierPublicKey([42; 32]); + let to_npk = hex::encode(to_npk_orig.0); + let to_ipk = Secp256k1Point::from_scalar(to_npk_orig.0); + + let command = Command::SendNativeTokenTransferPrivateForeignAccount { from: ACC_SENDER_PRIVATE.to_string(), - to: ACC_RECEIVER_PRIVATE.to_string(), + to_npk, + to_ipk: hex::encode(to_ipk.0), amount: 100, }; let from = produce_account_addr_from_hex(ACC_SENDER_PRIVATE.to_string()).unwrap(); - let to = produce_account_addr_from_hex(ACC_RECEIVER_PRIVATE.to_string()).unwrap(); let wallet_config = fetch_config().unwrap(); @@ -407,16 +412,17 @@ pub async fn test_success_private_transfer_to_another_foreign_account() { }; let new_commitment2 = { - let to_acc = wallet_storage - .storage - .user_data - .get_private_account_mut(&to) - .unwrap(); + let mut to_acc = nssa_core::account::Account::default(); - to_acc.1.balance += 100; - to_acc.1.nonce += 1; + to_acc.program_owner = [ + 1793544791, 852173979, 3315478100, 4158236927, 146723505, 3793635251, 999304864, + 2535706995, + ]; - nssa_core::Commitment::new(&to_acc.0.nullifer_public_key, &to_acc.1) + to_acc.balance += 100; + to_acc.nonce += 1; + + nssa_core::Commitment::new(&to_npk_orig, &to_acc) }; let proof1 = seq_client @@ -436,6 +442,159 @@ pub async fn test_success_private_transfer_to_another_foreign_account() { info!("Success!"); } +pub async fn test_success_deshielded_transfer_to_another_account() { + let command = Command::SendNativeTokenTransferDeshielded { + from: ACC_SENDER_PRIVATE.to_string(), + to: ACC_RECEIVER.to_string(), + amount: 100, + }; + + let from = produce_account_addr_from_hex(ACC_SENDER_PRIVATE.to_string()).unwrap(); + + let wallet_config = fetch_config().unwrap(); + + let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); + + let mut wallet_storage = WalletCore::start_from_config_update_chain(wallet_config).unwrap(); + + wallet::execute_subcommand(command).await.unwrap(); + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + let new_commitment1 = { + let from_acc = wallet_storage + .storage + .user_data + .get_private_account_mut(&from) + .unwrap(); + + from_acc.1.balance -= 100; + from_acc.1.nonce += 1; + + nssa_core::Commitment::new(&from_acc.0.nullifer_public_key, &from_acc.1) + }; + + let proof1 = seq_client + .get_proof_for_commitment(new_commitment1) + .await + .unwrap() + .unwrap(); + + let acc_2_balance = seq_client + .get_account_balance(ACC_RECEIVER.to_string()) + .await + .unwrap(); + + println!("New proof is {proof1:#?}"); + assert_eq!(acc_2_balance.balance, 20100); + + info!("Success!"); +} + +pub async fn test_success_shielded_transfer_to_another_owned_account() { + let command = Command::SendNativeTokenTransferShielded { + from: ACC_SENDER.to_string(), + to: ACC_RECEIVER_PRIVATE.to_string(), + amount: 100, + }; + + let to = produce_account_addr_from_hex(ACC_RECEIVER_PRIVATE.to_string()).unwrap(); + + let wallet_config = fetch_config().unwrap(); + + let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); + + let mut wallet_storage = WalletCore::start_from_config_update_chain(wallet_config).unwrap(); + + wallet::execute_subcommand(command).await.unwrap(); + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + let new_commitment2 = { + let to_acc = wallet_storage + .storage + .user_data + .get_private_account_mut(&to) + .unwrap(); + + to_acc.1.balance += 100; + to_acc.1.nonce += 1; + + nssa_core::Commitment::new(&to_acc.0.nullifer_public_key, &to_acc.1) + }; + + let acc_1_balance = seq_client + .get_account_balance(ACC_SENDER.to_string()) + .await + .unwrap(); + + let proof2 = seq_client + .get_proof_for_commitment(new_commitment2) + .await + .unwrap() + .unwrap(); + + assert_eq!(acc_1_balance.balance, 9900); + + println!("New proof is {proof2:#?}"); + + info!("Success!"); +} + +pub async fn test_success_shielded_transfer_to_another_foreign_account() { + let to_npk_orig = NullifierPublicKey([42; 32]); + let to_npk = hex::encode(to_npk_orig.0); + let to_ipk = Secp256k1Point::from_scalar(to_npk_orig.0); + + let command = Command::SendNativeTokenTransferShieldedForeignAccount { + from: ACC_SENDER.to_string(), + to_npk, + to_ipk: hex::encode(to_ipk.0), + amount: 100, + }; + + let wallet_config = fetch_config().unwrap(); + + let seq_client = SequencerClient::new(wallet_config.sequencer_addr.clone()).unwrap(); + + wallet::execute_subcommand(command).await.unwrap(); + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + let new_commitment2 = { + let mut to_acc = nssa_core::account::Account::default(); + + to_acc.program_owner = [ + 1793544791, 852173979, 3315478100, 4158236927, 146723505, 3793635251, 999304864, + 2535706995, + ]; + + to_acc.balance += 100; + to_acc.nonce += 1; + + nssa_core::Commitment::new(&to_npk_orig, &to_acc) + }; + + let acc_1_balance = seq_client + .get_account_balance(ACC_SENDER.to_string()) + .await + .unwrap(); + + let proof2 = seq_client + .get_proof_for_commitment(new_commitment2) + .await + .unwrap() + .unwrap(); + + assert_eq!(acc_1_balance.balance, 9900); + println!("New proof is {proof2:#?}"); + + info!("Success!"); +} + macro_rules! test_cleanup_wrap { ($home_dir:ident, $test_func:ident) => {{ let res = pre_test($home_dir.clone()).await.unwrap(); @@ -480,6 +639,30 @@ pub async fn main_tests_runner() -> Result<()> { test_success_private_transfer_to_another_owned_account ); } + "test_success_private_transfer_to_another_foreign_account" => { + test_cleanup_wrap!( + home_dir, + test_success_private_transfer_to_another_foreign_account + ); + } + "test_success_deshielded_transfer_to_another_account" => { + test_cleanup_wrap!( + home_dir, + test_success_deshielded_transfer_to_another_account + ); + } + "test_success_shielded_transfer_to_another_owned_account" => { + test_cleanup_wrap!( + home_dir, + test_success_shielded_transfer_to_another_owned_account + ); + } + "test_success_shielded_transfer_to_another_foreign_account" => { + test_cleanup_wrap!( + home_dir, + test_success_shielded_transfer_to_another_foreign_account + ); + } "all" => { test_cleanup_wrap!(home_dir, test_success_move_to_another_account); test_cleanup_wrap!(home_dir, test_success); @@ -490,6 +673,18 @@ pub async fn main_tests_runner() -> Result<()> { home_dir, test_success_private_transfer_to_another_owned_account ); + test_cleanup_wrap!( + home_dir, + test_success_private_transfer_to_another_foreign_account + ); + test_cleanup_wrap!( + home_dir, + test_success_deshielded_transfer_to_another_account + ); + test_cleanup_wrap!( + home_dir, + test_success_shielded_transfer_to_another_owned_account + ); } _ => { anyhow::bail!("Unknown test name"); diff --git a/wallet/src/lib.rs b/wallet/src/lib.rs index 082b382..3e23b3b 100644 --- a/wallet/src/lib.rs +++ b/wallet/src/lib.rs @@ -166,6 +166,51 @@ pub enum Command { #[arg(long)] amount: u128, }, + ///Send native token transfer from `from` to `to` for `amount` + /// + /// Deshielded operation + SendNativeTokenTransferDeshielded { + ///from - valid 32 byte hex string + #[arg(long)] + from: String, + ///to - valid 32 byte hex string + #[arg(long)] + to: String, + ///amount - amount of balance to move + #[arg(long)] + amount: u128, + }, + ///Send native token transfer from `from` to `to` for `amount` + /// + /// Shielded operation + SendNativeTokenTransferShielded { + ///from - valid 32 byte hex string + #[arg(long)] + from: String, + ///to - valid 32 byte hex string + #[arg(long)] + to: String, + ///amount - amount of balance to move + #[arg(long)] + amount: u128, + }, + ///Send native token transfer from `from` to `to` for `amount` + /// + /// Shielded operation + SendNativeTokenTransferShieldedForeignAccount { + ///from - valid 32 byte hex string + #[arg(long)] + from: String, + ///to_npk - valid 32 byte hex string + #[arg(long)] + to_npk: String, + ///to_ipk - valid 33 byte hex string + #[arg(long)] + to_ipk: String, + ///amount - amount of balance to move + #[arg(long)] + amount: u128, + }, ///Register new public account RegisterAccountPublic {}, ///Register new private account @@ -257,7 +302,12 @@ pub async fn execute_subcommand(command: Command) -> Result<()> { println!("Transaction data is {:?}", tx.message); } } - Command::SendNativeTokenTransferPrivateForeignAccount { from, to_npk, to_ipk, amount } => { + Command::SendNativeTokenTransferPrivateForeignAccount { + from, + to_npk, + to_ipk, + amount, + } => { let from = produce_account_addr_from_hex(from)?; let to_npk_res = hex::decode(to_npk)?; let mut to_npk = [0; 32]; @@ -267,7 +317,8 @@ pub async fn execute_subcommand(command: Command) -> Result<()> { let to_ipk_res = hex::decode(to_ipk)?; let mut to_ipk = [0u8; 33]; to_ipk.copy_from_slice(&to_ipk_res); - let to_ipk = nssa_core::encryption::shared_key_derivation::Secp256k1Point(to_ipk.to_vec()); + let to_ipk = + nssa_core::encryption::shared_key_derivation::Secp256k1Point(to_ipk.to_vec()); let (res, secret) = wallet_core .send_private_native_token_transfer_outer_account(from, to_npk, to_ipk, amount) @@ -302,6 +353,100 @@ pub async fn execute_subcommand(command: Command) -> Result<()> { println!("Transaction data is {:?}", tx.message); } } + Command::SendNativeTokenTransferDeshielded { from, to, amount } => { + let from = produce_account_addr_from_hex(from)?; + let to = produce_account_addr_from_hex(to)?; + + let (res, secret) = wallet_core + .send_deshielded_native_token_transfer(from, to, amount) + .await?; + + println!("Results of tx send is {res:#?}"); + + let transfer_tx = wallet_core.poll_native_token_transfer(res.tx_hash).await?; + + if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { + let from_ebc = tx.message.encrypted_private_post_states[0].clone(); + let from_comm = tx.message.new_commitments[0].clone(); + + let res_acc = nssa_core::EncryptionScheme::decrypt( + &from_ebc.ciphertext, + &secret, + &from_comm, + 0, + ) + .unwrap(); + + println!("RES acc {res_acc:#?}"); + + println!("Transaction data is {:?}", tx.message); + } + } + Command::SendNativeTokenTransferShielded { from, to, amount } => { + let from = produce_account_addr_from_hex(from)?; + let to = produce_account_addr_from_hex(to)?; + + let (res, secret) = wallet_core + .send_shiedled_native_token_transfer(from, to, amount) + .await?; + + println!("Results of tx send is {res:#?}"); + + let transfer_tx = wallet_core.poll_native_token_transfer(res.tx_hash).await?; + + if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { + let to_ebc = tx.message.encrypted_private_post_states[0].clone(); + let to_comm = tx.message.new_commitments[0].clone(); + + let res_acc_to = + nssa_core::EncryptionScheme::decrypt(&to_ebc.ciphertext, &secret, &to_comm, 0) + .unwrap(); + + println!("RES acc to {res_acc_to:#?}"); + + println!("Transaction data is {:?}", tx.message); + } + } + Command::SendNativeTokenTransferShieldedForeignAccount { + from, + to_npk, + to_ipk, + amount, + } => { + let from = produce_account_addr_from_hex(from)?; + + let to_npk_res = hex::decode(to_npk)?; + let mut to_npk = [0; 32]; + to_npk.copy_from_slice(&to_npk_res); + let to_npk = nssa_core::NullifierPublicKey(to_npk); + + let to_ipk_res = hex::decode(to_ipk)?; + let mut to_ipk = [0u8; 33]; + to_ipk.copy_from_slice(&to_ipk_res); + let to_ipk = + nssa_core::encryption::shared_key_derivation::Secp256k1Point(to_ipk.to_vec()); + + let (res, secret) = wallet_core + .send_shielded_native_token_transfer_maybe_outer_account(from, to_npk, to_ipk, amount) + .await?; + + println!("Results of tx send is {res:#?}"); + + let transfer_tx = wallet_core.poll_native_token_transfer(res.tx_hash).await?; + + if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx { + let to_ebc = tx.message.encrypted_private_post_states[0].clone(); + let to_comm = tx.message.new_commitments[0].clone(); + + let res_acc_to = + nssa_core::EncryptionScheme::decrypt(&to_ebc.ciphertext, &secret, &to_comm, 0) + .unwrap(); + + println!("RES acc to {res_acc_to:#?}"); + + println!("Transaction data is {:?}", tx.message); + } + } Command::RegisterAccountPublic {} => { let addr = wallet_core.create_new_account_public(); diff --git a/wallet/src/token_transfers/deshielded.rs b/wallet/src/token_transfers/deshielded.rs index eacd02c..1daf367 100644 --- a/wallet/src/token_transfers/deshielded.rs +++ b/wallet/src/token_transfers/deshielded.rs @@ -1,117 +1,28 @@ use common::{ExecutionFailureKind, sequencer_client::json::SendTxResponse}; -use key_protocol::key_management::ephemeral_key_holder::EphemeralKeyHolder; +use k256::elliptic_curve::rand_core::{OsRng, RngCore}; use nssa::Address; +use nssa_core::{SharedSecretKey, encryption::EphemeralPublicKey}; use crate::WalletCore; impl WalletCore { - pub async fn send_deshielded_native_token_transfer_maybe_outer_account( - &self, - from: Address, - to_addr: Address, - to_npk: nssa_core::NullifierPublicKey, - to_ipk: nssa_core::encryption::IncomingViewingPublicKey, - balance_to_move: u128, - ) -> Result { - let from_data = self.storage.user_data.get_private_account(&from); - - let Some((from_keys, from_acc)) = from_data else { - return Err(ExecutionFailureKind::KeyNotFoundError); - }; - - let to_acc = nssa_core::account::Account::default(); - - if from_acc.balance >= balance_to_move { - let program = nssa::program::Program::authenticated_transfer_program(); - let sender_commitment = - nssa_core::Commitment::new(&from_keys.nullifer_public_key, from_acc); - - let sender_pre = nssa_core::account::AccountWithMetadata { - account: from_acc.clone(), - is_authorized: true, - }; - let recipient_pre = nssa_core::account::AccountWithMetadata { - account: to_acc.clone(), - is_authorized: false, - }; - - let eph_holder = EphemeralKeyHolder::new( - to_npk.clone(), - from_keys.private_key_holder.outgoing_viewing_secret_key, - from_acc.nonce.try_into().unwrap(), - ); - - let shared_secret = eph_holder.calculate_shared_secret_sender(to_ipk.clone()); - - let (output, proof) = nssa::privacy_preserving_transaction::circuit::execute_and_prove( - &[sender_pre, recipient_pre], - &nssa::program::Program::serialize_instruction(balance_to_move).unwrap(), - &[1, 0], - &[from_acc.nonce + 1], - &[(from_keys.nullifer_public_key.clone(), shared_secret.clone())], - &[( - from_keys.private_key_holder.nullifier_secret_key, - self.sequencer_client - .get_proof_for_commitment(sender_commitment) - .await - .unwrap() - .unwrap(), - )], - &program, - ) - .unwrap(); - - let message = - nssa::privacy_preserving_transaction::message::Message::try_from_circuit_output( - vec![to_addr], - vec![], - vec![( - from_keys.nullifer_public_key.clone(), - from_keys.incoming_viewing_public_key.clone(), - eph_holder.generate_ephemeral_public_key(), - )], - output, - ) - .unwrap(); - - let witness_set = - nssa::privacy_preserving_transaction::witness_set::WitnessSet::for_message( - &message, - proof, - &[], - ); - - let tx = nssa::privacy_preserving_transaction::PrivacyPreservingTransaction::new( - message, - witness_set, - ); - - Ok(self.sequencer_client.send_tx_private(tx).await?) - } else { - Err(ExecutionFailureKind::InsufficientFundsError) - } - } - pub async fn send_deshielded_native_token_transfer( &self, from: Address, to: Address, balance_to_move: u128, - ) -> Result { + ) -> Result<(SendTxResponse, nssa_core::SharedSecretKey), ExecutionFailureKind> { let from_data = self.storage.user_data.get_private_account(&from); - let to_data = self.storage.user_data.get_private_account(&to); + let to_data = self.get_account(to).await; let Some((from_keys, from_acc)) = from_data else { return Err(ExecutionFailureKind::KeyNotFoundError); }; - let Some((to_keys, to_acc)) = to_data else { + let Ok(to_acc) = to_data else { return Err(ExecutionFailureKind::KeyNotFoundError); }; - let to_npk = to_keys.nullifer_public_key.clone(); - let to_ipk = to_keys.incoming_viewing_public_key.clone(); - if from_acc.balance >= balance_to_move { let program = nssa::program::Program::authenticated_transfer_program(); let sender_commitment = @@ -126,13 +37,11 @@ impl WalletCore { is_authorized: false, }; - let eph_holder = EphemeralKeyHolder::new( - to_npk.clone(), - from_keys.private_key_holder.outgoing_viewing_secret_key, - from_acc.nonce.try_into().unwrap(), - ); - - let shared_secret = eph_holder.calculate_shared_secret_sender(to_ipk.clone()); + //Move into different function + let mut esk = [0; 32]; + OsRng.fill_bytes(&mut esk); + let shared_secret = SharedSecretKey::new(&esk, &from_keys.incoming_viewing_public_key); + let epk = EphemeralPublicKey::from_scalar(esk); let (output, proof) = nssa::privacy_preserving_transaction::circuit::execute_and_prove( &[sender_pre, recipient_pre], @@ -159,7 +68,7 @@ impl WalletCore { vec![( from_keys.nullifer_public_key.clone(), from_keys.incoming_viewing_public_key.clone(), - eph_holder.generate_ephemeral_public_key(), + epk, )], output, ) @@ -177,7 +86,10 @@ impl WalletCore { witness_set, ); - Ok(self.sequencer_client.send_tx_private(tx).await?) + Ok(( + self.sequencer_client.send_tx_private(tx).await?, + shared_secret, + )) } else { Err(ExecutionFailureKind::InsufficientFundsError) } diff --git a/wallet/src/token_transfers/shielded.rs b/wallet/src/token_transfers/shielded.rs index 24a5292..1dfdb0b 100644 --- a/wallet/src/token_transfers/shielded.rs +++ b/wallet/src/token_transfers/shielded.rs @@ -1,6 +1,7 @@ use common::{ExecutionFailureKind, sequencer_client::json::SendTxResponse}; -use key_protocol::key_management::ephemeral_key_holder::EphemeralKeyHolder; +use k256::elliptic_curve::rand_core::{OsRng, RngCore}; use nssa::Address; +use nssa_core::{SharedSecretKey, encryption::EphemeralPublicKey}; use crate::WalletCore; @@ -10,11 +11,11 @@ impl WalletCore { from: Address, to: Address, balance_to_move: u128, - ) -> Result { - let from_data = self.storage.user_data.get_private_account(&from); + ) -> Result<(SendTxResponse, nssa_core::SharedSecretKey), ExecutionFailureKind> { + let from_data = self.get_account(from).await; let to_data = self.storage.user_data.get_private_account(&to); - let Some((from_keys, from_acc)) = from_data else { + let Ok(from_acc) = from_data else { return Err(ExecutionFailureKind::KeyNotFoundError); }; @@ -37,23 +38,22 @@ impl WalletCore { }; let recipient_pre = nssa_core::account::AccountWithMetadata { account: to_acc.clone(), - is_authorized: false, + is_authorized: true, }; - let eph_holder = EphemeralKeyHolder::new( - to_npk.clone(), - from_keys.private_key_holder.outgoing_viewing_secret_key, - from_acc.nonce.try_into().unwrap(), - ); - let shared_secret = eph_holder.calculate_shared_secret_sender(to_ipk.clone()); + //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 (output, proof) = nssa::privacy_preserving_transaction::circuit::execute_and_prove( &[sender_pre, recipient_pre], &nssa::program::Program::serialize_instruction(balance_to_move).unwrap(), &[0, 1], &[to_acc.nonce + 1], - &[(to_npk.clone(), shared_secret)], + &[(to_npk.clone(), shared_secret.clone())], &[( to_keys.private_key_holder.nullifier_secret_key, self.sequencer_client @@ -70,11 +70,7 @@ impl WalletCore { nssa::privacy_preserving_transaction::message::Message::try_from_circuit_output( vec![from], vec![from_acc.nonce], - vec![( - to_npk.clone(), - to_ipk.clone(), - eph_holder.generate_ephemeral_public_key(), - )], + vec![(to_npk.clone(), to_ipk.clone(), epk)], output, ) .unwrap(); @@ -97,7 +93,10 @@ impl WalletCore { witness_set, ); - Ok(self.sequencer_client.send_tx_private(tx).await?) + Ok(( + self.sequencer_client.send_tx_private(tx).await?, + shared_secret, + )) } else { Err(ExecutionFailureKind::InsufficientFundsError) } @@ -109,10 +108,10 @@ impl WalletCore { to_npk: nssa_core::NullifierPublicKey, to_ipk: nssa_core::encryption::IncomingViewingPublicKey, balance_to_move: u128, - ) -> Result { - let from_data = self.storage.user_data.get_private_account(&from); + ) -> Result<(SendTxResponse, nssa_core::SharedSecretKey), ExecutionFailureKind> { + let from_data = self.get_account(from).await; - let Some((from_keys, from_acc)) = from_data else { + let Ok(from_acc) = from_data else { return Err(ExecutionFailureKind::KeyNotFoundError); }; @@ -130,20 +129,18 @@ impl WalletCore { is_authorized: false, }; - let eph_holder = EphemeralKeyHolder::new( - to_npk.clone(), - from_keys.private_key_holder.outgoing_viewing_secret_key, - from_acc.nonce.try_into().unwrap(), - ); - - let shared_secret = eph_holder.calculate_shared_secret_sender(to_ipk.clone()); + //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 (output, proof) = nssa::privacy_preserving_transaction::circuit::execute_and_prove( &[sender_pre, recipient_pre], &nssa::program::Program::serialize_instruction(balance_to_move).unwrap(), &[0, 2], &[to_acc.nonce + 1], - &[(to_npk.clone(), shared_secret)], + &[(to_npk.clone(), shared_secret.clone())], &[], &program, ) @@ -153,11 +150,7 @@ impl WalletCore { nssa::privacy_preserving_transaction::message::Message::try_from_circuit_output( vec![from], vec![from_acc.nonce], - vec![( - to_npk.clone(), - to_ipk.clone(), - eph_holder.generate_ephemeral_public_key(), - )], + vec![(to_npk.clone(), to_ipk.clone(), epk)], output, ) .unwrap(); @@ -180,7 +173,10 @@ impl WalletCore { witness_set, ); - Ok(self.sequencer_client.send_tx_private(tx).await?) + Ok(( + self.sequencer_client.send_tx_private(tx).await?, + shared_secret, + )) } else { Err(ExecutionFailureKind::InsufficientFundsError) }