Pārlūkot izejas kodu

Update examples

Cesar Rodas 1 mēnesi atpakaļ
vecāks
revīzija
f6aa4b6515

+ 1 - 7
crates/cdk-integration-tests/tests/fake_auth.rs

@@ -331,13 +331,7 @@ async fn test_mint_with_auth() {
     let mint_amount: Amount = 100.into();
 
     let (_, proofs) = wallet
-        .mint_once_paid(
-            mint_amount,
-            None,
-            SplitTarget::default(),
-            None,
-            Duration::from_secs(10),
-        )
+        .mint_once_paid(mint_amount, None, Duration::from_secs(10))
         .await
         .unwrap();
 

+ 7 - 22
crates/cdk/examples/auth_wallet.rs

@@ -1,9 +1,9 @@
 use std::sync::Arc;
+use std::time::Duration;
 
-use cdk::amount::SplitTarget;
 use cdk::error::Error;
-use cdk::nuts::{CurrencyUnit, MintQuoteState, NotificationPayload};
-use cdk::wallet::{SendOptions, Wallet, WalletSubscription};
+use cdk::nuts::CurrencyUnit;
+use cdk::wallet::{SendOptions, Wallet};
 use cdk::{Amount, OidcClient};
 use cdk_common::{MintInfo, ProofsMethods};
 use cdk_sqlite::wallet::memory;
@@ -57,27 +57,12 @@ async fn main() -> Result<(), Error> {
         .await
         .expect("Could not mint blind auth");
 
-    // Request a mint quote from the wallet
-    let quote = wallet.mint_quote(amount, None).await?;
-
-    // Subscribe to updates on the mint quote state
-    let mut subscription = wallet
-        .subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
-            .id
-            .clone()]))
-        .await;
-
-    // Wait for the mint quote to be paid
-    while let Some(msg) = subscription.recv().await {
-        if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
-            if response.state == MintQuoteState::Paid {
-                break;
-            }
-        }
-    }
+    let (_invoice_to_pay, proofs) = wallet
+        .mint_once_paid(amount, None, Duration::from_secs(10))
+        .await?;
 
     // Mint the received amount
-    let receive_amount = wallet.mint(&quote.id, SplitTarget::default(), None).await?;
+    let receive_amount = proofs.await?;
 
     println!("Received: {}", receive_amount.total_amount()?);
 

+ 7 - 23
crates/cdk/examples/melt-token.rs

@@ -1,13 +1,13 @@
 use std::sync::Arc;
+use std::time::Duration;
 
 use bitcoin::hashes::{sha256, Hash};
 use bitcoin::hex::prelude::FromHex;
 use bitcoin::secp256k1::Secp256k1;
-use cdk::amount::SplitTarget;
 use cdk::error::Error;
 use cdk::nuts::nut00::ProofsMethods;
-use cdk::nuts::{CurrencyUnit, MintQuoteState, NotificationPayload, SecretKey};
-use cdk::wallet::{Wallet, WalletSubscription};
+use cdk::nuts::{CurrencyUnit, SecretKey};
+use cdk::wallet::Wallet;
 use cdk::Amount;
 use cdk_sqlite::wallet::memory;
 use lightning_invoice::{Currency, InvoiceBuilder, PaymentSecret};
@@ -29,28 +29,12 @@ async fn main() -> Result<(), Error> {
     // Create a new wallet
     let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), seed, None)?;
 
-    // Request a mint quote from the wallet
-    let quote = wallet.mint_quote(amount, None).await?;
-    println!("Quote: {:#?}", quote);
-
-    // Subscribe to updates on the mint quote state
-    let mut subscription = wallet
-        .subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
-            .id
-            .clone()]))
-        .await;
-
-    // Wait for the mint quote to be paid
-    while let Some(msg) = subscription.recv().await {
-        if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
-            if response.state == MintQuoteState::Paid {
-                break;
-            }
-        }
-    }
+    let (_invoice_to_pay, proofs) = wallet
+        .mint_once_paid(amount, None, Duration::from_secs(10))
+        .await?;
 
     // Mint the received amount
-    let proofs = wallet.mint(&quote.id, SplitTarget::default(), None).await?;
+    let proofs = proofs.await?;
     let receive_amount = proofs.total_amount()?;
     println!("Received {} from mint {}", receive_amount, mint_url);
 

