@canton-network/core-wallet-store-sql 1.15.0 → 2.1.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 (50) hide show
  1. package/dist/bootstrap.d.ts +1 -2
  2. package/dist/bootstrap.d.ts.map +1 -1
  3. package/dist/index.js +67 -32
  4. package/dist/index.js.map +1 -1
  5. package/dist/migrations/011-add-transaction-id-primary-key.d.ts.map +1 -1
  6. package/dist/migrations/011-add-transaction-id-primary-key.js +1 -2
  7. package/dist/migrations/020-add-message-external-tx-id-and-failure-reason.d.ts +5 -0
  8. package/dist/migrations/020-add-message-external-tx-id-and-failure-reason.d.ts.map +1 -0
  9. package/dist/migrations/020-add-message-external-tx-id-and-failure-reason.js +22 -0
  10. package/dist/migrator.d.ts.map +1 -1
  11. package/dist/schema.d.ts +3 -0
  12. package/dist/schema.d.ts.map +1 -1
  13. package/dist/schema.js +12 -0
  14. package/dist/store-sql.d.ts +5 -4
  15. package/dist/store-sql.d.ts.map +1 -1
  16. package/package.json +24 -26
  17. package/dist/index.cjs +0 -950
  18. package/dist/index.cjs.map +0 -1
  19. package/dist/migrations-test/global-setup.d.ts +0 -9
  20. package/dist/migrations-test/global-setup.d.ts.map +0 -1
  21. package/dist/migrations-test/helpers.d.ts +0 -32
  22. package/dist/migrations-test/helpers.d.ts.map +0 -1
  23. package/dist/migrations-test/seeds/001-init.d.ts +0 -43
  24. package/dist/migrations-test/seeds/001-init.d.ts.map +0 -1
  25. package/dist/migrations-test/seeds/002-transaction-timestamps.d.ts +0 -13
  26. package/dist/migrations-test/seeds/002-transaction-timestamps.d.ts.map +0 -1
  27. package/dist/migrations-test/seeds/003-transaction-origin.d.ts +0 -14
  28. package/dist/migrations-test/seeds/003-transaction-origin.d.ts.map +0 -1
  29. package/dist/migrations-test/seeds/004-add-session-id.d.ts +0 -9
  30. package/dist/migrations-test/seeds/004-add-session-id.d.ts.map +0 -1
  31. package/dist/migrations-test/seeds/005-add-wallet-disabled-reason.d.ts +0 -16
  32. package/dist/migrations-test/seeds/005-add-wallet-disabled-reason.d.ts.map +0 -1
  33. package/dist/migrations-test/seeds/008-transaction-external-tx-id.d.ts +0 -15
  34. package/dist/migrations-test/seeds/008-transaction-external-tx-id.d.ts.map +0 -1
  35. package/dist/migrations-test/seeds/009-add-wallet-rights-columns.d.ts +0 -14
  36. package/dist/migrations-test/seeds/009-add-wallet-rights-columns.d.ts.map +0 -1
  37. package/dist/migrations-test/seeds/010-transaction-with-network.d.ts +0 -16
  38. package/dist/migrations-test/seeds/010-transaction-with-network.d.ts.map +0 -1
  39. package/dist/migrations-test/seeds/012-add-messages-to-sign.d.ts +0 -16
  40. package/dist/migrations-test/seeds/012-add-messages-to-sign.d.ts.map +0 -1
  41. package/dist/migrations-test/seeds/013-add-api-key.d.ts +0 -13
  42. package/dist/migrations-test/seeds/013-add-api-key.d.ts.map +0 -1
  43. package/dist/migrations-test/seeds/014-add-service-account-column.d.ts +0 -15
  44. package/dist/migrations-test/seeds/014-add-service-account-column.d.ts.map +0 -1
  45. package/dist/migrations-test/seeds/016-add-failure-reason-and-awaiting-signature.d.ts +0 -18
  46. package/dist/migrations-test/seeds/016-add-failure-reason-and-awaiting-signature.d.ts.map +0 -1
  47. package/dist/migrations-test/seeds/017-make-idp-issuer-nullable.d.ts +0 -9
  48. package/dist/migrations-test/seeds/017-make-idp-issuer-nullable.d.ts.map +0 -1
  49. package/dist/migrations-test/seeds/019-make-session-access-token-nullable.d.ts +0 -10
  50. package/dist/migrations-test/seeds/019-make-session-access-token-nullable.d.ts.map +0 -1
