@breeztech/breez-sdk-spark 0.7.13 → 0.7.15-dev1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,6 +27,46 @@ try {
27
27
  const { StorageError } = require("./errors.cjs");
28
28
  const { MigrationManager } = require("./migrations.cjs");
29
29
 
30
+ /**
31
+ * Base query for payment lookups.
32
+ * All columns are accessed by name in _rowToPayment.
33
+ * parent_payment_id is only used by getPaymentsByParentIds.
34
+ */
35
+ const SELECT_PAYMENT_SQL = `
36
+ SELECT p.id,
37
+ p.payment_type,
38
+ p.status,
39
+ p.amount,
40
+ p.fees,
41
+ p.timestamp,
42
+ p.method,
43
+ p.withdraw_tx_id,
44
+ p.deposit_tx_id,
45
+ p.spark,
46
+ l.invoice AS lightning_invoice,
47
+ l.payment_hash AS lightning_payment_hash,
48
+ l.destination_pubkey AS lightning_destination_pubkey,
49
+ COALESCE(l.description, pm.lnurl_description) AS lightning_description,
50
+ l.preimage AS lightning_preimage,
51
+ pm.lnurl_pay_info,
52
+ pm.lnurl_withdraw_info,
53
+ pm.conversion_info,
54
+ t.metadata AS token_metadata,
55
+ t.tx_hash AS token_tx_hash,
56
+ t.invoice_details AS token_invoice_details,
57
+ s.invoice_details AS spark_invoice_details,
58
+ s.htlc_details AS spark_htlc_details,
59
+ lrm.nostr_zap_request AS lnurl_nostr_zap_request,
60
+ lrm.nostr_zap_receipt AS lnurl_nostr_zap_receipt,
61
+ lrm.sender_comment AS lnurl_sender_comment,
62
+ pm.parent_payment_id
63
+ FROM payments p
64
+ LEFT JOIN payment_details_lightning l ON p.id = l.payment_id
65
+ LEFT JOIN payment_details_token t ON p.id = t.payment_id
66
+ LEFT JOIN payment_details_spark s ON p.id = s.payment_id
67
+ LEFT JOIN payment_metadata pm ON p.id = pm.payment_id
68
+ LEFT JOIN lnurl_receive_metadata lrm ON l.payment_hash = lrm.payment_hash`;
69
+
30
70
  class SqliteStorage {
31
71
  constructor(dbPath, logger = null) {
32
72
  this.dbPath = dbPath;
@@ -221,50 +261,16 @@ class SqliteStorage {
221
261
  }
222
262
  }
223
263
 
264
+ // Exclude child payments (those with a parent_payment_id)
265
+ whereClauses.push("pm.parent_payment_id IS NULL");
266
+
224
267
  // Build the WHERE clause
225
268
  const whereSql =
226
269
  whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
227
270
 
228
271
  // Determine sort order
229
272
  const orderDirection = request.sortAscending ? "ASC" : "DESC";
230
-
231
- const query = `
232
- SELECT p.id
233
- , p.payment_type
234
- , p.status
235
- , p.amount
236
- , p.fees
237
- , p.timestamp
238
- , p.method
239
- , p.withdraw_tx_id
240
- , p.deposit_tx_id
241
- , p.spark
242
- , l.invoice AS lightning_invoice
243
- , l.payment_hash AS lightning_payment_hash
244
- , l.destination_pubkey AS lightning_destination_pubkey
245
- , COALESCE(l.description, pm.lnurl_description) AS lightning_description
246
- , l.preimage AS lightning_preimage
247
- , pm.lnurl_pay_info
248
- , pm.lnurl_withdraw_info
249
- , pm.conversion_info
250
- , t.metadata AS token_metadata
251
- , t.tx_hash AS token_tx_hash
252
- , t.invoice_details AS token_invoice_details
253
- , s.invoice_details AS spark_invoice_details
254
- , s.htlc_details AS spark_htlc_details
255
- , lrm.nostr_zap_request AS lnurl_nostr_zap_request
256
- , lrm.nostr_zap_receipt AS lnurl_nostr_zap_receipt
257
- , lrm.sender_comment AS lnurl_sender_comment
258
- FROM payments p
259
- LEFT JOIN payment_details_lightning l ON p.id = l.payment_id
260
- LEFT JOIN payment_details_token t ON p.id = t.payment_id
261
- LEFT JOIN payment_details_spark s ON p.id = s.payment_id
262
- LEFT JOIN payment_metadata pm ON p.id = pm.payment_id
263
- LEFT JOIN lnurl_receive_metadata lrm ON l.payment_hash = lrm.payment_hash
264
- ${whereSql}
265
- ORDER BY p.timestamp ${orderDirection}
266
- LIMIT ? OFFSET ?
267
- `;
273
+ const query = `${SELECT_PAYMENT_SQL} ${whereSql} ORDER BY p.timestamp ${orderDirection} LIMIT ? OFFSET ?`;
268
274
 
269
275
  params.push(actualLimit, actualOffset);
270
276
  const stmt = this.db.prepare(query);
@@ -408,43 +414,9 @@ class SqliteStorage {
408
414
  );
409
415
  }
410
416
 
411
- const stmt = this.db.prepare(`
412
- SELECT p.id
413
- , p.payment_type
414
- , p.status
415
- , p.amount
416
- , p.fees
417
- , p.timestamp
418
- , p.method
419
- , p.withdraw_tx_id
420
- , p.deposit_tx_id
421
- , p.spark
422
- , l.invoice AS lightning_invoice
423
- , l.payment_hash AS lightning_payment_hash
424
- , l.destination_pubkey AS lightning_destination_pubkey
425
- , COALESCE(l.description, pm.lnurl_description) AS lightning_description
426
- , l.preimage AS lightning_preimage
427
- , pm.lnurl_pay_info
428
- , pm.lnurl_withdraw_info
429
- , pm.conversion_info
430
- , t.metadata AS token_metadata
431
- , t.tx_hash AS token_tx_hash
432
- , t.invoice_details AS token_invoice_details
433
- , s.invoice_details AS spark_invoice_details
434
- , s.htlc_details AS spark_htlc_details
435
- , lrm.nostr_zap_request AS lnurl_nostr_zap_request
436
- , lrm.nostr_zap_receipt AS lnurl_nostr_zap_receipt
437
- , lrm.sender_comment AS lnurl_sender_comment
438
- FROM payments p
439
- LEFT JOIN payment_details_lightning l ON p.id = l.payment_id
440
- LEFT JOIN payment_details_token t ON p.id = t.payment_id
441
- LEFT JOIN payment_details_spark s ON p.id = s.payment_id
442
- LEFT JOIN payment_metadata pm ON p.id = pm.payment_id
443
- LEFT JOIN lnurl_receive_metadata lrm ON l.payment_hash = lrm.payment_hash
444
- WHERE p.id = ?
445
- `);
446
-
417
+ const stmt = this.db.prepare(`${SELECT_PAYMENT_SQL} WHERE p.id = ?`);
447
418
  const row = stmt.get(id);
419
+
448
420
  if (!row) {
449
421
  return Promise.reject(
450
422
  new StorageError(`Payment with id '${id}' not found`)
@@ -472,43 +444,9 @@ class SqliteStorage {
472
444
  );
473
445
  }
474
446
 
475
- const stmt = this.db.prepare(`
476
- SELECT p.id
477
- , p.payment_type
478
- , p.status
479
- , p.amount
480
- , p.fees
481
- , p.timestamp
482
- , p.method
483
- , p.withdraw_tx_id
484
- , p.deposit_tx_id
485
- , p.spark
486
- , l.invoice AS lightning_invoice
487
- , l.payment_hash AS lightning_payment_hash
488
- , l.destination_pubkey AS lightning_destination_pubkey
489
- , COALESCE(l.description, pm.lnurl_description) AS lightning_description
490
- , l.preimage AS lightning_preimage
491
- , pm.lnurl_pay_info
492
- , pm.lnurl_withdraw_info
493
- , pm.conversion_info
494
- , t.metadata AS token_metadata
495
- , t.tx_hash AS token_tx_hash
496
- , t.invoice_details AS token_invoice_details
497
- , s.invoice_details AS spark_invoice_details
498
- , s.htlc_details AS spark_htlc_details
499
- , lrm.nostr_zap_request AS lnurl_nostr_zap_request
500
- , lrm.nostr_zap_receipt AS lnurl_nostr_zap_receipt
501
- , lrm.sender_comment AS lnurl_sender_comment
502
- FROM payments p
503
- LEFT JOIN payment_details_lightning l ON p.id = l.payment_id
504
- LEFT JOIN payment_details_token t ON p.id = t.payment_id
505
- LEFT JOIN payment_details_spark s ON p.id = s.payment_id
506
- LEFT JOIN payment_metadata pm ON p.id = pm.payment_id
507
- LEFT JOIN lnurl_receive_metadata lrm ON l.payment_hash = lrm.payment_hash
508
- WHERE l.invoice = ?
509
- `);
510
-
447
+ const stmt = this.db.prepare(`${SELECT_PAYMENT_SQL} WHERE l.invoice = ?`);
511
448
  const row = stmt.get(invoice);
449
+
512
450
  if (!row) {
513
451
  return Promise.resolve(null);
514
452
  }
@@ -526,11 +464,66 @@ class SqliteStorage {
526
464
  }
527
465
  }
528
466
 
529
- setPaymentMetadata(paymentId, metadata) {
467
+ /**
468
+ * Gets payments that have any of the specified parent payment IDs.
469
+ * @param {string[]} parentPaymentIds - Array of parent payment IDs
470
+ * @returns {Promise<Object>} Map of parentPaymentId -> array of RelatedPayment objects
471
+ */
472
+ getPaymentsByParentIds(parentPaymentIds) {
473
+ try {
474
+ if (!parentPaymentIds || parentPaymentIds.length === 0) {
475
+ return Promise.resolve({});
476
+ }
477
+
478
+ // Early exit if no related payments exist
479
+ const hasRelated = this.db
480
+ .prepare(
481
+ "SELECT EXISTS(SELECT 1 FROM payment_metadata WHERE parent_payment_id IS NOT NULL LIMIT 1)"
482
+ )
483
+ .pluck()
484
+ .get();
485
+ if (!hasRelated) {
486
+ return Promise.resolve({});
487
+ }
488
+
489
+ const placeholders = parentPaymentIds.map(() => "?").join(", ");
490
+ const query = `${SELECT_PAYMENT_SQL} WHERE pm.parent_payment_id IN (${placeholders}) ORDER BY p.timestamp ASC`;
491
+
492
+ const stmt = this.db.prepare(query);
493
+ const rows = stmt.all(...parentPaymentIds);
494
+
495
+ // Group payments by parent_payment_id
496
+ const result = {};
497
+ for (const row of rows) {
498
+ const parentId = row.parent_payment_id;
499
+ if (!result[parentId]) {
500
+ result[parentId] = [];
501
+ }
502
+ result[parentId].push(this._rowToPayment(row));
503
+ }
504
+
505
+ return Promise.resolve(result);
506
+ } catch (error) {
507
+ return Promise.reject(
508
+ new StorageError(
509
+ `Failed to get payments by parent ids: ${error.message}`,
510
+ error
511
+ )
512
+ );
513
+ }
514
+ }
515
+
516
+ insertPaymentMetadata(paymentId, metadata) {
530
517
  try {
531
518
  const stmt = this.db.prepare(`
532
- INSERT OR REPLACE INTO payment_metadata (payment_id, parent_payment_id, lnurl_pay_info, lnurl_withdraw_info, lnurl_description, conversion_info)
519
+ INSERT INTO payment_metadata (payment_id, parent_payment_id, lnurl_pay_info, lnurl_withdraw_info, lnurl_description, conversion_info)
533
520
  VALUES (?, ?, ?, ?, ?, ?)
521
+ ON CONFLICT(payment_id) DO UPDATE SET
522
+ parent_payment_id = COALESCE(excluded.parent_payment_id, parent_payment_id),
523
+ lnurl_pay_info = COALESCE(excluded.lnurl_pay_info, lnurl_pay_info),
524
+ lnurl_withdraw_info = COALESCE(excluded.lnurl_withdraw_info, lnurl_withdraw_info),
525
+ lnurl_description = COALESCE(excluded.lnurl_description, lnurl_description),
526
+ conversion_info = COALESCE(excluded.conversion_info, conversion_info)
534
527
  `);
535
528
 
536
529
  stmt.run(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@breeztech/breez-sdk-spark",
3
- "version": "0.7.13",
3
+ "version": "0.7.15-dev1",
4
4
  "description": "Breez Spark SDK",
5
5
  "repository": "https://github.com/breez/spark-sdk",
6
6
  "author": "Breez <contact@breez.technology> (https://github.com/breez)",