@breeztech/breez-sdk-spark 0.16.1-dev1 → 0.17.1

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.
@@ -490,7 +490,7 @@ class MigrationManager {
490
490
  // Add deposit_vout to distinguish deposits sharing a funding tx. The
491
491
  // new field is carried inside the JSON `details` blob. We can't safely
492
492
  // backfill vout on the existing blobs: we never stored the original SSP
493
- // output_index, and vout=0 is a valid output index defaulting would
493
+ // output_index, and vout=0 is a valid output index, so defaulting would
494
494
  // silently mislabel. Instead we clear `details` on legacy deposit blobs
495
495
  // so the read path returns `details: None` (matches what the SQL
496
496
  // backends do by leaving the payments row but having no matching
@@ -545,6 +545,47 @@ class MigrationManager {
545
545
  }
546
546
  },
547
547
  },
548
+ {
549
+ name: "Backfill conversion_info type discriminator for serde tagged enum",
550
+ upgrade: (db, transaction) => {
551
+ if (db.objectStoreNames.contains("payment_metadata")) {
552
+ const store = transaction.objectStore("payment_metadata");
553
+ const request = store.openCursor();
554
+ request.onsuccess = (event) => {
555
+ const cursor = event.target.result;
556
+ if (cursor) {
557
+ const record = cursor.value;
558
+ if (record.conversionInfo) {
559
+ try {
560
+ const ci = typeof record.conversionInfo === "string"
561
+ ? JSON.parse(record.conversionInfo)
562
+ : record.conversionInfo;
563
+ if (!ci.type) {
564
+ ci.type = "amm";
565
+ record.conversionInfo = JSON.stringify(ci);
566
+ cursor.update(record);
567
+ }
568
+ } catch (e) {
569
+ // Skip unparseable records
570
+ }
571
+ }
572
+ cursor.continue();
573
+ }
574
+ };
575
+ }
576
+ },
577
+ },
578
+ {
579
+ // listActiveCrossChainSwaps scans and filters.
580
+ name: "Create cross_chain_swaps store",
581
+ upgrade: (db) => {
582
+ if (!db.objectStoreNames.contains("cross_chain_swaps")) {
583
+ db.createObjectStore("cross_chain_swaps", {
584
+ keyPath: ["provider", "id"],
585
+ });
586
+ }
587
+ },
588
+ },
548
589
  ];
549
590
  }
550
591
  }
@@ -573,7 +614,7 @@ class IndexedDBStorage {
573
614
  // so existing databases depend on indices never shifting. Never insert,
574
615
  // reorder, or delete a migration — only append. dbVersion MUST equal the
575
616
  // number of migrations (enforced by the guard in initialize()).
576
- this.dbVersion = 18; // Current schema version (= migration count)
617
+ this.dbVersion = 20; // Current schema version (= migration count)
577
618
  }
578
619
 
