thesimplekid пре 2 година
родитељ
комит
2940e9d67a
5 измењених фајлова са 57 додато и 11 уклоњено
  1. 15 3
      integration_test/src/main.rs
  2. 29 1
      src/cashu_wallet.rs
  3. 4 2
      src/client.rs
  4. 0 3
      src/dhke.rs
  5. 9 2
      src/types.rs

+ 15 - 3
integration_test/src/main.rs

@@ -31,12 +31,16 @@ async fn main() {
         Some("Hello World".to_string()),
         Some("Hello World".to_string()),
     );
     );
     let new_token = test_receive(&wallet, &token.to_string()).await;
     let new_token = test_receive(&wallet, &token.to_string()).await;
-    test_check_spendable(&client, &new_token).await;
 
 
     let proofs = TokenData::from_str(&new_token).unwrap().token[0]
     let proofs = TokenData::from_str(&new_token).unwrap().token[0]
         .clone()
         .clone()
         .proofs;
         .proofs;
-    test_send(&wallet, proofs).await;
+    test_send(&wallet, proofs.clone()).await;
+
+    let spendable = test_check_spendable(&client, &new_token).await;
+
+    let invoice = Invoice::from_str("lnbc20n1pjy3tp8pp5mmrp5vhzrmsz4d6sew77aw0wr7dfxptumxvstsl8peu8ypjhdmwsdq5g9kxy7fqd9h8vmmfvdjscqzpgxqyz5vqsp5aajwlqyxwwtk57tnxzgf9rk6mp0u3z33ksylqj6lu7et7dvlkdvs9qyyssq02rdva0hvamlgvfau0mqnknglk02v6d6x56xh5s8dtx9crtdrwf9hf6f87kk2n7tt0fsjg4xsyd50rqayxln5p9ygvetqtyrrtvy5ygpcjjwek").unwrap();
+    test_melt(&wallet, invoice, spendable).await;
 
 
     test_check_fees(&client).await;
     test_check_fees(&client).await;
 }
 }
@@ -103,7 +107,7 @@ async fn test_receive(wallet: &CashuWallet, token: &str) -> String {
     s
     s
 }
 }
 
 
-async fn test_check_spendable(client: &Client, token: &str) {
+async fn test_check_spendable(client: &Client, token: &str) -> Vec<Proof> {
     let mint_keys = client.get_keys().await.unwrap();
     let mint_keys = client.get_keys().await.unwrap();
 
 
     let wallet = CashuWallet::new(client.to_owned(), mint_keys);
     let wallet = CashuWallet::new(client.to_owned(), mint_keys);
@@ -116,6 +120,8 @@ async fn test_check_spendable(client: &Client, token: &str) {
 
 
     assert!(!spendable.spendable.is_empty());
     assert!(!spendable.spendable.is_empty());
     // println!("Spendable: {:?}", spendable);
     // println!("Spendable: {:?}", spendable);
+
+    spendable.spendable
 }
 }
 
 
 async fn test_send(wallet: &CashuWallet, proofs: Vec<Proof>) {
 async fn test_send(wallet: &CashuWallet, proofs: Vec<Proof>) {
@@ -131,6 +137,12 @@ async fn test_send(wallet: &CashuWallet, proofs: Vec<Proof>) {
     println!("Send Token: {send_token}");
     println!("Send Token: {send_token}");
 }
 }
 
 
+async fn test_melt(wallet: &CashuWallet, invoice: Invoice, proofs: Vec<Proof>) {
+    let res = wallet.melt(invoice, proofs).await.unwrap();
+
+    println!("{:?}", res);
+}
+
 async fn _test_get_mint_info(mint: &Client) {
 async fn _test_get_mint_info(mint: &Client) {
     let _mint_info = mint.get_info().await.unwrap();
     let _mint_info = mint.get_info().await.unwrap();
 
 

+ 29 - 1
src/cashu_wallet.rs

@@ -8,7 +8,7 @@ use crate::{
     dhke::construct_proofs,
     dhke::construct_proofs,
     error::Error,
     error::Error,
     types::{
     types::{
-        BlindedMessages, MintKeys, Proof, ProofsStatus, RequestMintResponse, SendProofs,
+        BlindedMessages, Melted, MintKeys, Proof, ProofsStatus, RequestMintResponse, SendProofs,
         SplitPayload, SplitRequest, TokenData,
         SplitPayload, SplitRequest, TokenData,
     },
     },
 };
 };
@@ -213,6 +213,34 @@ impl CashuWallet {
         })
         })
     }
     }
 
 