+ 7 - 23
crates/cdk/examples/mint-token.rs

@@ -1,10 +1,10 @@
 use std::sync::Arc;
+use std::time::Duration;
 
-use cdk::amount::SplitTarget;
 use cdk::error::Error;
 use cdk::nuts::nut00::ProofsMethods;
-use cdk::nuts::{CurrencyUnit, MintQuoteState, NotificationPayload};
-use cdk::wallet::{SendOptions, Wallet, WalletSubscription};
+use cdk::nuts::CurrencyUnit;
+use cdk::wallet::{SendOptions, Wallet};
 use cdk::Amount;
 use cdk_sqlite::wallet::memory;
 use rand::random;
@@ -35,28 +35,12 @@ async fn main() -> Result<(), Error> {
     // Create a new wallet
     let wallet = Wallet::new(mint_url, unit, localstore, seed, None)?;
 
-    // Request a mint quote from the wallet
-    let quote = wallet.mint_quote(amount, None).await?;
-    println!("Quote: {:#?}", quote);
-
-    // Subscribe to updates on the mint quote state
-    let mut subscription = wallet
-        .subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
-            .id
-            .clone()]))
-        .await;
-
-    // Wait for the mint quote to be paid
-    while let Some(msg) = subscription.recv().await {
-        if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
-            if response.state == MintQuoteState::Paid {
-                break;
-            }
-        }
-    }
+    let (_invoice_to_pay, proofs) = wallet
+        .mint_once_paid(amount, None, Duration::from_secs(10))
+        .await?;
 
     // Mint the received amount
-    let proofs = wallet.mint(&quote.id, SplitTarget::default(), None).await?;
+    let proofs = proofs.await?;
     let receive_amount = proofs.total_amount()?;
     println!("Received {} from mint {}", receive_amount, mint_url);
 

+ 7 - 24
crates/cdk/examples/p2pk.rs

@@ -1,9 +1,9 @@
 use std::sync::Arc;
+use std::time::Duration;
 
-use cdk::amount::SplitTarget;
 use cdk::error::Error;
-use cdk::nuts::{CurrencyUnit, MintQuoteState, NotificationPayload, SecretKey, SpendingConditions};
-use cdk::wallet::{ReceiveOptions, SendOptions, Wallet, WalletSubscription};
+use cdk::nuts::{CurrencyUnit, SecretKey, SpendingConditions};
+use cdk::wallet::{ReceiveOptions, SendOptions, Wallet};
 use cdk::Amount;
 use cdk_sqlite::wallet::memory;
 use rand::random;
@@ -34,29 +34,12 @@ async fn main() -> Result<(), Error> {
     // Create a new wallet
     let wallet = Wallet::new(mint_url, unit, localstore, seed, None).unwrap();
 
-    // Request a mint quote from the wallet
-    let quote = wallet.mint_quote(amount, None).await?;
-
-    println!("Minting nuts ...");
-
-    // Subscribe to updates on the mint quote state
-    let mut subscription = wallet
-        .subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
-            .id
-            .clone()]))
-        .await;
-
-    // Wait for the mint quote to be paid
-    while let Some(msg) = subscription.recv().await {
-        if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
-            if response.state == MintQuoteState::Paid {
-                break;
-            }
-        }
-    }
+    let (_invoice_to_pay, proofs) = wallet
+        .mint_once_paid(amount, None, Duration::from_secs(10))
+        .await?;
 
     // Mint the received amount
