2025-10-14 15:29:18 +03:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use clap::Subcommand;
|
2025-10-28 16:02:30 +02:00
|
|
|
use common::{PINATA_BASE58, transaction::NSSATransaction};
|
2025-10-14 15:29:18 +03:00
|
|
|
use log::info;
|
|
|
|
|
|
2025-10-28 16:02:30 +02:00
|
|
|
use crate::{
|
|
|
|
|
SubcommandReturnValue, WalletCore,
|
|
|
|
|
cli::WalletSubcommand,
|
2025-11-24 17:09:30 +03:00
|
|
|
helperfunctions::{AccountPrivacyKind, parse_addr_with_privacy_prefix},
|
2025-10-28 16:02:30 +02:00
|
|
|
};
|
|
|
|
|
|
2025-11-26 00:27:20 +03:00
|
|
|
/// Represents generic CLI subcommand for a wallet working with pinata program
|
2025-10-28 16:02:30 +02:00
|
|
|
#[derive(Subcommand, Debug, Clone)]
|
|
|
|
|
pub enum PinataProgramAgnosticSubcommand {
|
2025-11-26 00:27:20 +03:00
|
|
|
/// Claim pinata
|
2025-10-28 16:02:30 +02:00
|
|
|
Claim {
|
2025-11-26 00:27:20 +03:00
|
|
|
/// to_account_id - valid 32 byte base58 string with privacy prefix
|
2025-10-28 16:02:30 +02:00
|
|
|
#[arg(long)]
|
2025-11-24 17:09:30 +03:00
|
|
|
to_account_id: String,
|
2025-11-26 00:27:20 +03:00
|
|
|
/// solution - solution to pinata challenge
|
2025-10-28 16:02:30 +02:00
|
|
|
#[arg(long)]
|
|
|
|
|
solution: u128,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WalletSubcommand for PinataProgramAgnosticSubcommand {
|
|
|
|
|
async fn handle_subcommand(
|
|
|
|
|
self,
|
|
|
|
|
wallet_core: &mut WalletCore,
|
|
|
|
|
) -> Result<SubcommandReturnValue> {
|
|
|
|
|
let underlying_subcommand = match self {
|
2025-11-24 17:09:30 +03:00
|
|
|
PinataProgramAgnosticSubcommand::Claim {
|
|
|
|
|
to_account_id,
|
|
|
|
|
solution,
|
|
|
|
|
} => {
|
|
|
|
|
let (to_account_id, to_addr_privacy) =
|
|
|
|
|
parse_addr_with_privacy_prefix(&to_account_id)?;
|
2025-10-28 16:02:30 +02:00
|
|
|
|
|
|
|
|
match to_addr_privacy {
|
2025-11-24 17:09:30 +03:00
|
|
|
AccountPrivacyKind::Public => {
|
2025-10-28 16:02:30 +02:00
|
|
|
PinataProgramSubcommand::Public(PinataProgramSubcommandPublic::Claim {
|
2025-11-24 17:09:30 +03:00
|
|
|
pinata_account_id: PINATA_BASE58.to_string(),
|
|
|
|
|
winner_account_id: to_account_id,
|
2025-10-28 16:02:30 +02:00
|
|
|
solution,
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-11-24 17:09:30 +03:00
|
|
|
AccountPrivacyKind::Private => PinataProgramSubcommand::Private(
|
2025-10-28 16:02:30 +02:00
|
|
|
PinataProgramSubcommandPrivate::ClaimPrivateOwned {
|
2025-11-24 17:09:30 +03:00
|
|
|
pinata_account_id: PINATA_BASE58.to_string(),
|
|
|
|
|
winner_account_id: to_account_id,
|
2025-10-28 16:02:30 +02:00
|
|
|
solution,
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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 pinata program
|
2025-10-14 15:29:18 +03:00
|
|
|
#[derive(Subcommand, Debug, Clone)]
|
|
|
|
|
pub enum PinataProgramSubcommand {
|
2025-11-26 00:27:20 +03:00
|
|
|
/// Public execution
|
2025-10-14 15:29:18 +03:00
|
|
|
#[command(subcommand)]
|
|
|
|
|
Public(PinataProgramSubcommandPublic),
|
2025-11-26 00:27:20 +03:00
|
|
|
/// Private execution
|
2025-10-14 15:29:18 +03:00
|
|
|
#[command(subcommand)]
|
|
|
|
|
Private(PinataProgramSubcommandPrivate),
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-26 00:27:20 +03:00
|
|
|
/// Represents generic public CLI subcommand for a wallet working with pinata program
|
2025-10-14 15:29:18 +03:00
|
|
|
#[derive(Subcommand, Debug, Clone)]
|
|
|
|
|
pub enum PinataProgramSubcommandPublic {
|
|
|
|
|
// TODO: Testnet only. Refactor to prevent compilation on mainnet.
|
|
|
|
|
// Claim piñata prize
|
2025-10-20 10:01:54 +03:00
|
|
|
Claim {
|
2025-11-26 00:27:20 +03:00
|
|
|
/// pinata_account_id - valid 32 byte hex string
|
2025-10-14 15:29:18 +03:00
|
|
|
#[arg(long)]
|
2025-11-24 17:09:30 +03:00
|
|
|
pinata_account_id: String,
|
2025-11-26 00:27:20 +03:00
|
|
|
/// winner_account_id - valid 32 byte hex string
|
2025-10-14 15:29:18 +03:00
|
|
|
#[arg(long)]
|
2025-11-24 17:09:30 +03:00
|
|
|
winner_account_id: String,
|
2025-11-26 00:27:20 +03:00
|
|
|
/// solution - solution to pinata challenge
|
2025-10-14 15:29:18 +03:00
|
|
|
#[arg(long)]
|
|
|
|
|
solution: u128,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-26 00:27:20 +03:00
|
|
|
/// Represents generic private CLI subcommand for a wallet working with pinata program
|
2025-10-14 15:29:18 +03:00
|
|
|
#[derive(Subcommand, Debug, Clone)]
|
|
|
|
|
pub enum PinataProgramSubcommandPrivate {
|
|
|
|
|
// TODO: Testnet only. Refactor to prevent compilation on mainnet.
|
|
|
|
|
// Claim piñata prize
|
2025-10-20 10:01:54 +03:00
|
|
|
ClaimPrivateOwned {
|
2025-11-26 00:27:20 +03:00
|
|
|
/// pinata_account_id - valid 32 byte hex string
|
2025-10-14 15:29:18 +03:00
|
|
|
#[arg(long)]
|
2025-11-24 17:09:30 +03:00
|
|
|
pinata_account_id: String,
|
2025-11-26 00:27:20 +03:00
|
|
|
/// winner_account_id - valid 32 byte hex string
|
2025-10-14 15:29:18 +03:00
|
|
|
#[arg(long)]
|
2025-11-24 17:09:30 +03:00
|
|
|
winner_account_id: String,
|
2025-11-26 00:27:20 +03:00
|
|
|
/// solution - solution to pinata challenge
|
2025-10-14 15:29:18 +03:00
|
|
|
#[arg(long)]
|
|
|
|
|
solution: u128,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WalletSubcommand for PinataProgramSubcommandPublic {
|
|
|
|
|
async fn handle_subcommand(
|
|
|
|
|
self,
|
|
|
|
|
wallet_core: &mut WalletCore,
|
|
|
|
|
) -> Result<SubcommandReturnValue> {
|
|
|
|
|
match self {
|
2025-10-20 10:01:54 +03:00
|
|
|
PinataProgramSubcommandPublic::Claim {
|
2025-11-24 17:09:30 +03:00
|
|
|
pinata_account_id,
|
|
|
|
|
winner_account_id,
|
2025-10-14 15:29:18 +03:00
|
|
|
solution,
|
|
|
|
|
} => {
|
|
|
|
|
let res = wallet_core
|
|
|
|
|
.claim_pinata(
|
2025-11-24 17:09:30 +03:00
|
|
|
pinata_account_id.parse().unwrap(),
|
|
|
|
|
winner_account_id.parse().unwrap(),
|
2025-10-14 15:29:18 +03:00
|
|
|
solution,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
info!("Results of tx send is {res:#?}");
|
|
|
|
|
|
|
|
|
|
Ok(SubcommandReturnValue::Empty)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WalletSubcommand for PinataProgramSubcommandPrivate {
|
|
|
|
|
async fn handle_subcommand(
|
|
|
|
|
self,
|
|
|
|
|
wallet_core: &mut WalletCore,
|
|
|
|
|
) -> Result<SubcommandReturnValue> {
|
|
|
|
|
match self {
|
2025-10-20 10:01:54 +03:00
|
|
|
PinataProgramSubcommandPrivate::ClaimPrivateOwned {
|
2025-11-24 17:09:30 +03:00
|
|
|
pinata_account_id,
|
|
|
|
|
winner_account_id,
|
2025-10-14 15:29:18 +03:00
|
|
|
solution,
|
|
|
|
|
} => {
|
2025-11-24 17:09:30 +03:00
|
|
|
let pinata_account_id = pinata_account_id.parse().unwrap();
|
|
|
|
|
let winner_account_id = winner_account_id.parse().unwrap();
|
2025-10-14 15:29:18 +03:00
|
|
|
|
2025-10-15 15:32:15 +03:00
|
|
|
let winner_initialization = wallet_core
|
2025-11-24 17:09:30 +03:00
|
|
|
.check_private_account_initialized(&winner_account_id)
|
2025-10-15 15:32:15 +03:00
|
|
|
.await?;
|
2025-10-14 15:29:18 +03:00
|
|
|
|
2025-10-15 15:32:15 +03:00
|
|
|
let (res, [secret_winner]) = if let Some(winner_proof) = winner_initialization {
|
2025-10-14 15:29:18 +03:00
|
|
|
wallet_core
|
|
|
|
|
.claim_pinata_private_owned_account_already_initialized(
|
2025-11-24 17:09:30 +03:00
|
|
|
pinata_account_id,
|
|
|
|
|
winner_account_id,
|
2025-10-14 15:29:18 +03:00
|
|
|
solution,
|
2025-10-15 15:44:52 +03:00
|
|
|
winner_proof,
|
2025-10-14 15:29:18 +03:00
|
|
|
)
|
|
|
|
|
.await?
|
|
|
|
|
} else {
|
|
|
|
|
wallet_core
|
|
|
|
|
.claim_pinata_private_owned_account_not_initialized(
|
2025-11-24 17:09:30 +03:00
|
|
|
pinata_account_id,
|
|
|
|
|
winner_account_id,
|
2025-10-14 15:29:18 +03:00
|
|
|
solution,
|
|
|
|
|
)
|
|
|
|
|
.await?
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
info!("Results of tx send is {res:#?}");
|
|
|
|
|
|
|
|
|
|
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 {
|
2025-11-24 17:09:30 +03:00
|
|
|
let acc_decode_data = vec![(secret_winner, winner_account_id)];
|
2025-10-14 15:29:18 +03: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-14 15:29:18 +03:00
|
|
|
|
|
|
|
|
println!("Stored persistent accounts at {path:#?}");
|
|
|
|
|
|
|
|
|
|
Ok(SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WalletSubcommand for PinataProgramSubcommand {
|
|
|
|
|
async fn handle_subcommand(
|
|
|
|
|
self,
|
|
|
|
|
wallet_core: &mut WalletCore,
|
|
|
|
|
) -> Result<SubcommandReturnValue> {
|
|
|
|
|
match self {
|
|
|
|
|
PinataProgramSubcommand::Private(private_subcommand) => {
|
|
|
|
|
private_subcommand.handle_subcommand(wallet_core).await
|
|
|
|
|
}
|
|
|
|
|
PinataProgramSubcommand::Public(public_subcommand) => {
|
|
|
|
|
public_subcommand.handle_subcommand(wallet_core).await
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|