@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.
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) => {
@@ -273,15 +275,18 @@ function _defineProperty(e, r, t) {
273
275
  //#endregion
274
276
  //#region src/store-sql.ts
275
277
  var StoreSql = class StoreSql {
276
- constructor(db, logger, authContext) {
278
+ constructor(db, authContext) {
277
279
  this.db = db;
278
- this.logger = logger;
280
+ _defineProperty(this, "logger", getLogger([
281
+ "core",
282
+ "wallet-store-sql",
283
+ "StoreSql"
284
+ ]));
279
285
  _defineProperty(this, "authContext", void 0);
280
- this.logger = logger.child({ component: "StoreSql" });
281
286
  this.authContext = authContext;
282
287
  }
283
288
  withAuthContext(context) {
284
- return new StoreSql(this.db, this.logger, context);
289
+ return new StoreSql(this.db, context);
285
290
  }
286
291
  assertConnected() {
287
292
  return assertConnected(this.authContext).userId;
@@ -365,6 +370,23 @@ var StoreSql = class StoreSql {
365
370
  ])).execute();
366
371
  });
367
372
  }
373
+ async setAuthPartyWallet(partyId) {
374
+ const network = await this.getCurrentNetwork();
375
+ const userId = this.assertConnected();
376
+ await this.db.transaction().execute(async (trx) => {
377
+ await trx.updateTable("wallets").set({ isAuthParty: 0 }).where((eb) => eb.and([
378
+ eb("networkId", "=", network.id),
379
+ eb("userId", "=", userId),
380
+ eb("isAuthParty", "=", 1)
381
+ ])).execute();
382
+ if (partyId === null) return;
383
+ await trx.updateTable("wallets").set({ isAuthParty: 1 }).where((eb) => eb.and([
384
+ eb("partyId", "=", partyId),
385
+ eb("networkId", "=", network.id),
386
+ eb("userId", "=", userId)
387
+ ])).execute();
388
+ });
389
+ }
368
390
  async addWallet(wallet) {
369
391
  this.logger.info("Adding wallet");
370
392
  const userId = this.assertConnected();
@@ -461,25 +483,29 @@ var StoreSql = class StoreSql {
461
483
  eb("origin", "=", session.origin),
462
484
  eb("accessToken", "is not", null)
463
485
  ])).execute();
464
- this.logger.debug(deleted, "Deleted old session");
486
+ this.logger.info("Deleted old session", { deleted });
465
487
  }
466
488
  const inserted = await trx.insertInto("sessions").values({
467
489
  ...session,
468
490
  accessToken: session.accessToken ?? null,
469
491
  userId
470
492
  }).execute();
471
- this.logger.debug(inserted, "Inserted new session");
493
+ this.logger.info("Inserted new session", { inserted });
472
494
  });
473
495
  }
