@canton-network/core-wallet-store-sql 1.13.1 → 1.14.0

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 (88) hide show
  1. package/dist/bootstrap.d.ts +4 -4
  2. package/dist/bootstrap.d.ts.map +1 -1
  3. package/dist/index.cjs +79 -27
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.js +79 -27
  6. package/dist/index.js.map +1 -1
  7. package/dist/migrations/001-init.d.ts +2 -2
  8. package/dist/migrations/001-init.d.ts.map +1 -1
  9. package/dist/migrations/002-add-transaction-timestamps.d.ts +2 -2
  10. package/dist/migrations/002-add-transaction-timestamps.d.ts.map +1 -1
  11. package/dist/migrations/003-add-transaction-origin.d.ts +2 -2
  12. package/dist/migrations/003-add-transaction-origin.d.ts.map +1 -1
  13. package/dist/migrations/004-add-session-id.d.ts +2 -2
  14. package/dist/migrations/004-add-session-id.d.ts.map +1 -1
  15. package/dist/migrations/005-add-wallet-disabled-reason.d.ts +2 -2
  16. package/dist/migrations/005-add-wallet-disabled-reason.d.ts.map +1 -1
  17. package/dist/migrations/006-change-wallet-primary-key-to-composite.d.ts +2 -2
  18. package/dist/migrations/006-change-wallet-primary-key-to-composite.d.ts.map +1 -1
  19. package/dist/migrations/007-add-unique-primary-per-network.d.ts +2 -2
  20. package/dist/migrations/007-add-unique-primary-per-network.d.ts.map +1 -1
  21. package/dist/migrations/008-add-transaction-external-tx-id.d.ts +2 -2
  22. package/dist/migrations/008-add-transaction-external-tx-id.d.ts.map +1 -1
  23. package/dist/migrations/009-add-wallet-rights-columns.d.ts +2 -2
  24. package/dist/migrations/009-add-wallet-rights-columns.d.ts.map +1 -1
  25. package/dist/migrations/010-add-transaction-network-id.d.ts +2 -2
  26. package/dist/migrations/010-add-transaction-network-id.d.ts.map +1 -1
  27. package/dist/migrations/011-add-transaction-id-primary-key.d.ts +2 -2
  28. package/dist/migrations/011-add-transaction-id-primary-key.d.ts.map +1 -1
  29. package/dist/migrations/012-add-messages-to-sign.d.ts +1 -1
  30. package/dist/migrations/012-add-messages-to-sign.d.ts.map +1 -1
  31. package/dist/migrations/013-add-api-key.d.ts +1 -1
  32. package/dist/migrations/013-add-api-key.d.ts.map +1 -1
  33. package/dist/migrations/014-add-service-account-column.d.ts +2 -2
  34. package/dist/migrations/014-add-service-account-column.d.ts.map +1 -1
  35. package/dist/migrations/015-add-origin-field-session.d.ts +2 -2
  36. package/dist/migrations/015-add-origin-field-session.d.ts.map +1 -1
  37. package/dist/migrations/016-add-failure-reason-and-awaiting-signature.d.ts +5 -0
  38. package/dist/migrations/016-add-failure-reason-and-awaiting-signature.d.ts.map +1 -0
  39. package/dist/migrations/016-add-failure-reason-and-awaiting-signature.js +27 -0
  40. package/dist/migrations/017-make-idp-issuer-nullable.d.ts +5 -0
  41. package/dist/migrations/017-make-idp-issuer-nullable.d.ts.map +1 -0
  42. package/dist/migrations/017-make-idp-issuer-nullable.js +88 -0
  43. package/dist/migrations/018-add-wallet-is-auth-party.d.ts +5 -0
  44. package/dist/migrations/018-add-wallet-is-auth-party.d.ts.map +1 -0
  45. package/dist/migrations/018-add-wallet-is-auth-party.js +11 -0
  46. package/dist/migrations/019-make-session-access-token-nullable.d.ts +5 -0
  47. package/dist/migrations/019-make-session-access-token-nullable.d.ts.map +1 -0
  48. package/dist/migrations/019-make-session-access-token-nullable.js +92 -0
  49. package/dist/migrations-test/helpers.d.ts +2 -2
  50. package/dist/migrations-test/helpers.d.ts.map +1 -1
  51. package/dist/migrations-test/seeds/001-init.d.ts +2 -2
  52. package/dist/migrations-test/seeds/001-init.d.ts.map +1 -1
  53. package/dist/migrations-test/seeds/002-transaction-timestamps.d.ts +2 -2
  54. package/dist/migrations-test/seeds/002-transaction-timestamps.d.ts.map +1 -1
  55. package/dist/migrations-test/seeds/003-transaction-origin.d.ts +2 -2
  56. package/dist/migrations-test/seeds/003-transaction-origin.d.ts.map +1 -1
  57. package/dist/migrations-test/seeds/004-add-session-id.d.ts +2 -2
  58. package/dist/migrations-test/seeds/004-add-session-id.d.ts.map +1 -1
  59. package/dist/migrations-test/seeds/005-add-wallet-disabled-reason.d.ts +2 -2
  60. package/dist/migrations-test/seeds/005-add-wallet-disabled-reason.d.ts.map +1 -1
  61. package/dist/migrations-test/seeds/008-transaction-external-tx-id.d.ts +2 -2
  62. package/dist/migrations-test/seeds/008-transaction-external-tx-id.d.ts.map +1 -1
  63. package/dist/migrations-test/seeds/009-add-wallet-rights-columns.d.ts +2 -2
  64. package/dist/migrations-test/seeds/009-add-wallet-rights-columns.d.ts.map +1 -1
  65. package/dist/migrations-test/seeds/010-transaction-with-network.d.ts +2 -2
  66. package/dist/migrations-test/seeds/010-transaction-with-network.d.ts.map +1 -1
  67. package/dist/migrations-test/seeds/012-add-messages-to-sign.d.ts +2 -2
  68. package/dist/migrations-test/seeds/012-add-messages-to-sign.d.ts.map +1 -1
  69. package/dist/migrations-test/seeds/013-add-api-key.d.ts +2 -2
  70. package/dist/migrations-test/seeds/013-add-api-key.d.ts.map +1 -1
  71. package/dist/migrations-test/seeds/014-add-service-account-column.d.ts +2 -2
  72. package/dist/migrations-test/seeds/014-add-service-account-column.d.ts.map +1 -1
  73. package/dist/migrations-test/seeds/016-add-failure-reason-and-awaiting-signature.d.ts +18 -0
  74. package/dist/migrations-test/seeds/016-add-failure-reason-and-awaiting-signature.d.ts.map +1 -0
  75. package/dist/migrations-test/seeds/017-make-idp-issuer-nullable.d.ts +9 -0
  76. package/dist/migrations-test/seeds/017-make-idp-issuer-nullable.d.ts.map +1 -0
  77. package/dist/migrations-test/seeds/019-make-session-access-token-nullable.d.ts +10 -0
  78. package/dist/migrations-test/seeds/019-make-session-access-token-nullable.d.ts.map +1 -0
  79. package/dist/migrator.d.ts +2 -2
  80. package/dist/migrator.d.ts.map +1 -1
  81. package/dist/schema.d.ts +8 -6
  82. package/dist/schema.d.ts.map +1 -1
  83. package/dist/schema.js +26 -5
  84. package/dist/store-sql.d.ts +14 -7
  85. package/dist/store-sql.d.ts.map +1 -1
  86. package/dist/utils.d.ts +2 -2
  87. package/dist/utils.d.ts.map +1 -1
  88. package/package.json +8 -8
