lssa/wallet/src/cli/programs/native_token_transfer.rs

531 lines
20 KiB
Rust
Raw Normal View History

2025-10-14 15:29:18 +03:00
use anyhow::Result;
use clap::Subcommand;
use common::transaction::NSSATransaction;
use nssa::AccountId;
2025-10-14 15:29:18 +03:00
2025-10-27 14:32:28 +02:00
use crate::{
WalletCore,
cli::{SubcommandReturnValue, WalletSubcommand},
helperfunctions::{AccountPrivacyKind, parse_addr_with_privacy_prefix},
program_facades::native_token_transfer::NativeTokenTransfer,
2025-10-27 14:32:28 +02:00
};
2025-11-26 00:27:20 +03:00
/// Represents generic CLI subcommand for a wallet working with native token transfer program
2025-10-27 14:32:28 +02:00
#[derive(Subcommand, Debug, Clone)]
pub enum AuthTransferSubcommand {
2025-11-26 00:27:20 +03:00
/// Initialize account under authenticated transfer program
2025-10-27 14:32:28 +02:00
Init {
2025-11-26 00:27:20 +03:00
/// account_id - valid 32 byte base58 string with privacy prefix
2025-10-27 14:32:28 +02:00
#[arg(long)]
account_id: String,
2025-10-27 14:32:28 +02:00
},
2025-11-26 00:27:20 +03:00
/// Send native tokens from one account to another with variable privacy
2025-10-29 12:02:41 +02:00
///
2025-11-26 00:27:20 +03:00
/// If receiver is private, then `to` and (`to_npk` , `to_ipk`) is a mutually exclusive
/// patterns.
2025-10-29 12:02:41 +02:00
///
2025-11-26 00:27:20 +03:00
/// First is used for owned accounts, second otherwise.
2025-10-27 14:32:28 +02:00
Send {
2025-11-26 00:27:20 +03:00
/// from - valid 32 byte base58 string with privacy prefix
2025-10-27 14:32:28 +02:00
#[arg(long)]
from: String,
2025-11-26 00:27:20 +03:00
/// to - valid 32 byte base58 string with privacy prefix
2025-10-27 14:32:28 +02:00
#[arg(long)]
to: Option<String>,
2025-11-26 00:27:20 +03:00
/// to_npk - valid 32 byte hex string
2025-10-27 14:32:28 +02:00
#[arg(long)]
to_npk: Option<String>,
2025-11-26 00:27:20 +03:00
/// to_ipk - valid 33 byte hex string
2025-10-27 14:32:28 +02:00
#[arg(long)]
to_ipk: Option<String>,
2025-11-26 00:27:20 +03:00
/// amount - amount of balance to move
2025-10-27 14:32:28 +02:00
#[arg(long)]
amount: u128,
},
}
impl WalletSubcommand for AuthTransferSubcommand {
async fn handle_subcommand(
self,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
match self {
AuthTransferSubcommand::Init { account_id } => {
let (account_id, addr_privacy) = parse_addr_with_privacy_prefix(&account_id)?;
2025-10-27 14:32:28 +02:00
match addr_privacy {
AccountPrivacyKind::Public => {
let account_id = account_id.parse()?;
2025-10-28 14:40:16 +02:00
let res = NativeTokenTransfer(wallet_core)
.register_account(account_id)
2025-10-27 14:32:28 +02:00
.await?;
println!("Results of tx send are {res:#?}");
2025-10-27 14:32:28 +02:00
let transfer_tx =
wallet_core.poll_native_token_transfer(res.tx_hash).await?;
println!("Transaction data is {transfer_tx:?}");
2025-10-28 16:02:30 +02:00
let path = wallet_core.store_persistent_data().await?;
2025-10-27 14:32:28 +02:00
println!("Stored persistent accounts at {path:#?}");
}
AccountPrivacyKind::Private => {
let account_id = account_id.parse()?;
2025-10-28 16:02:30 +02:00
let (res, secret) = NativeTokenTransfer(wallet_core)
.register_account_private(account_id)
2025-10-27 14:32:28 +02:00
.await?;
println!("Results of tx send are {res:#?}");
2025-10-27 14:32:28 +02:00
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 {
let acc_decode_data = vec![(secret, account_id)];
2025-10-27 14:32:28 +02:00
wallet_core.decode_insert_privacy_preserving_transaction_results(
tx,
&acc_decode_data,
)?;
}
2025-10-28 16:02:30 +02:00
let path = wallet_core.store_persistent_data().await?;
2025-10-27 14:32:28 +02:00
println!("Stored persistent accounts at {path:#?}");
}
}
Ok(SubcommandReturnValue::Empty)
}
AuthTransferSubcommand::Send {
from,
to,
to_npk,
to_ipk,
amount,
} => {
let underlying_subcommand = match (to, to_npk, to_ipk) {
(None, None, None) => {
anyhow::bail!(
"Provide either account account_id of receiver or their public keys"
2025-10-27 14:32:28 +02:00
);
}
(Some(_), Some(_), Some(_)) => {
anyhow::bail!(
"Provide only one variant: either account account_id of receiver or their public keys"
2025-10-27 14:32:28 +02:00
);
}
(_, Some(_), None) | (_, None, Some(_)) => {
anyhow::bail!("List of public keys is uncomplete");
}
(Some(to), None, None) => {
let (from, from_privacy) = parse_addr_with_privacy_prefix(&from)?;
let (to, to_privacy) = parse_addr_with_privacy_prefix(&to)?;
match (from_privacy, to_privacy) {
(AccountPrivacyKind::Public, AccountPrivacyKind::Public) => {
2025-10-28 16:02:30 +02:00
NativeTokenTransferProgramSubcommand::Public { from, to, amount }
2025-10-27 14:32:28 +02:00
}
(AccountPrivacyKind::Private, AccountPrivacyKind::Private) => {
2025-10-27 14:32:28 +02:00
NativeTokenTransferProgramSubcommand::Private(
NativeTokenTransferProgramSubcommandPrivate::PrivateOwned {
2025-10-28 16:02:30 +02:00
from,
to,
2025-10-27 14:32:28 +02:00
amount,
},
)
}
(AccountPrivacyKind::Private, AccountPrivacyKind::Public) => {
2025-10-27 14:32:28 +02:00
NativeTokenTransferProgramSubcommand::Deshielded {
2025-10-28 16:02:30 +02:00
from,
to,
2025-10-27 14:32:28 +02:00
amount,
}
}
(AccountPrivacyKind::Public, AccountPrivacyKind::Private) => {
2025-10-27 14:32:28 +02:00
NativeTokenTransferProgramSubcommand::Shielded(
NativeTokenTransferProgramSubcommandShielded::ShieldedOwned {
2025-10-28 16:02:30 +02:00
from,
to,
2025-10-27 14:32:28 +02:00
amount,
},
)
}
}
}
(None, Some(to_npk), Some(to_ipk)) => {
let (from, from_privacy) = parse_addr_with_privacy_prefix(&from)?;
match from_privacy {
AccountPrivacyKind::Private => {
2025-10-27 14:32:28 +02:00
NativeTokenTransferProgramSubcommand::Private(
NativeTokenTransferProgramSubcommandPrivate::PrivateForeign {
2025-10-28 16:02:30 +02:00
from,
2025-10-27 14:32:28 +02:00
to_npk,
to_ipk,
amount,
},
)
}
AccountPrivacyKind::Public => {
2025-10-27 14:32:28 +02:00
NativeTokenTransferProgramSubcommand::Shielded(
NativeTokenTransferProgramSubcommandShielded::ShieldedForeign {
2025-10-28 16:02:30 +02:00
from,
2025-10-27 14:32:28 +02:00
to_npk,
to_ipk,
amount,
},
)
}
}
}
};
underlying_subcommand.handle_subcommand(wallet_core).await
}
}
}
}
2025-10-14 15:29:18 +03:00
2025-11-26 00:27:20 +03:00
/// Represents generic CLI subcommand for a wallet working with native token transfer program
2025-10-14 15:29:18 +03:00
#[derive(Subcommand, Debug, Clone)]
pub enum NativeTokenTransferProgramSubcommand {
2025-11-26 00:27:20 +03:00
/// Send native token transfer from `from` to `to` for `amount`
2025-10-14 15:29:18 +03:00
///
/// Public operation
2025-10-20 10:01:54 +03:00
Public {
2025-11-26 00:27:20 +03:00
/// from - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
from: String,
2025-11-26 00:27:20 +03:00
/// to - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
to: String,
2025-11-26 00:27:20 +03:00
/// amount - amount of balance to move
2025-10-14 15:29:18 +03:00
#[arg(long)]
amount: u128,
},
2025-11-26 00:27:20 +03:00
/// Private execution
2025-10-14 15:29:18 +03:00
#[command(subcommand)]
Private(NativeTokenTransferProgramSubcommandPrivate),
2025-11-26 00:27:20 +03:00
/// Send native token transfer from `from` to `to` for `amount`
2025-10-14 15:29:18 +03:00
///
/// Deshielded operation
2025-10-20 10:01:54 +03:00
Deshielded {
2025-11-26 00:27:20 +03:00
/// from - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
from: String,
2025-11-26 00:27:20 +03:00
/// to - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
to: String,
2025-11-26 00:27:20 +03:00
/// amount - amount of balance to move
2025-10-14 15:29:18 +03:00
#[arg(long)]
amount: u128,
},
2025-11-26 00:27:20 +03:00
/// Shielded execution
2025-10-14 15:29:18 +03:00
#[command(subcommand)]
Shielded(NativeTokenTransferProgramSubcommandShielded),
}
2025-11-26 00:27:20 +03:00
/// Represents generic shielded CLI subcommand for a wallet working with native token transfer
/// program
2025-10-14 15:29:18 +03:00
#[derive(Subcommand, Debug, Clone)]
pub enum NativeTokenTransferProgramSubcommandShielded {
2025-11-26 00:27:20 +03:00
/// Send native token transfer from `from` to `to` for `amount`
2025-10-14 15:29:18 +03:00
///
/// Shielded operation
2025-10-20 10:01:54 +03:00
ShieldedOwned {
2025-11-26 00:27:20 +03:00
/// from - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
from: String,
2025-11-26 00:27:20 +03:00
/// to - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
to: String,
2025-11-26 00:27:20 +03:00
/// amount - amount of balance to move
2025-10-14 15:29:18 +03:00
#[arg(long)]
amount: u128,
},
2025-11-26 00:27:20 +03:00
/// Send native token transfer from `from` to `to` for `amount`
2025-10-14 15:29:18 +03:00
///
/// Shielded operation
2025-10-20 10:01:54 +03:00
ShieldedForeign {
2025-11-26 00:27:20 +03:00
/// from - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
from: String,
2025-11-26 00:27:20 +03:00
/// to_npk - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
to_npk: String,
2025-11-26 00:27:20 +03:00
/// to_ipk - valid 33 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
to_ipk: String,
2025-11-26 00:27:20 +03:00
/// amount - amount of balance to move
2025-10-14 15:29:18 +03:00
#[arg(long)]
amount: u128,
},
}
2025-11-26 00:27:20 +03:00
/// Represents generic private CLI subcommand for a wallet working with native token transfer
/// program
2025-10-14 15:29:18 +03:00
#[derive(Subcommand, Debug, Clone)]
pub enum NativeTokenTransferProgramSubcommandPrivate {
2025-11-26 00:27:20 +03:00
/// Send native token transfer from `from` to `to` for `amount`
2025-10-14 15:29:18 +03:00
///
/// Private operation
2025-10-20 10:01:54 +03:00
PrivateOwned {
2025-11-26 00:27:20 +03:00
/// from - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
from: String,
2025-11-26 00:27:20 +03:00
/// to - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
to: String,
2025-11-26 00:27:20 +03:00
/// amount - amount of balance to move
2025-10-14 15:29:18 +03:00
#[arg(long)]
amount: u128,
},
2025-11-26 00:27:20 +03:00
/// Send native token transfer from `from` to `to` for `amount`
2025-10-14 15:29:18 +03:00
///
/// Private operation
2025-10-20 10:01:54 +03:00
PrivateForeign {
2025-11-26 00:27:20 +03:00
/// from - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
from: String,
2025-11-26 00:27:20 +03:00
/// to_npk - valid 32 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
to_npk: String,
2025-11-26 00:27:20 +03:00
/// to_ipk - valid 33 byte hex string
2025-10-14 15:29:18 +03:00
#[arg(long)]
to_ipk: String,
2025-11-26 00:27:20 +03:00
/// amount - amount of balance to move
2025-10-14 15:29:18 +03:00
#[arg(long)]
amount: u128,
},
}
impl WalletSubcommand for NativeTokenTransferProgramSubcommandPrivate {
async fn handle_subcommand(
self,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
match self {
2025-10-20 10:01:54 +03:00
NativeTokenTransferProgramSubcommandPrivate::PrivateOwned { from, to, amount } => {
let from: AccountId = from.parse().unwrap();
let to: AccountId = to.parse().unwrap();
2025-10-14 15:29:18 +03:00
let (res, [secret_from, secret_to]) = NativeTokenTransfer(wallet_core)
.send_private_transfer_to_owned_account(from, to, amount)
.await?;
2025-10-14 15:29:18 +03:00
println!("Results of tx send are {res:#?}");
2025-10-14 15:29:18 +03:00
2025-10-20 10:01:54 +03:00
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 {
let acc_decode_data = vec![(secret_from, from), (secret_to, to)];
wallet_core.decode_insert_privacy_preserving_transaction_results(
tx,
&acc_decode_data,
)?;
}
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::PrivacyPreservingTransfer { tx_hash })
2025-10-14 15:29:18 +03:00
}
2025-10-20 10:01:54 +03:00
NativeTokenTransferProgramSubcommandPrivate::PrivateForeign {
from,
to_npk,
to_ipk,
amount,
} => {
let from: AccountId = from.parse().unwrap();
2025-10-20 10:01:54 +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());
let (res, [secret_from, _]) = NativeTokenTransfer(wallet_core)
.send_private_transfer_to_outer_account(from, to_npk, to_ipk, amount)
2025-10-20 10:01:54 +03:00
.await?;
2025-10-14 15:29:18 +03:00
println!("Results of tx send are {res:#?}");
2025-10-14 15:29:18 +03:00
2025-10-20 10:01:54 +03:00
let tx_hash = res.tx_hash;
let transfer_tx = wallet_core
.poll_native_token_transfer(tx_hash.clone())
.await?;
2025-10-14 15:29:18 +03:00
2025-10-20 10:01:54 +03:00
if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx {
let acc_decode_data = vec![(secret_from, from)];
wallet_core.decode_insert_privacy_preserving_transaction_results(
tx,
&acc_decode_data,
)?;
}
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::PrivacyPreservingTransfer { tx_hash })
}
2025-10-14 15:29:18 +03:00
}
}
}
impl WalletSubcommand for NativeTokenTransferProgramSubcommandShielded {
async fn handle_subcommand(
self,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
match self {
2025-10-20 10:01:54 +03:00
NativeTokenTransferProgramSubcommandShielded::ShieldedOwned { from, to, amount } => {
let from: AccountId = from.parse().unwrap();
let to: AccountId = to.parse().unwrap();
2025-10-14 15:29:18 +03:00
let (res, secret) = NativeTokenTransfer(wallet_core)
.send_shielded_transfer(from, to, amount)
.await?;
2025-10-14 15:29:18 +03:00
println!("Results of tx send are {res:#?}");
2025-10-14 15:29:18 +03:00
2025-10-20 10:01:54 +03:00
let tx_hash = res.tx_hash;
let transfer_tx = wallet_core
.poll_native_token_transfer(tx_hash.clone())
.await?;
2025-10-14 15:29:18 +03:00
2025-10-20 10:01:54 +03:00
if let NSSATransaction::PrivacyPreserving(tx) = transfer_tx {
let acc_decode_data = vec![(secret, to)];
2025-10-14 15:29:18 +03:00
2025-10-20 10:01:54 +03:00
wallet_core.decode_insert_privacy_preserving_transaction_results(
tx,
&acc_decode_data,
)?;
}
2025-10-14 15:29:18 +03:00
2025-10-28 16:02:30 +02:00
let path = wallet_core.store_persistent_data().await?;
2025-10-14 15:29:18 +03:00
2025-10-20 10:01:54 +03:00
println!("Stored persistent accounts at {path:#?}");
2025-10-14 15:29:18 +03:00
2025-10-20 10:01:54 +03:00
Ok(SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash })
}
NativeTokenTransferProgramSubcommandShielded::ShieldedForeign {
from,
to_npk,
to_ipk,
amount,
} => {
let from: AccountId = from.parse().unwrap();
2025-10-14 15:29:18 +03:00
2025-10-20 10:01:54 +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);
2025-10-14 15:29:18 +03:00
2025-10-20 10:01:54 +03:00
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());
let (res, _) = NativeTokenTransfer(wallet_core)
.send_shielded_transfer_to_outer_account(from, to_npk, to_ipk, amount)
2025-10-20 10:01:54 +03:00
.await?;
println!("Results of tx send are {res:#?}");
2025-10-20 10:01:54 +03:00
let tx_hash = res.tx_hash;
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::PrivacyPreservingTransfer { tx_hash })
}
2025-10-14 15:29:18 +03:00
}
}
}
impl WalletSubcommand for NativeTokenTransferProgramSubcommand {
async fn handle_subcommand(
self,
wallet_core: &mut WalletCore,
) -> Result<SubcommandReturnValue> {
match self {
NativeTokenTransferProgramSubcommand::Private(private_subcommand) => {
private_subcommand.handle_subcommand(wallet_core).await
}
NativeTokenTransferProgramSubcommand::Shielded(shielded_subcommand) => {
shielded_subcommand.handle_subcommand(wallet_core).await
}
2025-10-20 10:01:54 +03:00
NativeTokenTransferProgramSubcommand::Deshielded { from, to, amount } => {
let from: AccountId = from.parse().unwrap();
let to: AccountId = to.parse().unwrap();
2025-10-14 15:29:18 +03:00
let (res, secret) = NativeTokenTransfer(wallet_core)
.send_deshielded_transfer(from, to, amount)
2025-10-14 15:29:18 +03:00
.await?;
println!("Results of tx send are {res:#?}");
2025-10-14 15:29:18 +03:00
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 {
let acc_decode_data = vec![(secret, from)];
wallet_core.decode_insert_privacy_preserving_transaction_results(
tx,
&acc_decode_data,
)?;
}
2025-10-28 16:02:30 +02:00
let path = wallet_core.store_persistent_data().await?;
2025-10-14 15:29:18 +03:00
println!("Stored persistent accounts at {path:#?}");
Ok(SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash })
}
2025-10-20 10:01:54 +03:00
NativeTokenTransferProgramSubcommand::Public { from, to, amount } => {
let from: AccountId = from.parse().unwrap();
let to: AccountId = to.parse().unwrap();
2025-10-14 15:29:18 +03:00
let res = NativeTokenTransfer(wallet_core)
.send_public_transfer(from, to, amount)
2025-10-14 15:29:18 +03:00
.await?;
println!("Results of tx send are {res:#?}");
2025-10-14 15:29:18 +03:00
let transfer_tx = wallet_core.poll_native_token_transfer(res.tx_hash).await?;
println!("Transaction data is {transfer_tx:?}");
2025-10-28 16:02:30 +02:00
let path = wallet_core.store_persistent_data().await?;
2025-10-14 15:29:18 +03:00
println!("Stored persistent accounts at {path:#?}");
Ok(SubcommandReturnValue::Empty)
}
}
}
}