@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
@@ -1,6 +1,6 @@
1
- import { Kysely } from 'kysely';
2
- import { BootstrapConfig } from '@canton-network/core-wallet-store';
3
- import { Logger } from 'pino';
4
- import { DB } from './schema';
1
+ import type { Kysely } from 'kysely';
2
+ import type { BootstrapConfig } from '@canton-network/core-wallet-store';
3
+ import type { Logger } from 'pino';
4
+ import type { DB } from './schema';
5
5
  export declare function bootstrap(db: Kysely<DB>, config: BootstrapConfig, logger: Logger): Promise<void>;
6
6
  //# sourceMappingURL=bootstrap.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bootstrap.d.ts","sourceRoot":"","sources":["../src/bootstrap.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAE/B,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAA;AACnE,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAC7B,OAAO,EAAE,EAAE,EAAE,MAAM,UAAU,CAAA;AAE7B,wBAAsB,SAAS,CAC3B,EAAE,EAAE,MAAM,CAAC,EAAE,CAAC,EACd,MAAM,EAAE,eAAe,EACvB,MAAM,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC,CAUf"}
1
+ {"version":3,"file":"bootstrap.d.ts","sourceRoot":"","sources":["../src/bootstrap.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAEpC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAA;AACxE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAClC,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,UAAU,CAAA;AAElC,wBAAsB,SAAS,CAC3B,EAAE,EAAE,MAAM,CAAC,EAAE,CAAC,EACd,MAAM,EAAE,eAAe,EACvB,MAAM,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC,CAUf"}
package/dist/index.cjs CHANGED
@@ -36,16 +36,23 @@ const toIdp = (table) => {
36
36
  switch (table.type) {
37
37
  case "oauth":
38
38
  if (!table.configUrl) throw new Error(`Missing configUrl for oauth IdP: ${table.id}`);
39
+ if (table.issuer === null) throw new Error(`Missing issuer for oauth IdP: ${table.id}`);
39
40
  return {
40
41
  id: table.id,
41
42
  type: table.type,
42
43
  issuer: table.issuer,
43
44
  configUrl: table.configUrl
44
45
  };
45
- case "self_signed": return {
46
+ case "self_signed":
47
+ if (table.issuer === null) throw new Error(`Missing issuer for self_signed IdP: ${table.id}`);
48
+ return {
49
+ id: table.id,
50
+ type: table.type,
51
+ issuer: table.issuer
52
+ };
53
+ case "self_issued": return {
46
54
  id: table.id,
47
- type: table.type,
48
- issuer: table.issuer
55
+ type: table.type
49
56
  };
50
57
  }
51
58
  };
@@ -61,7 +68,13 @@ const fromIdp = (idp) => {
61
68
  id: idp.id,
62
69
  type: idp.type,
63
70
  issuer: idp.issuer,
64
- configUrl: void 0
71
+ configUrl: null
72
+ };
73
+ case "self_issued": return {
74
+ id: idp.id,
75
+ type: idp.type,
76
+ issuer: null,
77
+ configUrl: null
65
78
  };
66
79
  }
67
80
  };
@@ -97,24 +110,25 @@ const toSession = (table) => {
97
110
  id: table.id,
98
111
  network: table.network,
99
112
  origin: table.origin,
100
- accessToken: table.accessToken,
113
+ ...table.accessToken ? { accessToken: table.accessToken } : {},
101
114
  userId: table.userId
102
115
  };
103
116
  };
104
117
  const fromWallet = (wallet, userId) => {
105
- const { externalTxId, topologyTransactions, rights, ...rest } = wallet;
118
+ const { externalTxId, topologyTransactions, rights, isAuthParty, ...rest } = wallet;
106
119
  return {
107
120
  ...rest,
108
121
  primary: wallet.primary ? 1 : 0,
109
122
  userId,
110
123
  disabled: wallet.disabled !== void 0 && wallet.disabled ? 1 : 0,
124
+ isAuthParty: isAuthParty ? 1 : 0,
111
125
  reason: wallet.reason ?? null,
112
126
  externalTxId: externalTxId && externalTxId !== "" ? externalTxId : null,
113
127
  topologyTransactions: topologyTransactions && topologyTransactions !== "" ? topologyTransactions : null
114
128
  };
115
129
  };