package/dist/index.js CHANGED
@@ -11,16 +11,23 @@ const toIdp = (table) => {
11
11
  switch (table.type) {
12
12
  case "oauth":
13
13
  if (!table.configUrl) throw new Error(`Missing configUrl for oauth IdP: ${table.id}`);
14
+ if (table.issuer === null) throw new Error(`Missing issuer for oauth IdP: ${table.id}`);
14
15
  return {
15
16
  id: table.id,
16
17
  type: table.type,
17
18
  issuer: table.issuer,
18
19
  configUrl: table.configUrl
19
20
  };
20
- case "self_signed": return {
21
+ case "self_signed":
22
+ if (table.issuer === null) throw new Error(`Missing issuer for self_signed IdP: ${table.id}`);
23
+ return {
24
+ id: table.id,
25
+ type: table.type,
26
+ issuer: table.issuer
27
+ };
28
+ case "self_issued": return {
21
29
  id: table.id,
22
- type: table.type,
23
- issuer: table.issuer
30
+ type: table.type
24
31
  };
25
32
  }
26
33
  };
@@ -36,7 +43,13 @@ const fromIdp = (idp) => {
36
43
  id: idp.id,
37
44
  type: idp.type,
38
45
  issuer: idp.issuer,
39
- configUrl: void 0
46
+ configUrl: null
47
+ };
48
+ case "self_issued": return {
49
+ id: idp.id,
50
+ type: idp.type,
51
+ issuer: null,
52
+ configUrl: null
40
53
  };
41
54
  }
