2025-10-14 15:29:18 +03:00
|
|
|
use std::{fs::File, io::Write, path::PathBuf, sync::Arc};
|
2024-12-05 13:05:58 +02:00
|
|
|
|
2025-09-05 22:50:10 -03:00
|
|
|
use base64::{Engine, engine::general_purpose::STANDARD as BASE64};
|
2025-07-22 15:22:20 +03:00
|
|
|
use common::{
|
2025-10-13 13:29:32 +03:00
|
|
|
sequencer_client::SequencerClient,
|
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-08-06 14:56:58 +03:00
|
|
|
use log::info;
|
2025-10-13 13:29:32 +03:00
|
|
|
use nssa::{Account, Address};
|
2024-12-03 09:32:35 +02:00
|
|
|
|
2025-08-06 14:56:58 +03:00
|
|
|
use clap::{Parser, Subcommand};
|
2025-10-13 13:29:32 +03:00
|
|
|
use nssa_core::Commitment;
|
2025-08-06 14:56:58 +03:00
|
|
|
|
2025-10-14 15:29:18 +03:00
|
|
|
use crate::cli::{
|
|
|
|
|
WalletSubcommand, chain::ChainSubcommand,
|
|
|
|
|
native_token_transfer_program::NativeTokenTransferProgramSubcommand,
|
|
|
|
|
pinata_program::PinataProgramSubcommand,
|
|
|
|
|
};
|
2025-08-21 15:58:31 +03:00
|
|
|
use crate::{
|
2025-10-13 17:25:36 +03:00
|
|
|
cli::token_program::TokenProgramSubcommand,
|
2025-08-21 15:58:31 +03:00
|
|
|
helperfunctions::{
|
2025-10-14 15:29:18 +03:00
|
|
|
fetch_config, fetch_persistent_accounts, get_home, produce_data_for_storage,
|
2025-08-21 15:58:31 +03:00
|
|
|
},
|
|
|
|
|
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;
|
2025-10-13 17:25:36 +03:00
|
|
|
pub mod cli;
|
2024-12-03 09:32:35 +02:00
|
|
|
pub mod config;
|
2025-08-07 14:07:34 +03:00
|
|
|
pub mod helperfunctions;
|
2025-10-13 13:29:32 +03:00
|
|
|
pub mod pinata_interactions;
|
2025-08-21 15:58:31 +03:00
|
|
|
pub mod poller;
|
2025-10-13 13:29:32 +03:00
|
|
|
pub mod token_program_interactions;
|
2025-09-16 14:53:00 +03:00
|
|
|
pub mod token_transfers;
|
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-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-09-05 22:50:10 -03:00
|
|
|
///Get account
|
2025-10-03 15:59:27 -03:00
|
|
|
pub async fn get_account_public(&self, addr: Address) -> Result<Account> {
|
2025-09-05 22:50:10 -03:00
|
|
|
let response = self.sequencer_client.get_account(addr.to_string()).await?;
|
|
|
|
|
Ok(response.account)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 15:59:27 -03:00
|
|
|
pub fn get_account_private(&self, addr: &Address) -> Option<Account> {
|
|
|
|
|
self.storage
|
|
|
|
|
.user_data
|
|
|
|
|
.user_private_accounts
|
|
|
|
|
.get(addr)
|
|
|
|
|
.map(|value| value.1.clone())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_private_account_commitment(&self, addr: &Address) -> Option<Commitment> {
|
|
|
|
|
let (keys, account) = self.storage.user_data.user_private_accounts.get(addr)?;
|
|
|
|
|
Some(Commitment::new(&keys.nullifer_public_key, account))
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 15:58:43 +03:00
|
|
|
///Poll transactions
|
2025-09-12 16:00:57 +03:00
|
|
|
pub async fn poll_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?;
|
2025-09-05 22:50:10 -03:00
|
|
|
let tx_base64_decode = BASE64.decode(transaction_encoded)?;
|
2025-09-25 11:53:42 +03:00
|
|
|
let pub_tx = borsh::from_slice::<EncodedTransaction>(&tx_base64_decode).unwrap();
|
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
|
|
|
}
|
2025-10-14 08:33:20 +03:00
|
|
|
|
|
|
|
|
pub async fn check_private_account_initialized(&self, addr: &Address) -> bool {
|
|
|
|
|
if let Some(acc_comm) = self.get_private_account_commitment(addr) {
|
|
|
|
|
matches!(
|
|
|
|
|
self.sequencer_client
|
|
|
|
|
.get_proof_for_commitment(acc_comm)
|
|
|
|
|
.await,
|
|
|
|
|
Ok(Some(_))
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn decode_insert_privacy_preserving_transaction_results(
|
|
|
|
|
&mut self,
|
|
|
|
|
tx: nssa::privacy_preserving_transaction::PrivacyPreservingTransaction,
|
|
|
|
|
acc_decode_data: &[(nssa_core::SharedSecretKey, Address)],
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
for (output_index, (secret, acc_address)) in acc_decode_data.iter().enumerate() {
|
|
|
|
|
let acc_ead = tx.message.encrypted_private_post_states[output_index].clone();
|
|
|
|
|
let acc_comm = tx.message.new_commitments[output_index].clone();
|
|
|
|
|
|
|
|
|
|
let res_acc = nssa_core::EncryptionScheme::decrypt(
|
|
|
|
|
&acc_ead.ciphertext,
|
2025-10-14 09:36:21 +03:00
|
|
|
secret,
|
2025-10-14 08:33:20 +03:00
|
|
|
&acc_comm,
|
|
|
|
|
output_index as u32,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
println!("Received new acc {res_acc:#?}");
|
|
|
|
|
|
|
|
|
|
self.storage
|
|
|
|
|
.insert_private_account_data(*acc_address, res_acc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println!("Transaction data is {:?}", tx.message);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
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-10-14 15:29:18 +03:00
|
|
|
///Transfer command
|
|
|
|
|
#[command(subcommand)]
|
|
|
|
|
Transfer(NativeTokenTransferProgramSubcommand),
|
|
|
|
|
///Chain command
|
|
|
|
|
#[command(subcommand)]
|
|
|
|
|
Chain(ChainSubcommand),
|
|
|
|
|
///Pinata command
|
|
|
|
|
#[command(subcommand)]
|
|
|
|
|
PinataProgram(PinataProgramSubcommand),
|
2025-10-14 10:18:54 +03:00
|
|
|
///Token command
|
2025-10-13 17:25:36 +03:00
|
|
|
#[command(subcommand)]
|
|
|
|
|
TokenProgram(TokenProgramSubcommand),
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-25 05:54:32 +03:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub enum SubcommandReturnValue {
|
|
|
|
|
PrivacyPreservingTransfer { tx_hash: String },
|
|
|
|
|
RegisterAccount { addr: nssa::Address },
|
2025-10-03 15:59:27 -03:00
|
|
|
Account(nssa::Account),
|
2025-09-25 05:54:32 +03:00
|
|
|
Empty,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn execute_subcommand(command: Command) -> Result<SubcommandReturnValue> {
|
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-09-25 05:54:32 +03:00
|
|
|
let subcommand_ret = match command {
|
2025-10-14 15:29:18 +03:00
|
|
|
Command::Transfer(transfer_subcommand) => {
|
|
|
|
|
transfer_subcommand
|
|
|
|
|
.handle_subcommand(&mut wallet_core)
|
|
|
|
|
.await?
|
|
|
|
|
}
|
|
|
|
|
Command::Chain(chain_subcommand) => {
|
|
|
|
|
chain_subcommand.handle_subcommand(&mut wallet_core).await?
|
|
|
|
|
}
|
|
|
|
|
Command::PinataProgram(pinata_subcommand) => {
|
|
|
|
|
pinata_subcommand
|
|
|
|
|
.handle_subcommand(&mut wallet_core)
|
|
|
|
|
.await?
|
2025-10-08 16:01:35 +03:00
|
|
|
}
|
2025-10-13 17:25:36 +03:00
|
|
|
Command::TokenProgram(token_subcommand) => {
|
|
|
|
|
token_subcommand.handle_subcommand(&mut wallet_core).await?
|
|
|
|
|
}
|
2025-09-25 05:54:32 +03:00
|
|
|
};
|
2025-08-06 14:56:58 +03:00
|
|
|
|
2025-09-25 05:54:32 +03:00
|
|
|
Ok(subcommand_ret)
|
2025-08-06 14:56:58 +03:00
|
|
|
}
|