116
130
  const toWalletUpdateProperties = (params) => {
117
- const { status, externalTxId, topologyTransactions, disabled, reason, primary, signingProviderId, publicKey, namespace } = params;
131
+ const { status, externalTxId, topologyTransactions, disabled, reason, primary, signingProviderId, publicKey, namespace, isAuthParty } = params;
118
132
  return {
119
133
  ...status !== void 0 && { status },
120
134
  ...externalTxId !== void 0 && { externalTxId },
@@ -124,7 +138,8 @@ const toWalletUpdateProperties = (params) => {
124
138
  ...reason !== void 0 && { reason },
125
139
  ...signingProviderId !== void 0 && { signingProviderId },
126
140
  ...publicKey !== void 0 && { publicKey },
127
- ...namespace !== void 0 && { namespace }
141
+ ...namespace !== void 0 && { namespace },
142
+ ...isAuthParty !== void 0 && { isAuthParty: isAuthParty ? 1 : 0 }
128
143
  };
129
144
  };
130
145
  const toWalletStatus = (status) => {
@@ -143,6 +158,7 @@ const toWallet = (table) => {
143
158
  networkId: table.networkId,
144
159
  signingProviderId: table.signingProviderId,
145
160
  disabled: table.disabled === 1,
161
+ isAuthParty: Boolean(table.isAuthParty),
146
162
  userId: table.userId,
147
163
  ...table.externalTxId !== null && { externalTxId: table.externalTxId },
148
164
  ...table.topologyTransactions !== null && { topologyTransactions: table.topologyTransactions },
@@ -241,7 +257,7 @@ const toApiKey = (apiKey) => {
241
257
  };
242
258
  };
243
259
  //#endregion
244
- //#region \0@oxc-project+runtime@0.142.0/helpers/esm/typeof.js
260
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/typeof.js
245
261
  function _typeof(o) {
246
262
  "@babel/helpers - typeof";
247
263
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -251,7 +267,7 @@ function _typeof(o) {
251
267
  }, _typeof(o);
252
268
  }
253
269
  //#endregion
254
- //#region \0@oxc-project+runtime@0.142.0/helpers/esm/toPrimitive.js
270
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/toPrimitive.js
255
271
  function toPrimitive(t, r) {
256
272
  if ("object" != _typeof(t) || !t) return t;
257
273
  var e = t[Symbol.toPrimitive];
@@ -263,13 +279,13 @@ function toPrimitive(t, r) {
263
279
  return ("string" === r ? String : Number)(t);
264
280
  }
265
281
  //#endregion
266
- //#region \0@oxc-project+runtime@0.142.0/helpers/esm/toPropertyKey.js
282
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/toPropertyKey.js
267
283
  function toPropertyKey(t) {
268
284
  var i = toPrimitive(t, "string");
269
285
  return "symbol" == _typeof(i) ? i : i + "";
270
286
  }
271
287
  //#endregion
272
- //#region \0@oxc-project+runtime@0.142.0/helpers/esm/defineProperty.js
288
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/defineProperty.js
273
289
  function _defineProperty(e, r, t) {
274
290
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
275
291
  value: t,
@@ -456,15 +472,39 @@ var StoreSql = class StoreSql {
456
472
  const userId = this.assertConnected();
457
473
  if (!session.origin) throw new Error("Session origin is required");
458
474
  await this.db.transaction().execute(async (trx) => {
459
- const deleted = await trx.deleteFrom("sessions").where((eb) => eb.and([eb("userId", "=", userId), eb("origin", "=", session.origin)])).execute();
460
- this.logger.debug(deleted, "Deleted old session");
475
+ if (session.accessToken) {
476
+ const deleted = await trx.deleteFrom("sessions").where((eb) => eb.and([
477
+ eb("userId", "=", userId),
478
+ eb("origin", "=", session.origin),
479
+ eb("accessToken", "is not", null)
480
+ ])).execute();
481
+ this.logger.debug(deleted, "Deleted old session");
482
+ }
461
483
  const inserted = await trx.insertInto("sessions").values({
462
484
  ...session,
485
+ accessToken: session.accessToken ?? null,
463
486
  userId
464
487
  }).execute();
465
488
  this.logger.debug(inserted, "Inserted new session");
466
489
  });
467
490
  }
491
+ async getOnboardingSession(sessionId) {
492
+ const row = await this.db.selectFrom("sessions").selectAll().where("id", "=", sessionId).where("accessToken", "is", null).executeTakeFirst();
493
+ return row ? toSession(row) : void 0;
494
+ }
495
+ async upgradeOnboardingSession(sessionId, accessToken) {
496
+ const userId = this.assertConnected();
497
+ return this.db.transaction().execute(async (trx) => {
498
+ const row = await trx.selectFrom("sessions").selectAll().where("id", "=", sessionId).where("userId", "=", userId).where("accessToken", "is", null).executeTakeFirst();
499
+ if (!row) throw new Error("Onboarding session not found");
500
+ await trx.deleteFrom("sessions").where("userId", "=", userId).where("origin", "=", row.origin).where("id", "!=", sessionId).execute();
501
+ await trx.updateTable("sessions").set({ accessToken }).where("id", "=", sessionId).execute();
502
+ return toSession({
503
+ ...row,
504
+ accessToken
505
+ });
506
+ });
507
+ }
468
508
  async removeSession(accessToken) {
469
509
  const userId = this.assertConnected();
470
510
  await this.db.deleteFrom("sessions").where((eb) => eb.and([eb("userId", "=", userId), eb("accessToken", "=", accessToken)])).execute();
@@ -473,7 +513,7 @@ var StoreSql = class StoreSql {
473
513
  * Lists all pending transactions across all users.
474
514
  */
475
515
  async listAllPendingTransactions() {
476
- return (await this.db.selectFrom("transactions").selectAll().where("status", "=", "pending").execute()).map((row) => toTransaction(row));
516
+ return (await this.db.selectFrom("transactions").selectAll().where((eb) => eb.or([eb("status", "=", "pending"), eb("status", "=", "awaiting-signature")])).execute()).map((row) => toTransaction(row));
477
517
  }
478
518
  async getIdp(idpId) {
479
519
  const idps = await this.listIdps();
@@ -513,16 +553,24 @@ var StoreSql = class StoreSql {
513
553
  return network;
514
554
  }
515
555
  async getCurrentNetwork() {
556
+ const userId = this.assertConnected();
516
557
  const token = this.authContext?.accessToken;
517
- if (!token) throw new Error("No access token found in auth context");
518
- const session = await this.getSession(token);
519
- if (!session) throw new Error("No session found");
520
- const networkId = session.network;
558
+ const onboardingSessionId = this.authContext && !this.authContext.isApiKey ? this.authContext.sessionId : void 0;
559
+ if (!token && !onboardingSessionId) throw new Error("No session found");
560
+ 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();
561
+ if (!sessionRow) throw new Error("No session found");
562
+ const networkId = toSession(sessionRow).network;
521
563
  if (!networkId) throw new Error("No current network set in session");
522
564
  const network = (await this.listNetworks()).find((n) => n.id === networkId);
523
565
  if (!network) throw new Error(`Network "${networkId}" not found`);
524
566
  return network;
525
567
  }
568
+ async getNetworkForTokenVerification(networkId) {
569
+ const row = await this.db.selectFrom("networks").selectAll().where("id", "=", networkId).executeTakeFirst();
570
+ if (!row) return void 0;
571
+ const network = toNetwork(row);
572
+ return network.auth.method === "self_signed" ? network : void 0;
573
+ }
526
574
  async listNetworks() {
527
575
  let query = this.db.selectFrom("networks").selectAll();
528
576
  if (this.authContext) {
@@ -560,6 +608,7 @@ var StoreSql = class StoreSql {
560
608
  const payload = updates.payload ?? existing.payload;
561
609
  const signedAt = updates.signedAt ?? existing.signedAt;
562
610
  const externalTxId = updates.externalTxId ?? existing.externalTxId;
611
+ const failureReason = updates.failureReason ?? existing.failureReason;
563
612
  return {
564
613
  id: existing.id,
565
614
  commandId: existing.commandId,
@@ -570,7 +619,8 @@ var StoreSql = class StoreSql {
570
619
  ...payload !== void 0 && { payload },
571
620
  ...existing.createdAt !== void 0 && { createdAt: existing.createdAt },
572
621
  ...signedAt !== void 0 && { signedAt },
573
- ...externalTxId !== void 0 && { externalTxId }
622
+ ...externalTxId !== void 0 && { externalTxId },
623
+ ...failureReason !== void 0 && { failureReason }
574
624
  };
575
625
  }
576
626
  async setTransaction(transaction) {
@@ -578,23 +628,25 @@ var StoreSql = class StoreSql {
578
628
  const network = await this.getCurrentNetwork();
579
629
  await this.db.insertInto("transactions").values(fromTransaction(transaction, userId, network.id)).execute();
580
630
  }
581
- async setTransactionSigned(transactionId, signedAt, externalTxId) {
582
- await this.setTransactionStatus(transactionId, "signed", {
631
+ async setTransactionSigned(transactionId, signedAt, externalTxId, opts) {
632
+ return await this.setTransactionStatus(transactionId, "signed", {
583
633
  signedAt,
584
634
  ...externalTxId !== void 0 && { externalTxId }
585
- });
635
+ }, opts);
586
636
  }
587
- async setTransactionStatus(transactionId, status, updates = {}) {
637
+ async setTransactionStatus(transactionId, status, updates = {}, opts) {
588
638
  const userId = this.assertConnected();
589
639
  const network = await this.getCurrentNetwork();
590
640
  const existing = await this.getTransaction(transactionId);
591
641
  if (!existing) throw new Error(`Transaction not found with id: ${transactionId}`);
642
+ if (opts?.expectedStatus && existing.status !== opts.expectedStatus) return false;
592
643
  const updated = this.mergeTransactionStatusUpdate(existing, status, updates);
593
- await this.db.updateTable("transactions").set(fromTransaction(updated, userId, network.id)).where((eb) => eb.and([
644
+ return (await this.db.updateTable("transactions").set(fromTransaction(updated, userId, network.id)).where((eb) => eb.and([
594
645
  eb("id", "=", transactionId),
595
646
  eb("userId", "=", userId),
596
- eb("networkId", "=", network.id)
597
- ])).execute();
647
+ eb("networkId", "=", network.id),
648
+ ...opts?.expectedStatus ? [eb("status", "=", opts.expectedStatus)] : []
649
+ ])).executeTakeFirst()).numUpdatedRows > 0n;
598
650
  }
599
651
  async getTransaction(transactionId) {
600
652
  const userId = this.assertConnected();