use random nonces for private accounts in wallet

This commit is contained in:
Sergio Chouhy 2025-10-03 01:30:40 -03:00
parent d54ea96bba
commit 9141fbf06c
5 changed files with 20 additions and 8 deletions

View File

@ -17,6 +17,7 @@ base64.workspace = true
k256 = { version = "0.13.3" }
bytemuck = "1.23.2"
hex.workspace = true
rand.workspace = true
[dependencies.key_protocol]
path = "../key_protocol"

View File

@ -1,4 +1,6 @@
use base64::{Engine, engine::general_purpose::STANDARD as BASE64};
use nssa_core::account::Nonce;
use rand::{RngCore, rngs::OsRng};
use std::{fs::File, io::BufReader, path::PathBuf, str::FromStr};
use anyhow::Result;
@ -82,6 +84,15 @@ pub fn produce_data_for_storage(user_data: &NSSAUserData) -> Vec<PersistentAccou
vec_for_storage
}
pub(crate) fn produce_random_nonces(size: usize) -> Vec<Nonce> {
let mut result = vec![[0; 16]; size];
result.iter_mut().for_each(|bytes| OsRng.fill_bytes(bytes));
result
.into_iter()
.map(Nonce::from_le_bytes)
.collect()
}
/// Human-readable representation of an account.
#[derive(Serialize)]
pub(crate) struct HumanReadableAccount {

View File

@ -4,7 +4,7 @@ use key_protocol::key_management::ephemeral_key_holder::EphemeralKeyHolder;
use nssa::Address;
use nssa_core::{SharedSecretKey, encryption::EphemeralPublicKey};
use crate::WalletCore;
use crate::{helperfunctions::produce_random_nonces, WalletCore};
impl WalletCore {
pub async fn send_deshielded_native_token_transfer(
@ -46,7 +46,7 @@ impl WalletCore {
&[sender_pre, recipient_pre],
&nssa::program::Program::serialize_instruction(balance_to_move).unwrap(),
&[1, 0],
&[from_acc.nonce + 1],
&produce_random_nonces(1),
&[(npk_from.clone(), shared_secret.clone())],
&[(
from_keys.private_key_holder.nullifier_secret_key,

View File

@ -2,7 +2,7 @@ use common::{ExecutionFailureKind, sequencer_client::json::SendTxResponse};
use key_protocol::key_management::ephemeral_key_holder::EphemeralKeyHolder;
use nssa::Address;
use crate::WalletCore;
use crate::{WalletCore, helperfunctions::produce_random_nonces};
impl WalletCore {
pub async fn send_private_native_token_transfer_outer_account(
@ -43,7 +43,7 @@ impl WalletCore {
&[sender_pre, recipient_pre],
&nssa::program::Program::serialize_instruction(balance_to_move).unwrap(),
&[1, 2],
&[from_acc.nonce + 1, to_acc.nonce + 1],
&produce_random_nonces(2),
&[
(from_npk.clone(), shared_secret_from.clone()),
(to_npk.clone(), shared_secret_to.clone()),
@ -144,7 +144,7 @@ impl WalletCore {
&[sender_pre, recipient_pre],
&nssa::program::Program::serialize_instruction(balance_to_move).unwrap(),
&[1, 1],
&[from_acc.nonce + 1, to_acc.nonce + 1],
&produce_random_nonces(2),
&[
(from_npk.clone(), shared_secret_from.clone()),
(to_npk.clone(), shared_secret_to.clone()),

View File

@ -4,7 +4,7 @@ use key_protocol::key_management::ephemeral_key_holder::{
};
use nssa::Address;
use crate::WalletCore;
use crate::{WalletCore, helperfunctions::produce_random_nonces};
impl WalletCore {
pub async fn send_shielded_native_token_transfer(
@ -49,7 +49,7 @@ impl WalletCore {
&[sender_pre, recipient_pre],
&nssa::program::Program::serialize_instruction(balance_to_move).unwrap(),
&[0, 1],
&[to_acc.nonce + 1],
&produce_random_nonces(1),
&[(to_npk.clone(), shared_secret.clone())],
&[(
to_keys.private_key_holder.nullifier_secret_key,
@ -138,7 +138,7 @@ impl WalletCore {
&[sender_pre, recipient_pre],
&nssa::program::Program::serialize_instruction(balance_to_move).unwrap(),
&[0, 2],
&[to_acc.nonce + 1],
&produce_random_nonces(1),
&[(to_npk.clone(), shared_secret.clone())],
&[],
&program,