474
- async getOnboardingSession(sessionId) {
496
+ async getSelfIssuedLoginSession(sessionId) {
475
497
  const row = await this.db.selectFrom("sessions").selectAll().where("id", "=", sessionId).where("accessToken", "is", null).executeTakeFirst();
476
498
  return row ? toSession(row) : void 0;
477
499
  }
478
- async upgradeOnboardingSession(sessionId, accessToken) {
500
+ async removeSelfIssuedLoginSession(sessionId) {
501
+ const userId = this.assertConnected();
502
+ await this.db.deleteFrom("sessions").where("id", "=", sessionId).where("userId", "=", userId).where("accessToken", "is", null).execute();
503
+ }
504
+ async upgradeSelfIssuedLoginSession(sessionId, accessToken) {
479
505
  const userId = this.assertConnected();
480
506
  return this.db.transaction().execute(async (trx) => {
481
507
  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");
508
+ if (!row) throw new Error("Self-issued login session not found");
483
509
  await trx.deleteFrom("sessions").where("userId", "=", userId).where("origin", "=", row.origin).where("id", "!=", sessionId).execute();
484
510
  await trx.updateTable("sessions").set({ accessToken }).where("id", "=", sessionId).execute();
485
511
  return toSession({
@@ -511,7 +537,7 @@ var StoreSql = class StoreSql {
511
537
  async updateIdp(idp) {
512
538
  await this.db.transaction().execute(async (trx) => {
513
539
  const idpEntry = fromIdp(idp);
514
- this.logger.info(idpEntry, "Updating idp table");
540
+ this.logger.info("Updating idp table", { idpEntry });
515
541
  await trx.updateTable("idps").set(idpEntry).where("id", "=", idp.id).execute();
516
542
  });
517
543
  }
@@ -538,9 +564,9 @@ var StoreSql = class StoreSql {
538
564
  async getCurrentNetwork() {
539
565
  const userId = this.assertConnected();
540
566
  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();
567
+ const selfIssuedLoginSessionId = this.authContext && !this.authContext.isApiKey ? this.authContext.sessionId : void 0;
568
+ if (!token && !selfIssuedLoginSessionId) throw providerErrors.unauthorized({ message: "No session found" });
569
+ 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
570
  if (!sessionRow) throw providerErrors.unauthorized({ message: "No session found" });
545
571
  const networkId = toSession(sessionRow).network;
546
572
  if (!networkId) throw new Error("No current network set in session");
@@ -568,7 +594,7 @@ var StoreSql = class StoreSql {
568
594
  const existing = (await trx.selectFrom("networks").selectAll().execute()).map(toNetwork);
569
595
  assertUniqueSelfIssuedAudience(existing, network, network.id);
570
596
  const networkEntry = fromNetwork(network, void 0);
571
- this.logger.info(networkEntry, "Updating network table");
597
+ this.logger.info("Updating network table", { networkEntry });
572
598
  await trx.updateTable("networks").set(networkEntry).where("id", "=", network.id).execute();
573
599
  });
574
600
  }
@@ -607,7 +633,7 @@ var StoreSql = class StoreSql {
607
633
  ...existing.createdAt !== void 0 && { createdAt: existing.createdAt },
608
634
  ...signedAt !== void 0 && { signedAt },
609
635
  ...externalTxId !== void 0 && { externalTxId },
610
- ...failureReason !== void 0 && { failureReason }
636
+ ...failureReason && { failureReason }
611
637
  };
612
638
  }
613
639
  async setTransaction(transaction) {
@@ -867,17 +893,18 @@ const migrator = (db) => {
867
893
  };
868
894
  //#endregion
869
895
  //#region src/bootstrap.ts
870
- async function bootstrap(db, config, logger) {
871
- const store = new StoreSql(db, logger);
896
+ async function bootstrap(db, config) {
897
+ const store = new StoreSql(db);
872
898
  await Promise.all(config.idps.map((idp) => store.addIdp(idp)));
873
899
  await Promise.all(config.networks.map((network) => store.addNetwork(network)));
874
900
  }
875
901
  //#endregion
876
902
  //#region src/cli.ts
877
- const logger = pino({
878
- name: "main",
879
- level: "debug"
880
- });
903
+ const logger = getLogger([
904
+ "core",
905
+ "wallet-store-sql",
906
+ "cli"
907
+ ]);
881
908
  function createCLI(config, bootstrapConfig) {
882
909
  const program = new Command();
883
910
  program.command("up").description("Run all pending migrations").action(async () => {
@@ -895,8 +922,8 @@ function createCLI(config, bootstrapConfig) {
895
922
  const umzug = migrator(db);
896
923
  const executed = await umzug.executed();
897
924
  const pending = await umzug.pending();
898
- console.log("Executed migrations:", executed);
899
- console.log("Pending migrations:", pending);
925
+ logger.info("Executed migrations: {executed}", { executed });
926
+ logger.info("Pending migrations: {pending}", { pending });
900
927
  await db.destroy();
901
928
  });
902
929
  program.command("reset").description("Rollback all migrations and reapply them").action(async () => {
@@ -910,7 +937,7 @@ function createCLI(config, bootstrapConfig) {
910
937
  program.command("bootstrap").description("Bootstrap DB from config").action(async () => {
911
938
  if (!bootstrapConfig) throw new Error("Bootstrap config (idps/networks) is required for the bootstrap command");
912
939
  const db = connection(config);
913
- await bootstrap(db, bootstrapConfig, logger);
940
+ await bootstrap(db, bootstrapConfig);
914
941
  await db.destroy();
915
942
  });
916
943
  return program;