@breeztech/breez-sdk-spark 0.13.10-dev → 0.13.12-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.
Files changed (49) hide show
  1. package/breez-sdk-spark.tgz +0 -0
  2. package/bundler/breez_sdk_spark_wasm.d.ts +157 -0
  3. package/bundler/breez_sdk_spark_wasm.js +1 -1
  4. package/bundler/breez_sdk_spark_wasm_bg.js +419 -41
  5. package/bundler/breez_sdk_spark_wasm_bg.wasm +0 -0
  6. package/bundler/breez_sdk_spark_wasm_bg.wasm.d.ts +27 -7
  7. package/deno/breez_sdk_spark_wasm.d.ts +157 -0
  8. package/deno/breez_sdk_spark_wasm.js +419 -41
  9. package/deno/breez_sdk_spark_wasm_bg.wasm +0 -0
  10. package/deno/breez_sdk_spark_wasm_bg.wasm.d.ts +27 -7
  11. package/nodejs/breez_sdk_spark_wasm.d.ts +157 -0
  12. package/nodejs/breez_sdk_spark_wasm.js +428 -41
  13. package/nodejs/breez_sdk_spark_wasm_bg.wasm +0 -0
  14. package/nodejs/breez_sdk_spark_wasm_bg.wasm.d.ts +27 -7
  15. package/nodejs/index.js +56 -0
  16. package/nodejs/index.mjs +9 -0
  17. package/nodejs/mysql-session-manager/errors.cjs +13 -0
  18. package/nodejs/mysql-session-manager/index.cjs +144 -0
  19. package/nodejs/mysql-session-manager/migrations.cjs +102 -0
  20. package/nodejs/mysql-session-manager/package.json +9 -0
  21. package/nodejs/mysql-storage/errors.cjs +19 -0
  22. package/nodejs/mysql-storage/index.cjs +1367 -0
  23. package/nodejs/mysql-storage/migrations.cjs +387 -0
  24. package/nodejs/mysql-storage/package.json +9 -0
  25. package/nodejs/mysql-token-store/errors.cjs +9 -0
  26. package/nodejs/mysql-token-store/index.cjs +980 -0
  27. package/nodejs/mysql-token-store/migrations.cjs +255 -0
  28. package/nodejs/mysql-token-store/package.json +9 -0
  29. package/nodejs/mysql-tree-store/errors.cjs +9 -0
  30. package/nodejs/mysql-tree-store/index.cjs +941 -0
  31. package/nodejs/mysql-tree-store/migrations.cjs +221 -0
  32. package/nodejs/mysql-tree-store/package.json +9 -0
  33. package/nodejs/package.json +5 -0
  34. package/nodejs/postgres-session-manager/errors.cjs +13 -0
  35. package/nodejs/postgres-session-manager/index.cjs +165 -0
  36. package/nodejs/postgres-session-manager/migrations.cjs +126 -0
  37. package/nodejs/postgres-session-manager/package.json +9 -0
  38. package/nodejs/postgres-storage/index.cjs +147 -92
  39. package/nodejs/postgres-storage/migrations.cjs +85 -4
  40. package/nodejs/postgres-token-store/index.cjs +178 -102
  41. package/nodejs/postgres-token-store/migrations.cjs +92 -3
  42. package/nodejs/postgres-tree-store/index.cjs +168 -83
  43. package/nodejs/postgres-tree-store/migrations.cjs +80 -3
  44. package/package.json +1 -1
  45. package/ssr/index.js +53 -0
  46. package/web/breez_sdk_spark_wasm.d.ts +184 -7
  47. package/web/breez_sdk_spark_wasm.js +419 -41
  48. package/web/breez_sdk_spark_wasm_bg.wasm +0 -0
  49. package/web/breez_sdk_spark_wasm_bg.wasm.d.ts +27 -7
