Эх сурвалжийг харах

feat: mint-proofs command

fix typo

make output more readable.

Co-authored-by: Pavol Rusnak <pavol@rusnak.io>
Ruben 5 сар өмнө
parent
commit
e0f5344e8f

+ 7 - 2
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
+        }
     }
 }

+ 41 - 0
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<Vec<(MintUrl, (Vec<Proof>, CurrencyUnit))>> {
+    let wallets_proofs: BTreeMap<MintUrl, (Vec<Proof>, 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)
+}

+ 1 - 0
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;

+ 5 - 1
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)
+        }
     }
 }
 

+ 10 - 5
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)
         }
     }
 }

+ 15 - 1
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<BTreeMap<MintUrl, (Vec<Proof>, 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(