diff --git a/integration_test/src/main.rs b/integration_test/src/main.rs index 5ccc82a7..297a9d64 100644 --- a/integration_test/src/main.rs +++ b/integration_test/src/main.rs @@ -31,12 +31,16 @@ async fn main() { Some("Hello World".to_string()), ); 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] .clone() .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; } @@ -103,7 +107,7 @@ async fn test_receive(wallet: &CashuWallet, token: &str) -> String { s } -async fn test_check_spendable(client: &Client, token: &str) { +async fn test_check_spendable(client: &Client, token: &str) -> Vec { let mint_keys = client.get_keys().await.unwrap(); 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()); // println!("Spendable: {:?}", spendable); + + spendable.spendable } async fn test_send(wallet: &CashuWallet, proofs: Vec) { @@ -131,6 +137,12 @@ async fn test_send(wallet: &CashuWallet, proofs: Vec) { println!("Send Token: {send_token}"); } +async fn test_melt(wallet: &CashuWallet, invoice: Invoice, proofs: Vec) { + let res = wallet.melt(invoice, proofs).await.unwrap(); + + println!("{:?}", res); +} + async fn _test_get_mint_info(mint: &Client) { let _mint_info = mint.get_info().await.unwrap(); diff --git a/src/cashu_wallet.rs b/src/cashu_wallet.rs index ac9008fd..14f2a9eb 100644 --- a/src/cashu_wallet.rs +++ b/src/cashu_wallet.rs @@ -8,7 +8,7 @@ use crate::{ dhke::construct_proofs, error::Error, types::{ - BlindedMessages, MintKeys, Proof, ProofsStatus, RequestMintResponse, SendProofs, + BlindedMessages, Melted, MintKeys, Proof, ProofsStatus, RequestMintResponse, SendProofs, SplitPayload, SplitRequest, TokenData, }, }; @@ -213,6 +213,34 @@ impl CashuWallet { }) } + pub async fn melt( + &self, + invoice: lightning_invoice::Invoice, + proofs: Vec, + ) -> Result { + 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, memo: Option) -> String { TokenData::new(self.client.mint_url.clone(), proofs, memo).to_string() } diff --git a/src/client.rs b/src/client.rs index 40a17ba3..df4a1b71 100644 --- a/src/client.rs +++ b/src/client.rs @@ -108,10 +108,12 @@ impl Client { outputs, }; - Ok(minreq::post(url) + let value = minreq::post(url) .with_json(&request)? .send()? - .json::()?) + .json::()?; + + Ok(serde_json::from_value(value)?) } /// Split Token [NUT-06] diff --git a/src/dhke.rs b/src/dhke.rs index 73dace4d..61fb457e 100644 --- a/src/dhke.rs +++ b/src/dhke.rs @@ -73,7 +73,6 @@ pub fn construct_proofs( for (i, promise) in promises.into_iter().enumerate() { let blinded_c = promise.c; 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 proof = Proof { @@ -87,8 +86,6 @@ pub fn construct_proofs( proofs.push(proof); } - println!("proofs: {:?}", proofs); - Ok(proofs) } diff --git a/src/types.rs b/src/types.rs index e6388f4c..665b4df7 100644 --- a/src/types.rs +++ b/src/types.rs @@ -184,8 +184,15 @@ pub struct MeltRequest { #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct MeltResponse { pub paid: bool, - pub preimage: String, - pub change: Option, + pub preimage: Option, + pub change: Option>, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Melted { + pub paid: bool, + pub preimage: Option, + pub change: Option>, } /// Split Request [NUT-06]