From d78bdde81fad8eaa034b6c20449c925d3800c627 Mon Sep 17 00:00:00 2001 From: elnosh Date: Thu, 20 Jun 2024 09:38:57 -0500 Subject: [PATCH] fix: make blinded messages in melt optional (#298) --- moksha-core/src/primitives.rs | 2 +- moksha-mint/src/mint.rs | 54 ++++++++++++++--------- moksha-mint/src/routes/default.rs | 2 +- moksha-wallet/src/client/crossplatform.rs | 2 +- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/moksha-core/src/primitives.rs b/moksha-core/src/primitives.rs index ac953018..f9c6c1d8 100644 --- a/moksha-core/src/primitives.rs +++ b/moksha-core/src/primitives.rs @@ -201,7 +201,7 @@ impl From for PostMeltQuoteBolt11Response { pub struct PostMeltBolt11Request { pub quote: String, pub inputs: Proofs, - pub outputs: Vec, + pub outputs: Option>, } #[derive(Deserialize, Serialize, Debug, Clone, ToSchema)] diff --git a/moksha-mint/src/mint.rs b/moksha-mint/src/mint.rs index 4540da4d..1c8673d9 100644 --- a/moksha-mint/src/mint.rs +++ b/moksha-mint/src/mint.rs @@ -182,7 +182,7 @@ where payment_request: String, fee_reserve: u64, proofs: &Proofs, - blinded_messages: &[BlindedMessage], + blinded_messages: Option>, keyset: &MintKeyset, ) -> Result<(bool, String, Vec), MokshaMintError> { let invoice = self @@ -212,26 +212,33 @@ where let result = self.lightning.pay_invoice(payment_request).await?; self.db.add_used_proofs(tx, proofs).await?; - let change = if fee_reserve > 0 { - let return_fees = Amount(fee_reserve - result.total_fees).split(); - - if (return_fees.len()) > blinded_messages.len() { - // FIXME better handle case when there are more fees than blinded messages + let change = match blinded_messages { + Some(blinded_messages) => { + if fee_reserve > 0 { + let return_fees = Amount(fee_reserve - result.total_fees).split(); + + if (return_fees.len()) > blinded_messages.len() { + // FIXME better handle case when there are more fees than blinded messages + vec![] + } else { + let out: Vec<_> = blinded_messages[0..return_fees.len()] + .iter() + .zip(return_fees.into_iter()) + .map(|(message, fee)| BlindedMessage { + amount: fee, + ..message.clone() + }) + .collect(); + + self.create_blinded_signatures(&out, keyset)? + } + } else { + vec![] + } + } + None => { vec![] - } else { - let out: Vec<_> = blinded_messages[0..return_fees.len()] - .iter() - .zip(return_fees.into_iter()) - .map(|(message, fee)| BlindedMessage { - amount: fee, - ..message.clone() - }) - .collect(); - - self.create_blinded_signatures(&out, keyset)? } - } else { - vec![] }; Ok((true, result.payment_hash, change)) } @@ -673,7 +680,14 @@ mod tests { let mut tx = mint.db.begin_tx().await?; let (paid, _payment_hash, change) = mint - .melt_bolt11(&mut tx, invoice, 4, &tokens.proofs(), &change, &mint.keyset) + .melt_bolt11( + &mut tx, + invoice, + 4, + &tokens.proofs(), + Some(change), + &mint.keyset, + ) .await?; assert!(paid); diff --git a/moksha-mint/src/routes/default.rs b/moksha-mint/src/routes/default.rs index d063a0c2..a8cc3036 100644 --- a/moksha-mint/src/routes/default.rs +++ b/moksha-mint/src/routes/default.rs @@ -257,7 +257,7 @@ pub async fn post_melt_bolt11( quote.payment_request.to_owned(), quote.fee_reserve, &melt_request.inputs, - &melt_request.outputs, + melt_request.outputs, &mint.keyset, ) .await?; diff --git a/moksha-wallet/src/client/crossplatform.rs b/moksha-wallet/src/client/crossplatform.rs index fda03676..934a0e48 100644 --- a/moksha-wallet/src/client/crossplatform.rs +++ b/moksha-wallet/src/client/crossplatform.rs @@ -61,7 +61,7 @@ impl CashuClient for CrossPlatformHttpClient { let body = PostMeltBolt11Request { quote, inputs, - outputs, + outputs: Some(outputs), }; self.do_post(&mint_url.join("v1/melt/bolt11")?, &body).await