lssa/wallet/src/lib.rs

743 lines
24 KiB
Rust
Raw Normal View History

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
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-13 17:25:36 +03:00
use crate::cli::WalletSubcommand;
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::{
HumanReadableAccount, fetch_config, fetch_persistent_accounts, get_home,
2025-10-13 13:29:32 +03:00
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;
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)
}
///Get account
2025-10-03 15:59:27 -03:00
pub async fn get_account_public(&self, addr: Address) -> Result<Account> {
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?;
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-08-08 15:22:04 +03:00
///Send native token transfer from `from` to `to` for `amount`
2025-09-12 16:00:57 +03:00
///
/// Public operation
SendNativeTokenTransferPublic {
///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`
///
/// Private operation
2025-09-30 14:44:43 -03:00
SendNativeTokenTransferPrivateOwnedAccount {
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)]
amount: u128,
2025-08-06 14:56:58 +03:00
},
2025-09-22 16:38:25 +03:00
///Send native token transfer from `from` to `to` for `amount`
///
/// Private operation
SendNativeTokenTransferPrivateForeignAccount {
///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,
},
2025-09-23 09:54:33 +03:00
///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 {
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)]
amount: u128,
2025-08-06 14:56:58 +03:00
},
2025-09-23 09:54:33 +03:00
///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,
},
2025-09-24 12:57:17 +03:00
///Claim account `acc_addr` generated in transaction `tx_hash`, using secret `sh_secret` at ciphertext id `ciph_id`
2025-10-03 17:06:45 -03:00
FetchPrivateAccount {
2025-09-24 12:57:17 +03:00
///tx_hash - valid 32 byte hex string
#[arg(long)]
tx_hash: String,
///acc_addr - valid 32 byte hex string
#[arg(long)]
acc_addr: String,
2025-10-03 17:06:45 -03:00
///output_id - id of the output in the transaction
2025-09-24 12:57:17 +03:00
#[arg(long)]
2025-10-03 17:06:45 -03:00
output_id: usize,
2025-09-24 12:57:17 +03:00
},
2025-10-03 15:59:27 -03:00
///Get private account with `addr` from storage
GetPrivateAccount {
#[arg(short, long)]
addr: String,
},
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
2025-10-03 17:06:45 -03:00
GetPublicAccountBalance {
2025-09-02 09:01:33 +03:00
#[arg(short, long)]
addr: String,
},
///Get account `addr` nonce
2025-10-03 17:06:45 -03:00
GetPublicAccountNonce {
2025-09-02 09:01:33 +03:00
#[arg(short, long)]
addr: String,
},
///Get account at address `addr`
2025-10-03 17:06:45 -03:00
GetPublicAccount {
#[arg(short, long)]
addr: String,
},
2025-09-05 23:45:44 -03:00
// TODO: Testnet only. Refactor to prevent compilation on mainnet.
2025-09-04 17:05:12 -03:00
// Claim piñata prize
ClaimPinata {
///pinata_addr - valid 32 byte hex string
#[arg(long)]
pinata_addr: String,
///winner_addr - valid 32 byte hex string
#[arg(long)]
winner_addr: String,
///solution - solution to pinata challenge
#[arg(long)]
solution: u128,
},
// TODO: Testnet only. Refactor to prevent compilation on mainnet.
// Claim piñata prize
ClaimPinataPrivateReceiverOwned {
///pinata_addr - valid 32 byte hex string
#[arg(long)]
pinata_addr: String,
///winner_addr - valid 32 byte hex string
#[arg(long)]
winner_addr: String,
///solution - solution to pinata challenge
#[arg(long)]
solution: u128,
},
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-09-12 16:00:57 +03:00
Command::SendNativeTokenTransferPublic { from, to, amount } => {
2025-10-03 15:59:27 -03:00
let from: Address = from.parse().unwrap();
let to: Address = to.parse().unwrap();
2025-08-06 14:56:58 +03:00
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?;
println!("Results of tx send is {res:#?}");
2025-08-11 14:01:13 +03:00
2025-09-12 16:00:57 +03:00
let transfer_tx = wallet_core.poll_native_token_transfer(res.tx_hash).await?;
println!("Transaction data is {transfer_tx:?}");
2025-09-23 14:47:18 +03:00
let path = wallet_core.store_persistent_accounts()?;
println!("Stored persistent accounts at {path:#?}");
2025-09-25 05:54:32 +03:00
SubcommandReturnValue::Empty
2025-09-12 16:00:57 +03:00
}
2025-09-30 14:44:43 -03:00
Command::SendNativeTokenTransferPrivateOwnedAccount { from, to, amount } => {
2025-10-03 15:59:27 -03:00
let from: Address = from.parse().unwrap();
let to: Address = to.parse().unwrap();
2025-09-12 16:00:57 +03:00
2025-10-02 22:30:33 -03:00
let (res, [secret_from, secret_to]) = wallet_core
2025-09-30 14:44:43 -03:00
.send_private_native_token_transfer_owned_account(from, to, amount)
2025-08-21 15:58:31 +03:00
.await?;
println!("Results of tx send is {res:#?}");
2025-09-12 16:00:57 +03:00
2025-09-25 05:54:32 +03:00
let tx_hash = res.tx_hash;
2025-08-21 15:58:31 +03:00
let transfer_tx = wallet_core
2025-09-25 05:54:32 +03:00
.poll_native_token_transfer(tx_hash.clone())
2025-08-21 15:58:31 +03:00
.await?;
if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx {
2025-10-14 08:33:20 +03:00
let acc_decode_data = vec![(secret_from, from), (secret_to, to)];
2025-09-23 14:47:18 +03:00
wallet_core
2025-10-14 08:33:20 +03:00
.decode_insert_privacy_preserving_transaction_results(tx, &acc_decode_data)?;
2025-09-22 16:38:25 +03:00
}
2025-09-23 14:47:18 +03:00
let path = wallet_core.store_persistent_accounts()?;
println!("Stored persistent accounts at {path:#?}");
2025-09-25 05:54:32 +03:00
SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash }
2025-08-08 10:07:10 +03:00
}
2025-09-23 09:54:33 +03:00
Command::SendNativeTokenTransferPrivateForeignAccount {
from,
to_npk,
to_ipk,
amount,
} => {
2025-10-03 15:59:27 -03:00
let from: Address = from.parse().unwrap();
2025-09-22 16:38:25 +03:00
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);
2025-09-23 09:54:33 +03:00
let to_ipk =
nssa_core::encryption::shared_key_derivation::Secp256k1Point(to_ipk.to_vec());
2025-09-22 16:38:25 +03:00
2025-10-14 08:33:20 +03:00
let (res, [secret_from, _]) = wallet_core
2025-09-22 16:38:25 +03:00
.send_private_native_token_transfer_outer_account(from, to_npk, to_ipk, amount)
.await?;
println!("Results of tx send is {res:#?}");
2025-09-25 05:54:32 +03:00
let tx_hash = res.tx_hash;
let transfer_tx = wallet_core
.poll_native_token_transfer(tx_hash.clone())
.await?;
2025-09-22 16:38:25 +03:00
if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx {
2025-10-14 08:33:20 +03:00
let acc_decode_data = vec![(secret_from, from)];
2025-09-23 14:47:18 +03:00
wallet_core
2025-10-14 08:33:20 +03:00
.decode_insert_privacy_preserving_transaction_results(tx, &acc_decode_data)?;
}
2025-09-23 14:47:18 +03:00
let path = wallet_core.store_persistent_accounts()?;
println!("Stored persistent accounts at {path:#?}");
2025-09-25 05:54:32 +03:00
SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash }
2025-08-08 10:07:10 +03:00
}
2025-09-23 09:54:33 +03:00
Command::SendNativeTokenTransferDeshielded { from, to, amount } => {
2025-10-03 15:59:27 -03:00
let from: Address = from.parse().unwrap();
let to: Address = to.parse().unwrap();
2025-09-23 09:54:33 +03:00
let (res, secret) = wallet_core
.send_deshielded_native_token_transfer(from, to, amount)
.await?;
println!("Results of tx send is {res:#?}");
2025-09-25 05:54:32 +03:00
let tx_hash = res.tx_hash;
let transfer_tx = wallet_core
.poll_native_token_transfer(tx_hash.clone())
.await?;
2025-09-23 09:54:33 +03:00
if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx {
2025-10-14 08:33:20 +03:00
let acc_decode_data = vec![(secret, from)];
2025-09-23 14:47:18 +03:00
wallet_core
2025-10-14 08:33:20 +03:00
.decode_insert_privacy_preserving_transaction_results(tx, &acc_decode_data)?;
2025-09-23 09:54:33 +03:00
}
2025-09-23 14:47:18 +03:00
let path = wallet_core.store_persistent_accounts()?;
println!("Stored persistent accounts at {path:#?}");
2025-09-25 05:54:32 +03:00
SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash }
2025-09-23 09:54:33 +03:00
}
Command::SendNativeTokenTransferShielded { from, to, amount } => {
2025-10-03 15:59:27 -03:00
let from: Address = from.parse().unwrap();
let to: Address = to.parse().unwrap();
2025-09-23 09:54:33 +03:00
let (res, secret) = wallet_core
2025-09-30 14:17:46 -03:00
.send_shielded_native_token_transfer(from, to, amount)
2025-09-23 09:54:33 +03:00
.await?;
println!("Results of tx send is {res:#?}");
2025-09-25 05:54:32 +03:00
let tx_hash = res.tx_hash;
let transfer_tx = wallet_core
.poll_native_token_transfer(tx_hash.clone())
.await?;
2025-09-23 09:54:33 +03:00
if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx {
2025-10-14 08:33:20 +03:00
let acc_decode_data = vec![(secret, to)];
2025-09-23 09:54:33 +03:00
2025-10-14 08:33:20 +03:00
wallet_core
.decode_insert_privacy_preserving_transaction_results(tx, &acc_decode_data)?;
2025-09-23 09:54:33 +03:00
}
2025-09-23 14:47:18 +03:00
let path = wallet_core.store_persistent_accounts()?;
println!("Stored persistent accounts at {path:#?}");
2025-09-25 05:54:32 +03:00
SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash }
2025-09-23 09:54:33 +03:00
}
Command::SendNativeTokenTransferShieldedForeignAccount {
from,
to_npk,
to_ipk,
amount,
} => {
2025-10-03 15:59:27 -03:00
let from: Address = from.parse().unwrap();
2025-09-23 09:54:33 +03:00
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());
2025-10-14 08:33:20 +03:00
let (res, _) = wallet_core
2025-10-02 22:30:33 -03:00
.send_shielded_native_token_transfer_outer_account(from, to_npk, to_ipk, amount)
2025-09-23 09:54:33 +03:00
.await?;
println!("Results of tx send is {res:#?}");
2025-09-25 05:54:32 +03:00
let tx_hash = res.tx_hash;
2025-09-23 14:47:18 +03:00
let path = wallet_core.store_persistent_accounts()?;
2025-09-24 12:57:17 +03:00
println!("Stored persistent accounts at {path:#?}");
2025-09-25 05:54:32 +03:00
SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash }
2025-09-24 12:57:17 +03:00
}
2025-10-03 17:06:45 -03:00
Command::FetchPrivateAccount {
2025-09-24 12:57:17 +03:00
tx_hash,
acc_addr,
2025-10-03 17:06:45 -03:00
output_id: ciph_id,
2025-09-24 12:57:17 +03:00
} => {
2025-10-03 15:59:27 -03:00
let acc_addr: Address = acc_addr.parse().unwrap();
2025-09-24 12:57:17 +03:00
2025-09-25 05:54:32 +03:00
let account_key_chain = wallet_core
.storage
.user_data
.user_private_accounts
.get(&acc_addr);
let Some((account_key_chain, _)) = account_key_chain else {
anyhow::bail!("Account not found");
};
2025-09-24 12:57:17 +03:00
let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?;
if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx {
let to_ebc = tx.message.encrypted_private_post_states[ciph_id].clone();
let to_comm = tx.message.new_commitments[ciph_id].clone();
2025-09-25 05:54:32 +03:00
let shared_secret = account_key_chain.calculate_shared_secret_receiver(to_ebc.epk);
2025-09-24 12:57:17 +03:00
let res_acc_to = nssa_core::EncryptionScheme::decrypt(
&to_ebc.ciphertext,
2025-09-25 05:54:32 +03:00
&shared_secret,
2025-09-24 12:57:17 +03:00
&to_comm,
ciph_id as u32,
)
.unwrap();
println!("RES acc to {res_acc_to:#?}");
println!("Transaction data is {:?}", tx.message);
wallet_core
.storage
.insert_private_account_data(acc_addr, res_acc_to);
}
let path = wallet_core.store_persistent_accounts()?;
2025-09-23 14:47:18 +03:00
println!("Stored persistent accounts at {path:#?}");
2025-09-25 05:54:32 +03:00
SubcommandReturnValue::Empty
2025-09-23 09:54:33 +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
println!("Generated new account with addr {addr}");
2025-09-23 14:47:18 +03:00
let path = wallet_core.store_persistent_accounts()?;
println!("Stored persistent accounts at {path:#?}");
2025-09-25 05:54:32 +03:00
SubcommandReturnValue::RegisterAccount { addr }
2025-08-20 17:16:51 +03:00
}
2025-09-11 18:32:46 +03:00
Command::RegisterAccountPrivate {} => {
let addr = wallet_core.create_new_account_private();
2025-10-03 08:08:54 -03:00
let (key, _) = wallet_core
2025-09-11 18:32:46 +03:00
.storage
.user_data
.get_private_account(&addr)
.unwrap();
2025-10-02 22:30:33 -03:00
println!("Generated new account with addr {addr}");
println!("With npk {}", hex::encode(&key.nullifer_public_key));
println!(
"With ipk {}",
2025-10-03 08:08:54 -03:00
hex::encode(key.incoming_viewing_public_key.to_bytes())
);
2025-09-23 14:47:18 +03:00
let path = wallet_core.store_persistent_accounts()?;
println!("Stored persistent accounts at {path:#?}");
2025-09-25 05:54:32 +03:00
SubcommandReturnValue::RegisterAccount { addr }
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?;
println!("Transaction object {tx_obj:#?}");
2025-09-25 05:54:32 +03:00
SubcommandReturnValue::Empty
2025-08-20 17:16:51 +03:00
}
2025-10-03 17:06:45 -03:00
Command::GetPublicAccountBalance { addr } => {
2025-09-02 09:01:33 +03:00
let addr = Address::from_str(&addr)?;
let balance = wallet_core.get_account_balance(addr).await?;
println!("Accounts {addr} balance is {balance}");
2025-09-25 05:54:32 +03:00
SubcommandReturnValue::Empty
2025-09-02 09:01:33 +03:00
}
2025-10-03 17:06:45 -03:00
Command::GetPublicAccountNonce { addr } => {
2025-09-02 09:01:33 +03:00
let addr = Address::from_str(&addr)?;
let nonce = wallet_core.get_accounts_nonces(vec![addr]).await?[0];
println!("Accounts {addr} nonce is {nonce}");
2025-09-25 05:54:32 +03:00
SubcommandReturnValue::Empty
}
2025-10-03 17:06:45 -03:00
Command::GetPublicAccount { addr } => {
let addr: Address = addr.parse()?;
2025-10-03 15:59:27 -03:00
let account = wallet_core.get_account_public(addr).await?;
let account_hr: HumanReadableAccount = account.clone().into();
println!("{}", serde_json::to_string(&account_hr).unwrap());
2025-09-25 05:54:32 +03:00
2025-10-03 15:59:27 -03:00
SubcommandReturnValue::Account(account)
}
Command::GetPrivateAccount { addr } => {
let addr: Address = addr.parse()?;
if let Some(account) = wallet_core.get_account_private(&addr) {
println!("{}", serde_json::to_string(&account).unwrap());
} else {
println!("Private account not found.");
}
2025-09-25 05:54:32 +03:00
SubcommandReturnValue::Empty
2025-09-02 09:01:33 +03:00
}
2025-09-04 17:05:12 -03:00
Command::ClaimPinata {
pinata_addr,
winner_addr,
solution,
} => {
let res = wallet_core
.claim_pinata(
pinata_addr.parse().unwrap(),
winner_addr.parse().unwrap(),
solution,
)
.await?;
info!("Results of tx send is {res:#?}");
2025-09-25 05:54:32 +03:00
SubcommandReturnValue::Empty
2025-09-02 09:01:33 +03:00
}
Command::ClaimPinataPrivateReceiverOwned {
pinata_addr,
winner_addr,
solution,
} => {
let pinata_addr = pinata_addr.parse().unwrap();
let winner_addr = winner_addr.parse().unwrap();
2025-10-14 08:33:20 +03:00
let winner_intialized = wallet_core
.check_private_account_initialized(&winner_addr)
.await;
let (res, [secret_winner]) = if winner_intialized {
wallet_core
.claim_pinata_private_owned_account_already_initialized(
pinata_addr,
winner_addr,
solution,
)
.await?
} else {
wallet_core
.claim_pinata_private_owned_account_not_initialized(
pinata_addr,
winner_addr,
solution,
)
.await?
};
info!("Results of tx send is {res:#?}");
let tx_hash = res.tx_hash;
let transfer_tx = wallet_core
.poll_native_token_transfer(tx_hash.clone())
.await?;
if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx {
2025-10-14 08:33:20 +03:00
let acc_decode_data = vec![(secret_winner, winner_addr)];
wallet_core
2025-10-14 08:33:20 +03:00
.decode_insert_privacy_preserving_transaction_results(tx, &acc_decode_data)?;
}
let path = wallet_core.store_persistent_accounts()?;
println!("Stored persistent accounts at {path:#?}");
SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash }
}
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
}