-    let received_proofs = wallet.mint(&quote.id, SplitTarget::default(), None).await?;
+    let received_proofs = proofs.await?;
     println!(
         "Minted nuts: {:?}",
         received_proofs

+ 7 - 23
crates/cdk/examples/proof-selection.rs

@@ -2,11 +2,11 @@
 
 use std::collections::HashMap;
 use std::sync::Arc;
+use std::time::Duration;
 
-use cdk::amount::SplitTarget;
 use cdk::nuts::nut00::ProofsMethods;
-use cdk::nuts::{CurrencyUnit, MintQuoteState, NotificationPayload};
-use cdk::wallet::{Wallet, WalletSubscription};
+use cdk::nuts::CurrencyUnit;
+use cdk::wallet::Wallet;
 use cdk::Amount;
 use cdk_common::nut02::KeySetInfosMethods;
 use cdk_sqlite::wallet::memory;
@@ -31,28 +31,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
     for amount in [64] {
         let amount = Amount::from(amount);
 
-        // Request a mint quote from the wallet
-        let quote = wallet.mint_quote(amount, None).await?;
-        println!("Pay request: {}", quote.request);
-
-        // Subscribe to the wallet for updates on the mint quote state
-        let mut subscription = wallet
-            .subscribe(WalletSubscription::Bolt11MintQuoteState(vec![quote
-                .id
-                .clone()]))
-            .await;
-
-        // Wait for the mint quote to be paid
-        while let Some(msg) = subscription.recv().await {
-            if let NotificationPayload::MintQuoteBolt11Response(response) = msg {
-                if response.state == MintQuoteState::Paid {
-                    break;
-                }
-            }
-        }
+        let (_invoice_to_pay, proofs) = wallet
+            .mint_once_paid(amount, None, Duration::from_secs(10))
+            .await?;
 
         // Mint the received amount
-        let proofs = wallet.mint(&quote.id, SplitTarget::default(), None).await?;
+        let proofs = proofs.await?;
         let receive_amount = proofs.total_amount()?;
         println!("Minted {}", receive_amount);
     }

+ 5 - 29
crates/cdk/examples/wallet.rs

@@ -1,14 +1,12 @@
 use std::sync::Arc;
 use std::time::Duration;
 
-use cdk::amount::SplitTarget;
 use cdk::nuts::nut00::ProofsMethods;
-use cdk::nuts::{CurrencyUnit, MintQuoteState};
+use cdk::nuts::CurrencyUnit;
 use cdk::wallet::{SendOptions, Wallet};
 use cdk::Amount;
 use cdk_sqlite::wallet::memory;
 use rand::random;
-use tokio::time::sleep;
 
 #[tokio::main]
 async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -26,34 +24,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
     // Create a new wallet
     let wallet = Wallet::new(mint_url, unit, localstore, seed, None)?;
 
-    // Request a mint quote from the wallet
-    let quote = wallet.mint_quote(amount, None).await?;
-
-    println!("Pay request: {}", quote.request);
-
-    // Check the quote state in a loop with a timeout
-    let timeout = Duration::from_secs(60); // Set a timeout duration
-    let start = std::time::Instant::now();
-
-    loop {
-        let status = wallet.mint_quote_state(&quote.id).await?;
-
-        if status.state == MintQuoteState::Paid {
-            break;
-        }
-
-        if start.elapsed() >= timeout {
-            eprintln!("Timeout while waiting for mint quote to be paid");
-            return Err("Timeout while waiting for mint quote to be paid".into());
-        }
-
-        println!("Quote state: {}", status.state);
-
-        sleep(Duration::from_secs(5)).await;
-    }
+    let (_invoice_to_pay, proofs) = wallet
+        .mint_once_paid(amount, None, Duration::from_secs(10))
+        .await?;
 
     // Mint the received amount
-    let proofs = wallet.mint(&quote.id, SplitTarget::default(), None).await?;
+    let proofs = proofs.await?;
     let receive_amount = proofs.total_amount()?;
     println!("Minted {}", receive_amount);
 

+ 18 - 0
crates/cdk/src/wallet/wait.rs

@@ -42,6 +42,7 @@ impl From<WaitableEvent> for WalletSubscription {
 }
 
 impl Wallet {
+    #[inline(always)]
     async fn wait_and_mint_quote(
         &self,
         quote: MintQuote,
@@ -60,6 +61,23 @@ impl Wallet {
         &self,
         amount: Amount,
         description: Option<String>,
+        timeout_duration: Duration,
+    ) -> Result<(String, impl Future<Output = Result<Proofs, Error>> + '_), Error> {
+        self.mint_once_paid_ex(
+            amount,
+            description,
+            Default::default(),
+            None,
+            timeout_duration,
+        )
+        .await
+    }
+
+    /// Similar function to mint_once_paid but with no default options
+    pub async fn mint_once_paid_ex(
+        &self,
+        amount: Amount,
+        description: Option<String>,
         amount_split_target: SplitTarget,
         spending_conditions: Option<SpendingConditions>,
         timeout_duration: Duration,