@@ -1,6 +1,5 @@
1
1
  import type { Kysely } from 'kysely';
2
2
  import type { BootstrapConfig } from '@canton-network/core-wallet-store';
3
- import type { Logger } from 'pino';
4
3
  import type { DB } from './schema';
5
- export declare function bootstrap(db: Kysely<DB>, config: BootstrapConfig, logger: Logger): Promise<void>;
4
+ export declare function bootstrap(db: Kysely<DB>, config: BootstrapConfig): Promise<void>;
6
5
  //# sourceMappingURL=bootstrap.d.ts.map
@@ -1 +1 @@
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"}
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,EAAE,EAAE,MAAM,UAAU,CAAA;AAElC,wBAAsB,SAAS,CAC3B,EAAE,EAAE,MAAM,CAAC,EAAE,CAAC,EACd,MAAM,EAAE,eAAe,GACxB,OAAO,CAAC,IAAI,CAAC,CAUf"}
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { getLogger } from "@logtape/logtape";
1
2
  import { assertConnected, authSchema } from "@canton-network/core-wallet-auth";
2
3
  import { providerErrors } from "@canton-network/core-rpc-errors";
3
4
  import { PartyLevelRight, UserLevelRight, assertUniqueSelfIssuedAudience, resolveSelfIssuedNetworkByAudiences } from "@canton-network/core-wallet-store";
@@ -6,7 +7,6 @@ import Database from "better-sqlite3";
6
7
  import pg from "pg";
7
8
  import { Umzug } from "umzug";
8
9
  import { Command } from "commander";
9
- import { pino } from "pino";
10
10
  //#region src/schema.ts
