From e0f5344e8fee5da982fa5d49687e51f88e837577 Mon Sep 17 00:00:00 2001 From: Ruben Date: Sat, 12 Oct 2024 10:21:20 +0200 Subject: [PATCH] feat: mint-proofs command fix typo make output more readable. Co-authored-by: Pavol Rusnak --- crates/cdk-cli/src/main.rs | 9 +++- .../src/sub_commands/list_mint_proofs.rs | 41 +++++++++++++++++++ crates/cdk-cli/src/sub_commands/mod.rs | 1 + crates/cdk/src/amount.rs | 6 ++- crates/cdk/src/nuts/nut00/mod.rs | 15 ++++--- crates/cdk/src/wallet/multi_mint_wallet.rs | 16 +++++++- 6 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 crates/cdk-cli/src/sub_commands/list_mint_proofs.rs diff --git a/crates/cdk-cli/src/main.rs b/crates/cdk-cli/src/main.rs index 2da00dc8f..0de66bc51 100644 --- a/crates/cdk-cli/src/main.rs +++ b/crates/cdk-cli/src/main.rs @@ -70,6 +70,8 @@ enum Commands { Restore(sub_commands::restore::RestoreSubCommand), /// Update Mint Url UpdateMintUrl(sub_commands::update_mint_url::UpdateMintUrlSubCommand), + /// Get proofs from mint. + ListMintProofs, } #[tokio::main] @@ -123,10 +125,10 @@ async fn main() -> Result<()> { let mut rng = rand::thread_rng(); let random_bytes: [u8; 32] = rng.gen(); - let mnemnic = Mnemonic::from_entropy(&random_bytes)?; + let mnemonic = Mnemonic::from_entropy(&random_bytes)?; tracing::info!("Using randomly generated seed you will not be able to restore"); - mnemnic + mnemonic } }; @@ -199,5 +201,8 @@ async fn main() -> Result<()> { sub_commands::update_mint_url::update_mint_url(&multi_mint_wallet, sub_command_args) .await } + Commands::ListMintProofs => { + sub_commands::list_mint_proofs::proofs(&multi_mint_wallet).await + } } } diff --git a/crates/cdk-cli/src/sub_commands/list_mint_proofs.rs b/crates/cdk-cli/src/sub_commands/list_mint_proofs.rs new file mode 100644 index 000000000..f4e8a4d6d --- /dev/null +++ b/crates/cdk-cli/src/sub_commands/list_mint_proofs.rs @@ -0,0 +1,41 @@ +use std::collections::BTreeMap; + +use anyhow::Result; +use cdk::{ + mint_url::MintUrl, + nuts::{CurrencyUnit, Proof}, + wallet::multi_mint_wallet::MultiMintWallet, +}; + +pub async fn proofs(multi_mint_wallet: &MultiMintWallet) -> Result<()> { + list_proofs(multi_mint_wallet).await?; + Ok(()) +} + +async fn list_proofs( + multi_mint_wallet: &MultiMintWallet, +) -> Result, CurrencyUnit))>> { + let wallets_proofs: BTreeMap, CurrencyUnit)> = + multi_mint_wallet.list_proofs().await?; + + let mut proofs_vec = Vec::with_capacity(wallets_proofs.len()); + + for (i, (mint_url, proofs)) in wallets_proofs.iter().enumerate() { + let mint_url = mint_url.clone(); + println!("{i}: {mint_url}"); + println!("| Amount | Unit | Secret | DLEQ proof included"); + println!("|----------|------|------------------------------------------------------------------|--------------------"); + for proof in &proofs.0 { + println!( + "| {:8} | {:4} | {:64} | {}", + proof.amount, + proofs.1, + proof.secret, + proof.dleq.is_some() + ); + } + println!(); + proofs_vec.push((mint_url, proofs.clone())) + } + Ok(proofs_vec) +} diff --git a/crates/cdk-cli/src/sub_commands/mod.rs b/crates/cdk-cli/src/sub_commands/mod.rs index cb79db557..eee3cf327 100644 --- a/crates/cdk-cli/src/sub_commands/mod.rs +++ b/crates/cdk-cli/src/sub_commands/mod.rs @@ -2,6 +2,7 @@ pub mod balance; pub mod burn; pub mod check_spent; pub mod decode_token; +pub mod list_mint_proofs; pub mod melt; pub mod mint; pub mod mint_info; diff --git a/crates/cdk/src/amount.rs b/crates/cdk/src/amount.rs index 7e2d4e26f..2d9fa34bd 100644 --- a/crates/cdk/src/amount.rs +++ b/crates/cdk/src/amount.rs @@ -138,7 +138,11 @@ impl Default for &Amount { impl fmt::Display for Amount { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.0) + if let Some(width) = f.width() { + write!(f, "{:width$}", self.0, width = width) + } else { + write!(f, "{}", self.0) + } } } diff --git a/crates/cdk/src/nuts/nut00/mod.rs b/crates/cdk/src/nuts/nut00/mod.rs index 58fff63a4..98223792b 100644 --- a/crates/cdk/src/nuts/nut00/mod.rs +++ b/crates/cdk/src/nuts/nut00/mod.rs @@ -378,11 +378,16 @@ impl FromStr for CurrencyUnit { impl fmt::Display for CurrencyUnit { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - CurrencyUnit::Sat => write!(f, "sat"), - CurrencyUnit::Msat => write!(f, "msat"), - CurrencyUnit::Usd => write!(f, "usd"), - CurrencyUnit::Eur => write!(f, "eur"), + let s = match self { + CurrencyUnit::Sat => "sat", + CurrencyUnit::Msat => "msat", + CurrencyUnit::Usd => "usd", + CurrencyUnit::Eur => "eur", + }; + if let Some(width) = f.width() { + write!(f, "{:width$}", s, width = width) + } else { + write!(f, "{}", s) } } } diff --git a/crates/cdk/src/wallet/multi_mint_wallet.rs b/crates/cdk/src/wallet/multi_mint_wallet.rs index c20336050..3852fe496 100644 --- a/crates/cdk/src/wallet/multi_mint_wallet.rs +++ b/crates/cdk/src/wallet/multi_mint_wallet.rs @@ -16,7 +16,7 @@ use super::types::SendKind; use super::Error; use crate::amount::SplitTarget; use crate::mint_url::MintUrl; -use crate::nuts::{CurrencyUnit, SecretKey, SpendingConditions, Token}; +use crate::nuts::{CurrencyUnit, Proof, SecretKey, SpendingConditions, Token}; use crate::types::Melted; use crate::wallet::types::MintQuote; use crate::{Amount, Wallet}; @@ -117,6 +117,20 @@ impl MultiMintWallet { Ok(balances) } + /// List proofs. + #[instrument(skip(self))] + pub async fn list_proofs( + &self, + ) -> Result, CurrencyUnit)>, Error> { + let mut mint_proofs = BTreeMap::new(); + + for (WalletKey { mint_url, unit: u }, wallet) in self.wallets.lock().await.iter() { + let wallet_proofs = wallet.get_proofs().await?; + mint_proofs.insert(mint_url.clone(), (wallet_proofs, *u)); + } + Ok(mint_proofs) + } + /// Create cashu token #[instrument(skip(self))] pub async fn send(