2025-11-30 03:16:47 +03:00
|
|
|
use anyhow::{Context, Result};
|
2025-10-14 15:29:18 +03:00
|
|
|
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
|
|
|
|
2025-10-28 16:02:30 +02:00
|
|
|
use crate::{
|
2025-11-27 22:07:53 +03:00
|
|
|
WalletCore,
|
|
|
|
|
cli::{SubcommandReturnValue, WalletSubcommand},
|
2025-11-24 17:09:30 +03:00
|
|
|
helperfunctions::{AccountPrivacyKind, parse_addr_with_privacy_prefix},
|
2025-11-30 01:57:59 +03:00
|
|
|
program_facades::pinata::Pinata,
|
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-30 03:16:47 +03:00
|
|
|
/// to - valid 32 byte base58 string with privacy prefix
|
2025-10-28 16:02:30 +02:00
|
|
|
#[arg(long)]
|
2025-11-30 03:16:47 +03:00
|
|
|
to: String,
|
2025-10-28 16:02:30 +02:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WalletSubcommand for PinataProgramAgnosticSubcommand {
|
|
|
|
|
async fn handle_subcommand(
|
|
|
|
|
self,
|
|
|
|
|
wallet_core: &mut WalletCore,
|
|
|
|
|
) -> Result<SubcommandReturnValue> {
|
|
|
|
|
let underlying_subcommand = match self {
|
2025-11-30 03:16:47 +03:00
|
|
|
PinataProgramAgnosticSubcommand::Claim { to } => {
|
|
|
|
|
let (to, to_addr_privacy) = parse_addr_with_privacy_prefix(&to)?;
|
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(),
|
2025-11-30 03:16:47 +03:00
|
|
|
winner_account_id: to,
|
2025-10-28 16:02:30 +02:00
|
|
|
})
|
|
|
|
|
}
|
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(),
|
2025-11-30 03:16:47 +03:00
|
|
|
winner_account_id: to,
|
2025-10-28 16:02:30 +02:00
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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-10-14 15:29:18 +03:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
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-10-14 15:29:18 +03:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
} => {
|
2025-11-30 03:16:47 +03:00
|
|
|
let pinata_account_id = pinata_account_id.parse().unwrap();
|
|
|
|
|
let solution = find_solution(wallet_core, pinata_account_id)
|
|
|
|
|
.await
|
|
|
|
|
.context("failed to compute solution")?;
|
|
|
|
|
|
2025-11-30 01:57:59 +03:00
|
|
|
let res = Pinata(wallet_core)
|
|
|
|
|
.claim(
|
2025-11-30 03:16:47 +03:00
|
|
|
pinata_account_id,
|
2025-11-24 17:09:30 +03:00
|
|
|
winner_account_id.parse().unwrap(),
|
2025-10-14 15:29:18 +03:00
|
|
|
solution,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
2025-11-30 02:37:43 +03:00
|
|
|
|
2025-11-30 03:16:47 +03:00
|
|
|
println!("Results of tx send are {res:#?}");
|
2025-11-30 02:37:43 +03:00
|
|
|
|
|
|
|
|
let tx_hash = res.tx_hash;
|
|
|
|
|
let transfer_tx = wallet_core
|
|
|
|
|
.poll_native_token_transfer(tx_hash.clone())
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
println!("Transaction data is {transfer_tx:?}");
|
|
|
|
|
|
2025-10-14 15:29:18 +03:00
|
|
|
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
|
|
|
} => {
|
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-11-30 03:16:47 +03:00
|
|
|
let solution = find_solution(wallet_core, pinata_account_id)
|
|
|
|
|
.await
|
|
|
|
|
.context("failed to compute solution")?;
|
2025-10-14 15:29:18 +03:00
|
|
|
|
2025-11-30 01:57:59 +03:00
|
|
|
let (res, secret_winner) = Pinata(wallet_core)
|
|
|
|
|
.claim_private_owned_account(pinata_account_id, winner_account_id, solution)
|
2025-10-15 15:32:15 +03:00
|
|
|
.await?;
|
2025-10-14 15:29:18 +03:00
|
|
|
|
2025-11-30 03:16:47 +03:00
|
|
|
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?;
|
|
|
|
|
|
2025-11-30 02:37:43 +03:00
|
|
|
println!("Transaction data is {transfer_tx:?}");
|
|
|
|
|
|
2025-10-14 15:29:18 +03:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-30 03:16:47 +03:00
|
|
|
|
|
|
|
|
async fn find_solution(wallet: &WalletCore, pinata_account_id: nssa::AccountId) -> Result<u128> {
|
|
|
|
|
let account = wallet.get_account_public(pinata_account_id).await?;
|
|
|
|
|
let data: [u8; 33] = account
|
|
|
|
|
.data
|
2025-12-05 02:17:09 +03:00
|
|
|
.as_ref()
|
2025-11-30 03:16:47 +03:00
|
|
|
.try_into()
|
|
|
|
|
.map_err(|_| anyhow::Error::msg("invalid pinata account data"))?;
|
|
|
|
|
|
|
|
|
|
println!("Computing solution for pinata...");
|
|
|
|
|
let now = std::time::Instant::now();
|
|
|
|
|
|
|
|
|
|
let solution = compute_solution(data);
|
|
|
|
|
|
|
|
|
|
println!("Found solution {solution} in {:?}", now.elapsed());
|
|
|
|
|
Ok(solution)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn compute_solution(data: [u8; 33]) -> u128 {
|
|
|
|
|
let difficulty = data[0];
|
|
|
|
|
let seed = &data[1..];
|
|
|
|
|
|
|
|
|
|
let mut solution = 0u128;
|
|
|
|
|
while !validate_solution(difficulty, seed, solution) {
|
|
|
|
|
solution = solution.checked_add(1).expect("solution overflowed u128");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
solution
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn validate_solution(difficulty: u8, seed: &[u8], solution: u128) -> bool {
|
|
|
|
|
use sha2::{Digest as _, digest::FixedOutput as _};
|
|
|
|
|
|
|
|
|
|
let mut bytes = [0; 32 + 16];
|
|
|
|
|
bytes[..32].copy_from_slice(seed);
|
|
|
|
|
bytes[32..].copy_from_slice(&solution.to_le_bytes());
|
|
|
|
|
|
|
|
|
|
let mut hasher = sha2::Sha256::new();
|
|
|
|
|
hasher.update(bytes);
|
|
|
|
|
let digest: [u8; 32] = hasher.finalize_fixed().into();
|
|
|
|
|
|
|
|
|
|
let difficulty = difficulty as usize;
|
|
|
|
|
digest[..difficulty].iter().all(|&b| b == 0)
|
|
|
|
|
}
|