@canton-network/core-wallet-store-sql 1.15.0 → 2.0.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.
@@ -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.cjs CHANGED
@@ -21,6 +21,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
21
21
  enumerable: true
22
22
  }) : target, mod));
23
23
  //#endregion
24
+ let _logtape_logtape = require("@logtape/logtape");
24
25
  let _canton_network_core_wallet_auth = require("@canton-network/core-wallet-auth");
25
26
  let _canton_network_core_rpc_errors = require("@canton-network/core-rpc-errors");
26
27
  let _canton_network_core_wallet_store = require("@canton-network/core-wallet-store");
@@ -31,7 +32,6 @@ let pg = require("pg");
31
32
  pg = __toESM(pg, 1);
32
33
  let umzug = require("umzug");
33
34
  let commander = require("commander");
34
- let pino = require("pino");
35
35
  //#region src/schema.ts
36
36
  const toIdp = (table) => {
37
37
  switch (table.type) {
@@ -182,7 +182,8 @@ const fromTransaction = (transaction, userId, networkId) => {
182
182
  userId,
183
183
  createdAt: transaction.createdAt?.toISOString() || null,
184
184
  signedAt: transaction.signedAt?.toISOString() || null,
185
- externalTxId: transaction.externalTxId ?? null
185
+ externalTxId: transaction.externalTxId ?? null,
186
+ failureReason: transaction.failureReason || null
186
187
  };
187
188
  };
188
189
  const toTransaction = (table) => {
@@ -200,6 +201,7 @@ const toTransaction = (table) => {
200
201
  if (table.createdAt) result.createdAt = new Date(table.createdAt);
201
202
  if (table.signedAt) result.signedAt = new Date(table.signedAt);
202
203
  if (table.externalTxId) result.externalTxId = table.externalTxId;
204
+ if (table.failureReason) result.failureReason = table.failureReason;
203
205
  return result;
204
206
  };
205
207
  const fromMessageRaw = (message, userId, networkId) => {
@@ -298,15 +300,18 @@ function _defineProperty(e, r, t) {
298
300
  //#endregion
299
301
  //#region src/store-sql.ts
300
302
  var StoreSql = class StoreSql {
301
- constructor(db, logger, authContext) {
303
+ constructor(db, authContext) {
302
304
  this.db = db;
303
- this.logger = logger;
305
+ _defineProperty(this, "logger", (0, _logtape_logtape.getLogger)([
306
+ "core",
307
+ "wallet-store-sql",
308
+ "StoreSql"
309
+ ]));
304
310
  _defineProperty(this, "authContext", void 0);
305
- this.logger = logger.child({ component: "StoreSql" });
306
311
  this.authContext = authContext;
307
312
  }
308
313
  withAuthContext(context) {
309
- return new StoreSql(this.db, this.logger, context);
314
+ return new StoreSql(this.db, context);
310
315
  }
311
316
  assertConnected() {
312
317
  return (0, _canton_network_core_wallet_auth.assertConnected)(this.authContext).userId;
@@ -390,6 +395,23 @@ var StoreSql = class StoreSql {
390
395
  ])).execute();
391
396
  });
392
397
  }
398
+ async setAuthPartyWallet(partyId) {
399
+ const network = await this.getCurrentNetwork();
400
+ const userId = this.assertConnected();
401
+ await this.db.transaction().execute(async (trx) => {
402
+ await trx.updateTable("wallets").set({ isAuthParty: 0 }).where((eb) => eb.and([
403
+ eb("networkId", "=", network.id),
404
+ eb("userId", "=", userId),
405
+ eb("isAuthParty", "=", 1)
406
+ ])).execute();
407
+ if (partyId === null) return;
408
+ await trx.updateTable("wallets").set({ isAuthParty: 1 }).where((eb) => eb.and([
409
+ eb("partyId", "=", partyId),
410
+ eb("networkId", "=", network.id),
411
+ eb("userId", "=", userId)
412
+ ])).execute();
413
+ });
414
+ }
393
415
  async addWallet(wallet) {
394
416
  this.logger.info("Adding wallet");
395
417
  const userId = this.assertConnected();
@@ -486,25 +508,29 @@ var StoreSql = class StoreSql {
486
508
  eb("origin", "=", session.origin),
487
509
  eb("accessToken", "is not", null)
488
510
  ])).execute();
489
- this.logger.debug(deleted, "Deleted old session");
511
+ this.logger.info("Deleted old session", { deleted });
490
512
  }
491
513
  const inserted = await trx.insertInto("sessions").values({
492
514
  ...session,
493
515
  accessToken: session.accessToken ?? null,
494
516
  userId
495
517
  }).execute();
496
- this.logger.debug(inserted, "Inserted new session");
518
+ this.logger.info("Inserted new session", { inserted });
497
519
  });
498
520
  }