42
55
  };
@@ -72,24 +85,25 @@ const toSession = (table) => {
72
85
  id: table.id,
73
86
  network: table.network,
74
87
  origin: table.origin,
75
- accessToken: table.accessToken,
88
+ ...table.accessToken ? { accessToken: table.accessToken } : {},
76
89
  userId: table.userId
77
90
  };
78
91
  };
79
92
  const fromWallet = (wallet, userId) => {
80
- const { externalTxId, topologyTransactions, rights, ...rest } = wallet;
93
+ const { externalTxId, topologyTransactions, rights, isAuthParty, ...rest } = wallet;
81
94
  return {
82
95
  ...rest,
83
96
  primary: wallet.primary ? 1 : 0,
84
97
  userId,
85
98
  disabled: wallet.disabled !== void 0 && wallet.disabled ? 1 : 0,
99
+ isAuthParty: isAuthParty ? 1 : 0,
86
100
  reason: wallet.reason ?? null,
87
101
  externalTxId: externalTxId && externalTxId !== "" ? externalTxId : null,
88
102
  topologyTransactions: topologyTransactions && topologyTransactions !== "" ? topologyTransactions : null
89
103
  };
90
104
  };
91
105
  const toWalletUpdateProperties = (params) => {
92
- const { status, externalTxId, topologyTransactions, disabled, reason, primary, signingProviderId, publicKey, namespace } = params;
106
+ const { status, externalTxId, topologyTransactions, disabled, reason, primary, signingProviderId, publicKey, namespace, isAuthParty } = params;
93
107
  return {
94
108
  ...status !== void 0 && { status },
95
109
  ...externalTxId !== void 0 && { externalTxId },
@@ -99,7 +113,8 @@ const toWalletUpdateProperties = (params) => {
99
113
  ...reason !== void 0 && { reason },
100
114
  ...signingProviderId !== void 0 && { signingProviderId },
101
115
  ...publicKey !== void 0 && { publicKey },
102
- ...namespace !== void 0 && { namespace }
116
+ ...namespace !== void 0 && { namespace },
117
+ ...isAuthParty !== void 0 && { isAuthParty: isAuthParty ? 1 : 0 }
103
118
  };
104
119
  };
105
120
  const toWalletStatus = (status) => {
@@ -118,6 +133,7 @@ const toWallet = (table) => {
118
133
  networkId: table.networkId,
119
134
  signingProviderId: table.signingProviderId,
120
135
  disabled: table.disabled === 1,
136
+ isAuthParty: Boolean(table.isAuthParty),
121
137
  userId: table.userId,
122
138
  ...table.externalTxId !== null && { externalTxId: table.externalTxId },
123
139
  ...table.topologyTransactions !== null && { topologyTransactions: table.topologyTransactions },
@@ -216,7 +232,7 @@ const toApiKey = (apiKey) => {
216
232
  };
217
233
  };
218
234
  //#endregion
219
- //#region \0@oxc-project+runtime@0.142.0/helpers/esm/typeof.js
235
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/typeof.js
220
236
  function _typeof(o) {
221
237
  "@babel/helpers - typeof";
222
238
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -226,7 +242,7 @@ function _typeof(o) {
226
242
  }, _typeof(o);
227
243
  }
228
244
  //#endregion
229
- //#region \0@oxc-project+runtime@0.142.0/helpers/esm/toPrimitive.js
245
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/toPrimitive.js
230
246
  function toPrimitive(t, r) {
231
247
  if ("object" != _typeof(t) || !t) return t;
232
248
  var e = t[Symbol.toPrimitive];
@@ -238,13 +254,13 @@ function toPrimitive(t, r) {
238
254
  return ("string" === r ? String : Number)(t);
239
255
  }
240
256
  //#endregion
241
- //#region \0@oxc-project+runtime@0.142.0/helpers/esm/toPropertyKey.js
257
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/toPropertyKey.js
242
258
  function toPropertyKey(t) {
243
259
  var i = toPrimitive(t, "string");
244
260
  return "symbol" == _typeof(i) ? i : i + "";
245
261
  }
246
262
  //#endregion
247
- //#region \0@oxc-project+runtime@0.142.0/helpers/esm/defineProperty.js
263
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/defineProperty.js
248
264
  function _defineProperty(e, r, t) {
249
265
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
250
266
  value: t,
@@ -431,15 +447,39 @@ var StoreSql = class StoreSql {
431
447
  const userId = this.assertConnected();
432
448
  if (!session.origin) throw new Error("Session origin is required");
433
449
  await this.db.transaction().execute(async (trx) => {
434
- const deleted = await trx.deleteFrom("sessions").where((eb) => eb.and([eb("userId", "=", userId), eb("origin", "=", session.origin)])).execute();
435
- this.logger.debug(deleted, "Deleted old session");
450
+ if (session.accessToken) {
451
+ const deleted = await trx.deleteFrom("sessions").where((eb) => eb.and([
452
+ eb("userId", "=", userId),
453
+ eb("origin", "=", session.origin),
454
+ eb("accessToken", "is not", null)
455
+ ])).execute();
456
+ this.logger.debug(deleted, "Deleted old session");
457
+ }
436
458
  const inserted = await trx.insertInto("sessions").values({
437
459
  ...session,
460
+ accessToken: session.accessToken ?? null,
438
461
  userId
439
462
  }).execute();
440
463
  this.logger.debug(inserted, "Inserted new session");
441
464
  });
442
465
  }
466
+ async getOnboardingSession(sessionId) {
467
+ const row = await this.db.selectFrom("sessions").selectAll().where("id", "=", sessionId).where("accessToken", "is", null).executeTakeFirst();
468
+ return row ? toSession(row) : void 0;
469
+ }
470
+ async upgradeOnboardingSession(sessionId, accessToken) {
471
+ const userId = this.assertConnected();
472
+ return this.db.transaction().execute(async (trx) => {
473
+ const row = await trx.selectFrom("sessions").selectAll().where("id", "=", sessionId).where("userId", "=", userId).where("accessToken", "is", null).executeTakeFirst();
474
+ if (!row) throw new Error("Onboarding session not found");
475
+ await trx.deleteFrom("sessions").where("userId", "=", userId).where("origin", "=", row.origin).where("id", "!=", sessionId).execute();
476
+ await trx.updateTable("sessions").set({ accessToken }).where("id", "=", sessionId).execute();
477
+ return toSession({
478
+ ...row,
479
+ accessToken
480
+ });
481
+ });
482
+ }
443
483
  async removeSession(accessToken) {
444
484
  const userId = this.assertConnected();
445
485
  await this.db.deleteFrom("sessions").where((eb) => eb.and([eb("userId", "=", userId), eb("accessToken", "=", accessToken)])).execute();
@@ -448,7 +488,7 @@ var StoreSql = class StoreSql {
448
488
  * Lists all pending transactions across all users.
449
489
  */
450
490
  async listAllPendingTransactions() {
451
- return (await this.db.selectFrom("transactions").selectAll().where("status", "=", "pending").execute()).map((row) => toTransaction(row));
491
+ return (await this.db.selectFrom("transactions").selectAll().where((eb) => eb.or([eb("status", "=", "pending"), eb("status", "=", "awaiting-signature")])).execute()).map((row) => toTransaction(row));
452
492
  }
453
493
  async getIdp(idpId) {
454
494
  const idps = await this.listIdps();
@@ -488,16 +528,24 @@ var StoreSql = class StoreSql {
488
528
  return network;
489
529
  }
490
530
  async getCurrentNetwork() {
531
+ const userId = this.assertConnected();
491
532
  const token = this.authContext?.accessToken;
492
- if (!token) throw new Error("No access token found in auth context");
493
- const session = await this.getSession(token);
494
- if (!session) throw new Error("No session found");
495
- const networkId = session.network;
533
+ const onboardingSessionId = this.authContext && !this.authContext.isApiKey ? this.authContext.sessionId : void 0;
534
+ if (!token && !onboardingSessionId) throw new Error("No session found");
535
+ const sessionRow = await this.db.selectFrom("sessions").selectAll().where("userId", "=", userId).where((eb) => token ? eb("accessToken", "=", token) : eb.and([eb("id", "=", onboardingSessionId), eb("accessToken", "is", null)])).executeTakeFirst();
536
+ if (!sessionRow) throw new Error("No session found");
537
+ const networkId = toSession(sessionRow).network;
496
538
  if (!networkId) throw new Error("No current network set in session");
497
539
  const network = (await this.listNetworks()).find((n) => n.id === networkId);
498
540
  if (!network) throw new Error(`Network "${networkId}" not found`);
499
541
  return network;
500
542
  }
543
+ async getNetworkForTokenVerification(networkId) {
544
+ const row = await this.db.selectFrom("networks").selectAll().where("id", "=", networkId).executeTakeFirst();
545
+ if (!row) return void 0;
546
+ const network = toNetwork(row);
547
+ return network.auth.method === "self_signed" ? network : void 0;
548
+ }
501
549
  async listNetworks() {
502
550
  let query = this.db.selectFrom("networks").selectAll();
503
551
  if (this.authContext) {
@@ -535,6 +583,7 @@ var StoreSql = class StoreSql {
535
583
  const payload = updates.payload ?? existing.payload;
536
584
  const signedAt = updates.signedAt ?? existing.signedAt;
537
585
  const externalTxId = updates.externalTxId ?? existing.externalTxId;
586
+ const failureReason = updates.failureReason ?? existing.failureReason;
538
587
  return {
539
588
  id: existing.id,
540
589
  commandId: existing.commandId,
@@ -545,7 +594,8 @@ var StoreSql = class StoreSql {
545
594
  ...payload !== void 0 && { payload },
546
595
  ...existing.createdAt !== void 0 && { createdAt: existing.createdAt },
547
596
  ...signedAt !== void 0 && { signedAt },
548
- ...externalTxId !== void 0 && { externalTxId }
597
+ ...externalTxId !== void 0 && { externalTxId },
598
+ ...failureReason !== void 0 && { failureReason }
549
599
  };
550
600
  }
551
601
  async setTransaction(transaction) {
@@ -553,23 +603,25 @@ var StoreSql = class StoreSql {
553
603
  const network = await this.getCurrentNetwork();
554
604
  await this.db.insertInto("transactions").values(fromTransaction(transaction, userId, network.id)).execute();
555
605
  }
556
- async setTransactionSigned(transactionId, signedAt, externalTxId) {
557
- await this.setTransactionStatus(transactionId, "signed", {
606
+ async setTransactionSigned(transactionId, signedAt, externalTxId, opts) {
607
+ return await this.setTransactionStatus(transactionId, "signed", {
558
608
  signedAt,
559
609
  ...externalTxId !== void 0 && { externalTxId }
560
- });
610
+ }, opts);
561
611
  }
562
- async setTransactionStatus(transactionId, status, updates = {}) {
612
+ async setTransactionStatus(transactionId, status, updates = {}, opts) {
563
613
  const userId = this.assertConnected();
564
614
  const network = await this.getCurrentNetwork();
565
615
  const existing = await this.getTransaction(transactionId);
566
616
  if (!existing) throw new Error(`Transaction not found with id: ${transactionId}`);
617
+ if (opts?.expectedStatus && existing.status !== opts.expectedStatus) return false;
567
618
  const updated = this.mergeTransactionStatusUpdate(existing, status, updates);
568
- await this.db.updateTable("transactions").set(fromTransaction(updated, userId, network.id)).where((eb) => eb.and([
619
+ return (await this.db.updateTable("transactions").set(fromTransaction(updated, userId, network.id)).where((eb) => eb.and([
569
620
  eb("id", "=", transactionId),
570
621
  eb("userId", "=", userId),
571
- eb("networkId", "=", network.id)
572
- ])).execute();
622
+ eb("networkId", "=", network.id),
623
+ ...opts?.expectedStatus ? [eb("status", "=", opts.expectedStatus)] : []
624
+ ])).executeTakeFirst()).numUpdatedRows > 0n;
573
625
  }
574
626
  async getTransaction(transactionId) {
575
627
  const userId = this.assertConnected();