579
620
  /**
@@ -2190,6 +2231,77 @@ class IndexedDBStorage {
2190
2231
  });
2191
2232
  }
2192
2233
 
2234
+ // ===== Cross-Chain Swap Operations =====
2235
+
2236
+ async setCrossChainSwap(swap) {
2237
+ if (!this.db) {
2238
+ throw new StorageError("Database not initialized");
2239
+ }
2240
+
2241
+ return new Promise((resolve, reject) => {
2242
+ const transaction = this.db.transaction("cross_chain_swaps", "readwrite");
2243
+ const store = transaction.objectStore("cross_chain_swaps");
2244
+ // IndexedDB stores nested objects natively, so `data` is kept as-is.
2245
+ const request = store.put(swap);
2246
+ request.onsuccess = () => resolve();
2247
+ request.onerror = () => {
2248
+ reject(
2249
+ new StorageError(
2250
+ `Failed to set cross-chain swap '${swap.provider}:${swap.id}': ${request.error?.message || "Unknown error"}`,
2251
+ request.error
2252
+ )
2253
+ );
2254
+ };
2255
+ });
2256
+ }
2257
+
2258
+ async getCrossChainSwap(provider, id) {
2259
+ if (!this.db) {
2260
+ throw new StorageError("Database not initialized");
2261
+ }
2262
+
2263
+ return new Promise((resolve, reject) => {
2264
+ const transaction = this.db.transaction("cross_chain_swaps", "readonly");
2265
+ const store = transaction.objectStore("cross_chain_swaps");
2266
+ const request = store.get([provider, id]);
2267
+ request.onsuccess = () => resolve(request.result || null);
2268
+ request.onerror = () => {
2269
+ reject(
2270
+ new StorageError(
2271
+ `Failed to get cross-chain swap '${provider}:${id}': ${request.error?.message || "Unknown error"}`,
2272
+ request.error
2273
+ )
2274
+ );
2275
+ };
2276
+ });
2277
+ }
2278
+
2279
+ async listActiveCrossChainSwaps(provider) {
2280
+ if (!this.db) {
2281
+ throw new StorageError("Database not initialized");
2282
+ }
2283
+
2284
+ return new Promise((resolve, reject) => {
2285
+ const transaction = this.db.transaction("cross_chain_swaps", "readonly");
2286
+ const store = transaction.objectStore("cross_chain_swaps");
2287
+ const request = store.getAll();
2288
+ request.onsuccess = () => {
2289
+ const all = request.result || [];
2290
+ resolve(
2291
+ all.filter((swap) => swap.provider === provider && !swap.isTerminal)
2292
+ );
2293
+ };
2294
+ request.onerror = () => {
2295
+ reject(
2296
+ new StorageError(
2297
+ `Failed to list active cross-chain swaps for '${provider}': ${request.error?.message || "Unknown error"}`,
2298
+ request.error
2299
+ )
2300
+ );
2301
+ };
2302
+ });
2303
+ }
2304
+
2193
2305
  // ===== Private Helper Methods =====
2194
2306
 
2195
2307
  _paymentToStore(payment) {
@@ -2265,6 +2377,10 @@ class IndexedDBStorage {
2265
2377
  // Filter by payment details. If any filter matches, we include the payment
2266
2378
  let paymentDetailsFilterMatches = false;
2267
2379
  for (const paymentDetailsFilter of request.paymentDetailsFilter) {
2380
+ // Base type check: the payment's details type must match the filter type
2381
+ if (details.type !== paymentDetailsFilter.type) {
2382
+ continue;
2383
+ }
2268
2384
  // Filter by HTLC status (Spark or Lightning)
2269
2385
  if (
2270
2386
  (paymentDetailsFilter.type === "spark" ||
@@ -2282,11 +2398,12 @@ class IndexedDBStorage {
2282
2398
  continue;
2283
2399
  }
2284
2400
  }
2285
- // Filter by token conversion info presence
2401
+ // Filter by conversion type + status
2286
2402
  if (
2287
2403
  (paymentDetailsFilter.type === "spark" ||
2288
- paymentDetailsFilter.type === "token") &&
2289
- paymentDetailsFilter.conversionRefundNeeded != null
2404
+ paymentDetailsFilter.type === "token" ||
2405
+ paymentDetailsFilter.type === "lightning") &&
2406
+ paymentDetailsFilter.conversionFilter != null
2290
2407
  ) {
2291
2408
  if (
2292
2409
  details.type !== paymentDetailsFilter.type ||
@@ -2295,11 +2412,20 @@ class IndexedDBStorage {
2295
2412
  continue;
2296
2413
  }
2297
2414
 
2298
- if (
2299
- paymentDetailsFilter.conversionRefundNeeded ===
2300
- (details.conversionInfo.status !== "refundNeeded")
2301
- ) {
2302
- continue;
2415
+ const ci = details.conversionInfo;
2416
+ if (paymentDetailsFilter.conversionFilter === "ammRefundNeeded") {
2417
+ if (ci.type !== "amm" || ci.status !== "refundNeeded") {
2418
+ continue;
2419
+ }
2420
+ } else if (paymentDetailsFilter.conversionFilter === "orchestraPending") {
2421
+ if (ci.type !== "orchestra" || ["completed", "failed", "refunded"].includes(ci.status)) {
2422
+ continue;
2423
+ }
2424
+ } else if (paymentDetailsFilter.conversionFilter === "boltzPending") {
2425
+ // Boltz conversion lives on the Lightning leg.
2426
+ if (ci.type !== "boltz" || ["completed", "failed", "refunded"].includes(ci.status)) {
2427
+ continue;
2428
+ }
2303
2429
  }
2304
2430
  }
2305
2431
  // Filter by token transaction hash
@@ -2440,17 +2566,23 @@ class IndexedDBStorage {
2440
2566
  );
2441
2567
  }
2442
2568
  }
2443
- } else if (details.type == "spark" || details.type == "token") {
2444
- // If conversionInfo exists, parse and add to details
2445
- if (metadata.conversionInfo) {
2446
- try {
2447
- details.conversionInfo = JSON.parse(metadata.conversionInfo);
2448
- } catch (e) {
2449
- throw new StorageError(
2450
- `Failed to parse conversionInfo JSON for payment ${payment.id}: ${e.message}`,
2451
- e
2452
- );
2453
- }
2569
+ }
2570
+
2571
+ // conversionInfo is surfaced on Lightning (Boltz hold-invoice pay),
2572
+ // Spark, and Token details — every variant that can carry a conversion.
2573
+ if (
2574
+ (details.type == "lightning" ||
2575
+ details.type == "spark" ||
2576
+ details.type == "token") &&
2577
+ metadata.conversionInfo
2578
+ ) {
2579
+ try {
2580
+ details.conversionInfo = JSON.parse(metadata.conversionInfo);
2581
+ } catch (e) {
2582
+ throw new StorageError(
2583
+ `Failed to parse conversionInfo JSON for payment ${payment.id}: ${e.message}`,
2584
+ e
2585
+ );
2454
2586
  }
2455
2587
  }
2456
2588
  }