499
- async getOnboardingSession(sessionId) {
521
+ async getSelfIssuedLoginSession(sessionId) {
500
522
  const row = await this.db.selectFrom("sessions").selectAll().where("id", "=", sessionId).where("accessToken", "is", null).executeTakeFirst();
501
523
  return row ? toSession(row) : void 0;
502
524
  }
503
- async upgradeOnboardingSession(sessionId, accessToken) {
525
+ async removeSelfIssuedLoginSession(sessionId) {
526
+ const userId = this.assertConnected();
527
+ await this.db.deleteFrom("sessions").where("id", "=", sessionId).where("userId", "=", userId).where("accessToken", "is", null).execute();
528
+ }
529
+ async upgradeSelfIssuedLoginSession(sessionId, accessToken) {
504
530
  const userId = this.assertConnected();
505
531
  return this.db.transaction().execute(async (trx) => {
506
532
  const row = await trx.selectFrom("sessions").selectAll().where("id", "=", sessionId).where("userId", "=", userId).where("accessToken", "is", null).executeTakeFirst();
507
- if (!row) throw new Error("Onboarding session not found");
533
+ if (!row) throw new Error("Self-issued login session not found");
508
534
  await trx.deleteFrom("sessions").where("userId", "=", userId).where("origin", "=", row.origin).where("id", "!=", sessionId).execute();
509
535
  await trx.updateTable("sessions").set({ accessToken }).where("id", "=", sessionId).execute();
510
536
  return toSession({
@@ -536,7 +562,7 @@ var StoreSql = class StoreSql {
536
562
  async updateIdp(idp) {
537
563
  await this.db.transaction().execute(async (trx) => {
538
564
  const idpEntry = fromIdp(idp);
539
- this.logger.info(idpEntry, "Updating idp table");
565
+ this.logger.info("Updating idp table", { idpEntry });
540
566
  await trx.updateTable("idps").set(idpEntry).where("id", "=", idp.id).execute();
541
567
  });
542
568
  }
@@ -563,9 +589,9 @@ var StoreSql = class StoreSql {
563
589
  async getCurrentNetwork() {
564
590
  const userId = this.assertConnected();
565
591
  const token = this.authContext?.accessToken;
566
- const onboardingSessionId = this.authContext && !this.authContext.isApiKey ? this.authContext.sessionId : void 0;
567
- if (!token && !onboardingSessionId) throw _canton_network_core_rpc_errors.providerErrors.unauthorized({ message: "No session found" });
568
- 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();
592
+ const selfIssuedLoginSessionId = this.authContext && !this.authContext.isApiKey ? this.authContext.sessionId : void 0;
593
+ if (!token && !selfIssuedLoginSessionId) throw _canton_network_core_rpc_errors.providerErrors.unauthorized({ message: "No session found" });
594
+ 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();
569
595
  if (!sessionRow) throw _canton_network_core_rpc_errors.providerErrors.unauthorized({ message: "No session found" });
570
596
  const networkId = toSession(sessionRow).network;
571
597
  if (!networkId) throw new Error("No current network set in session");
@@ -593,7 +619,7 @@ var StoreSql = class StoreSql {
593
619
  const existing = (await trx.selectFrom("networks").selectAll().execute()).map(toNetwork);
594
620
  (0, _canton_network_core_wallet_store.assertUniqueSelfIssuedAudience)(existing, network, network.id);
595
621
  const networkEntry = fromNetwork(network, void 0);
596
- this.logger.info(networkEntry, "Updating network table");
622
+ this.logger.info("Updating network table", { networkEntry });
597
623
  await trx.updateTable("networks").set(networkEntry).where("id", "=", network.id).execute();
598
624
  });
599
625
  }
@@ -632,7 +658,7 @@ var StoreSql = class StoreSql {
632
658
  ...existing.createdAt !== void 0 && { createdAt: existing.createdAt },
633
659
  ...signedAt !== void 0 && { signedAt },
634
660
  ...externalTxId !== void 0 && { externalTxId },
635
- ...failureReason !== void 0 && { failureReason }
661
+ ...failureReason && { failureReason }
636
662
  };
637
663
  }
638
664
  async setTransaction(transaction) {
@@ -892,17 +918,18 @@ const migrator = (db) => {
892
918
  };
893
919
  //#endregion
894
920
  //#region src/bootstrap.ts
895
- async function bootstrap(db, config, logger) {
896
- const store = new StoreSql(db, logger);
921
+ async function bootstrap(db, config) {
922
+ const store = new StoreSql(db);
897
923
  await Promise.all(config.idps.map((idp) => store.addIdp(idp)));
898
924
  await Promise.all(config.networks.map((network) => store.addNetwork(network)));
899
925
  }
900
926
  //#endregion
901
927
  //#region src/cli.ts
902
- const logger = (0, pino.pino)({
903
- name: "main",
904
- level: "debug"
905
- });
928
+ const logger = (0, _logtape_logtape.getLogger)([
929
+ "core",
930
+ "wallet-store-sql",
931
+ "cli"
932
+ ]);
906
933
  function createCLI(config, bootstrapConfig) {
907
934
  const program = new commander.Command();
908
935
  program.command("up").description("Run all pending migrations").action(async () => {
@@ -920,8 +947,8 @@ function createCLI(config, bootstrapConfig) {
920
947
  const umzug = migrator(db);
921
948
  const executed = await umzug.executed();
922
949
  const pending = await umzug.pending();
923
- console.log("Executed migrations:", executed);
924
- console.log("Pending migrations:", pending);
950
+ logger.info("Executed migrations: {executed}", { executed });
951
+ logger.info("Pending migrations: {pending}", { pending });
925
952
  await db.destroy();
926
953
  });
927
954
  program.command("reset").description("Rollback all migrations and reapply them").action(async () => {
@@ -935,7 +962,7 @@ function createCLI(config, bootstrapConfig) {
935
962
  program.command("bootstrap").description("Bootstrap DB from config").action(async () => {
936
963
  if (!bootstrapConfig) throw new Error("Bootstrap config (idps/networks) is required for the bootstrap command");
937
964
  const db = connection(config);
938
- await bootstrap(db, bootstrapConfig, logger);
965
+ await bootstrap(db, bootstrapConfig);
939
966
  await db.destroy();
940
967
  });
941
968
  return program;