Skip to content

Commit

Permalink
Fix duplicate payment when paying lightning address
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed May 8, 2024
1 parent 256876a commit 120897d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
24 changes: 14 additions & 10 deletions mutiny-core/src/federation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,12 @@ impl<S: MutinyStorage> FederationClient<S> {
fn maybe_update_after_checking_fedimint(
&self,
updated_invoice: MutinyInvoice,
) -> Result<(), MutinyError> {
) -> Result<MutinyInvoice, MutinyError> {
maybe_update_after_checking_fedimint(
updated_invoice,
self.logger.clone(),
self.storage.clone(),
)?;
Ok(())
)
}

pub(crate) async fn pay_invoice(
Expand Down Expand Up @@ -605,7 +604,7 @@ impl<S: MutinyStorage> FederationClient<S> {
};
inv.fees_paid = Some(sats_round_up(&outgoing_payment.fee));

self.maybe_update_after_checking_fedimint(inv.clone())?;
inv = self.maybe_update_after_checking_fedimint(inv)?;

match inv.status {
HTLCStatus::Succeeded => Ok(inv),
Expand Down Expand Up @@ -798,23 +797,28 @@ fn subscribe_operation_ext<S: MutinyStorage>(
}

fn maybe_update_after_checking_fedimint<S: MutinyStorage>(
updated_invoice: MutinyInvoice,
mut updated_invoice: MutinyInvoice,
logger: Arc<MutinyLogger>,
storage: S,
) -> Result<(), MutinyError> {
) -> Result<MutinyInvoice, MutinyError> {
match updated_invoice.status {
HTLCStatus::Succeeded | HTLCStatus::Failed => {
log_debug!(logger, "Saving updated payment");
let hash = updated_invoice.payment_hash.into_32();
let inbound = updated_invoice.inbound;
let mut payment_info = PaymentInfo::from(updated_invoice);
payment_info.last_update = now().as_secs();
updated_invoice.last_updated = now().as_secs();
let payment_info = PaymentInfo::from(updated_invoice.clone());
log_debug!(
logger,
"Saving updated payment: {} {}",
hash.to_lower_hex_string(),
payment_info.last_update
);
persist_payment_info(&storage, &hash, &payment_info, inbound)?;
}
HTLCStatus::Pending | HTLCStatus::InFlight => (),
}

Ok(())
Ok(updated_invoice)
}

impl<S: MutinyStorage> FedimintClient for FederationClient<S> {
Expand Down
21 changes: 12 additions & 9 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2816,15 +2816,18 @@ impl<S: MutinyStorage> MutinyWallet<S> {
}

let mut inv = self.pay_invoice(&invoice, None, labels).await?;
// save privacy level to storage
inv.privacy_level = privacy_level;
persist_payment_info(
&self.storage,
&inv.payment_hash.into_32(),
&inv.clone().into(),
false,
)?;

// save privacy level to storage, can skip if its the default privacy level
if privacy_level != PrivacyLevel::default() {
inv.privacy_level = privacy_level;
let hash = inv.payment_hash.into_32();
log_debug!(
self.logger,
"Saving updated payment: {} {}",
hash.to_lower_hex_string(),
inv.last_updated
);
persist_payment_info(&self.storage, &hash, &inv.clone().into(), false)?;
}
Ok(inv)
} else {
log_error!(self.logger, "LNURL return invoice with incorrect amount");
Expand Down

0 comments on commit 120897d

Please sign in to comment.