+    pub async fn melt(
+        &self,
+        invoice: lightning_invoice::Invoice,
+        proofs: Vec<Proof>,
+    ) -> Result<Melted, Error> {
+        let change = BlindedMessages::blank()?;
+        let melt_response = self
+            .client
+            .melt(proofs, invoice, Some(change.blinded_messages))
+            .await?;
+
+        let change = match melt_response.change {
+            Some(promises) => Some(construct_proofs(
+                promises,
+                change.rs,
+                change.secrets,
+                &self.mint_keys,
+            )?),
+            None => None,
+        };
+
+        Ok(Melted {
+            paid: melt_response.paid,
+            preimage: melt_response.preimage,
+            change,
+        })
+    }
+
     pub fn proofs_to_token(&self, proofs: Vec<Proof>, memo: Option<String>) -> String {
     pub fn proofs_to_token(&self, proofs: Vec<Proof>, memo: Option<String>) -> String {
         TokenData::new(self.client.mint_url.clone(), proofs, memo).to_string()
         TokenData::new(self.client.mint_url.clone(), proofs, memo).to_string()
     }
     }

+ 4 - 2
src/client.rs

@@ -108,10 +108,12 @@ impl Client {
             outputs,
             outputs,
         };
         };
 
 
-        Ok(minreq::post(url)
+        let value = minreq::post(url)
             .with_json(&request)?
             .with_json(&request)?
             .send()?
             .send()?
-            .json::<MeltResponse>()?)
+            .json::<Value>()?;
+
+        Ok(serde_json::from_value(value)?)
     }
     }
 
 
     /// Split Token [NUT-06]
     /// Split Token [NUT-06]

+ 0 - 3
src/dhke.rs

@@ -73,7 +73,6 @@ pub fn construct_proofs(
     for (i, promise) in promises.into_iter().enumerate() {
     for (i, promise) in promises.into_iter().enumerate() {
         let blinded_c = promise.c;
         let blinded_c = promise.c;
         let a: PublicKey = keys.0.get(&promise.amount.to_sat()).unwrap().to_owned();
         let a: PublicKey = keys.0.get(&promise.amount.to_sat()).unwrap().to_owned();
-        // println!("Construct proof Pub {:?}", serde_json::to_string(&a));
         let unblinded_signature = unblind_message(blinded_c, rs[i].clone(), a)?;
         let unblinded_signature = unblind_message(blinded_c, rs[i].clone(), a)?;
 
 
         let proof = Proof {
         let proof = Proof {
@@ -87,8 +86,6 @@ pub fn construct_proofs(
         proofs.push(proof);
         proofs.push(proof);
     }
     }
 
 
-    println!("proofs: {:?}", proofs);
-
     Ok(proofs)
     Ok(proofs)
 }
 }
 
 

+ 9 - 2
src/types.rs

@@ -184,8 +184,15 @@ pub struct MeltRequest {
 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 pub struct MeltResponse {
 pub struct MeltResponse {
     pub paid: bool,
     pub paid: bool,
-    pub preimage: String,
-    pub change: Option<Promise>,
+    pub preimage: Option<String>,
+    pub change: Option<Vec<Promise>>,
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct Melted {
+    pub paid: bool,
+    pub preimage: Option<String>,
+    pub change: Option<Vec<Proof>>,
 }
 }
 
 
 /// Split Request [NUT-06]
 /// Split Request [NUT-06]