@@ -20,9 +20,14 @@ class PostgresMigrationManager {
20
20
 
21
21
  /**
22
22
  * Run all pending migrations inside a single transaction with an advisory lock.
23
+ *
23
24
  * @param {import('pg').Pool} pool
25
+ * @param {Buffer|Uint8Array} identity - 33-byte secp256k1 compressed pubkey
26
+ * identifying the tenant. Used to backfill `user_id` columns in the
27
+ * multi-tenant migration so that pre-existing single-tenant data remains
28
+ * readable.
24
29
  */
25
- async migrate(pool) {
30
+ async migrate(pool, identity) {
26
31
  const client = await pool.connect();
27
32
  try {
28
33
  await client.query("BEGIN");
@@ -44,7 +49,7 @@ class PostgresMigrationManager {
44
49
  );
45
50
  const currentVersion = versionResult.rows[0].version;
46
51
 
47
- const migrations = this._getMigrations();
52
+ const migrations = this._getMigrations(identity);
48
53
 
49
54
  if (currentVersion >= migrations.length) {
50
55
  this._log("info", `Database is up to date (version ${currentVersion})`);
@@ -97,8 +102,27 @@ class PostgresMigrationManager {
97
102
  * Single migration creating all tables at their final schema.
98
103
  * This mirrors the Rust-native PostgresStorage schema but uses camelCase
99
104
  * enum values (as produced by the WASM bridge).
105
+ *
106
+ * @param {Buffer|Uint8Array} identity - 33-byte tenant identity. Inlined as
107
+ * a hex BYTEA literal in the multi-tenant scoping migration. Safe because
108
+ * the bytes come from a typed secp256k1 pubkey (character set
109
+ * `[0-9a-f]{66}` after hex encoding) — not user-controlled input.
100
110
  */
101
- _getMigrations() {
111
+ _getMigrations(identity) {
112
+ const idHex = Buffer.from(identity).toString("hex");
113
+ const idLit = `'\\x${idHex}'::bytea`;
114
+
115
+ // Helper for the per-table backfill: ADD COLUMN nullable -> UPDATE -> SET
116
+ // NOT NULL + drop/recreate PK. Returns an array of statements.
117
+ const scopeTable = (table, pkCols) => [
118
+ `ALTER TABLE ${table} ADD COLUMN user_id BYTEA`,
119
+ `UPDATE ${table} SET user_id = ${idLit}`,
120
+ `ALTER TABLE ${table}
121
+ ALTER COLUMN user_id SET NOT NULL,
122
+ DROP CONSTRAINT IF EXISTS ${table}_pkey,
123
+ ADD PRIMARY KEY (user_id, ${pkCols})`,
124
+ ];
125
+
102
126
  return [
103
127
  {
104
128
  name: "Create all tables at final schema",
@@ -252,12 +276,69 @@ class PostgresMigrationManager {
252
276
  `ALTER TABLE unclaimed_deposits ADD COLUMN is_mature BOOLEAN NOT NULL DEFAULT TRUE`,
253
277
  ],
254
278
  },
255
- {
279
+ {
256
280
  name: "Add conversion_status to payment_metadata",
257
281
  sql: [
258
282
  `ALTER TABLE payment_metadata ADD COLUMN IF NOT EXISTS conversion_status TEXT`,
259
283
  ],
260
284
  },
285
+ {
286
+ name: "Multi-tenant scoping: add user_id and rewrite primary keys",
287
+ sql: [
288
+ // Per-user tables
289
+ ...scopeTable("payments", "id"),
290
+ `DROP INDEX IF EXISTS idx_payments_timestamp`,
291
+ `DROP INDEX IF EXISTS idx_payments_payment_type`,
292
+ `DROP INDEX IF EXISTS idx_payments_status`,
293
+ `CREATE INDEX idx_payments_user_timestamp ON payments(user_id, timestamp)`,
294
+ `CREATE INDEX idx_payments_user_payment_type ON payments(user_id, payment_type)`,
295
+ `CREATE INDEX idx_payments_user_status ON payments(user_id, status)`,
296
+
297
+ ...scopeTable("payment_metadata", "payment_id"),
298
+ `DROP INDEX IF EXISTS idx_payment_metadata_parent`,
299
+ `CREATE INDEX idx_payment_metadata_user_parent
300
+ ON payment_metadata(user_id, parent_payment_id)`,
301
+
302
+ ...scopeTable("payment_details_lightning", "payment_id"),
303
+ `DROP INDEX IF EXISTS idx_payment_details_lightning_invoice`,
304
+ `DROP INDEX IF EXISTS idx_payment_details_lightning_payment_hash`,
305
+ `CREATE INDEX idx_payment_details_lightning_user_invoice
306
+ ON payment_details_lightning(user_id, invoice)`,
307
+ `CREATE INDEX idx_payment_details_lightning_user_payment_hash
308
+ ON payment_details_lightning(user_id, payment_hash)`,
309
+
310
+ ...scopeTable("payment_details_token", "payment_id"),
311
+ ...scopeTable("payment_details_spark", "payment_id"),
312
+ ...scopeTable("lnurl_receive_metadata", "payment_hash"),
313
+ ...scopeTable("unclaimed_deposits", "txid, vout"),
314
+ ...scopeTable("contacts", "id"),
315
+ ...scopeTable("settings", "key"),
316
+
317
+ // sync_revision: drop the singleton id (CASCADE removes PK + CHECK),
318
+ // then re-key by user_id so each tenant has its own revision row.
319
+ `ALTER TABLE sync_revision DROP COLUMN id CASCADE`,
320
+ `ALTER TABLE sync_revision ADD COLUMN user_id BYTEA`,
321
+ `UPDATE sync_revision SET user_id = ${idLit}`,
322
+ `ALTER TABLE sync_revision
323
+ ALTER COLUMN user_id SET NOT NULL,
324
+ ADD PRIMARY KEY (user_id)`,
325
+
326
+ // sync_outgoing has no PK, only an index — just add user_id and rewrite the index.
327
+ `ALTER TABLE sync_outgoing ADD COLUMN user_id BYTEA`,
328
+ `UPDATE sync_outgoing SET user_id = ${idLit}`,
329
+ `ALTER TABLE sync_outgoing ALTER COLUMN user_id SET NOT NULL`,
330
+ `DROP INDEX IF EXISTS idx_sync_outgoing_data_id_record_type`,
331
+ `CREATE INDEX idx_sync_outgoing_user_record_type_data_id
332
+ ON sync_outgoing(user_id, record_type, data_id)`,
333
+
334
+ ...scopeTable("sync_state", "record_type, data_id"),
335
+
336
+ ...scopeTable("sync_incoming", "record_type, data_id, revision"),
337
+ `DROP INDEX IF EXISTS idx_sync_incoming_revision`,
338
+ `CREATE INDEX idx_sync_incoming_user_revision
339
+ ON sync_incoming(user_id, revision)`,
340
+ ],
341
+ },
261
342
  ];
262
343
  }
263
344
  }