lssa/wallet/src/cli/account.rs

346 lines
11 KiB
Rust
Raw Normal View History

2025-10-20 10:01:54 +03:00
use anyhow::Result;
2025-10-23 17:33:25 +03:00
use base58::ToBase58;
2025-10-20 10:01:54 +03:00
use clap::Subcommand;
2025-11-27 04:22:49 +03:00
use itertools::Itertools as _;
2025-11-28 09:49:05 +02:00
use key_protocol::key_management::key_tree::chain_index::ChainIndex;
use nssa::{Account, AccountId, program::Program};
2025-10-24 15:26:30 +03:00
use serde::Serialize;
2025-10-20 10:01:54 +03:00
use crate::{
WalletCore,
cli::{SubcommandReturnValue, WalletSubcommand},
helperfunctions::{
AccountPrivacyKind, HumanReadableAccount, parse_addr_with_privacy_prefix, parse_block_range,
},
2025-10-20 10:01:54 +03:00
};
2025-10-24 15:26:30 +03:00
const TOKEN_DEFINITION_TYPE: u8 = 0;
const TOKEN_DEFINITION_DATA_SIZE: usize = 23;
const TOKEN_HOLDING_TYPE: u8 = 1;
const TOKEN_HOLDING_DATA_SIZE: usize = 49;
struct TokenDefinition {
#[allow(unused)]
account_type: u8,
name: [u8; 6],
total_supply: u128,
}
struct TokenHolding {
#[allow(unused)]
account_type: u8,
definition_id: AccountId,
2025-10-24 15:26:30 +03:00
balance: u128,
}
impl TokenDefinition {
fn parse(data: &[u8]) -> Option<Self> {
if data.len() != TOKEN_DEFINITION_DATA_SIZE || data[0] != TOKEN_DEFINITION_TYPE {
None
} else {
let account_type = data[0];
let name = data[1..7].try_into().unwrap();
let total_supply = u128::from_le_bytes(data[7..].try_into().unwrap());
Some(Self {
account_type,
name,
total_supply,
})
}
}
}
impl TokenHolding {
fn parse(data: &[u8]) -> Option<Self> {
if data.len() != TOKEN_HOLDING_DATA_SIZE || data[0] != TOKEN_HOLDING_TYPE {
None
} else {
let account_type = data[0];
let definition_id = AccountId::new(data[1..33].try_into().unwrap());
2025-10-24 15:26:30 +03:00
let balance = u128::from_le_bytes(data[33..].try_into().unwrap());
Some(Self {
definition_id,
balance,
account_type,
})
}
}
}
2025-11-26 00:27:20 +03:00
/// Represents generic chain CLI subcommand
2025-10-20 10:01:54 +03:00
#[derive(Subcommand, Debug, Clone)]
pub enum AccountSubcommand {
2025-11-26 00:27:20 +03:00
/// Get account data
2025-10-24 15:26:30 +03:00
Get {
2025-11-26 00:27:20 +03:00
/// Flag to get raw account data
2025-10-28 16:53:39 +02:00
#[arg(short, long)]
2025-10-24 15:26:30 +03:00
raw: bool,
2025-11-26 00:27:20 +03:00
/// Valid 32 byte base58 string with privacy prefix
2025-10-24 15:26:30 +03:00
#[arg(short, long)]
account_id: String,
2025-10-24 15:26:30 +03:00
},
2025-11-26 00:27:20 +03:00
/// Produce new public or private account
2025-10-20 10:01:54 +03:00
#[command(subcommand)]
2025-10-23 17:33:25 +03:00
New(NewSubcommand),
2025-11-26 00:27:20 +03:00
/// Sync private accounts
2025-10-27 14:32:28 +02:00
SyncPrivate {},
2025-11-27 04:22:49 +03:00
/// List all accounts owned by the wallet
#[command(visible_alias = "ls")]
List {},
2025-10-20 10:01:54 +03:00
}
2025-11-26 00:27:20 +03:00
/// Represents generic register CLI subcommand
2025-10-20 10:01:54 +03:00
#[derive(Subcommand, Debug, Clone)]
2025-10-23 17:33:25 +03:00
pub enum NewSubcommand {
2025-11-26 00:27:20 +03:00
/// Register new public account
2025-11-11 12:15:20 +02:00
Public {
2025-11-10 16:29:33 +02:00
#[arg(long)]
2025-11-26 07:32:35 +02:00
/// Chain index of a parent node
2025-11-11 12:15:20 +02:00
cci: ChainIndex,
2025-11-10 16:29:33 +02:00
},
2025-11-26 00:27:20 +03:00
/// Register new private account
2025-11-10 16:29:33 +02:00
Private {
#[arg(long)]
2025-11-26 07:32:35 +02:00
/// Chain index of a parent node
2025-11-11 12:15:20 +02:00
cci: ChainIndex,
2025-11-10 16:29:33 +02:00
},
2025-10-20 10:01:54 +03:00
}
2025-10-23 17:33:25 +03:00
impl WalletSubcommand for NewSubcommand {
2025-10-20 10:01:54 +03:00
async fn handle_subcommand(
self,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
match self {
2025-11-10 16:29:33 +02:00
NewSubcommand::Public { cci } => {
let account_id = wallet_core.create_new_account_public(cci);
2025-10-20 10:01:54 +03:00
println!("Generated new account with account_id Public/{account_id}");
2025-10-20 10:01:54 +03:00
2025-10-28 16:02:30 +02:00
let path = wallet_core.store_persistent_data().await?;
2025-10-20 10:01:54 +03:00
println!("Stored persistent accounts at {path:#?}");
Ok(SubcommandReturnValue::RegisterAccount { account_id })
2025-10-20 10:01:54 +03:00
}
2025-11-10 16:29:33 +02:00
NewSubcommand::Private { cci } => {
let account_id = wallet_core.create_new_account_private(cci);
2025-10-20 10:01:54 +03:00
let (key, _) = wallet_core
.storage
.user_data
.get_private_account(&account_id)
2025-10-20 10:01:54 +03:00
.unwrap();
2025-10-24 11:12:32 +03:00
println!(
"Generated new account with account_id Private/{}",
account_id.to_bytes().to_base58()
2025-10-24 11:12:32 +03:00
);
2025-10-27 14:32:28 +02:00
println!("With npk {}", hex::encode(key.nullifer_public_key.0));
2025-10-20 10:01:54 +03:00
println!(
"With ipk {}",
hex::encode(key.incoming_viewing_public_key.to_bytes())
);
2025-10-28 16:02:30 +02:00
let path = wallet_core.store_persistent_data().await?;
2025-10-20 10:01:54 +03:00
println!("Stored persistent accounts at {path:#?}");
Ok(SubcommandReturnValue::RegisterAccount { account_id })
2025-10-20 10:01:54 +03:00
}
}
}
}
2025-10-24 15:26:30 +03:00
#[derive(Debug, Serialize)]
pub struct AuthenticatedTransferAccountView {
pub balance: u128,
}
impl From<nssa::Account> for AuthenticatedTransferAccountView {
fn from(value: nssa::Account) -> Self {
Self {
balance: value.balance,
}
}
}
#[derive(Debug, Serialize)]
pub struct TokedDefinitionAccountView {
pub account_type: String,
pub name: String,
pub total_supply: u128,
}
impl From<TokenDefinition> for TokedDefinitionAccountView {
fn from(value: TokenDefinition) -> Self {
Self {
account_type: "Token definition".to_string(),
name: hex::encode(value.name),
total_supply: value.total_supply,
}
}
}
#[derive(Debug, Serialize)]
pub struct TokedHoldingAccountView {
pub account_type: String,
pub definition_id: String,
pub balance: u128,
}
impl From<TokenHolding> for TokedHoldingAccountView {
fn from(value: TokenHolding) -> Self {
Self {
account_type: "Token holding".to_string(),
definition_id: value.definition_id.to_string(),
balance: value.balance,
}
}
}
2025-10-20 10:01:54 +03:00
impl WalletSubcommand for AccountSubcommand {
async fn handle_subcommand(
self,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
match self {
AccountSubcommand::Get { raw, account_id } => {
let (account_id, addr_kind) = parse_addr_with_privacy_prefix(&account_id)?;
2025-10-24 15:26:30 +03:00
let account_id = account_id.parse()?;
2025-10-28 16:02:30 +02:00
2025-10-24 15:26:30 +03:00
let account = match addr_kind {
AccountPrivacyKind::Public => {
wallet_core.get_account_public(account_id).await?
}
AccountPrivacyKind::Private => wallet_core
.get_account_private(&account_id)
2025-10-24 15:26:30 +03:00
.ok_or(anyhow::anyhow!("Private account not found in storage"))?,
};
2025-10-30 15:00:21 +02:00
if account == Account::default() {
println!("Account is Uninitialized");
return Ok(SubcommandReturnValue::Empty);
}
2025-10-24 15:26:30 +03:00
if raw {
let account_hr: HumanReadableAccount = account.clone().into();
println!("{}", serde_json::to_string(&account_hr).unwrap());
return Ok(SubcommandReturnValue::Empty);
}
let auth_tr_prog_id = Program::authenticated_transfer_program().id();
let token_prog_id = Program::token().id();
let acc_view = match &account.program_owner {
2025-10-27 14:32:28 +02:00
_ if account.program_owner == auth_tr_prog_id => {
2025-10-24 15:26:30 +03:00
let acc_view: AuthenticatedTransferAccountView = account.into();
2025-10-30 15:00:21 +02:00
println!("Account owned by authenticated transfer program");
2025-10-24 15:26:30 +03:00
serde_json::to_string(&acc_view)?
}
2025-10-27 14:32:28 +02:00
_ if account.program_owner == token_prog_id => {
2025-10-24 15:26:30 +03:00
if let Some(token_def) = TokenDefinition::parse(&account.data) {
let acc_view: TokedDefinitionAccountView = token_def.into();
2025-10-30 15:00:21 +02:00
println!("Definition account owned by token program");
2025-10-24 15:26:30 +03:00
serde_json::to_string(&acc_view)?
} else if let Some(token_hold) = TokenHolding::parse(&account.data) {
let acc_view: TokedHoldingAccountView = token_hold.into();
2025-10-30 15:00:21 +02:00
println!("Holding account owned by token program");
2025-10-24 15:26:30 +03:00
serde_json::to_string(&acc_view)?
} else {
anyhow::bail!(
"Invalid data for account {account_id:#?} with token program"
);
2025-10-24 15:26:30 +03:00
}
}
_ => {
let account_hr: HumanReadableAccount = account.clone().into();
serde_json::to_string(&account_hr).unwrap()
}
};
println!("{}", acc_view);
Ok(SubcommandReturnValue::Empty)
2025-10-20 10:01:54 +03:00
}
2025-10-23 17:33:25 +03:00
AccountSubcommand::New(new_subcommand) => {
new_subcommand.handle_subcommand(wallet_core).await
2025-10-20 10:01:54 +03:00
}
2025-10-27 14:32:28 +02:00
AccountSubcommand::SyncPrivate {} => {
2025-10-28 16:02:30 +02:00
let last_synced_block = wallet_core.last_synced_block;
let curr_last_block = wallet_core
.sequencer_client
.get_last_block()
.await?
.last_block;
2025-11-26 07:32:35 +02:00
if wallet_core
2025-10-28 16:02:30 +02:00
.storage
.user_data
2025-11-10 16:29:33 +02:00
.private_key_tree
.account_id_map
2025-10-28 16:02:30 +02:00
.is_empty()
{
2025-11-26 07:32:35 +02:00
wallet_core.last_synced_block = curr_last_block;
let path = wallet_core.store_persistent_data().await?;
println!("Stored persistent data at {path:#?}");
} else {
2025-10-28 16:02:30 +02:00
parse_block_range(
2025-10-28 16:53:39 +02:00
last_synced_block + 1,
2025-10-28 16:02:30 +02:00
curr_last_block,
wallet_core.sequencer_client.clone(),
wallet_core,
)
.await?;
}
Ok(SubcommandReturnValue::SyncedToBlock(curr_last_block))
2025-10-27 14:32:28 +02:00
}
2025-11-27 04:22:49 +03:00
AccountSubcommand::List {} => {
let user_data = &wallet_core.storage.user_data;
let accounts = user_data
.default_pub_account_signing_keys
2025-11-27 04:22:49 +03:00
.keys()
.map(|id| format!("Preconfigured Public/{id}"))
2025-11-27 04:22:49 +03:00
.chain(
user_data
.default_user_private_accounts
2025-11-27 04:22:49 +03:00
.keys()
.map(|id| format!("Preconfigured Private/{id}")),
)
.chain(
user_data
.public_key_tree
.account_id_map
.iter()
.map(|(id, chain_index)| format!("{chain_index} Public/{id}")),
)
.chain(
user_data
.private_key_tree
.account_id_map
.iter()
.map(|(id, chain_index)| format!("{chain_index} Private/{id}")),
2025-11-27 04:22:49 +03:00
)
.format(",\n");
println!("{accounts}");
Ok(SubcommandReturnValue::Empty)
}
2025-10-20 10:01:54 +03:00
}
}
}