Skip to content

Commit

Permalink
fix: make blinded messages in melt optional (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh authored Jun 20, 2024
1 parent 58965d9 commit d78bdde
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl From<Bolt11MeltQuote> for PostMeltQuoteBolt11Response {
pub struct PostMeltBolt11Request {
pub quote: String,
pub inputs: Proofs,
pub outputs: Vec<BlindedMessage>,
pub outputs: Option<Vec<BlindedMessage>>,
}

#[derive(Deserialize, Serialize, Debug, Clone, ToSchema)]
Expand Down
54 changes: 34 additions & 20 deletions moksha-mint/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ where
payment_request: String,
fee_reserve: u64,
proofs: &Proofs,
blinded_messages: &[BlindedMessage],
blinded_messages: Option<Vec<BlindedMessage>>,
keyset: &MintKeyset,
) -> Result<(bool, String, Vec<BlindedSignature>), MokshaMintError> {
let invoice = self
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion moksha-mint/src/routes/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
2 changes: 1 addition & 1 deletion moksha-wallet/src/client/crossplatform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d78bdde

Please sign in to comment.