瀏覽代碼

feat(example): p2pk token

thesimplekid 10 月之前
父節點
當前提交
267e5f044d
共有 3 個文件被更改,包括 88 次插入1 次删除
  1. 4 0
      crates/cdk/Cargo.toml
  2. 2 1
      crates/cdk/examples/mint-token.rs
  3. 82 0
      crates/cdk/examples/p2pk.rs

+ 4 - 0
crates/cdk/Cargo.toml

@@ -65,5 +65,9 @@ instant = { version = "0.1", features = ["wasm-bindgen", "inaccurate"] }
 name = "mint-token"
 required-features = ["wallet"]
 
+[[example]]
+name = "p2pk"
+required-features = ["wallet"]
+
 [dev-dependencies]
 rand = "0.8.5"

+ 2 - 1
crates/cdk/examples/mint-token.rs

@@ -3,6 +3,7 @@ use std::sync::Arc;
 use std::time::Duration;
 
 use cdk::amount::SplitTarget;
+use cdk::cdk_database::WalletMemoryDatabase;
 use cdk::error::Error;
 use cdk::nuts::CurrencyUnit;
 use cdk::wallet::Wallet;
@@ -12,7 +13,7 @@ use tokio::time::sleep;
 
 #[tokio::main]
 async fn main() -> Result<(), Error> {
-    let localstore = cdk::cdk_database::wallet_memory::WalletMemoryDatabase::default();
+    let localstore = WalletMemoryDatabase::default();
     let seed = rand::thread_rng().gen::<[u8; 32]>();
 
     let mint_url = UncheckedUrl::from_str("https://testnut.cashu.space").unwrap();

+ 82 - 0
crates/cdk/examples/p2pk.rs

@@ -0,0 +1,82 @@
+use std::str::FromStr;
+use std::sync::Arc;
+use std::time::Duration;
+
+use cdk::amount::SplitTarget;
+use cdk::cdk_database::WalletMemoryDatabase;
+use cdk::error::Error;
+use cdk::nuts::{Conditions, CurrencyUnit, SecretKey, SpendingConditions};
+use cdk::wallet::Wallet;
+use cdk::{Amount, UncheckedUrl};
+use rand::Rng;
+use tokio::time::sleep;
+
+#[tokio::main]
+async fn main() -> Result<(), Error> {
+    let localstore = WalletMemoryDatabase::default();
+    let seed = rand::thread_rng().gen::<[u8; 32]>();
+
+    let mint_url = UncheckedUrl::from_str("https://testnut.cashu.space").unwrap();
+    let unit = CurrencyUnit::Sat;
+    let amount = Amount::from(10);
+
+    let mut wallet = Wallet::new(Arc::new(localstore), &seed, vec![]);
+
+    let quote = wallet
+        .mint_quote(mint_url.clone(), amount, unit.clone())
+        .await
+        .unwrap();
+
+    println!("Minting nuts ...");
+
+    loop {
+        let status = wallet
+            .mint_quote_status(mint_url.clone(), &quote.id)
+            .await
+            .unwrap();
+
+        println!("Quote status: {}", status.paid);
+
+        if status.paid {
+            break;
+        }
+
+        sleep(Duration::from_secs(5)).await;
+    }
+
+    let _receive_amount = wallet
+        .mint(mint_url.clone(), &quote.id, SplitTarget::default(), None)
+        .await
+        .unwrap();
+
+    let secret = SecretKey::generate();
+
+    let spending_conditions =
+        SpendingConditions::new_p2pk(secret.public_key(), Conditions::default());
+
+    let token = wallet
+        .send(
+            &mint_url,
+            unit,
+            None,
+            amount,
+            &SplitTarget::None,
+            Some(spending_conditions),
+        )
+        .await
+        .unwrap();
+
+    println!("Created token locked to pubkey: {}", secret.public_key());
+    println!("{}", token);
+
+    wallet.add_p2pk_signing_key(secret).await;
+
+    let amount = wallet
+        .receive(&token, &SplitTarget::default(), None)
+        .await
+        .unwrap();
+
+    println!("Redeamed locked token worth: {}", u64::from(amount));
+
+    Ok(())
+}