2025-09-02 09:01:33 +03:00
|
|
|
use std::{fs::File, io::Write, path::PathBuf, str::FromStr, sync::Arc};
|
2024-12-05 13:05:58 +02:00
|
|
|
|
2025-08-21 15:58:31 +03:00
|
|
|
use base64::Engine;
|
2025-07-22 15:22:20 +03:00
|
|
|
use common::{
|
2025-08-07 14:07:34 +03:00
|
|
|
ExecutionFailureKind,
|
2025-09-04 14:38:41 +03:00
|
|
|
sequencer_client::{SequencerClient, json::SendTxResponse},
|
2025-09-08 10:11:04 +03:00
|
|
|
transaction::{EncodedTransaction, NSSATransaction},
|
2025-07-22 15:22:20 +03:00
|
|
|
};
|
2024-12-25 09:50:54 +02:00
|
|
|
|
2025-08-07 14:07:34 +03:00
|
|
|
use anyhow::Result;
|
2025-08-11 08:55:08 +03:00
|
|
|
use chain_storage::WalletChainStore;
|
|
|
|
|
use config::WalletConfig;
|
2025-09-11 18:32:46 +03:00
|
|
|
use key_protocol::key_management::ephemeral_key_holder::EphemeralKeyHolder;
|
2025-08-06 14:56:58 +03:00
|
|
|
use log::info;
|
2025-08-13 13:42:00 +03:00
|
|
|
use nssa::Address;
|
2024-12-03 09:32:35 +02:00
|
|
|
|
2025-08-06 14:56:58 +03:00
|
|
|
use clap::{Parser, Subcommand};
|
|
|
|
|
|
2025-08-21 15:58:31 +03:00
|
|
|
use crate::{
|
|
|
|
|
helperfunctions::{
|
|
|
|
|
fetch_config, fetch_persistent_accounts, get_home, produce_account_addr_from_hex,
|
|
|
|
|
produce_data_for_storage,
|
|
|
|
|
},
|
|
|
|
|
poller::TxPoller,
|
2025-08-08 15:22:04 +03:00
|
|
|
};
|
2025-08-07 14:07:34 +03:00
|
|
|
|
|
|
|
|
pub const HOME_DIR_ENV_VAR: &str = "NSSA_WALLET_HOME_DIR";
|
2024-12-25 09:50:54 +02:00
|
|
|
|
2025-04-24 15:51:34 +03:00
|
|
|
pub mod chain_storage;
|
2024-12-03 09:32:35 +02:00
|
|
|
pub mod config;
|
2025-08-07 14:07:34 +03:00
|
|
|
pub mod helperfunctions;
|
2025-08-21 15:58:31 +03:00
|
|
|
pub mod poller;
|
2024-12-22 16:14:52 +02:00
|
|
|
|
2025-08-11 08:55:08 +03:00
|
|
|
pub struct WalletCore {
|
2025-08-11 09:05:18 +03:00
|
|
|
pub storage: WalletChainStore,
|
2025-08-21 15:58:31 +03:00
|
|
|
pub poller: TxPoller,
|
2024-12-20 11:02:12 +02:00
|
|
|
pub sequencer_client: Arc<SequencerClient>,
|
2024-12-05 13:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-11 08:55:08 +03:00
|
|
|
impl WalletCore {
|
2025-09-02 07:32:39 +03:00
|
|
|
pub fn start_from_config_update_chain(config: WalletConfig) -> Result<Self> {
|
2025-08-07 14:07:34 +03:00
|
|
|
let client = Arc::new(SequencerClient::new(config.sequencer_addr.clone())?);
|
2025-09-04 13:33:17 +03:00
|
|
|
let tx_poller = TxPoller::new(config.clone(), client.clone());
|
2024-12-05 13:05:58 +02:00
|
|
|
|
2025-09-02 09:01:33 +03:00
|
|
|
let mut storage = WalletChainStore::new(config)?;
|
|
|
|
|
|
|
|
|
|
let persistent_accounts = fetch_persistent_accounts()?;
|
|
|
|
|
for pers_acc_data in persistent_accounts {
|
|
|
|
|
storage.insert_account_data(pers_acc_data);
|
|
|
|
|
}
|
2024-12-05 13:05:58 +02:00
|
|
|
|
|
|
|
|
Ok(Self {
|
2025-08-11 09:05:18 +03:00
|
|
|
storage,
|
2025-08-21 15:58:31 +03:00
|
|
|
poller: tx_poller,
|
2024-12-20 11:02:12 +02:00
|
|
|
sequencer_client: client.clone(),
|
2024-12-05 13:05:58 +02:00
|
|
|
})
|
|
|
|
|
}
|
2024-12-22 16:14:52 +02:00
|
|
|
|
2025-09-04 13:33:17 +03:00
|
|
|
///Store persistent accounts at home
|
2025-08-20 17:16:51 +03:00
|
|
|
pub fn store_persistent_accounts(&self) -> Result<PathBuf> {
|
|
|
|
|
let home = get_home()?;
|
|
|
|
|
let accs_path = home.join("curr_accounts.json");
|
2025-08-22 15:58:43 +03:00
|
|
|
|
|
|
|
|
let data = produce_data_for_storage(&self.storage.user_data);
|
|
|
|
|
let accs = serde_json::to_vec_pretty(&data)?;
|
2025-08-20 17:16:51 +03:00
|
|
|
|
|
|
|
|
let mut accs_file = File::create(accs_path.as_path())?;
|
|
|
|
|
accs_file.write_all(&accs)?;
|
|
|
|
|
|
|
|
|
|
info!("Stored accounts data at {accs_path:#?}");
|
|
|
|
|
|
|
|
|
|
Ok(accs_path)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-11 18:32:46 +03:00
|
|
|
pub fn create_new_account_public(&mut self) -> Address {
|
2025-09-08 14:48:58 +03:00
|
|
|
self.storage
|
|
|
|
|
.user_data
|
|
|
|
|
.generate_new_public_transaction_private_key()
|
2024-12-25 09:50:54 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-11 18:32:46 +03:00
|
|
|
pub fn create_new_account_private(&mut self) -> Address {
|
|
|
|
|
self.storage
|
|
|
|
|
.user_data
|
|
|
|
|
.generate_new_privacy_preserving_transaction_key_chain()
|
2025-09-02 07:32:39 +03:00
|
|
|
}
|
|
|
|
|
|
2025-07-17 11:47:38 +03:00
|
|
|
pub async fn send_public_native_token_transfer(
|
|
|
|
|
&self,
|
2025-08-13 13:42:00 +03:00
|
|
|
from: Address,
|
|
|
|
|
to: Address,
|
2025-08-09 06:36:13 -03:00
|
|
|
balance_to_move: u128,
|
2025-07-17 11:47:38 +03:00
|
|
|
) -> Result<SendTxResponse, ExecutionFailureKind> {
|
2025-09-04 13:33:17 +03:00
|
|
|
let Ok(balance) = self.get_account_balance(from).await else {
|
|
|
|
|
return Err(ExecutionFailureKind::SequencerError);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if balance >= balance_to_move {
|
|
|
|
|
let Ok(nonces) = self.get_accounts_nonces(vec![from]).await else {
|
|
|
|
|
return Err(ExecutionFailureKind::SequencerError);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let addresses = vec![from, to];
|
|
|
|
|
let program_id = nssa::program::Program::authenticated_transfer_program().id();
|
|
|
|
|
let message = nssa::public_transaction::Message::try_new(
|
|
|
|
|
program_id,
|
|
|
|
|
addresses,
|
|
|
|
|
nonces,
|
|
|
|
|
balance_to_move,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
2025-09-08 15:03:02 +03:00
|
|
|
let signing_key = self.storage.user_data.get_pub_account_signing_key(&from);
|
2025-09-04 13:33:17 +03:00
|
|
|
|
|
|
|
|
let Some(signing_key) = signing_key else {
|
|
|
|
|
return Err(ExecutionFailureKind::KeyNotFoundError);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let witness_set =
|
|
|
|
|
nssa::public_transaction::WitnessSet::for_message(&message, &[signing_key]);
|
|
|
|
|
|
|
|
|
|
let tx = nssa::PublicTransaction::new(message, witness_set);
|
|
|
|
|
|
2025-09-08 09:09:32 +03:00
|
|
|
Ok(self.sequencer_client.send_tx_public(tx).await?)
|
2025-08-13 14:15:05 +03:00
|
|
|
} else {
|
2025-09-04 13:33:17 +03:00
|
|
|
Err(ExecutionFailureKind::InsufficientFundsError)
|
2025-08-11 14:01:13 +03:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-21 15:58:31 +03:00
|
|
|
|
2025-09-11 18:32:46 +03:00
|
|
|
pub async fn send_private_native_token_transfer(
|
|
|
|
|
&self,
|
|
|
|
|
from: Address,
|
|
|
|
|
to: Address,
|
|
|
|
|
balance_to_move: u128,
|
|
|
|
|
) -> Result<SendTxResponse, ExecutionFailureKind> {
|
|
|
|
|
let from_data = self.storage.user_data.get_private_account(&from);
|
|
|
|
|
let to_data = self.storage.user_data.get_private_account(&to);
|
|
|
|
|
|
|
|
|
|
let Some((from_keys, from_acc)) = from_data else {
|
|
|
|
|
return Err(ExecutionFailureKind::KeyNotFoundError);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let Some((to_keys, to_acc)) = to_data else {
|
|
|
|
|
return Err(ExecutionFailureKind::KeyNotFoundError);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if from_acc.balance >= balance_to_move {
|
|
|
|
|
let program = nssa::program::Program::authenticated_transfer_program();
|
|
|
|
|
let sender_commitment = nssa_core::Commitment::new(
|
|
|
|
|
&nssa_core::NullifierPublicKey(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_keys.nullifer_public_key,
|
|
|
|
|
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_keys.incoming_viewing_public_key);
|
|
|
|
|
|
|
|
|
|
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, 2],
|
|
|
|
|
&[from_acc.nonce + 1, to_acc.nonce + 1],
|
|
|
|
|
&[
|
|
|
|
|
(
|
|
|
|
|
nssa_core::NullifierPublicKey(from_keys.nullifer_public_key),
|
|
|
|
|
shared_secret,
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
nssa_core::NullifierPublicKey(to_keys.nullifer_public_key),
|
|
|
|
|
shared_secret,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
&[(
|
|
|
|
|
from_keys.private_key_holder.nullifier_secret_key,
|
|
|
|
|
state.get_proof_for_commitment(&sender_commitment).unwrap(),
|
|
|
|
|
)],
|
|
|
|
|
&program,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let message = Message::try_from_circuit_output(
|
|
|
|
|
vec![],
|
|
|
|
|
vec![],
|
|
|
|
|
vec![
|
|
|
|
|
(sender_keys.npk(), sender_keys.ivk(), epk_1),
|
|
|
|
|
(recipient_keys.npk(), recipient_keys.ivk(), epk_2),
|
|
|
|
|
],
|
|
|
|
|
output,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let witness_set = WitnessSet::for_message(&message, proof, &[]);
|
|
|
|
|
|
|
|
|
|
let tx = PrivacyPreservingTransaction::new(message, witness_set);
|
|
|
|
|
|
|
|
|
|
Ok(self.sequencer_client.send_tx_public(tx).await?)
|
|
|
|
|
} else {
|
|
|
|
|
Err(ExecutionFailureKind::InsufficientFundsError)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-02 09:01:33 +03:00
|
|
|
///Get account balance
|
|
|
|
|
pub async fn get_account_balance(&self, acc: Address) -> Result<u128> {
|
|
|
|
|
Ok(self
|
|
|
|
|
.sequencer_client
|
|
|
|
|
.get_account_balance(acc.to_string())
|
|
|
|
|
.await?
|
|
|
|
|
.balance)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///Get accounts nonces
|
|
|
|
|
pub async fn get_accounts_nonces(&self, accs: Vec<Address>) -> Result<Vec<u128>> {
|
|
|
|
|
Ok(self
|
|
|
|
|
.sequencer_client
|
|
|
|
|
.get_accounts_nonces(accs.into_iter().map(|acc| acc.to_string()).collect())
|
|
|
|
|
.await?
|
|
|
|
|
.nonces)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 15:58:43 +03:00
|
|
|
///Poll transactions
|
2025-09-08 10:11:04 +03:00
|
|
|
pub async fn poll_public_native_token_transfer(&self, hash: String) -> Result<NSSATransaction> {
|
2025-08-21 15:58:31 +03:00
|
|
|
let transaction_encoded = self.poller.poll_tx(hash).await?;
|
|
|
|
|
let tx_base64_decode =
|
|
|
|
|
base64::engine::general_purpose::STANDARD.decode(transaction_encoded)?;
|
2025-09-08 10:11:04 +03:00
|
|
|
let pub_tx = EncodedTransaction::from_bytes(tx_base64_decode);
|
2025-08-21 15:58:31 +03:00
|
|
|
|
2025-09-08 10:11:04 +03:00
|
|
|
Ok(NSSATransaction::try_from(&pub_tx)?)
|
2025-08-21 15:58:31 +03:00
|
|
|
}
|
2024-12-03 09:32:35 +02:00
|
|
|
}
|
2025-01-10 03:00:32 +02:00
|
|
|
|
2025-08-06 14:56:58 +03:00
|
|
|
///Represents CLI command for a wallet
|
|
|
|
|
#[derive(Subcommand, Debug, Clone)]
|
2025-08-07 14:07:34 +03:00
|
|
|
#[clap(about)]
|
2025-08-06 14:56:58 +03:00
|
|
|
pub enum Command {
|
2025-08-08 15:22:04 +03:00
|
|
|
///Send native token transfer from `from` to `to` for `amount`
|
2025-08-06 14:56:58 +03:00
|
|
|
SendNativeTokenTransfer {
|
2025-08-07 14:07:34 +03:00
|
|
|
///from - valid 32 byte hex string
|
2025-08-06 14:56:58 +03:00
|
|
|
#[arg(long)]
|
|
|
|
|
from: String,
|
2025-08-07 14:07:34 +03:00
|
|
|
///to - valid 32 byte hex string
|
2025-08-06 14:56:58 +03:00
|
|
|
#[arg(long)]
|
|
|
|
|
to: String,
|
2025-08-07 14:07:34 +03:00
|
|
|
///amount - amount of balance to move
|
2025-08-06 14:56:58 +03:00
|
|
|
#[arg(long)]
|
2025-08-09 06:36:13 -03:00
|
|
|
amount: u128,
|
2025-08-06 14:56:58 +03:00
|
|
|
},
|
2025-09-11 18:32:46 +03:00
|
|
|
///Register new public account
|
|
|
|
|
RegisterAccountPublic {},
|
|
|
|
|
///Register new private account
|
|
|
|
|
RegisterAccountPrivate {},
|
2025-08-20 17:16:51 +03:00
|
|
|
///Fetch transaction by `hash`
|
|
|
|
|
FetchTx {
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
tx_hash: String,
|
|
|
|
|
},
|
2025-09-02 09:01:33 +03:00
|
|
|
///Get account `addr` balance
|
|
|
|
|
GetAccountBalance {
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
addr: String,
|
|
|
|
|
},
|
|
|
|
|
///Get account `addr` nonce
|
|
|
|
|
GetAccountNonce {
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
addr: String,
|
|
|
|
|
},
|
2025-08-06 14:56:58 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-07 14:07:34 +03:00
|
|
|
///To execute commands, env var NSSA_WALLET_HOME_DIR must be set into directory with config
|
2025-08-06 14:56:58 +03:00
|
|
|
#[derive(Parser, Debug)]
|
2025-08-07 14:07:34 +03:00
|
|
|
#[clap(version, about)]
|
2025-08-06 14:56:58 +03:00
|
|
|
pub struct Args {
|
|
|
|
|
/// Wallet command
|
|
|
|
|
#[command(subcommand)]
|
|
|
|
|
pub command: Command,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn execute_subcommand(command: Command) -> Result<()> {
|
2025-08-20 17:16:51 +03:00
|
|
|
let wallet_config = fetch_config()?;
|
2025-09-02 09:01:33 +03:00
|
|
|
let mut wallet_core = WalletCore::start_from_config_update_chain(wallet_config)?;
|
2025-08-20 17:16:51 +03:00
|
|
|
|
2025-08-06 14:56:58 +03:00
|
|
|
match command {
|
2025-09-02 09:01:33 +03:00
|
|
|
Command::SendNativeTokenTransfer { from, to, amount } => {
|
2025-08-06 14:56:58 +03:00
|
|
|
let from = produce_account_addr_from_hex(from)?;
|
|
|
|
|
let to = produce_account_addr_from_hex(to)?;
|
|
|
|
|
|
|
|
|
|
let res = wallet_core
|
2025-09-02 09:01:33 +03:00
|
|
|
.send_public_native_token_transfer(from, to, amount)
|
2025-08-06 14:56:58 +03:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
info!("Results of tx send is {res:#?}");
|
2025-08-11 14:01:13 +03:00
|
|
|
|
2025-08-21 15:58:31 +03:00
|
|
|
let transfer_tx = wallet_core
|
|
|
|
|
.poll_public_native_token_transfer(res.tx_hash)
|
|
|
|
|
.await?;
|
|
|
|
|
|
2025-08-22 15:58:43 +03:00
|
|
|
info!("Transaction data is {transfer_tx:?}");
|
2025-08-08 10:07:10 +03:00
|
|
|
}
|
2025-09-11 18:32:46 +03:00
|
|
|
Command::RegisterAccountPublic {} => {
|
|
|
|
|
let addr = wallet_core.create_new_account_public();
|
2025-08-20 17:16:51 +03:00
|
|
|
|
2025-09-08 15:23:32 +03:00
|
|
|
let key = wallet_core
|
|
|
|
|
.storage
|
|
|
|
|
.user_data
|
|
|
|
|
.get_pub_account_signing_key(&addr);
|
2025-08-20 17:16:51 +03:00
|
|
|
|
|
|
|
|
info!("Generated new account with addr {addr:#?}");
|
|
|
|
|
info!("With key {key:#?}");
|
|
|
|
|
}
|
2025-09-11 18:32:46 +03:00
|
|
|
Command::RegisterAccountPrivate {} => {
|
|
|
|
|
let addr = wallet_core.create_new_account_private();
|
|
|
|
|
|
|
|
|
|
let (key, account) = wallet_core
|
|
|
|
|
.storage
|
|
|
|
|
.user_data
|
|
|
|
|
.get_private_account(&addr)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
info!("Generated new account with addr {addr:#?}");
|
|
|
|
|
info!("With key {key:#?}");
|
|
|
|
|
info!("With account {account:#?}");
|
|
|
|
|
}
|
2025-08-20 17:16:51 +03:00
|
|
|
Command::FetchTx { tx_hash } => {
|
|
|
|
|
let tx_obj = wallet_core
|
|
|
|
|
.sequencer_client
|
|
|
|
|
.get_transaction_by_hash(tx_hash)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
info!("Transaction object {tx_obj:#?}");
|
|
|
|
|
}
|
2025-09-02 09:01:33 +03:00
|
|
|
Command::GetAccountBalance { addr } => {
|
|
|
|
|
let addr = Address::from_str(&addr)?;
|
|
|
|
|
|
|
|
|
|
let balance = wallet_core.get_account_balance(addr).await?;
|
|
|
|
|
info!("Accounts {addr:#?} balance is {balance}");
|
|
|
|
|
}
|
|
|
|
|
Command::GetAccountNonce { addr } => {
|
|
|
|
|
let addr = Address::from_str(&addr)?;
|
|
|
|
|
|
|
|
|
|
let nonce = wallet_core.get_accounts_nonces(vec![addr]).await?[0];
|
|
|
|
|
info!("Accounts {addr:#?} nonce is {nonce}");
|
|
|
|
|
}
|
2025-08-06 14:56:58 +03:00
|
|
|
}
|
|
|
|
|
|
2025-08-20 17:16:51 +03:00
|
|
|
wallet_core.store_persistent_accounts()?;
|
|
|
|
|
|
2025-08-06 14:56:58 +03:00
|
|
|
Ok(())
|
|
|
|
|
}
|