2025-08-08 15:22:04 +03:00
|
|
|
use std::{fs::File, io::Write, path::PathBuf, sync::Arc};
|
2024-12-05 13:05:58 +02:00
|
|
|
|
2025-07-22 15:22:20 +03:00
|
|
|
use common::{
|
2025-08-07 14:07:34 +03:00
|
|
|
sequencer_client::{json::SendTxResponse, SequencerClient},
|
|
|
|
|
ExecutionFailureKind,
|
2025-07-22 15:22:20 +03:00
|
|
|
};
|
2024-12-25 09:50:54 +02:00
|
|
|
|
2025-08-13 13:42:00 +03:00
|
|
|
use accounts::account_core::Account;
|
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-08-13 13:42:00 +03:00
|
|
|
use nssa::Address;
|
2025-08-04 15:09:28 +03:00
|
|
|
use tokio::sync::RwLock;
|
2024-12-03 09:32:35 +02:00
|
|
|
|
2025-08-06 14:56:58 +03:00
|
|
|
use clap::{Parser, Subcommand};
|
|
|
|
|
|
2025-08-08 15:22:04 +03:00
|
|
|
use crate::helperfunctions::{
|
|
|
|
|
fetch_config, fetch_persistent_accounts, get_home, produce_account_addr_from_hex,
|
|
|
|
|
};
|
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
|
|
|
pub const BLOCK_GEN_DELAY_SECS: u64 = 20;
|
|
|
|
|
|
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;
|
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-11 08:55:08 +03:00
|
|
|
pub wallet_config: WalletConfig,
|
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 {
|
|
|
|
|
pub async 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())?);
|
2024-12-05 13:05:58 +02:00
|
|
|
|
2025-08-11 08:55:08 +03:00
|
|
|
let mut storage = WalletChainStore::new(config.clone())?;
|
2025-07-30 14:01:40 +03:00
|
|
|
for acc in config.clone().initial_accounts {
|
2025-07-23 15:16:53 +03:00
|
|
|
storage.acc_map.insert(acc.address, acc);
|
|
|
|
|
}
|
2024-12-05 13:05:58 +02:00
|
|
|
|
2025-08-08 15:22:04 +03:00
|
|
|
//Persistent accounts take precedence for initial accounts
|
|
|
|
|
let persistent_accounts = fetch_persistent_accounts()?;
|
|
|
|
|
for acc in persistent_accounts {
|
|
|
|
|
storage.acc_map.insert(acc.address, acc);
|
|
|
|
|
}
|
2024-12-05 13:05:58 +02:00
|
|
|
|
|
|
|
|
Ok(Self {
|
2025-08-11 09:05:18 +03:00
|
|
|
storage,
|
2025-08-11 08:55:08 +03:00
|
|
|
wallet_config: config.clone(),
|
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-08-13 13:42:00 +03:00
|
|
|
pub async fn create_new_account(&mut self) -> Address {
|
2024-12-25 09:50:54 +02:00
|
|
|
let account = Account::new();
|
2024-12-30 07:35:05 +01:00
|
|
|
account.log();
|
2024-12-25 09:50:54 +02:00
|
|
|
|
|
|
|
|
let addr = account.address;
|
|
|
|
|
|
2025-08-08 15:22:04 +03:00
|
|
|
self.storage.acc_map.insert(account.address, account);
|
2024-12-25 09:50:54 +02:00
|
|
|
|
|
|
|
|
addr
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
2025-08-09 06:36:13 -03:00
|
|
|
nonce: u128,
|
2025-08-13 13:42:00 +03:00
|
|
|
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-07-22 15:22:20 +03:00
|
|
|
{
|
|
|
|
|
let read_guard = self.storage.read().await;
|
|
|
|
|
|
2025-08-08 15:22:04 +03:00
|
|
|
if let Some(account) = account {
|
|
|
|
|
let key_to_sign_transaction = account.key_holder.get_pub_account_signing_key();
|
2025-07-22 15:22:20 +03:00
|
|
|
|
|
|
|
|
if let Some(account) = account {
|
2025-08-13 13:42:00 +03:00
|
|
|
let addresses = vec![from, to];
|
2025-08-09 06:36:13 -03:00
|
|
|
let nonces = vec![nonce];
|
2025-08-10 00:53:53 -03:00
|
|
|
let program_id = nssa::program::Program::authenticated_transfer_program().id();
|
2025-08-11 12:07:30 -03:00
|
|
|
let message = nssa::public_transaction::Message::try_new(
|
2025-08-09 06:36:13 -03:00
|
|
|
program_id,
|
|
|
|
|
addresses,
|
|
|
|
|
nonces,
|
|
|
|
|
balance_to_move,
|
2025-08-12 16:48:53 -03:00
|
|
|
)
|
|
|
|
|
.unwrap();
|
2025-07-22 15:22:20 +03:00
|
|
|
|
2025-08-09 06:36:13 -03:00
|
|
|
let signing_key = account.key_holder.get_pub_account_signing_key();
|
|
|
|
|
let witness_set =
|
2025-08-09 18:40:32 -03:00
|
|
|
nssa::public_transaction::WitnessSet::for_message(&message, &[signing_key]);
|
2025-07-22 15:22:20 +03:00
|
|
|
|
2025-08-09 06:36:13 -03:00
|
|
|
let tx = nssa::PublicTransaction::new(message, witness_set);
|
2025-08-08 15:22:04 +03:00
|
|
|
|
2025-08-09 06:36:13 -03:00
|
|
|
Ok(self.sequencer_client.send_tx(tx).await?)
|
2025-07-22 15:22:20 +03:00
|
|
|
} else {
|
|
|
|
|
Err(ExecutionFailureKind::AmountMismatchError)
|
2025-08-11 14:01:13 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-08 15:22:04 +03:00
|
|
|
///Dumps all accounts from acc_map at `path`
|
|
|
|
|
///
|
|
|
|
|
///Currently storing everything in one file
|
|
|
|
|
///
|
|
|
|
|
///ToDo: extend storage
|
|
|
|
|
pub fn store_present_accounts_at_path(&self, path: PathBuf) -> Result<PathBuf> {
|
|
|
|
|
let dump_path = path.join("curr_accounts.json");
|
|
|
|
|
|
2025-08-08 17:01:21 +03:00
|
|
|
let curr_accs: Vec<Account> = self.storage.acc_map.values().cloned().collect();
|
2025-08-08 15:22:04 +03:00
|
|
|
let accs_serialized = serde_json::to_vec_pretty(&curr_accs)?;
|
|
|
|
|
|
|
|
|
|
let mut acc_file = File::create(&dump_path).unwrap();
|
|
|
|
|
acc_file.write_all(&accs_serialized).unwrap();
|
|
|
|
|
|
|
|
|
|
Ok(dump_path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///Dumps all accounts from acc_map at `NSSA_WALLET_HOME_DIR`
|
|
|
|
|
///
|
|
|
|
|
///Currently storing everything in one file
|
|
|
|
|
///
|
|
|
|
|
///ToDo: extend storage
|
|
|
|
|
pub fn store_present_accounts_at_home(&self) -> Result<PathBuf> {
|
|
|
|
|
let home = get_home()?;
|
|
|
|
|
self.store_present_accounts_at_path(home)
|
|
|
|
|
}
|
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-11 09:08:06 -03:00
|
|
|
///nonce - u128 integer
|
2025-08-11 08:55:08 +03:00
|
|
|
#[arg(long)]
|
2025-08-11 09:08:06 -03:00
|
|
|
nonce: u128,
|
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-08-08 15:22:04 +03:00
|
|
|
///Dump accounts at destination
|
|
|
|
|
DumpAccountsOnDisc {
|
|
|
|
|
///Dump path for accounts
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
dump_path: PathBuf,
|
|
|
|
|
},
|
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<()> {
|
|
|
|
|
match command {
|
2025-08-11 08:55:08 +03:00
|
|
|
Command::SendNativeTokenTransfer {
|
|
|
|
|
from,
|
|
|
|
|
nonce,
|
|
|
|
|
to,
|
|
|
|
|
amount,
|
|
|
|
|
} => {
|
|
|
|
|
let wallet_config = fetch_config()?;
|
2025-08-06 14:56:58 +03:00
|
|
|
|
|
|
|
|
let from = produce_account_addr_from_hex(from)?;
|
|
|
|
|
let to = produce_account_addr_from_hex(to)?;
|
|
|
|
|
|
2025-08-11 14:01:13 +03:00
|
|
|
let mut wallet_core = WalletCore::start_from_config_update_chain(wallet_config).await?;
|
2025-08-06 14:56:58 +03:00
|
|
|
|
|
|
|
|
let res = wallet_core
|
2025-08-11 08:55:08 +03:00
|
|
|
.send_public_native_token_transfer(from, nonce, 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
|
|
|
|
|
|
|
|
//ToDo: Insert transaction polling logic here
|
|
|
|
|
wallet_core.increment_nonces(&[from, to]);
|
|
|
|
|
|
|
|
|
|
let acc_storage_path = wallet_core.store_present_accounts_at_home()?;
|
|
|
|
|
|
|
|
|
|
info!("Accounts stored at {acc_storage_path:#?}");
|
2025-08-06 14:56:58 +03:00
|
|
|
}
|
2025-08-08 15:22:04 +03:00
|
|
|
Command::DumpAccountsOnDisc { dump_path } => {
|
|
|
|
|
let node_config = fetch_config()?;
|
|
|
|
|
|
2025-08-11 09:05:18 +03:00
|
|
|
let wallet_core = WalletCore::start_from_config_update_chain(node_config).await?;
|
2025-08-08 15:22:04 +03:00
|
|
|
|
|
|
|
|
wallet_core.store_present_accounts_at_path(dump_path.clone())?;
|
|
|
|
|
|
|
|
|
|
info!("Accounts stored at path {dump_path:#?}");
|
2025-08-08 10:07:10 +03:00
|
|
|
}
|
2025-08-06 14:56:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|