Skip to content

Commit

Permalink
feat: add jit payment filter to list_payments
Browse files Browse the repository at this point in the history
  • Loading branch information
hydra-yse authored Feb 12, 2024
1 parent 5ce2d1b commit 04c5b11
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 24 deletions.
1 change: 1 addition & 0 deletions libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ dictionary LnPaymentDetails {
string payment_preimage;
boolean keysend;
string bolt11;
string? open_channel_bolt11;
SuccessActionProcessed? lnurl_success_action;
string? lnurl_pay_domain;
string? lnurl_metadata;
Expand Down
23 changes: 16 additions & 7 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,7 @@ impl BreezServices {
swap_info: None,
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -2065,7 +2066,7 @@ impl Receiver for PaymentReceiver {
}

info!("Creating invoice on NodeAPI");
let invoice = &self
let mut invoice = self
.node_api
.create_invoice(
destination_invoice_amount_msat,
Expand All @@ -2078,7 +2079,7 @@ impl Receiver for PaymentReceiver {
.await?;
info!("Invoice created {}", invoice);

let mut parsed_invoice = parse_invoice(invoice)?;
let mut parsed_invoice = parse_invoice(&invoice)?;
// check if the lsp hint already exists
info!("Existing routing hints {:?}", parsed_invoice.routing_hints);

Expand Down Expand Up @@ -2143,9 +2144,9 @@ impl Receiver for PaymentReceiver {
};

if let Some(raw_invoice) = optional_modified_invoice {
let signed_invoice = self.node_api.sign_invoice(raw_invoice)?;
info!("Signed invoice with hint = {}", signed_invoice);
parsed_invoice = parse_invoice(&signed_invoice)?;
invoice = self.node_api.sign_invoice(raw_invoice)?;
info!("Signed invoice with hint = {}", invoice);
parsed_invoice = parse_invoice(&invoice)?;
}

// register the payment at the lsp if needed
Expand Down Expand Up @@ -2181,8 +2182,11 @@ impl Receiver for PaymentReceiver {
.await?;
info!("Payment registered");
// Make sure we save the large amount so we can deduce the fees later.
self.persister
.insert_open_channel_payment_info(&parsed_invoice.payment_hash, req.amount_msat)?;
self.persister.insert_open_channel_payment_info(
&parsed_invoice.payment_hash,
req.amount_msat,
&invoice,
)?;
}

// return the signed, converted invoice with hints
Expand Down Expand Up @@ -2371,6 +2375,7 @@ pub(crate) mod tests {
swap_info: None,
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -2400,6 +2405,7 @@ pub(crate) mod tests {
swap_info: None,
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -2429,6 +2435,7 @@ pub(crate) mod tests {
swap_info: None,
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -2458,6 +2465,7 @@ pub(crate) mod tests {
swap_info: Some(swap_info.clone()),
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -2487,6 +2495,7 @@ pub(crate) mod tests {
swap_info: None,
reverse_swap_info: Some(rev_swap_info.clone()),
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down
1 change: 1 addition & 0 deletions libs/sdk-core/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,7 @@ impl support::IntoDart for LnPaymentDetails {
self.payment_preimage.into_into_dart().into_dart(),
self.keysend.into_into_dart().into_dart(),
self.bolt11.into_into_dart().into_dart(),
self.open_channel_bolt11.into_dart(),
self.lnurl_success_action.into_dart(),
self.lnurl_pay_domain.into_dart(),
self.ln_address.into_dart(),
Expand Down
5 changes: 5 additions & 0 deletions libs/sdk-core/src/greenlight/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,7 @@ impl TryFrom<OffChainPayment> for Payment {
swap_info: None,
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -1705,6 +1706,7 @@ impl TryFrom<gl_client::signer::model::greenlight::Invoice> for Payment {
swap_info: None,
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -1763,6 +1765,7 @@ impl TryFrom<gl_client::signer::model::greenlight::Payment> for Payment {
swap_info: None,
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -1808,6 +1811,7 @@ impl TryFrom<cln::ListinvoicesInvoices> for Payment {
swap_info: None,
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -1876,6 +1880,7 @@ impl TryFrom<cln::ListpaysPays> for Payment {
swap_info: None,
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down
4 changes: 4 additions & 0 deletions libs/sdk-core/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,10 @@ pub struct LnPaymentDetails {
pub keysend: bool,
pub bolt11: String,

/// Only set for [PaymentType::Received], payments which require to open a channel.
/// Represents the actual invoice paid by the sender
pub open_channel_bolt11: Option<String>,

/// Only set for [PaymentType::Sent] payments that are part of a LNURL-pay workflow where
/// the endpoint returns a success action
pub lnurl_success_action: Option<SuccessActionProcessed>,
Expand Down
1 change: 1 addition & 0 deletions libs/sdk-core/src/persist/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,5 +555,6 @@ pub(crate) fn current_sync_migrations() -> Vec<&'static str> {
) STRICT;
",
"ALTER TABLE payments_external_info ADD COLUMN lnurl_pay_domain TEXT;",
"ALTER TABLE open_channel_payment_info ADD COLUMN open_channel_bolt11 TEXT;",
]
}
5 changes: 3 additions & 2 deletions libs/sdk-core/src/persist/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ impl SqliteStorage {
INSERT INTO sync.open_channel_payment_info
SELECT
payment_hash,
payer_amount_msat
payer_amount_msat,
open_channel_bolt11
FROM remote_sync.open_channel_payment_info
WHERE payment_hash NOT IN (SELECT payment_hash FROM sync.open_channel_payment_info);",
[],
Expand Down Expand Up @@ -294,7 +295,7 @@ mod tests {
remote_storage.init()?;
remote_storage.insert_swap(remote_swap_info)?;

remote_storage.insert_open_channel_payment_info("123", 100000)?;
remote_storage.insert_open_channel_payment_info("123", 100000, "")?;

remote_storage.import_remote_changes(&local_storage, false)?;
local_storage.import_remote_changes(&remote_storage, true)?;
Expand Down
60 changes: 54 additions & 6 deletions libs/sdk-core/src/persist/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,21 @@ impl SqliteStorage {
&self,
payment_hash: &str,
payer_amount_msat: u64,
open_channel_bolt11: &str,
) -> PersistResult<()> {
let con = self.get_connection()?;
let mut prep_statement = con.prepare(
"
INSERT INTO sync.open_channel_payment_info (
payment_hash,
payer_amount_msat
payer_amount_msat,
open_channel_bolt11
)
VALUES (?1,?2)
VALUES (?1,?2,?3)
",
)?;

_ = prep_statement.execute((payment_hash, payer_amount_msat))?;
_ = prep_statement.execute((payment_hash, payer_amount_msat, open_channel_bolt11))?;

Ok(())
}
Expand Down Expand Up @@ -232,6 +234,7 @@ impl SqliteStorage {
e.attempted_amount_msat,
e.attempted_error,
o.payer_amount_msat,
o.open_channel_bolt11,
m.metadata,
e.lnurl_pay_domain
FROM payments p
Expand Down Expand Up @@ -310,6 +313,7 @@ impl SqliteStorage {
e.attempted_amount_msat,
e.attempted_error,
o.payer_amount_msat,
o.open_channel_bolt11,
m.metadata,
e.lnurl_pay_domain
FROM payments p
Expand Down Expand Up @@ -361,12 +365,12 @@ impl SqliteStorage {
description: row.get(6)?,
details: row.get(7)?,
error: row.get(13)?,
metadata: row.get(15)?,
metadata: row.get(16)?,
};

if let PaymentDetails::Ln { ref mut data } = payment.details {
data.lnurl_success_action = row.get(8)?;
data.lnurl_pay_domain = row.get(16)?;
data.lnurl_pay_domain = row.get(17)?;
data.lnurl_metadata = row.get(9)?;
data.ln_address = row.get(10)?;
data.lnurl_withdraw_endpoint = row.get(11)?;
Expand All @@ -387,6 +391,13 @@ impl SqliteStorage {
payment.fee_msat = payer_amount - amount_msat;
}

// Add the payer invoice if it exists, in case of a received payment
if let Some(open_channel_bolt11) = row.get(15)? {
if let PaymentDetails::Ln { data } = &mut payment.details {
data.open_channel_bolt11 = Some(open_channel_bolt11);
}
}

Ok(payment)
}
}
Expand Down Expand Up @@ -605,6 +616,7 @@ fn test_ln_transactions() -> PersistResult<(), Box<dyn std::error::Error>> {
swap_info: None,
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -634,6 +646,7 @@ fn test_ln_transactions() -> PersistResult<(), Box<dyn std::error::Error>> {
swap_info: None,
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -663,6 +676,7 @@ fn test_ln_transactions() -> PersistResult<(), Box<dyn std::error::Error>> {
swap_info: Some(swap_info.clone()),
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -692,6 +706,7 @@ fn test_ln_transactions() -> PersistResult<(), Box<dyn std::error::Error>> {
swap_info: None,
reverse_swap_info: Some(rev_swap_info.clone()),
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -721,6 +736,7 @@ fn test_ln_transactions() -> PersistResult<(), Box<dyn std::error::Error>> {
swap_info: None,
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -751,6 +767,7 @@ fn test_ln_transactions() -> PersistResult<(), Box<dyn std::error::Error>> {
swap_info: None,
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down Expand Up @@ -854,7 +871,7 @@ fn test_ln_transactions() -> PersistResult<(), Box<dyn std::error::Error>> {
assert_eq!(retrieve_txs.len(), 5);
assert_eq!(retrieve_txs, txs);

storage.insert_open_channel_payment_info("123", 150)?;
storage.insert_open_channel_payment_info("123", 150, "")?;
let retrieve_txs = storage.list_payments(ListPaymentsRequest::default())?;
assert_eq!(retrieve_txs[0].fee_msat, 50);

Expand Down Expand Up @@ -936,5 +953,36 @@ fn test_ln_transactions() -> PersistResult<(), Box<dyn std::error::Error>> {
assert_eq!(retrieve_txs[0].id, payment_hash_with_lnurl_withdraw);
assert_eq!(retrieve_txs[0].metadata, Some(test_json.to_string()),);

// test open_channel_bolt11
storage.insert_open_channel_payment_info(
payment_hash_with_lnurl_withdraw,
150,
"original_invoice",
)?;
let retrieve_txs = storage.list_payments(ListPaymentsRequest {
filters: Some(vec![PaymentTypeFilter::Received]),
..Default::default()
})?;

let filtered_txs: Vec<&Payment> = retrieve_txs
.iter()
.filter(|p| {
if let PaymentDetails::Ln { data } = &p.details {
return data.open_channel_bolt11 == Some("original_invoice".to_string());
}
false
})
.collect();

assert_eq!(filtered_txs.len(), 1);
assert_eq!(filtered_txs[0].id, payment_hash_with_lnurl_withdraw);
assert!(matches!(filtered_txs[0].details, PaymentDetails::Ln { .. }));
if let PaymentDetails::Ln { data } = &filtered_txs[0].details {
assert_eq!(
data.open_channel_bolt11,
Some("original_invoice".to_string())
);
}

Ok(())
}
1 change: 1 addition & 0 deletions libs/sdk-core/src/swap_in/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,7 @@ mod tests {
swap_info: None,
reverse_swap_info: None,
pending_expiration_block: None,
open_channel_bolt11: None,
},
},
metadata: None,
Expand Down
Loading

0 comments on commit 04c5b11

Please sign in to comment.