2026-03-04 18:42:33 +03:00
|
|
|
use anyhow::{Context as _, 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};
|
2026-03-03 23:21:18 +01:00
|
|
|
use nssa::{Account, AccountId};
|
2025-10-14 15:29:18 +03:00
|
|
|
|
2025-10-28 16:02:30 +02:00
|
|
|
use crate::{
|
2025-12-11 14:46:16 +02:00
|
|
|
AccDecodeData::Decode,
|
2025-11-27 22:07:53 +03:00
|
|
|
WalletCore,
|
|
|
|
|
cli::{SubcommandReturnValue, WalletSubcommand},
|
feat: add --account-label as alternative to --account-id across all wallet subcommands
Allow users to identify accounts by their human-readable label instead of the
full `Privacy/base58` account ID. This makes the CLI much more ergonomic for
users who have labeled their accounts.
- [x] Add `resolve_account_label()` in `helperfunctions.rs` that looks up a label,
determines account privacy (public/private), and returns the full `Privacy/id` string
- [x] Add `--account-label` (or `--from-label`, `--to-label`, `--definition-label`,
`--holder-label`, `--user-holding-*-label`) as mutually exclusive alternative to
every `--account-id`-style flag across all subcommands:
- `account get`, `account label`
- `auth-transfer init`, `auth-transfer send`
- `token new`, `token send`, `token burn`, `token mint`
- `pinata claim`
- `amm new`, `amm swap`, `amm add-liquidity`, `amm remove-liquidity`
- [x] Update zsh completion script with `_wallet_account_labels()` helper
- [x] Add bash completion script with `_wallet_get_account_labels()` helper
1. Start a local sequencer
2. Create accounts and label them: `wallet account new public --label alice`
3. Use labels in commands: `wallet account get --account-label alice`
4. Verify mutual exclusivity: `wallet account get --account-id <id> --account-label alice` should error
5. Test shell completions: `wallet account get --account-label <TAB>` should list labels
None
None
- [x] Complete PR description
- [x] Implement the core functionality
- [ ] Add/update tests
- [x] Add/update documentation and inline comments
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 13:33:51 +11:00
|
|
|
helperfunctions::{AccountPrivacyKind, parse_addr_with_privacy_prefix, resolve_id_or_label},
|
2025-11-30 01:57:59 +03:00
|
|
|
program_facades::pinata::Pinata,
|
2025-10-28 16:02:30 +02:00
|
|
|
};
|
|
|
|
|
|
2026-03-10 00:17:43 +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 {
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Claim pinata.
|
2025-10-28 16:02:30 +02:00
|
|
|
Claim {
|
2026-03-10 00:17:43 +03:00
|
|
|
/// to - valid 32 byte base58 string with privacy prefix.
|
feat: add --account-label as alternative to --account-id across all wallet subcommands
Allow users to identify accounts by their human-readable label instead of the
full `Privacy/base58` account ID. This makes the CLI much more ergonomic for
users who have labeled their accounts.
- [x] Add `resolve_account_label()` in `helperfunctions.rs` that looks up a label,
determines account privacy (public/private), and returns the full `Privacy/id` string
- [x] Add `--account-label` (or `--from-label`, `--to-label`, `--definition-label`,
`--holder-label`, `--user-holding-*-label`) as mutually exclusive alternative to
every `--account-id`-style flag across all subcommands:
- `account get`, `account label`
- `auth-transfer init`, `auth-transfer send`
- `token new`, `token send`, `token burn`, `token mint`
- `pinata claim`
- `amm new`, `amm swap`, `amm add-liquidity`, `amm remove-liquidity`
- [x] Update zsh completion script with `_wallet_account_labels()` helper
- [x] Add bash completion script with `_wallet_get_account_labels()` helper
1. Start a local sequencer
2. Create accounts and label them: `wallet account new public --label alice`
3. Use labels in commands: `wallet account get --account-label alice`
4. Verify mutual exclusivity: `wallet account get --account-id <id> --account-label alice` should error
5. Test shell completions: `wallet account get --account-label <TAB>` should list labels
None
None
- [x] Complete PR description
- [x] Implement the core functionality
- [ ] Add/update tests
- [x] Add/update documentation and inline comments
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 13:33:51 +11:00
|
|
|
#[arg(
|
|
|
|
|
long,
|
|
|
|
|
conflicts_with = "to_label",
|
|
|
|
|
required_unless_present = "to_label"
|
|
|
|
|
)]
|
|
|
|
|
to: Option<String>,
|
|
|
|
|
/// To account label (alternative to --to).
|
|
|
|
|
#[arg(long, conflicts_with = "to")]
|
|
|
|
|
to_label: Option<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 {
|
feat: add --account-label as alternative to --account-id across all wallet subcommands
Allow users to identify accounts by their human-readable label instead of the
full `Privacy/base58` account ID. This makes the CLI much more ergonomic for
users who have labeled their accounts.
- [x] Add `resolve_account_label()` in `helperfunctions.rs` that looks up a label,
determines account privacy (public/private), and returns the full `Privacy/id` string
- [x] Add `--account-label` (or `--from-label`, `--to-label`, `--definition-label`,
`--holder-label`, `--user-holding-*-label`) as mutually exclusive alternative to
every `--account-id`-style flag across all subcommands:
- `account get`, `account label`
- `auth-transfer init`, `auth-transfer send`
- `token new`, `token send`, `token burn`, `token mint`
- `pinata claim`
- `amm new`, `amm swap`, `amm add-liquidity`, `amm remove-liquidity`
- [x] Update zsh completion script with `_wallet_account_labels()` helper
- [x] Add bash completion script with `_wallet_get_account_labels()` helper
1. Start a local sequencer
2. Create accounts and label them: `wallet account new public --label alice`
3. Use labels in commands: `wallet account get --account-label alice`
4. Verify mutual exclusivity: `wallet account get --account-id <id> --account-label alice` should error
5. Test shell completions: `wallet account get --account-label <TAB>` should list labels
None
None
- [x] Complete PR description
- [x] Implement the core functionality
- [ ] Add/update tests
- [x] Add/update documentation and inline comments
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 13:33:51 +11:00
|
|
|
Self::Claim { to, to_label } => {
|
|
|
|
|
let to = resolve_id_or_label(
|
|
|
|
|
to,
|
|
|
|
|
to_label,
|
|
|
|
|
&wallet_core.storage.labels,
|
|
|
|
|
&wallet_core.storage.user_data,
|
|
|
|
|
)?;
|
2025-11-30 03:16:47 +03:00
|
|
|
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 {
|
2026-03-04 18:42:33 +03:00
|
|
|
pinata_account_id: PINATA_BASE58.to_owned(),
|
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 {
|
2026-03-04 18:42:33 +03:00
|
|
|
pinata_account_id: PINATA_BASE58.to_owned(),
|
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
|
|
|
|
2026-03-10 00:17:43 +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 {
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Public execution.
|
2025-10-14 15:29:18 +03:00
|
|
|
#[command(subcommand)]
|
|
|
|
|
Public(PinataProgramSubcommandPublic),
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Private execution.
|
2025-10-14 15:29:18 +03:00
|
|
|
#[command(subcommand)]
|
|
|
|
|
Private(PinataProgramSubcommandPrivate),
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-10 00:17:43 +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 {
|
2026-03-10 00:17:43 +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,
|
2026-03-10 00:17:43 +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
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-10 00:17:43 +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 {
|
2026-03-10 00:17:43 +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,
|
2026-03-10 00:17:43 +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 {
|
2026-03-09 18:27:56 +03:00
|
|
|
Self::Claim {
|
2025-11-24 17:09:30 +03:00
|
|
|
pinata_account_id,
|
|
|
|
|
winner_account_id,
|
2025-10-14 15:29:18 +03:00
|
|
|
} => {
|
2026-03-03 23:21:18 +01:00
|
|
|
let pinata_account_id = pinata_account_id.parse()?;
|
|
|
|
|
let winner_account_id: AccountId = winner_account_id.parse()?;
|
|
|
|
|
|
|
|
|
|
ensure_public_recipient_initialized(wallet_core, winner_account_id).await?;
|
|
|
|
|
|
2025-11-30 03:16:47 +03:00
|
|
|
let solution = find_solution(wallet_core, pinata_account_id)
|
|
|
|
|
.await
|
|
|
|
|
.context("failed to compute solution")?;
|
|
|
|
|
|
2026-03-13 22:38:23 +03:00
|
|
|
let tx_hash = Pinata(wallet_core)
|
2026-03-03 23:21:18 +01:00
|
|
|
.claim(pinata_account_id, winner_account_id, solution)
|
2025-10-14 15:29:18 +03:00
|
|
|
.await?;
|
2025-11-30 02:37:43 +03:00
|
|
|
|
2026-03-14 03:20:37 +03:00
|
|
|
println!("Transaction hash is {tx_hash}");
|
2025-11-30 02:37:43 +03:00
|
|
|
|
2026-01-29 22:20:42 +03:00
|
|
|
let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?;
|
2025-11-30 02:37:43 +03:00
|
|
|
|
|
|
|
|
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 {
|
2026-03-09 18:27:56 +03:00
|
|
|
Self::ClaimPrivateOwned {
|
2025-11-24 17:09:30 +03:00
|
|
|
pinata_account_id,
|
|
|
|
|
winner_account_id,
|
2025-10-14 15:29:18 +03:00
|
|
|
} => {
|
2026-03-03 23:21:18 +01:00
|
|
|
let pinata_account_id = pinata_account_id.parse()?;
|
|
|
|
|
let winner_account_id: AccountId = winner_account_id.parse()?;
|
|
|
|
|
|
|
|
|
|
ensure_private_owned_recipient_initialized(wallet_core, winner_account_id)?;
|
|
|
|
|
|
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
|
|
|
|
2026-03-13 22:38:23 +03:00
|
|
|
let (tx_hash, secret_winner) = Pinata(wallet_core)
|
2025-11-30 01:57:59 +03:00
|
|
|
.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
|
|
|
|
2026-03-14 03:20:37 +03:00
|
|
|
println!("Transaction hash is {tx_hash}");
|
2025-10-14 15:29:18 +03:00
|
|
|
|
2026-01-29 22:20:42 +03:00
|
|
|
let transfer_tx = wallet_core.poll_native_token_transfer(tx_hash).await?;
|
2025-10-14 15:29:18 +03:00
|
|
|
|
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-12-11 14:46:16 +02:00
|
|
|
let acc_decode_data = vec![Decode(secret_winner, winner_account_id)];
|
2025-10-14 15:29:18 +03:00
|
|
|
|
|
|
|
|
wallet_core.decode_insert_privacy_preserving_transaction_results(
|
2026-03-03 23:21:08 +03:00
|
|
|
&tx,
|
2025-10-14 15:29:18 +03:00
|
|
|
&acc_decode_data,
|
|
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-31 04:02:25 +03:00
|
|
|
wallet_core.store_persistent_data().await?;
|
2025-10-14 15:29:18 +03:00
|
|
|
|
|
|
|
|
Ok(SubcommandReturnValue::PrivacyPreservingTransfer { tx_hash })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WalletSubcommand for PinataProgramSubcommand {
|
|
|
|
|
async fn handle_subcommand(
|
|
|
|
|
self,
|
|
|
|
|
wallet_core: &mut WalletCore,
|
|
|
|
|
) -> Result<SubcommandReturnValue> {
|
|
|
|
|
match self {
|
2026-03-09 18:27:56 +03:00
|
|
|
Self::Private(private_subcommand) => {
|
2025-10-14 15:29:18 +03:00
|
|
|
private_subcommand.handle_subcommand(wallet_core).await
|
|
|
|
|
}
|
2026-03-09 18:27:56 +03:00
|
|
|
Self::Public(public_subcommand) => {
|
2025-10-14 15:29:18 +03:00
|
|
|
public_subcommand.handle_subcommand(wallet_core).await
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-30 03:16:47 +03:00
|
|
|
|
2026-03-03 23:21:18 +01:00
|
|
|
async fn ensure_public_recipient_initialized(
|
|
|
|
|
wallet_core: &WalletCore,
|
|
|
|
|
winner_account_id: AccountId,
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
let account = wallet_core
|
|
|
|
|
.get_account_public(winner_account_id)
|
|
|
|
|
.await
|
|
|
|
|
.with_context(|| format!("failed to fetch recipient account Public/{winner_account_id}"))?;
|
|
|
|
|
|
|
|
|
|
if account == Account::default() {
|
|
|
|
|
anyhow::bail!(
|
|
|
|
|
"Recipient account Public/{winner_account_id} is uninitialized.\n\
|
|
|
|
|
Initialize it first:\n \
|
|
|
|
|
wallet auth-transfer init --account-id Public/{winner_account_id}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ensure_private_owned_recipient_initialized(
|
|
|
|
|
wallet_core: &WalletCore,
|
|
|
|
|
winner_account_id: AccountId,
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
let Some(account) = wallet_core.get_account_private(winner_account_id) else {
|
|
|
|
|
anyhow::bail!(
|
|
|
|
|
"Recipient account Private/{winner_account_id} is not found in this wallet.\n\
|
|
|
|
|
`wallet pinata claim --to Private/...` supports owned private accounts only."
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if account == Account::default() {
|
|
|
|
|
anyhow::bail!(
|
|
|
|
|
"Recipient account Private/{winner_account_id} is uninitialized.\n\
|
|
|
|
|
Initialize it first:\n \
|
|
|
|
|
wallet auth-transfer init --account-id Private/{winner_account_id}\n\
|
|
|
|
|
Then sync private state:\n \
|
|
|
|
|
wallet account sync-private"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn find_solution(wallet: &WalletCore, pinata_account_id: AccountId) -> Result<u128> {
|
2025-11-30 03:16:47 +03:00
|
|
|
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()
|
2026-03-04 18:42:33 +03:00
|
|
|
.map_err(|_err| anyhow::Error::msg("invalid pinata account data"))?;
|
2025-11-30 03:16:47 +03:00
|
|
|
|
|
|
|
|
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..];
|
|
|
|
|
|
2026-03-04 18:42:33 +03:00
|
|
|
let mut solution = 0_u128;
|
2025-11-30 03:16:47 +03:00
|
|
|
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();
|
|
|
|
|
|
2026-03-04 18:42:33 +03:00
|
|
|
let difficulty = usize::from(difficulty);
|
2025-11-30 03:16:47 +03:00
|
|
|
digest[..difficulty].iter().all(|&b| b == 0)
|
|
|
|
|
}
|