11
11
  const toIdp = (table) => {
12
12
  switch (table.type) {
@@ -157,7 +157,8 @@ const fromTransaction = (transaction, userId, networkId) => {
157
157
  userId,
158
158
  createdAt: transaction.createdAt?.toISOString() || null,
159
159
  signedAt: transaction.signedAt?.toISOString() || null,
160
- externalTxId: transaction.externalTxId ?? null
160
+ externalTxId: transaction.externalTxId ?? null,
161
+ failureReason: transaction.failureReason || null
161
162
  };
162
163
  };
163
164
  const toTransaction = (table) => {
@@ -175,6 +176,7 @@ const toTransaction = (table) => {
175
176
  if (table.createdAt) result.createdAt = new Date(table.createdAt);
176
177
  if (table.signedAt) result.signedAt = new Date(table.signedAt);
177
178
  if (table.externalTxId) result.externalTxId = table.externalTxId;
179
+ if (table.failureReason) result.failureReason = table.failureReason;
178
180
  return result;
179
181
  };
180
182
  const fromMessageRaw = (message, userId, networkId) => {
@@ -190,7 +192,9 @@ const fromMessageRaw = (message, userId, networkId) => {
190
192
  networkId,
191
193
  createdAt: message.createdAt.toISOString(),
192
194
  signedAt: message.signedAt?.toISOString() || null,
193
- signature: message.signature ?? null
195
+ signature: message.signature ?? null,
196
+ externalTxId: message.externalTxId ?? null,
197
+ failureReason: message.failureReason ?? null
194
198
  };
195
199
  };
196
200
  const toMessageRaw = (table) => {
@@ -206,6 +210,8 @@ const toMessageRaw = (table) => {
206
210
  };
207
211
  if (table.signedAt) result.signedAt = new Date(table.signedAt);
208
212
  if (table.signature) result.signature = table.signature;
213
+ if (table.externalTxId) result.externalTxId = table.externalTxId;
214
+ if (table.failureReason) result.failureReason = table.failureReason;
209
215
  return result;
210
216
  };
211
217
  const fromApiKey = (table) => {
@@ -233,7 +239,7 @@ const toApiKey = (apiKey) => {
233
239
  };
234
240
  };
235
241
  //#endregion
236
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/typeof.js
242
+ //#region \0@oxc-project+runtime@0.154.0/helpers/esm/typeof.js
237
243
  function _typeof(o) {
238
244
  "@babel/helpers - typeof";
239
245
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -243,7 +249,7 @@ function _typeof(o) {
243
249
  }, _typeof(o);
244
250
  }
245
251
  //#endregion
246
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/toPrimitive.js
252
+ //#region \0@oxc-project+runtime@0.154.0/helpers/esm/toPrimitive.js
247
253
  function toPrimitive(t, r) {
248
254
  if ("object" != _typeof(t) || !t) return t;
249
255
  var e = t[Symbol.toPrimitive];
@@ -255,13 +261,13 @@ function toPrimitive(t, r) {
255
261
  return ("string" === r ? String : Number)(t);
256
262
  }
257
263
  //#endregion
258
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/toPropertyKey.js
264
+ //#region \0@oxc-project+runtime@0.154.0/helpers/esm/toPropertyKey.js
259
265
  function toPropertyKey(t) {
260
266
  var i = toPrimitive(t, "string");
261
267
  return "symbol" == _typeof(i) ? i : i + "";
262
268
  }
263
269
  //#endregion
264
- //#region \0@oxc-project+runtime@0.153.0/helpers/esm/defineProperty.js
270
+ //#region \0@oxc-project+runtime@0.154.0/helpers/esm/defineProperty.js
265
271
  function _defineProperty(e, r, t) {
266
272
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
267
273
  value: t,
@@ -273,15 +279,18 @@ function _defineProperty(e, r, t) {
273
279
  //#endregion
274
280
  //#region src/store-sql.ts
275
281
  var StoreSql = class StoreSql {
276
- constructor(db, logger, authContext) {
282
+ constructor(db, authContext) {
277
283
  this.db = db;
278
- this.logger = logger;
284
+ _defineProperty(this, "logger", getLogger([
285
+ "core",
286
+ "wallet-store-sql",
287
+ "StoreSql"
288
+ ]));
279
289
  _defineProperty(this, "authContext", void 0);
280
- this.logger = logger.child({ component: "StoreSql" });
281
290
  this.authContext = authContext;
282
291
  }
283
292
  withAuthContext(context) {
284
- return new StoreSql(this.db, this.logger, context);
293
+ return new StoreSql(this.db, context);
285
294
  }
286
295
  assertConnected() {
287
296
  return assertConnected(this.authContext).userId;
@@ -365,6 +374,23 @@ var StoreSql = class StoreSql {
365
374
  ])).execute();
366
375
  });
367
376
  }
377
+ async setAuthPartyWallet(partyId) {
378
+ const network = await this.getCurrentNetwork();
379
+ const userId = this.assertConnected();
380
+ await this.db.transaction().execute(async (trx) => {
381
+ await trx.updateTable("wallets").set({ isAuthParty: 0 }).where((eb) => eb.and([
382
+ eb("networkId", "=", network.id),
383
+ eb("userId", "=", userId),
384
+ eb("isAuthParty", "=", 1)
385
+ ])).execute();
386
+ if (partyId === null) return;
387
+ await trx.updateTable("wallets").set({ isAuthParty: 1 }).where((eb) => eb.and([
388
+ eb("partyId", "=", partyId),
389
+ eb("networkId", "=", network.id),
390
+ eb("userId", "=", userId)
391
+ ])).execute();
392
+ });
393
+ }
368
394
  async addWallet(wallet) {
369
395
  this.logger.info("Adding wallet");
370
396
  const userId = this.assertConnected();
@@ -461,25 +487,29 @@ var StoreSql = class StoreSql {
461
487
  eb("origin", "=", session.origin),
462
488
  eb("accessToken", "is not", null)
463
489
  ])).execute();
464
- this.logger.debug(deleted, "Deleted old session");
490
+ this.logger.info("Deleted old session", { deleted });
465
491
  }
466
492
  const inserted = await trx.insertInto("sessions").values({
467
493
  ...session,
468
494
  accessToken: session.accessToken ?? null,
469
495
  userId
470
496
  }).execute();
471
- this.logger.debug(inserted, "Inserted new session");
497
+ this.logger.info("Inserted new session", { inserted });
472
498
  });
