lssa/wallet/src/cli/account.rs

338 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;
2025-12-22 04:42:32 +02:00
use nssa::{Account, program::Program};
2025-10-24 15:26:30 +03:00
use serde::Serialize;
2025-10-20 10:01:54 +03:00
use crate::{
2025-12-22 04:42:32 +02:00
TokenDefinition, TokenHolding, WalletCore,
cli::{SubcommandReturnValue, WalletSubcommand},
2025-12-03 00:17:12 +03:00
helperfunctions::{AccountPrivacyKind, HumanReadableAccount, parse_addr_with_privacy_prefix},
2025-10-20 10:01:54 +03:00
};
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-12-03 13:10:07 +02:00
cci: Option<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-12-03 13:10:07 +02:00
cci: Option<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 } => {
2025-12-03 13:10:07 +02:00
let (account_id, chain_index) = wallet_core.create_new_account_public(cci);
2025-10-20 10:01:54 +03:00
2025-12-03 13:10:07 +02:00
println!(
"Generated new account with account_id Public/{account_id} at path {chain_index}"
);
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 } => {
2025-12-03 13:10:07 +02:00
let (account_id, chain_index) = 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!(
2025-12-03 13:10:07 +02:00
"Generated new account with account_id Private/{} at path {chain_index}",
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(),
2025-12-03 13:50:10 +02:00
name: {
2025-12-04 14:34:11 +02:00
// Assuming, that name does not have UTF-8 NULL and all zeroes are padding.
let name_trimmed: Vec<_> =
value.name.into_iter().take_while(|ch| *ch != 0).collect();
String::from_utf8(name_trimmed).unwrap_or(hex::encode(value.name))
2025-12-03 13:50:10 +02:00
},
2025-10-24 15:26:30 +03:00
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 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-12-03 00:17:12 +03:00
wallet_core.sync_to_block(curr_last_block).await?;
2025-10-28 16:02:30 +02:00
}
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
}
}
}
2025-12-03 13:50:10 +02:00
#[cfg(test)]
mod tests {
use nssa::AccountId;
2025-12-03 13:50:10 +02:00
use crate::cli::account::{TokedDefinitionAccountView, TokenDefinition};
#[test]
fn test_invalid_utf_8_name_of_token() {
let token_def = TokenDefinition {
account_type: 1,
name: [137, 12, 14, 3, 5, 4],
total_supply: 100,
metadata_id: AccountId::new([0; 32]),
2025-12-03 13:50:10 +02:00
};
let token_def_view: TokedDefinitionAccountView = token_def.into();
assert_eq!(token_def_view.name, "890c0e030504");
}
#[test]
fn test_valid_utf_8_name_of_token_all_bytes() {
let token_def = TokenDefinition {
account_type: 1,
name: [240, 159, 146, 150, 66, 66],
total_supply: 100,
metadata_id: AccountId::new([0; 32]),
2025-12-03 13:50:10 +02:00
};
let token_def_view: TokedDefinitionAccountView = token_def.into();
assert_eq!(token_def_view.name, "💖BB");
}
#[test]
fn test_valid_utf_8_name_of_token_less_bytes() {
let token_def = TokenDefinition {
account_type: 1,
name: [78, 65, 77, 69, 0, 0],
total_supply: 100,
metadata_id: AccountId::new([0; 32]),
2025-12-03 13:50:10 +02:00
};
let token_def_view: TokedDefinitionAccountView = token_def.into();
assert_eq!(token_def_view.name, "NAME");
}
}