2025-08-20 17:16:51 +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-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;
|
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::{
|
2025-08-20 17:16:51 +03:00
|
|
|
fetch_config, fetch_persistent_accounts, get_home, produce_account_addr_from_hex,
|
|
|
|
|
produce_data_for_storage,
|
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
|
|
|
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,
|
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-19 14:14:09 +03:00
|
|
|
let mut storage = WalletChainStore::new(config)?;
|
2024-12-05 13:05:58 +02:00
|
|
|
|
2025-08-20 17:16:51 +03:00
|
|
|
//Updating user data with stored accounts
|
|
|
|
|
//We do not store/update any key data connected to private executions
|
|
|
|
|
//ToDo: Add this into persistent data
|
2025-08-08 15:22:04 +03:00
|
|
|
let persistent_accounts = fetch_persistent_accounts()?;
|
|
|
|
|
for acc in persistent_accounts {
|
2025-08-19 14:14:09 +03:00
|
|
|
storage
|
|
|
|
|
.user_data
|
|
|
|
|
.update_account_balance(acc.address, acc.account.balance);
|
2025-08-08 15:22:04 +03:00
|
|
|
}
|
2024-12-05 13:05:58 +02:00
|
|
|
|
|
|
|
|
Ok(Self {
|
2025-08-11 09:05:18 +03:00
|
|
|
storage,
|
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-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");
|
|
|
|
|
let accs = serde_json::to_vec(&produce_data_for_storage(&self.storage.user_data))?;
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn create_new_account(&mut self) -> Address {
|
2025-08-19 14:14:09 +03:00
|
|
|
self.storage.user_data.generate_new_account()
|
2024-12-25 09:50:54 +02: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,
|
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-08-19 14:14:09 +03:00
|
|
|
let account = self.storage.user_data.get_account(&from);
|
2025-07-22 15:22:20 +03:00
|
|
|
|
2025-08-08 15:22:04 +03:00
|
|
|
if let Some(account) = account {
|
2025-08-19 14:14:09 +03:00
|
|
|
if account.balance >= balance_to_move {
|
|
|
|
|
let addresses = vec![from, to];
|
|
|
|
|
let nonces = vec![nonce];
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
let signing_key = self.storage.user_data.get_account_signing_key(&from);
|
|
|
|
|
|
|
|
|
|
if let Some(signing_key) = signing_key {
|
|
|
|
|
let witness_set =
|
|
|
|
|
nssa::public_transaction::WitnessSet::for_message(&message, &[signing_key]);
|
|
|
|
|
|
|
|
|
|
let tx = nssa::PublicTransaction::new(message, witness_set);
|
|
|
|
|
|
|
|
|
|
Ok(self.sequencer_client.send_tx(tx).await?)
|
|
|
|
|
} else {
|
|
|
|
|
Err(ExecutionFailureKind::KeyNotFoundError)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Err(ExecutionFailureKind::InsufficientFundsError)
|
|
|
|
|
}
|
2025-08-13 14:15:05 +03:00
|
|
|
} else {
|
|
|
|
|
Err(ExecutionFailureKind::AmountMismatchError)
|
2025-08-11 14:01:13 +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-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-20 17:16:51 +03:00
|
|
|
///Register new account
|
|
|
|
|
RegisterAccount {},
|
|
|
|
|
///Fetch transaction by `hash`
|
|
|
|
|
FetchTx {
|
|
|
|
|
#[arg(short, long)]
|
|
|
|
|
tx_hash: 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()?;
|
|
|
|
|
let mut wallet_core = WalletCore::start_from_config_update_chain(wallet_config).await?;
|
|
|
|
|
|
2025-08-06 14:56:58 +03:00
|
|
|
match command {
|
2025-08-11 08:55:08 +03:00
|
|
|
Command::SendNativeTokenTransfer {
|
|
|
|
|
from,
|
|
|
|
|
nonce,
|
|
|
|
|
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-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
|
2025-08-08 10:07:10 +03:00
|
|
|
}
|
2025-08-20 17:16:51 +03:00
|
|
|
Command::RegisterAccount {} => {
|
|
|
|
|
let addr = wallet_core.create_new_account();
|
|
|
|
|
|
|
|
|
|
let key = wallet_core.storage.user_data.get_account_signing_key(&addr);
|
|
|
|
|
|
|
|
|
|
info!("Generated new account with addr {addr:#?}");
|
|
|
|
|
info!("With key {key:#?}");
|
|
|
|
|
}
|
|
|
|
|
Command::FetchTx { tx_hash } => {
|
|
|
|
|
let tx_obj = wallet_core
|
|
|
|
|
.sequencer_client
|
|
|
|
|
.get_transaction_by_hash(tx_hash)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
info!("Transaction object {tx_obj:#?}");
|
|
|
|
|
}
|
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(())
|
|
|
|
|
}
|