473
499
  }
474
- async getOnboardingSession(sessionId) {
500
+ async getSelfIssuedLoginSession(sessionId) {
475
501
  const row = await this.db.selectFrom("sessions").selectAll().where("id", "=", sessionId).where("accessToken", "is", null).executeTakeFirst();
476
502
  return row ? toSession(row) : void 0;
477
503
  }
478
- async upgradeOnboardingSession(sessionId, accessToken) {
504
+ async removeSelfIssuedLoginSession(sessionId) {
505
+ const userId = this.assertConnected();
506
+ await this.db.deleteFrom("sessions").where("id", "=", sessionId).where("userId", "=", userId).where("accessToken", "is", null).execute();
507
+ }
508
+ async upgradeSelfIssuedLoginSession(sessionId, accessToken) {
479
509
  const userId = this.assertConnected();
480
510
  return this.db.transaction().execute(async (trx) => {
481
511
  const row = await trx.selectFrom("sessions").selectAll().where("id", "=", sessionId).where("userId", "=", userId).where("accessToken", "is", null).executeTakeFirst();
482
- if (!row) throw new Error("Onboarding session not found");
512
+ if (!row) throw new Error("Self-issued login session not found");
483
513
  await trx.deleteFrom("sessions").where("userId", "=", userId).where("origin", "=", row.origin).where("id", "!=", sessionId).execute();
484
514
  await trx.updateTable("sessions").set({ accessToken }).where("id", "=", sessionId).execute();
485
515
  return toSession({
@@ -511,7 +541,7 @@ var StoreSql = class StoreSql {
511
541
  async updateIdp(idp) {
512
542
  await this.db.transaction().execute(async (trx) => {
513
543
  const idpEntry = fromIdp(idp);
514
- this.logger.info(idpEntry, "Updating idp table");
544
+ this.logger.info("Updating idp table", { idpEntry });
515
545
  await trx.updateTable("idps").set(idpEntry).where("id", "=", idp.id).execute();
516
546
  });
517
547
  }
@@ -538,9 +568,9 @@ var StoreSql = class StoreSql {
538
568
  async getCurrentNetwork() {
539
569
  const userId = this.assertConnected();
540
570
  const token = this.authContext?.accessToken;
541
- const onboardingSessionId = this.authContext && !this.authContext.isApiKey ? this.authContext.sessionId : void 0;
542
- if (!token && !onboardingSessionId) throw providerErrors.unauthorized({ message: "No session found" });
543
- 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();
571
+ const selfIssuedLoginSessionId = this.authContext && !this.authContext.isApiKey ? this.authContext.sessionId : void 0;
572
+ if (!token && !selfIssuedLoginSessionId) throw providerErrors.unauthorized({ message: "No session found" });
573
+ const sessionRow = await this.db.selectFrom("sessions").selectAll().where("userId", "=", userId).where((eb) => token ? eb("accessToken", "=", token) : eb.and([eb("id", "=", selfIssuedLoginSessionId), eb("accessToken", "is", null)])).executeTakeFirst();
544
574
  if (!sessionRow) throw providerErrors.unauthorized({ message: "No session found" });
545
575
  const networkId = toSession(sessionRow).network;
546
576
  if (!networkId) throw new Error("No current network set in session");
@@ -568,7 +598,7 @@ var StoreSql = class StoreSql {
568
598
  const existing = (await trx.selectFrom("networks").selectAll().execute()).map(toNetwork);
569
599
  assertUniqueSelfIssuedAudience(existing, network, network.id);
570
600
  const networkEntry = fromNetwork(network, void 0);
571
- this.logger.info(networkEntry, "Updating network table");
601
+ this.logger.info("Updating network table", { networkEntry });
572
602
  await trx.updateTable("networks").set(networkEntry).where("id", "=", network.id).execute();
573
603
  });
574
604
  }
@@ -607,7 +637,7 @@ var StoreSql = class StoreSql {
607
637
  ...existing.createdAt !== void 0 && { createdAt: existing.createdAt },
608
638
  ...signedAt !== void 0 && { signedAt },
609
639
  ...externalTxId !== void 0 && { externalTxId },
610
- ...failureReason !== void 0 && { failureReason }
640
+ ...failureReason && { failureReason }
611
641
  };
612
642
  }
613
643
  async setTransaction(transaction) {
@@ -715,11 +745,15 @@ var StoreSql = class StoreSql {
715
745
  mergeMessageRawStatusUpdate(existing, status, updates = {}) {
716
746
  const signedAt = updates.signedAt ?? existing.signedAt;
717
747
  const signature = updates.signature ?? existing.signature;
748
+ const externalTxId = updates.externalTxId ?? existing.externalTxId;
749
+ const failureReason = updates.failureReason ?? existing.failureReason;
718
750
  return {
719
751
  ...existing,
720
752
  status,
721
753
  ...signedAt !== void 0 && { signedAt },
722
- ...signature !== void 0 && { signature }
754
+ ...signature !== void 0 && { signature },
755
+ ...externalTxId !== void 0 && { externalTxId },
756
+ ...failureReason !== void 0 && { failureReason }
723
757
  };
724
758
  }
725
759
  async setMessageRaw(message) {
@@ -867,17 +901,18 @@ const migrator = (db) => {
867
901
  };
868
902
  //#endregion
869
903
  //#region src/bootstrap.ts
870
- async function bootstrap(db, config, logger) {
871
- const store = new StoreSql(db, logger);
904
+ async function bootstrap(db, config) {
905
+ const store = new StoreSql(db);
872
906
  await Promise.all(config.idps.map((idp) => store.addIdp(idp)));
873
907
  await Promise.all(config.networks.map((network) => store.addNetwork(network)));
874
908
  }
875
909
  //#endregion
876
910
  //#region src/cli.ts
877
- const logger = pino({
878
- name: "main",
879
- level: "debug"
880
- });
911
+ const logger = getLogger([
912
+ "core",
913
+ "wallet-store-sql",
914
+ "cli"
915
+ ]);
881
916
  function createCLI(config, bootstrapConfig) {
882
917
  const program = new Command();
883
918
  program.command("up").description("Run all pending migrations").action(async () => {
@@ -895,8 +930,8 @@ function createCLI(config, bootstrapConfig) {
895
930
  const umzug = migrator(db);
896
931
  const executed = await umzug.executed();
897
932
  const pending = await umzug.pending();
898
- console.log("Executed migrations:", executed);
899
- console.log("Pending migrations:", pending);
933
+ logger.info("Executed migrations: {executed}", { executed });
934
+ logger.info("Pending migrations: {pending}", { pending });
900
935
  await db.destroy();
901
936
  });
902
937
  program.command("reset").description("Rollback all migrations and reapply them").action(async () => {
@@ -910,7 +945,7 @@ function createCLI(config, bootstrapConfig) {
910
945
  program.command("bootstrap").description("Bootstrap DB from config").action(async () => {
911
946
  if (!bootstrapConfig) throw new Error("Bootstrap config (idps/networks) is required for the bootstrap command");
912
947
  const db = connection(config);
913
- await bootstrap(db, bootstrapConfig, logger);
948
+ await bootstrap(db, bootstrapConfig);
914
949
  await db.destroy();
915
950
  });
916
951
  return program;