add melt to wallet

This commit is contained in:
thesimplekid
2023-04-26 00:47:31 -04:00
parent 2b9f84f90e
commit 2940e9d67a
5 changed files with 57 additions and 11 deletions

View File

@@ -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<Proof> {
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<Proof>) {
@@ -131,6 +137,12 @@ async fn test_send(wallet: &CashuWallet, proofs: Vec<Proof>) {
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) {
let _mint_info = mint.get_info().await.unwrap();

View File

@@ -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<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 {
TokenData::new(self.client.mint_url.clone(), proofs, memo).to_string()
}

View File

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

View File

@@ -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)
}

View File

@@ -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<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]