@canton-network/core-wallet-store-sql 1.14.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/bootstrap.d.ts +1 -2
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/index.cjs +72 -33
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +72 -33
- package/dist/index.js.map +1 -1
- package/dist/migrations/016-add-failure-reason-and-awaiting-signature.d.ts +2 -2
- package/dist/migrations/016-add-failure-reason-and-awaiting-signature.d.ts.map +1 -1
- package/dist/migrations-test/seeds/016-add-failure-reason-and-awaiting-signature.d.ts +2 -2
- package/dist/migrations-test/seeds/016-add-failure-reason-and-awaiting-signature.d.ts.map +1 -1
- package/dist/schema.d.ts +1 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +4 -0
- package/dist/store-sql.d.ts +6 -4
- package/dist/store-sql.d.ts.map +1 -1
- package/package.json +19 -12
package/dist/index.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
import { getLogger } from "@logtape/logtape";
|
|
1
2
|
import { assertConnected, authSchema } from "@canton-network/core-wallet-auth";
|
|
3
|
+
import { providerErrors } from "@canton-network/core-rpc-errors";
|
|
4
|
+
import { PartyLevelRight, UserLevelRight, assertUniqueSelfIssuedAudience, resolveSelfIssuedNetworkByAudiences } from "@canton-network/core-wallet-store";
|
|
2
5
|
import { CamelCasePlugin, Kysely, PostgresDialect, SqliteDialect, sql } from "kysely";
|
|
3
6
|
import Database from "better-sqlite3";
|
|
4
|
-
import { PartyLevelRight, UserLevelRight } from "@canton-network/core-wallet-store";
|
|
5
7
|
import pg from "pg";
|
|
6
8
|
import { Umzug } from "umzug";
|
|
7
9
|
import { Command } from "commander";
|
|
8
|
-
import { pino } from "pino";
|
|
9
10
|
//#region src/schema.ts
|
|
10
11
|
const toIdp = (table) => {
|
|
11
12
|
switch (table.type) {
|
|
@@ -156,7 +157,8 @@ const fromTransaction = (transaction, userId, networkId) => {
|
|
|
156
157
|
userId,
|
|
157
158
|
createdAt: transaction.createdAt?.toISOString() || null,
|
|
158
159
|
signedAt: transaction.signedAt?.toISOString() || null,
|
|
159
|
-
externalTxId: transaction.externalTxId ?? null
|
|
160
|
+
externalTxId: transaction.externalTxId ?? null,
|
|
161
|
+
failureReason: transaction.failureReason || null
|
|
160
162
|
};
|
|
161
163
|
};
|
|
162
164
|
const toTransaction = (table) => {
|
|
@@ -174,6 +176,7 @@ const toTransaction = (table) => {
|
|
|
174
176
|
if (table.createdAt) result.createdAt = new Date(table.createdAt);
|
|
175
177
|
if (table.signedAt) result.signedAt = new Date(table.signedAt);
|
|
176
178
|
if (table.externalTxId) result.externalTxId = table.externalTxId;
|
|
179
|
+
if (table.failureReason) result.failureReason = table.failureReason;
|
|
177
180
|
return result;
|
|
178
181
|
};
|
|
179
182
|
const fromMessageRaw = (message, userId, networkId) => {
|
|
@@ -232,7 +235,7 @@ const toApiKey = (apiKey) => {
|
|
|
232
235
|
};
|
|
233
236
|
};
|
|
234
237
|
//#endregion
|
|
235
|
-
//#region \0@oxc-project+runtime@0.
|
|
238
|
+
//#region \0@oxc-project+runtime@0.153.0/helpers/esm/typeof.js
|
|
236
239
|
function _typeof(o) {
|
|
237
240
|
"@babel/helpers - typeof";
|
|
238
241
|
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
@@ -242,7 +245,7 @@ function _typeof(o) {
|
|
|
242
245
|
}, _typeof(o);
|
|
243
246
|
}
|
|
244
247
|
//#endregion
|
|
245
|
-
//#region \0@oxc-project+runtime@0.
|
|
248
|
+
//#region \0@oxc-project+runtime@0.153.0/helpers/esm/toPrimitive.js
|
|
246
249
|
function toPrimitive(t, r) {
|
|
247
250
|
if ("object" != _typeof(t) || !t) return t;
|
|
248
251
|
var e = t[Symbol.toPrimitive];
|
|
@@ -254,13 +257,13 @@ function toPrimitive(t, r) {
|
|
|
254
257
|
return ("string" === r ? String : Number)(t);
|
|
255
258
|
}
|
|
256
259
|
//#endregion
|
|
257
|
-
//#region \0@oxc-project+runtime@0.
|
|
260
|
+
//#region \0@oxc-project+runtime@0.153.0/helpers/esm/toPropertyKey.js
|
|
258
261
|
function toPropertyKey(t) {
|
|
259
262
|
var i = toPrimitive(t, "string");
|
|
260
263
|
return "symbol" == _typeof(i) ? i : i + "";
|
|
261
264
|
}
|
|
262
265
|
//#endregion
|
|
263
|
-
//#region \0@oxc-project+runtime@0.
|
|
266
|
+
//#region \0@oxc-project+runtime@0.153.0/helpers/esm/defineProperty.js
|
|
264
267
|
function _defineProperty(e, r, t) {
|
|
265
268
|
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
266
269
|
value: t,
|
|
@@ -272,15 +275,18 @@ function _defineProperty(e, r, t) {
|
|
|
272
275
|
//#endregion
|
|
273
276
|
//#region src/store-sql.ts
|
|
274
277
|
var StoreSql = class StoreSql {
|
|
275
|
-
constructor(db,
|
|
278
|
+
constructor(db, authContext) {
|
|
276
279
|
this.db = db;
|
|
277
|
-
this
|
|
280
|
+
_defineProperty(this, "logger", getLogger([
|
|
281
|
+
"core",
|
|
282
|
+
"wallet-store-sql",
|
|
283
|
+
"StoreSql"
|
|
284
|
+
]));
|
|
278
285
|
_defineProperty(this, "authContext", void 0);
|
|
279
|
-
this.logger = logger.child({ component: "StoreSql" });
|
|
280
286
|
this.authContext = authContext;
|
|
281
287
|
}
|
|
282
288
|
withAuthContext(context) {
|
|
283
|
-
return new StoreSql(this.db,
|
|
289
|
+
return new StoreSql(this.db, context);
|
|
284
290
|
}
|
|
285
291
|
assertConnected() {
|
|
286
292
|
return assertConnected(this.authContext).userId;
|
|
@@ -335,6 +341,13 @@ var StoreSql = class StoreSql {
|
|
|
335
341
|
rights: rightRows.map(({ right }) => fromPartyRight(right)).filter((right) => right !== void 0)
|
|
336
342
|
};
|
|
337
343
|
}
|
|
344
|
+
async getWalletByJwt(userId, partyId, audiences) {
|
|
345
|
+
const networks = (await this.db.selectFrom("networks").selectAll().execute()).map(toNetwork);
|
|
346
|
+
const network = resolveSelfIssuedNetworkByAudiences(networks, audiences);
|
|
347
|
+
if (!network) return;
|
|
348
|
+
const row = await this.db.selectFrom("wallets").selectAll().where("userId", "=", userId).where("partyId", "=", partyId).where("networkId", "=", network.id).executeTakeFirst();
|
|
349
|
+
return row ? toWallet(row) : void 0;
|
|
350
|
+
}
|
|
338
351
|
async getPrimaryWallet() {
|
|
339
352
|
return (await this.getWallets()).find((w) => w.primary === true);
|
|
340
353
|
}
|
|
@@ -357,6 +370,23 @@ var StoreSql = class StoreSql {
|
|
|
357
370
|
])).execute();
|
|
358
371
|
});
|
|
359
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
|
+
}
|
|
360
390
|
async addWallet(wallet) {
|
|
361
391
|
this.logger.info("Adding wallet");
|
|
362
392
|
const userId = this.assertConnected();
|
|
@@ -453,25 +483,29 @@ var StoreSql = class StoreSql {
|
|
|
453
483
|
eb("origin", "=", session.origin),
|
|
454
484
|
eb("accessToken", "is not", null)
|
|
455
485
|
])).execute();
|
|
456
|
-
this.logger.
|
|
486
|
+
this.logger.info("Deleted old session", { deleted });
|
|
457
487
|
}
|
|
458
488
|
const inserted = await trx.insertInto("sessions").values({
|
|
459
489
|
...session,
|
|
460
490
|
accessToken: session.accessToken ?? null,
|
|
461
491
|
userId
|
|
462
492
|
}).execute();
|
|
463
|
-
this.logger.
|
|
493
|
+
this.logger.info("Inserted new session", { inserted });
|
|
464
494
|
});
|
|
465
495
|
}
|
|
466
|
-
async
|
|
496
|
+
async getSelfIssuedLoginSession(sessionId) {
|
|
467
497
|
const row = await this.db.selectFrom("sessions").selectAll().where("id", "=", sessionId).where("accessToken", "is", null).executeTakeFirst();
|
|
468
498
|
return row ? toSession(row) : void 0;
|
|
469
499
|
}
|
|
470
|
-
async
|
|
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) {
|
|
471
505
|
const userId = this.assertConnected();
|
|
472
506
|
return this.db.transaction().execute(async (trx) => {
|
|
473
507
|
const row = await trx.selectFrom("sessions").selectAll().where("id", "=", sessionId).where("userId", "=", userId).where("accessToken", "is", null).executeTakeFirst();
|
|
474
|
-
if (!row) throw new Error("
|
|
508
|
+
if (!row) throw new Error("Self-issued login session not found");
|
|
475
509
|
await trx.deleteFrom("sessions").where("userId", "=", userId).where("origin", "=", row.origin).where("id", "!=", sessionId).execute();
|
|
476
510
|
await trx.updateTable("sessions").set({ accessToken }).where("id", "=", sessionId).execute();
|
|
477
511
|
return toSession({
|
|
@@ -503,7 +537,7 @@ var StoreSql = class StoreSql {
|
|
|
503
537
|
async updateIdp(idp) {
|
|
504
538
|
await this.db.transaction().execute(async (trx) => {
|
|
505
539
|
const idpEntry = fromIdp(idp);
|
|
506
|
-
this.logger.info(
|
|
540
|
+
this.logger.info("Updating idp table", { idpEntry });
|
|
507
541
|
await trx.updateTable("idps").set(idpEntry).where("id", "=", idp.id).execute();
|
|
508
542
|
});
|
|
509
543
|
}
|
|
@@ -530,10 +564,10 @@ var StoreSql = class StoreSql {
|
|
|
530
564
|
async getCurrentNetwork() {
|
|
531
565
|
const userId = this.assertConnected();
|
|
532
566
|
const token = this.authContext?.accessToken;
|
|
533
|
-
const
|
|
534
|
-
if (!token && !
|
|
535
|
-
const sessionRow = await this.db.selectFrom("sessions").selectAll().where("userId", "=", userId).where((eb) => token ? eb("accessToken", "=", token) : eb.and([eb("id", "=",
|
|
536
|
-
if (!sessionRow) throw
|
|
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();
|
|
570
|
+
if (!sessionRow) throw providerErrors.unauthorized({ message: "No session found" });
|
|
537
571
|
const networkId = toSession(sessionRow).network;
|
|
538
572
|
if (!networkId) throw new Error("No current network set in session");
|
|
539
573
|
const network = (await this.listNetworks()).find((n) => n.id === networkId);
|
|
@@ -557,8 +591,10 @@ var StoreSql = class StoreSql {
|
|
|
557
591
|
async updateNetwork(network) {
|
|
558
592
|
this.assertConnected();
|
|
559
593
|
await this.db.transaction().execute(async (trx) => {
|
|
594
|
+
const existing = (await trx.selectFrom("networks").selectAll().execute()).map(toNetwork);
|
|
595
|
+
assertUniqueSelfIssuedAudience(existing, network, network.id);
|
|
560
596
|
const networkEntry = fromNetwork(network, void 0);
|
|
561
|
-
this.logger.info(
|
|
597
|
+
this.logger.info("Updating network table", { networkEntry });
|
|
562
598
|
await trx.updateTable("networks").set(networkEntry).where("id", "=", network.id).execute();
|
|
563
599
|
});
|
|
564
600
|
}
|
|
@@ -567,7 +603,9 @@ var StoreSql = class StoreSql {
|
|
|
567
603
|
if (!(await this.listIdps()).find((idp) => idp.id === network.identityProviderId)) throw new Error(`Identity provider "${network.identityProviderId}" not found`);
|
|
568
604
|
await this.db.transaction().execute(async (trx) => {
|
|
569
605
|
if (await trx.selectFrom("networks").selectAll().where("id", "=", network.id).executeTakeFirst()) throw new Error(`Network ${network.id} already exists`);
|
|
570
|
-
|
|
606
|
+
const existing = (await trx.selectFrom("networks").selectAll().execute()).map(toNetwork);
|
|
607
|
+
assertUniqueSelfIssuedAudience(existing, network);
|
|
608
|
+
await trx.insertInto("networks").values(fromNetwork(network, userId)).execute();
|
|
571
609
|
});
|
|
572
610
|
}
|
|
573
611
|
async removeNetwork(networkId) {
|
|
@@ -595,7 +633,7 @@ var StoreSql = class StoreSql {
|
|
|
595
633
|
...existing.createdAt !== void 0 && { createdAt: existing.createdAt },
|
|
596
634
|
...signedAt !== void 0 && { signedAt },
|
|
597
635
|
...externalTxId !== void 0 && { externalTxId },
|
|
598
|
-
...failureReason
|
|
636
|
+
...failureReason && { failureReason }
|
|
599
637
|
};
|
|
600
638
|
}
|
|
601
639
|
async setTransaction(transaction) {
|
|
@@ -855,17 +893,18 @@ const migrator = (db) => {
|
|
|
855
893
|
};
|
|
856
894
|
//#endregion
|
|
857
895
|
//#region src/bootstrap.ts
|
|
858
|
-
async function bootstrap(db, config
|
|
859
|
-
const store = new StoreSql(db
|
|
896
|
+
async function bootstrap(db, config) {
|
|
897
|
+
const store = new StoreSql(db);
|
|
860
898
|
await Promise.all(config.idps.map((idp) => store.addIdp(idp)));
|
|
861
899
|
await Promise.all(config.networks.map((network) => store.addNetwork(network)));
|
|
862
900
|
}
|
|
863
901
|
//#endregion
|
|
864
902
|
//#region src/cli.ts
|
|
865
|
-
const logger =
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
903
|
+
const logger = getLogger([
|
|
904
|
+
"core",
|
|
905
|
+
"wallet-store-sql",
|
|
906
|
+
"cli"
|
|
907
|
+
]);
|
|
869
908
|
function createCLI(config, bootstrapConfig) {
|
|
870
909
|
const program = new Command();
|
|
871
910
|
program.command("up").description("Run all pending migrations").action(async () => {
|
|
@@ -883,8 +922,8 @@ function createCLI(config, bootstrapConfig) {
|
|
|
883
922
|
const umzug = migrator(db);
|
|
884
923
|
const executed = await umzug.executed();
|
|
885
924
|
const pending = await umzug.pending();
|
|
886
|
-
|
|
887
|
-
|
|
925
|
+
logger.info("Executed migrations: {executed}", { executed });
|
|
926
|
+
logger.info("Pending migrations: {pending}", { pending });
|
|
888
927
|
await db.destroy();
|
|
889
928
|
});
|
|
890
929
|
program.command("reset").description("Rollback all migrations and reapply them").action(async () => {
|
|
@@ -898,7 +937,7 @@ function createCLI(config, bootstrapConfig) {
|
|
|
898
937
|
program.command("bootstrap").description("Bootstrap DB from config").action(async () => {
|
|
899
938
|
if (!bootstrapConfig) throw new Error("Bootstrap config (idps/networks) is required for the bootstrap command");
|
|
900
939
|
const db = connection(config);
|
|
901
|
-
await bootstrap(db, bootstrapConfig
|
|
940
|
+
await bootstrap(db, bootstrapConfig);
|
|
902
941
|
await db.destroy();
|
|
903
942
|
});
|
|
904
943
|
return program;
|