@yourself_id/siwys-api-js 0.0.11 → 0.0.13-bump-mdip.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.cjs CHANGED
@@ -47,6 +47,47 @@ function _create_class(Constructor, protoProps, staticProps) {
47
47
  if (staticProps) _defineProperties(Constructor, staticProps);
48
48
  return Constructor;
49
49
  }
50
+ function _getRequireWildcardCache(nodeInterop) {
51
+ if (typeof WeakMap !== "function") return null;
52
+ var cacheBabelInterop = new WeakMap();
53
+ var cacheNodeInterop = new WeakMap();
54
+ return (_getRequireWildcardCache = function(nodeInterop) {
55
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
56
+ })(nodeInterop);
57
+ }
58
+ function _interop_require_wildcard(obj, nodeInterop) {
59
+ if (!nodeInterop && obj && obj.__esModule) {
60
+ return obj;
61
+ }
62
+ if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
63
+ return {
64
+ default: obj
65
+ };
66
+ }
67
+ var cache = _getRequireWildcardCache(nodeInterop);
68
+ if (cache && cache.has(obj)) {
69
+ return cache.get(obj);
70
+ }
71
+ var newObj = {
72
+ __proto__: null
73
+ };
74
+ var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
75
+ for(var key in obj){
76
+ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
77
+ var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
78
+ if (desc && (desc.get || desc.set)) {
79
+ Object.defineProperty(newObj, key, desc);
80
+ } else {
81
+ newObj[key] = obj[key];
82
+ }
83
+ }
84
+ }
85
+ newObj.default = obj;
86
+ if (cache) {
87
+ cache.set(obj, newObj);
88
+ }
89
+ return newObj;
90
+ }
50
91
  function _type_of(obj) {
51
92
  "@swc/helpers - typeof";
52
93
  return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
@@ -213,24 +254,12 @@ __export(src_exports, {
213
254
  });
214
255
  module.exports = __toCommonJS(src_exports);
215
256
  // src/keymaster.ts
216
- var cipher = __toESM(require("@mdip/cipher/node"), 1);
217
- var gatekeeper_sdk = __toESM(require("@mdip/gatekeeper/sdk"), 1);
218
- var keymaster_lib = __toESM(require("@mdip/keymaster/lib"), 1);
219
257
  var Keymaster = /*#__PURE__*/ function() {
220
258
  function Keymaster(config) {
221
259
  _class_call_check(this, Keymaster);
222
- this._serviceStarted = false;
223
- this._gatekeeperConfig = {
224
- url: config.gatekeeperConfig.url,
225
- waitUntilReady: true,
226
- intervalSeconds: 5,
227
- chatty: true
228
- };
229
- this._walletConfig = config.walletConfig;
230
- this._walletDb = {
231
- saveWallet: config.onSaveWallet,
232
- loadWallet: config.onLoadWallet
233
- };
260
+ this.serviceStarted = false;
261
+ this.validateConfig(config);
262
+ this.config = config;
234
263
  }
235
264
  _create_class(Keymaster, [
236
265
  {
@@ -238,69 +267,98 @@ var Keymaster = /*#__PURE__*/ function() {
238
267
  value: function start() {
239
268
  var _this = this;
240
269
  return _async_to_generator(function() {
241
- var e, e1, e2;
242
270
  return _ts_generator(this, function(_state) {
243
271
  switch(_state.label){
244
272
  case 0:
245
- _state.trys.push([
246
- 0,
247
- 2,
248
- ,
249
- 3
250
- ]);
273
+ if (!_this.config.gatekeeperConfig) return [
274
+ 3,
275
+ 2
276
+ ];
251
277
  return [
252
278
  4,
253
- gatekeeper_sdk.start(_this._gatekeeperConfig)
279
+ _this.startIntegratedKeymaster()
254
280
  ];
255
281
  case 1:
256
- _state.sent();
282
+ _this.serviceStarted = _state.sent();
257
283
  return [
258
284
  3,
259
285
  3
260
286
  ];
261
287
  case 2:
262
- e = _state.sent();
263
- console.error("Error starting Gatekeeper service:", e);
288
+ if (_this.config.keymasterConfig) {
289
+ _this.serviceStarted = _this.startExternalKeymaster();
290
+ } else {
291
+ throw "Missing Gatekeeper or Keymaster config";
292
+ }
293
+ _state.label = 3;
294
+ case 3:
264
295
  return [
265
- 3,
266
- 3
296
+ 2,
297
+ _this.serviceStarted
267
298
  ];
268
- case 3:
299
+ }
300
+ });
301
+ })();
302
+ }
303
+ },
304
+ {
305
+ key: "startIntegratedKeymaster",
306
+ value: function startIntegratedKeymaster() {
307
+ var _this = this;
308
+ return _async_to_generator(function() {
309
+ var gatekeeper_sdk, cipher, e;
310
+ return _ts_generator(this, function(_state) {
311
+ switch(_state.label){
312
+ case 0:
313
+ console.log("Starting integrated Keymaster service");
314
+ _state.label = 1;
315
+ case 1:
269
316
  _state.trys.push([
270
- 3,
271
- 5,
317
+ 1,
318
+ 8,
272
319
  ,
273
- 6
320
+ 9
274
321
  ]);
275
322
  return [
276
323
  4,
277
- keymaster_lib.start({
278
- gatekeeper: gatekeeper_sdk,
279
- wallet: _this._walletDb,
280
- cipher: cipher
324
+ Promise.resolve().then(function() {
325
+ return /*#__PURE__*/ _interop_require_wildcard(require("@mdip/gatekeeper/sdk"));
281
326
  })
282
327
  ];
283
- case 4:
328
+ case 2:
329
+ gatekeeper_sdk = _state.sent();
330
+ return [
331
+ 4,
332
+ gatekeeper_sdk.start(_this.config.gatekeeperConfig)
333
+ ];
334
+ case 3:
284
335
  _state.sent();
285
- _this._serviceStarted = true;
286
336
  return [
287
- 3,
288
- 6
337
+ 4,
338
+ Promise.resolve().then(function() {
339
+ return /*#__PURE__*/ _interop_require_wildcard(require("@mdip/cipher/node"));
340
+ })
341
+ ];
342
+ case 4:
343
+ cipher = _state.sent();
344
+ return [
345
+ 4,
346
+ Promise.resolve().then(function() {
347
+ return /*#__PURE__*/ _interop_require_wildcard(require("@mdip/keymaster/lib"));
348
+ })
289
349
  ];
290
350
  case 5:
291
- e1 = _state.sent();
292
- console.error("Error starting Keymaster service:", e1);
351
+ _this.keymasterService = _state.sent();
293
352
  return [
294
- 3,
295
- 6
353
+ 4,
354
+ _this.keymasterService.start({
355
+ cipher: cipher,
356
+ gatekeeper: gatekeeper_sdk,
357
+ wallet: _this.config.walletDb
358
+ })
296
359
  ];
297
360
  case 6:
298
- _state.trys.push([
299
- 6,
300
- 8,
301
- ,
302
- 9
303
- ]);
361
+ _state.sent();
304
362
  return [
305
363
  4,
306
364
  _this.ensureWalletExists()
@@ -312,16 +370,71 @@ var Keymaster = /*#__PURE__*/ function() {
312
370
  9
313
371
  ];
314
372
  case 8:
315
- e2 = _state.sent();
316
- console.error("Error verifying existing wallet:", e2);
373
+ e = _state.sent();
374
+ console.error("Error starting Keymaster service:", e);
317
375
  return [
318
- 3,
319
- 9
376
+ 2,
377
+ false
320
378
  ];
321
379
  case 9:
380
+ console.log("Started integrated Keymaster service");
322
381
  return [
323
382
  2,
324
- _this._serviceStarted
383
+ true
384
+ ];
385
+ }
386
+ });
387
+ })();
388
+ }
389
+ },
390
+ {
391
+ key: "startExternalKeymaster",
392
+ value: function startExternalKeymaster() {
393
+ var _this = this;
394
+ return _async_to_generator(function() {
395
+ var e;
396
+ return _ts_generator(this, function(_state) {
397
+ switch(_state.label){
398
+ case 0:
399
+ console.log("Starting external Keymaster service");
400
+ _state.label = 1;
401
+ case 1:
402
+ _state.trys.push([
403
+ 1,
404
+ 4,
405
+ ,
406
+ 5
407
+ ]);
408
+ return [
409
+ 4,
410
+ Promise.resolve().then(function() {
411
+ return /*#__PURE__*/ _interop_require_wildcard(require("@mdip/keymaster/sdk"));
412
+ })
413
+ ];
414
+ case 2:
415
+ _this.keymasterService = _state.sent();
416
+ return [
417
+ 4,
418
+ _this.keymasterService.start(_this.config.keymasterConfig)
419
+ ];
420
+ case 3:
421
+ _state.sent();
422
+ return [
423
+ 3,
424
+ 5
425
+ ];
426
+ case 4:
427
+ e = _state.sent();
428
+ console.error("Error starting Keymaster service:", e);
429
+ return [
430
+ 2,
431
+ false
432
+ ];
433
+ case 5:
434
+ console.log("Started external Keymaster service");
435
+ return [
436
+ 2,
437
+ true
325
438
  ];
326
439
  }
327
440
  });
@@ -347,7 +460,7 @@ var Keymaster = /*#__PURE__*/ function() {
347
460
  };
348
461
  return [
349
462
  4,
350
- keymaster_lib.createChallenge(challengeSpec, options)
463
+ _this.keymasterService.createChallenge(challengeSpec, options)
351
464
  ];
352
465
  case 1:
353
466
  challenge = _state.sent();
@@ -363,6 +476,25 @@ var Keymaster = /*#__PURE__*/ function() {
363
476
  })();
364
477
  }
365
478
  },
479
+ {
480
+ key: "showMnemonic",
481
+ value: function showMnemonic() {
482
+ var _this = this;
483
+ return _async_to_generator(function() {
484
+ return _ts_generator(this, function(_state) {
485
+ if (!_this.serviceRunning()) {
486
+ return [
487
+ 2
488
+ ];
489
+ }
490
+ return [
491
+ 2,
492
+ _this.keymasterService.decryptMnemonic()
493
+ ];
494
+ });
495
+ })();
496
+ }
497
+ },
366
498
  {
367
499
  key: "verifyResponse",
368
500
  value: function verifyResponse(did, options) {
@@ -379,7 +511,7 @@ var Keymaster = /*#__PURE__*/ function() {
379
511
  }
380
512
  return [
381
513
  4,
382
- keymaster_lib.verifyResponse(did, options)
514
+ _this.keymasterService.verifyResponse(did, options)
383
515
  ];
384
516
  case 1:
385
517
  response = _state.sent();
@@ -397,13 +529,13 @@ var Keymaster = /*#__PURE__*/ function() {
397
529
  value: function ensureWalletExists() {
398
530
  var _this = this;
399
531
  return _async_to_generator(function() {
400
- var existing;
532
+ var existing, walletConfig;
401
533
  return _ts_generator(this, function(_state) {
402
534
  switch(_state.label){
403
535
  case 0:
404
536
  return [
405
537
  4,
406
- _this._walletDb.loadWallet()
538
+ _this.config.walletDb.loadWallet()
407
539
  ];
408
540
  case 1:
409
541
  existing = _state.sent();
@@ -413,14 +545,15 @@ var Keymaster = /*#__PURE__*/ function() {
413
545
  2
414
546
  ];
415
547
  }
416
- console.log("Creating wallet with ID ".concat(_this._walletConfig.id));
417
- if (!_this._walletConfig.mnemonic) return [
548
+ walletConfig = _this.config.walletConfig;
549
+ console.log("Creating wallet with ID ".concat(walletConfig.id));
550
+ if (!walletConfig.mnemonic) return [
418
551
  3,
419
552
  3
420
553
  ];
421
554
  return [
422
555
  4,
423
- keymaster_lib.newWallet(spec.mnenomic, true)
556
+ _this.keymasterService.newWallet(spec.mnenomic, true)
424
557
  ];
425
558
  case 2:
426
559
  _state.sent();
@@ -428,11 +561,11 @@ var Keymaster = /*#__PURE__*/ function() {
428
561
  case 3:
429
562
  return [
430
563
  4,
431
- keymaster_lib.createId(_this._walletConfig.id, _this._walletConfig.registry)
564
+ _this.keymasterService.createId(walletConfig.id, walletConfig.registry)
432
565
  ];
433
566
  case 4:
434
567
  _state.sent();
435
- console.log("Created wallet with ID ".concat(_this._walletConfig.id));
568
+ console.log("Created wallet with ID ".concat(walletConfig.id));
436
569
  return [
437
570
  2
438
571
  ];
@@ -450,7 +583,7 @@ var Keymaster = /*#__PURE__*/ function() {
450
583
  return _ts_generator(this, function(_state) {
451
584
  switch(_state.label){
452
585
  case 0:
453
- _tmp = _this._serviceStarted;
586
+ _tmp = _this.serviceStarted;
454
587
  if (_tmp) return [
455
588
  3,
456
589
  2
@@ -471,6 +604,26 @@ var Keymaster = /*#__PURE__*/ function() {
471
604
  });
472
605
  })();
473
606
  }
607
+ },
608
+ {
609
+ key: "validateConfig",
610
+ value: function validateConfig(config) {
611
+ if (config.gatekeeperConfig && config.keymasterConfig) {
612
+ throw new Error("Cannot provide both a Gatekeeper and Keymaster config");
613
+ }
614
+ if (config.gatekeeperConfig) {
615
+ var _config_walletDb, _config_walletDb1;
616
+ if (!config.walletConfig) {
617
+ throw new Error("Missing wallet config");
618
+ }
619
+ if (!((_config_walletDb = config.walletDb) === null || _config_walletDb === void 0 ? void 0 : _config_walletDb.loadWallet)) {
620
+ throw new Error("Missing load wallet callback");
621
+ }
622
+ if (!((_config_walletDb1 = config.walletDb) === null || _config_walletDb1 === void 0 ? void 0 : _config_walletDb1.saveWallet)) {
623
+ throw new Error("Missing save wallet callback");
624
+ }
625
+ }
626
+ }
474
627
  }
475
628
  ]);
476
629
  return Keymaster;
@@ -1 +1 @@
1
- {"version":3,"sources":["/home/runner/work/siwys-js/siwys-js/packages/api-js/dist/index.cjs","../src/index.ts","../src/keymaster.ts"],"names":["__create","Object","create","__defProp","defineProperty","__getOwnPropDesc","getOwnPropertyDescriptor","__getOwnPropNames","getOwnPropertyNames","__getProtoOf","getPrototypeOf","__hasOwnProp","prototype","hasOwnProperty","__export","target","all","name","get","enumerable","__copyProps","to","from","except","desc","key","call","__toESM","mod","isNodeMode","__esModule","value","__toCommonJS","src_exports","Keymaster","module","exports","cipher","require","gatekeeper_sdk","keymaster_lib","config","_serviceStarted","_gatekeeperConfig","url","gatekeeperConfig","waitUntilReady","intervalSeconds","chatty","_walletConfig","walletConfig","_walletDb","saveWallet","onSaveWallet","loadWallet","onLoadWallet","start","e","console","error","gatekeeper","wallet","ensureWalletExists","createChallenge","spec","options","challengeSpec","challenge","serviceRunning","challengeUrl","callback","verifyResponse","did","response","existing","current","log","id","mnemonic","newWallet","mnenomic","createId","registry"],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,IAAIA,WAAWC,OAAOC,MAAM;AAC5B,IAAIC,YAAYF,OAAOG,cAAc;AACrC,IAAIC,mBAAmBJ,OAAOK,wBAAwB;AACtD,IAAIC,oBAAoBN,OAAOO,mBAAmB;AAClD,IAAIC,eAAeR,OAAOS,cAAc;AACxC,IAAIC,eAAeV,OAAOW,SAAS,CAACC,cAAc;AAClD,IAAIC,WAAW,SAACC,QAAQC;IACtB,IAAK,IAAIC,QAAQD,IACfb,UAAUY,QAAQE,MAAM;QAAEC,KAAKF,GAAG,CAACC,KAAK;QAAEE,YAAY;IAAK;AAC/D;AACA,IAAIC,cAAc,SAACC,IAAIC,MAAMC,QAAQC;IACnC,IAAIF,QAAQ,CAAA,OAAOA,qCAAP,SAAOA,KAAG,MAAM,YAAY,OAAOA,SAAS,YAAY;YAC7D,kCAAA,2BAA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;;oCAAoB5B,cAAA+B,cAAA,CAC3DC,KACAP;;;gCAFIQ,WAAmC;gCAIzC;;oCAAOA;;;;gBACT;;;;YAEcX,KAAAA;mBAAd,SAAcA;;uBAAd,oBAAA;wBACQY;;;;gCAA0B;;oCAAM,MAAKvB,SAAA,CAAUG,UAAA;;;gCAA/CoB,WAA0B;gCAChC,IAAIA,qBAAAA,+BAAAA,SAAUC,OAAA,EAAS;oCACrBjB,QAAQkB,GAAA,CAAI,iCAAiD,OAAhBF,SAASC,OAAO;oCAC7D;;;gCACF;gCAEAjB,QAAQkB,GAAA,CAAI,2BAAgD,OAArB,MAAK3B,aAAA,CAAc4B,EAAE;qCAExD,MAAK5B,aAAA,CAAc6B,QAAA,EAAnB;;;;gCAEF;;oCAAoBtC,cAAAuC,SAAA,CAAUf,KAAKgB,QAAA,EAAU;;;gCAA7C;;;gCAGF;;oCAAoBxC,cAAAyC,QAAA,CAClB,MAAKhC,aAAA,CAAc4B,EAAA,EACnB,MAAK5B,aAAA,CAAciC,QAAA;;;gCAFrB;gCAIAxB,QAAQkB,GAAA,CAAI,0BAA+C,OAArB,MAAK3B,aAAA,CAAc4B,EAAE;;;;;;gBAC7D;;;;YAEcT,KAAAA;mBAAd,SAAcA;;uBAAd,oBAAA;;;;;uCACS,MAAK1B,eAAA;0CAAL;;;;gCAAyB;;oCAAM,MAAKc,KAAA;;;uCAAX;;;gCAAhC;;;;;;gBACF;;;;;;AFlDF,6DAA6D;AAC7D,KAAMrB,CAAAA,OAAOC,OAAO,GAAG;IACrBF,WAAAA;AACF,CAAA","sourcesContent":["\"use strict\";\nvar __create = Object.create;\nvar __defProp = Object.defineProperty;\nvar __getOwnPropDesc = Object.getOwnPropertyDescriptor;\nvar __getOwnPropNames = Object.getOwnPropertyNames;\nvar __getProtoOf = Object.getPrototypeOf;\nvar __hasOwnProp = Object.prototype.hasOwnProperty;\nvar __export = (target, all) => {\n for (var name in all)\n __defProp(target, name, { get: all[name], enumerable: true });\n};\nvar __copyProps = (to, from, except, desc) => {\n if (from && typeof from === \"object\" || typeof from === \"function\") {\n for (let key of __getOwnPropNames(from))\n if (!__hasOwnProp.call(to, key) && key !== except)\n __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n }\n return to;\n};\nvar __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(\n // If the importer is in node compatibility mode or this is not an ESM\n // file that has been converted to a CommonJS file using a Babel-\n // compatible transform (i.e. \"__esModule\" has not been set), then set\n // \"default\" to the CommonJS \"module.exports\" for node compatibility.\n isNodeMode || !mod || !mod.__esModule ? __defProp(target, \"default\", { value: mod, enumerable: true }) : target,\n mod\n));\nvar __toCommonJS = (mod) => __copyProps(__defProp({}, \"__esModule\", { value: true }), mod);\n\n// src/index.ts\nvar src_exports = {};\n__export(src_exports, {\n Keymaster: () => Keymaster\n});\nmodule.exports = __toCommonJS(src_exports);\n\n// src/keymaster.ts\nvar cipher = __toESM(require(\"@mdip/cipher/node\"), 1);\nvar gatekeeper_sdk = __toESM(require(\"@mdip/gatekeeper/sdk\"), 1);\nvar keymaster_lib = __toESM(require(\"@mdip/keymaster/lib\"), 1);\nvar Keymaster = class {\n constructor(config) {\n this._serviceStarted = false;\n this._gatekeeperConfig = {\n url: config.gatekeeperConfig.url,\n waitUntilReady: true,\n intervalSeconds: 5,\n chatty: true\n };\n this._walletConfig = config.walletConfig;\n this._walletDb = {\n saveWallet: config.onSaveWallet,\n loadWallet: config.onLoadWallet\n };\n }\n async start() {\n try {\n await gatekeeper_sdk.start(this._gatekeeperConfig);\n } catch (e) {\n console.error(\"Error starting Gatekeeper service:\", e);\n }\n try {\n await keymaster_lib.start({\n gatekeeper: gatekeeper_sdk,\n wallet: this._walletDb,\n cipher\n });\n this._serviceStarted = true;\n } catch (e) {\n console.error(\"Error starting Keymaster service:\", e);\n }\n try {\n await this.ensureWalletExists();\n } catch (e) {\n console.error(\"Error verifying existing wallet:\", e);\n }\n return this._serviceStarted;\n }\n async createChallenge(spec2, options) {\n if (!this.serviceRunning()) {\n return;\n }\n const challengeSpec = { challenge: spec2 };\n const challenge = await keymaster_lib.createChallenge(\n challengeSpec,\n options\n );\n return {\n challenge,\n challengeUrl: `${spec2.callback}?challenge=${challenge}`\n };\n }\n async verifyResponse(did, options) {\n if (!this.serviceRunning()) {\n return;\n }\n const response = await keymaster_lib.verifyResponse(\n did,\n options\n );\n return response;\n }\n async ensureWalletExists() {\n const existing = await this._walletDb.loadWallet();\n if (existing?.current) {\n console.log(`Using existing wallet with ID ${existing.current}`);\n return;\n }\n console.log(`Creating wallet with ID ${this._walletConfig.id}`);\n if (this._walletConfig.mnemonic) {\n await keymaster_lib.newWallet(spec.mnenomic, true);\n }\n await keymaster_lib.createId(\n this._walletConfig.id,\n this._walletConfig.registry\n );\n console.log(`Created wallet with ID ${this._walletConfig.id}`);\n }\n async serviceRunning() {\n return this._serviceStarted || await this.start();\n }\n};\n// Annotate the CommonJS export names for ESM import in node:\n0 && (module.exports = {\n Keymaster\n});\n","export {\n GatekeeperConfig,\n Keymaster,\n KeymasterConfig,\n CreateChallengeOptions,\n CreateChallengeResponse,\n CreateChallengeSpec,\n VerifyResponseOptions,\n VerifyResponseResponse,\n Wallet,\n WalletConfig,\n WalletSeed,\n} from \"./keymaster\";\n","// @ts-ignore\n// @ts-nocheck\nimport * as cipher from \"@mdip/cipher/node\";\nimport * as gatekeeper_sdk from \"@mdip/gatekeeper/sdk\";\nimport * as keymaster_lib from \"@mdip/keymaster/lib\";\n\nimport { initalizeWalletDb, DbType } from \"./db\";\nimport * as wallet_db from \"./db/test\";\n\nexport interface CreateChallengeSpec {\n callback: string;\n credentials?: [];\n}\n\nexport interface CreateChallengeOptions {\n registry?: string;\n validUntil?: string; // ISO string\n}\n\nexport interface CreateChallengeResponse {\n challenge: string; // DID\n challengeUrl: string;\n}\n\nexport interface VerifyResponseOptions {\n retries?: number;\n delay?: number;\n}\n\nexport interface VerifyResponseResponse {\n match: boolean;\n challenge: string;\n responder: string;\n}\n\nexport interface WalletSeed {\n mnemonic: string;\n hdkey: {\n xpriv: string;\n xpub: string;\n };\n}\n\nexport interface Wallet {\n seed: WalletSeed;\n counter: number;\n ids: any;\n current?: string;\n}\n\nexport interface GatekeeperConfig {\n url: string;\n waitUntilReady?: boolean;\n intervalSeconds?: number;\n chatty?: boolean;\n}\n\nexport interface WalletConfig {\n id: string;\n mnemonic?: string;\n registry?: string;\n}\n\nexport interface KeymasterConfig {\n gatekeeperConfig: GatekeeperConfig;\n walletConfig: WalletConfig;\n onSaveWallet: (w: Wallet, overwrite?: boolean) => Promise<boolean>;\n onLoadWallet: () => Promise<Wallet | null>;\n}\n\nexport class Keymaster {\n private _gatekeeperConfig;\n private _walletConfig;\n private _walletDb;\n private _serviceStarted = false;\n\n constructor(config: KeymasterConfig) {\n this._gatekeeperConfig = {\n url: config.gatekeeperConfig.url,\n waitUntilReady: true,\n intervalSeconds: 5,\n chatty: true,\n };\n this._walletConfig = config.walletConfig;\n this._walletDb = {\n saveWallet: config.onSaveWallet,\n loadWallet: config.onLoadWallet,\n };\n }\n\n public async start(): Promise<boolean> {\n try {\n await gatekeeper_sdk.start(this._gatekeeperConfig);\n } catch (e) {\n console.error(\"Error starting Gatekeeper service:\", e);\n }\n\n try {\n await keymaster_lib.start({\n gatekeeper: gatekeeper_sdk,\n wallet: this._walletDb,\n cipher: cipher,\n });\n this._serviceStarted = true;\n } catch (e) {\n console.error(\"Error starting Keymaster service:\", e);\n }\n\n try {\n await this.ensureWalletExists();\n } catch (e) {\n console.error(\"Error verifying existing wallet:\", e);\n }\n\n return this._serviceStarted;\n }\n\n async createChallenge(\n spec: CreateChallengeSpec,\n options?: CreateChallengeOptions\n ): Promise<CreateChallengeResponse> {\n if (!this.serviceRunning()) {\n return;\n }\n const challengeSpec = { challenge: spec };\n const challenge: string = await keymaster_lib.createChallenge(\n challengeSpec,\n options\n );\n return {\n challenge: challenge,\n challengeUrl: `${spec.callback}?challenge=${challenge}`,\n };\n }\n\n async verifyResponse(\n did: string,\n options?: VerifyResponseOptions\n ): Promise<VerifyResponseResponse> {\n if (!this.serviceRunning()) {\n return;\n }\n const response: VerifyResponseResponse = await keymaster_lib.verifyResponse(\n did,\n options\n );\n return response;\n }\n\n private async ensureWalletExists(): Promise<void> {\n const existing: Wallet | null = await this._walletDb.loadWallet();\n if (existing?.current) {\n console.log(`Using existing wallet with ID ${existing.current}`);\n return;\n }\n\n console.log(`Creating wallet with ID ${this._walletConfig.id}`);\n // wallet was just created, recreate based on config\n if (this._walletConfig.mnemonic) {\n // create the wallet with a custom mnemonic first\n await keymaster_lib.newWallet(spec.mnenomic, true);\n }\n // create ID and set as current, createId() will save wallet\n await keymaster_lib.createId(\n this._walletConfig.id,\n this._walletConfig.registry\n );\n console.log(`Created wallet with ID ${this._walletConfig.id}`);\n }\n\n private async serviceRunning(): Promise<boolean> {\n return this._serviceStarted || (await this.start());\n }\n}\n"]}
1
+ {"version":3,"sources":["/home/runner/work/siwys-js/siwys-js/packages/api-js/dist/index.cjs","../src/index.ts","../src/keymaster.ts"],"names":["__create","Object","create","__defProp","defineProperty","__getOwnPropDesc","getOwnPropertyDescriptor","__getOwnPropNames","getOwnPropertyNames","__getProtoOf","getPrototypeOf","__hasOwnProp","prototype","hasOwnProperty","__export","target","all","name","get","enumerable","__copyProps","to","from","except","desc","key","call","__toESM","mod","isNodeMode","__esModule","value","__toCommonJS","src_exports","Keymaster","module","exports","config","serviceStarted","validateConfig","start","gatekeeperConfig","startIntegratedKeymaster","keymasterConfig","startExternalKeymaster","gatekeeper_sdk","cipher","e","console","log","keymasterService","gatekeeper","wallet","walletDb","ensureWalletExists","error","createChallenge","spec","options","challengeSpec","challenge","serviceRunning","challengeUrl","callback","showMnemonic","decryptMnemonic","verifyResponse","did","response","existing","walletConfig","loadWallet","current","id","mnemonic","newWallet","mnenomic","createId","registry","Error","saveWallet"],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;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OiC,YAAA,EAAc;wBACxB,MAAM,IAAIS,MAAM;oBAClB;oBACA,IAAI,GAAC1C,mBAAAA,OAAOgB,QAAA,cAAPhB,uCAAAA,iBAAiBkC,UAAA,GAAY;wBAChC,MAAM,IAAIQ,MAAM;oBAClB;oBACA,IAAI,GAAC1C,oBAAAA,OAAOgB,QAAA,cAAPhB,wCAAAA,kBAAiB2C,UAAA,GAAY;wBAChC,MAAM,IAAID,MAAM;oBAClB;gBACF;YACF;;;;;AFvFF,6DAA6D;AAC7D,KAAM5C,CAAAA,OAAOC,OAAO,GAAG;IACrBF,WAAAA;AACF,CAAA","sourcesContent":["\"use strict\";\nvar __create = Object.create;\nvar __defProp = Object.defineProperty;\nvar __getOwnPropDesc = Object.getOwnPropertyDescriptor;\nvar __getOwnPropNames = Object.getOwnPropertyNames;\nvar __getProtoOf = Object.getPrototypeOf;\nvar __hasOwnProp = Object.prototype.hasOwnProperty;\nvar __export = (target, all) => {\n for (var name in all)\n __defProp(target, name, { get: all[name], enumerable: true });\n};\nvar __copyProps = (to, from, except, desc) => {\n if (from && typeof from === \"object\" || typeof from === \"function\") {\n for (let key of __getOwnPropNames(from))\n if (!__hasOwnProp.call(to, key) && key !== except)\n __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });\n }\n return to;\n};\nvar __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(\n // If the importer is in node compatibility mode or this is not an ESM\n // file that has been converted to a CommonJS file using a Babel-\n // compatible transform (i.e. \"__esModule\" has not been set), then set\n // \"default\" to the CommonJS \"module.exports\" for node compatibility.\n isNodeMode || !mod || !mod.__esModule ? __defProp(target, \"default\", { value: mod, enumerable: true }) : target,\n mod\n));\nvar __toCommonJS = (mod) => __copyProps(__defProp({}, \"__esModule\", { value: true }), mod);\n\n// src/index.ts\nvar src_exports = {};\n__export(src_exports, {\n Keymaster: () => Keymaster\n});\nmodule.exports = __toCommonJS(src_exports);\n\n// src/keymaster.ts\nvar Keymaster = class {\n constructor(config) {\n this.serviceStarted = false;\n this.validateConfig(config);\n this.config = config;\n }\n async start() {\n if (this.config.gatekeeperConfig) {\n this.serviceStarted = await this.startIntegratedKeymaster();\n } else if (this.config.keymasterConfig) {\n this.serviceStarted = this.startExternalKeymaster();\n } else {\n throw \"Missing Gatekeeper or Keymaster config\";\n }\n return this.serviceStarted;\n }\n async startIntegratedKeymaster() {\n console.log(`Starting integrated Keymaster service`);\n try {\n const gatekeeper_sdk = await import(\"@mdip/gatekeeper/sdk\");\n await gatekeeper_sdk.start(this.config.gatekeeperConfig);\n const cipher = await import(\"@mdip/cipher/node\");\n this.keymasterService = await import(\"@mdip/keymaster/lib\");\n await this.keymasterService.start({\n cipher,\n gatekeeper: gatekeeper_sdk,\n wallet: this.config.walletDb\n });\n await this.ensureWalletExists();\n } catch (e) {\n console.error(\"Error starting Keymaster service:\", e);\n return false;\n }\n console.log(`Started integrated Keymaster service`);\n return true;\n }\n async startExternalKeymaster() {\n console.log(`Starting external Keymaster service`);\n try {\n this.keymasterService = await import(\"@mdip/keymaster/sdk\");\n await this.keymasterService.start(this.config.keymasterConfig);\n } catch (e) {\n console.error(`Error starting Keymaster service:`, e);\n return false;\n }\n console.log(`Started external Keymaster service`);\n return true;\n }\n async createChallenge(spec2, options) {\n if (!this.serviceRunning()) {\n return;\n }\n const challengeSpec = { challenge: spec2 };\n const challenge = await this.keymasterService.createChallenge(\n challengeSpec,\n options\n );\n return {\n challenge,\n challengeUrl: `${spec2.callback}?challenge=${challenge}`\n };\n }\n async showMnemonic() {\n if (!this.serviceRunning()) {\n return;\n }\n return this.keymasterService.decryptMnemonic();\n }\n async verifyResponse(did, options) {\n if (!this.serviceRunning()) {\n return;\n }\n const response = await this.keymasterService.verifyResponse(did, options);\n return response;\n }\n async ensureWalletExists() {\n const existing = await this.config.walletDb.loadWallet();\n if (existing?.current) {\n console.log(`Using existing wallet with ID ${existing.current}`);\n return;\n }\n const walletConfig = this.config.walletConfig;\n console.log(`Creating wallet with ID ${walletConfig.id}`);\n if (walletConfig.mnemonic) {\n await this.keymasterService.newWallet(spec.mnenomic, true);\n }\n await this.keymasterService.createId(\n walletConfig.id,\n walletConfig.registry\n );\n console.log(`Created wallet with ID ${walletConfig.id}`);\n }\n async serviceRunning() {\n return this.serviceStarted || await this.start();\n }\n validateConfig(config) {\n if (config.gatekeeperConfig && config.keymasterConfig) {\n throw new Error(\"Cannot provide both a Gatekeeper and Keymaster config\");\n }\n if (config.gatekeeperConfig) {\n if (!config.walletConfig) {\n throw new Error(\"Missing wallet config\");\n }\n if (!config.walletDb?.loadWallet) {\n throw new Error(\"Missing load wallet callback\");\n }\n if (!config.walletDb?.saveWallet) {\n throw new Error(\"Missing save wallet callback\");\n }\n }\n }\n};\n// Annotate the CommonJS export names for ESM import in node:\n0 && (module.exports = {\n Keymaster\n});\n","export {\n CreateChallengeOptions,\n CreateChallengeResponse,\n CreateChallengeSpec,\n Keymaster,\n KeymasterConfig,\n SdkConfig,\n VerifyResponseOptions,\n VerifyResponseResponse,\n Wallet,\n WalletConfig,\n WalletDb,\n WalletSeed,\n} from \"./keymaster\";\n","// @ts-ignore\n// @ts-nocheck\nimport { initalizeWalletDb, DbType } from \"./db\";\nimport * as wallet_db from \"./db/test\";\n\nexport interface CreateChallengeSpec {\n callback: string;\n credentials?: [];\n}\n\nexport interface CreateChallengeOptions {\n registry?: string;\n validUntil?: string; // ISO string\n}\n\nexport interface CreateChallengeResponse {\n challenge: string; // DID\n challengeUrl: string;\n}\n\nexport interface VerifyResponseOptions {\n retries?: number;\n delay?: number;\n}\n\nexport interface VerifyResponseResponse {\n match: boolean;\n challenge: string;\n responder: string;\n}\n\nexport interface WalletSeed {\n mnemonic: string;\n hdkey: {\n xpriv: string;\n xpub: string;\n };\n}\n\nexport interface Wallet {\n seed: WalletSeed;\n counter: number;\n ids: any;\n current?: string;\n}\n\n// config for connecting to an external Gatekeeper or Keymaster\nexport interface SdkConfig {\n url: string;\n waitUntilReady?: boolean;\n intervalSeconds?: number;\n chatty?: boolean;\n}\n\nexport interface WalletConfig {\n id: string;\n mnemonic?: string;\n registry?: string;\n}\n\nexport interface WalletDb {\n loadWallet: () => Promise<Wallet | null>;\n saveWallet: (w: Wallet, overwrite?: boolean) => Promise<boolean>;\n}\n\nexport interface KeymasterConfig {\n /**\n * Config for connecting to an external Keymaster service\n * when the consuming applciation is connecting to an external Keymaster.\n */\n keymasterConfig?: SdkConfig;\n /**\n * Config for connecting to an external Keymaster service when\n * the consuming applciation is acting as its own (integrated) Keymaster service.\n */\n gatekeeperConfig?: SdkConfig;\n /**\n * Config for custom user-defined values of the underlying wallet.\n *\n * Required for an integrated Keymaster.\n */\n walletConfig?: WalletConfig;\n /**\n * Config that contains the callbacks for persisting a\n * wallet to a backing Database.\n */\n walletDb?: WalletDb;\n}\n\nexport class Keymaster {\n private config: KeymasterConfig;\n /**\n * Underlying Keymaster libray based on the use-case:\n *\n * Integrated Keymaster = keymaster_lib\n *\n * External Keymaster = keymaster_sdk\n */\n private keymasterService;\n private serviceStarted = false;\n\n constructor(config: KeymasterConfig) {\n this.validateConfig(config);\n this.config = config;\n }\n\n public async start(): Promise<boolean> {\n if (this.config.gatekeeperConfig) {\n this.serviceStarted = await this.startIntegratedKeymaster();\n } else if (this.config.keymasterConfig) {\n this.serviceStarted = this.startExternalKeymaster();\n } else {\n throw \"Missing Gatekeeper or Keymaster config\";\n }\n return this.serviceStarted;\n }\n\n private async startIntegratedKeymaster(): Promise<boolean> {\n console.log(`Starting integrated Keymaster service`);\n\n try {\n const gatekeeper_sdk = await import(\"@mdip/gatekeeper/sdk\");\n await gatekeeper_sdk.start(this.config.gatekeeperConfig);\n\n const cipher = await import(\"@mdip/cipher/node\");\n\n this.keymasterService = await import(\"@mdip/keymaster/lib\");\n await this.keymasterService.start({\n cipher: cipher,\n gatekeeper: gatekeeper_sdk,\n wallet: this.config.walletDb,\n });\n\n await this.ensureWalletExists();\n } catch (e) {\n console.error(\"Error starting Keymaster service:\", e);\n return false;\n }\n\n console.log(`Started integrated Keymaster service`);\n return true;\n }\n\n private async startExternalKeymaster(): Promise<boolean> {\n console.log(`Starting external Keymaster service`);\n try {\n this.keymasterService = await import(\"@mdip/keymaster/sdk\");\n await this.keymasterService.start(this.config.keymasterConfig);\n } catch (e) {\n console.error(`Error starting Keymaster service:`, e);\n return false;\n }\n\n console.log(`Started external Keymaster service`);\n return true;\n }\n\n async createChallenge(\n spec: CreateChallengeSpec,\n options?: CreateChallengeOptions\n ): Promise<CreateChallengeResponse> {\n if (!this.serviceRunning()) {\n return;\n }\n const challengeSpec = { challenge: spec };\n const challenge: string = await this.keymasterService.createChallenge(\n challengeSpec,\n options\n );\n return {\n challenge: challenge,\n challengeUrl: `${spec.callback}?challenge=${challenge}`,\n };\n }\n\n async showMnemonic(): Promise<string> {\n if (!this.serviceRunning()) {\n return;\n }\n return this.keymasterService.decryptMnemonic();\n }\n\n async verifyResponse(\n did: string,\n options?: VerifyResponseOptions\n ): Promise<VerifyResponseResponse> {\n if (!this.serviceRunning()) {\n return;\n }\n const response: VerifyResponseResponse =\n await this.keymasterService.verifyResponse(did, options);\n return response;\n }\n\n private async ensureWalletExists(): Promise<void> {\n const existing: Wallet | null = await this.config.walletDb.loadWallet();\n if (existing?.current) {\n console.log(`Using existing wallet with ID ${existing.current}`);\n return;\n }\n\n const walletConfig = this.config.walletConfig;\n console.log(`Creating wallet with ID ${walletConfig.id}`);\n // wallet was just created, recreate based on config\n if (walletConfig.mnemonic) {\n // create the wallet with a custom mnemonic first\n await this.keymasterService.newWallet(spec.mnenomic, true);\n }\n // create ID and set as current, createId() will save wallet\n await this.keymasterService.createId(\n walletConfig.id,\n walletConfig.registry\n );\n console.log(`Created wallet with ID ${walletConfig.id}`);\n }\n\n private async serviceRunning(): Promise<boolean> {\n return this.serviceStarted || (await this.start());\n }\n\n private validateConfig(config: KeymasterConfig): void {\n if (config.gatekeeperConfig && config.keymasterConfig) {\n throw new Error(\"Cannot provide both a Gatekeeper and Keymaster config\");\n }\n\n if (config.gatekeeperConfig) {\n if (!config.walletConfig) {\n throw new Error(\"Missing wallet config\");\n }\n if (!config.walletDb?.loadWallet) {\n throw new Error(\"Missing load wallet callback\");\n }\n if (!config.walletDb?.saveWallet) {\n throw new Error(\"Missing save wallet callback\");\n }\n }\n }\n}\n"]}
package/dist/index.d.cts CHANGED
@@ -32,7 +32,7 @@ interface Wallet {
32
32
  ids: any;
33
33
  current?: string;
34
34
  }
35
- interface GatekeeperConfig {
35
+ interface SdkConfig {
36
36
  url: string;
37
37
  waitUntilReady?: boolean;
38
38
  intervalSeconds?: number;
@@ -43,23 +43,54 @@ interface WalletConfig {
43
43
  mnemonic?: string;
44
44
  registry?: string;
45
45
  }
46
+ interface WalletDb {
47
+ loadWallet: () => Promise<Wallet | null>;
48
+ saveWallet: (w: Wallet, overwrite?: boolean) => Promise<boolean>;
49
+ }
46
50
  interface KeymasterConfig {
47
- gatekeeperConfig: GatekeeperConfig;
48
- walletConfig: WalletConfig;
49
- onSaveWallet: (w: Wallet, overwrite?: boolean) => Promise<boolean>;
50
- onLoadWallet: () => Promise<Wallet | null>;
51
+ /**
52
+ * Config for connecting to an external Keymaster service
53
+ * when the consuming applciation is connecting to an external Keymaster.
54
+ */
55
+ keymasterConfig?: SdkConfig;
56
+ /**
57
+ * Config for connecting to an external Keymaster service when
58
+ * the consuming applciation is acting as its own (integrated) Keymaster service.
59
+ */
60
+ gatekeeperConfig?: SdkConfig;
61
+ /**
62
+ * Config for custom user-defined values of the underlying wallet.
63
+ *
64
+ * Required for an integrated Keymaster.
65
+ */
66
+ walletConfig?: WalletConfig;
67
+ /**
68
+ * Config that contains the callbacks for persisting a
69
+ * wallet to a backing Database.
70
+ */
71
+ walletDb?: WalletDb;
51
72
  }
52
73
  declare class Keymaster {
53
- private _gatekeeperConfig;
54
- private _walletConfig;
55
- private _walletDb;
56
- private _serviceStarted;
74
+ private config;
75
+ /**
76
+ * Underlying Keymaster libray based on the use-case:
77
+ *
78
+ * Integrated Keymaster = keymaster_lib
79
+ *
80
+ * External Keymaster = keymaster_sdk
81
+ */
82
+ private keymasterService;
83
+ private serviceStarted;
57
84
  constructor(config: KeymasterConfig);
58
85
  start(): Promise<boolean>;
86
+ private startIntegratedKeymaster;
87
+ private startExternalKeymaster;
59
88
  createChallenge(spec: CreateChallengeSpec, options?: CreateChallengeOptions): Promise<CreateChallengeResponse>;
89
+ showMnemonic(): Promise<string>;
60
90
  verifyResponse(did: string, options?: VerifyResponseOptions): Promise<VerifyResponseResponse>;
61
91
  private ensureWalletExists;
62
92
  private serviceRunning;
93
+ private validateConfig;
63
94
  }
64
95
 
65
- export { type CreateChallengeOptions, type CreateChallengeResponse, type CreateChallengeSpec, type GatekeeperConfig, Keymaster, type KeymasterConfig, type VerifyResponseOptions, type VerifyResponseResponse, type Wallet, type WalletConfig, type WalletSeed };
96
+ export { type CreateChallengeOptions, type CreateChallengeResponse, type CreateChallengeSpec, Keymaster, type KeymasterConfig, type SdkConfig, type VerifyResponseOptions, type VerifyResponseResponse, type Wallet, type WalletConfig, type WalletDb, type WalletSeed };
package/dist/index.d.ts CHANGED
@@ -32,7 +32,7 @@ interface Wallet {
32
32
  ids: any;
33
33
  current?: string;
34
34
  }
35
- interface GatekeeperConfig {
35
+ interface SdkConfig {
36
36
  url: string;
37
37
  waitUntilReady?: boolean;
38
38
  intervalSeconds?: number;
@@ -43,23 +43,54 @@ interface WalletConfig {
43
43
  mnemonic?: string;
44
44
  registry?: string;
45
45
  }
46
+ interface WalletDb {
47
+ loadWallet: () => Promise<Wallet | null>;
48
+ saveWallet: (w: Wallet, overwrite?: boolean) => Promise<boolean>;
49
+ }
46
50
  interface KeymasterConfig {
47
- gatekeeperConfig: GatekeeperConfig;
48
- walletConfig: WalletConfig;
49
- onSaveWallet: (w: Wallet, overwrite?: boolean) => Promise<boolean>;
50
- onLoadWallet: () => Promise<Wallet | null>;
51
+ /**
52
+ * Config for connecting to an external Keymaster service
53
+ * when the consuming applciation is connecting to an external Keymaster.
54
+ */
55
+ keymasterConfig?: SdkConfig;
56
+ /**
57
+ * Config for connecting to an external Keymaster service when
58
+ * the consuming applciation is acting as its own (integrated) Keymaster service.
59
+ */
60
+ gatekeeperConfig?: SdkConfig;
61
+ /**
62
+ * Config for custom user-defined values of the underlying wallet.
63
+ *
64
+ * Required for an integrated Keymaster.
65
+ */
66
+ walletConfig?: WalletConfig;
67
+ /**
68
+ * Config that contains the callbacks for persisting a
69
+ * wallet to a backing Database.
70
+ */
71
+ walletDb?: WalletDb;
51
72
  }
52
73
  declare class Keymaster {
53
- private _gatekeeperConfig;
54
- private _walletConfig;
55
- private _walletDb;
56
- private _serviceStarted;
74
+ private config;
75
+ /**
76
+ * Underlying Keymaster libray based on the use-case:
77
+ *
78
+ * Integrated Keymaster = keymaster_lib
79
+ *
80
+ * External Keymaster = keymaster_sdk
81
+ */
82
+ private keymasterService;
83
+ private serviceStarted;
57
84
  constructor(config: KeymasterConfig);
58
85
  start(): Promise<boolean>;
86
+ private startIntegratedKeymaster;
87
+ private startExternalKeymaster;
59
88
  createChallenge(spec: CreateChallengeSpec, options?: CreateChallengeOptions): Promise<CreateChallengeResponse>;
89
+ showMnemonic(): Promise<string>;
60
90
  verifyResponse(did: string, options?: VerifyResponseOptions): Promise<VerifyResponseResponse>;
61
91
  private ensureWalletExists;
62
92
  private serviceRunning;
93
+ private validateConfig;
63
94
  }
64
95
 
65
- export { type CreateChallengeOptions, type CreateChallengeResponse, type CreateChallengeSpec, type GatekeeperConfig, Keymaster, type KeymasterConfig, type VerifyResponseOptions, type VerifyResponseResponse, type Wallet, type WalletConfig, type WalletSeed };
96
+ export { type CreateChallengeOptions, type CreateChallengeResponse, type CreateChallengeSpec, Keymaster, type KeymasterConfig, type SdkConfig, type VerifyResponseOptions, type VerifyResponseResponse, type Wallet, type WalletConfig, type WalletDb, type WalletSeed };
package/dist/index.js CHANGED
@@ -142,25 +142,13 @@ function _ts_generator(thisArg, body) {
142
142
  };
143
143
  }
144
144
  }
145
- import * as cipher from "@mdip/cipher/node";
146
- import * as gatekeeper_sdk from "@mdip/gatekeeper/sdk";
147
- import * as keymaster_lib from "@mdip/keymaster/lib";
148
145
  var Keymaster = /*#__PURE__*/ function() {
149
146
  "use strict";
150
147
  function Keymaster(config) {
151
148
  _class_call_check(this, Keymaster);
152
- this._serviceStarted = false;
153
- this._gatekeeperConfig = {
154
- url: config.gatekeeperConfig.url,
155
- waitUntilReady: true,
156
- intervalSeconds: 5,
157
- chatty: true
158
- };
159
- this._walletConfig = config.walletConfig;
160
- this._walletDb = {
161
- saveWallet: config.onSaveWallet,
162
- loadWallet: config.onLoadWallet
163
- };
149
+ this.serviceStarted = false;
150
+ this.validateConfig(config);
151
+ this.config = config;
164
152
  }
165
153
  _create_class(Keymaster, [
166
154
  {
@@ -168,69 +156,92 @@ var Keymaster = /*#__PURE__*/ function() {
168
156
  value: function start() {
169
157
  var _this = this;
170
158
  return _async_to_generator(function() {
171
- var e, e1, e2;
172
159
  return _ts_generator(this, function(_state) {
173
160
  switch(_state.label){
174
161
  case 0:
175
- _state.trys.push([
176
- 0,
177
- 2,
178
- ,
179
- 3
180
- ]);
162
+ if (!_this.config.gatekeeperConfig) return [
163
+ 3,
164
+ 2
165
+ ];
181
166
  return [
182
167
  4,
183
- gatekeeper_sdk.start(_this._gatekeeperConfig)
168
+ _this.startIntegratedKeymaster()
184
169
  ];
185
170
  case 1:
186
- _state.sent();
171
+ _this.serviceStarted = _state.sent();
187
172
  return [
188
173
  3,
189
174
  3
190
175
  ];
191
176
  case 2:
192
- e = _state.sent();
193
- console.error("Error starting Gatekeeper service:", e);
177
+ if (_this.config.keymasterConfig) {
178
+ _this.serviceStarted = _this.startExternalKeymaster();
179
+ } else {
180
+ throw "Missing Gatekeeper or Keymaster config";
181
+ }
182
+ _state.label = 3;
183
+ case 3:
194
184
  return [
195
- 3,
196
- 3
185
+ 2,
186
+ _this.serviceStarted
197
187
  ];
198
- case 3:
188
+ }
189
+ });
190
+ })();
191
+ }
192
+ },
193
+ {
194
+ key: "startIntegratedKeymaster",
195
+ value: function startIntegratedKeymaster() {
196
+ var _this = this;
197
+ return _async_to_generator(function() {
198
+ var gatekeeper_sdk, cipher, e;
199
+ return _ts_generator(this, function(_state) {
200
+ switch(_state.label){
201
+ case 0:
202
+ console.log("Starting integrated Keymaster service");
203
+ _state.label = 1;
204
+ case 1:
199
205
  _state.trys.push([
200
- 3,
201
- 5,
206
+ 1,
207
+ 8,
202
208
  ,
203
- 6
209
+ 9
204
210
  ]);
205
211
  return [
206
212
  4,
207
- keymaster_lib.start({
208
- gatekeeper: gatekeeper_sdk,
209
- wallet: _this._walletDb,
210
- cipher: cipher
211
- })
213
+ import("@mdip/gatekeeper/sdk")
212
214
  ];
213
- case 4:
215
+ case 2:
216
+ gatekeeper_sdk = _state.sent();
217
+ return [
218
+ 4,
219
+ gatekeeper_sdk.start(_this.config.gatekeeperConfig)
220
+ ];
221
+ case 3:
214
222
  _state.sent();
215
- _this._serviceStarted = true;
216
223
  return [
217
- 3,
218
- 6
224
+ 4,
225
+ import("@mdip/cipher/node")
226
+ ];
227
+ case 4:
228
+ cipher = _state.sent();
229
+ return [
230
+ 4,
231
+ import("@mdip/keymaster/lib")
219
232
  ];
220
233
  case 5:
221
- e1 = _state.sent();
222
- console.error("Error starting Keymaster service:", e1);
234
+ _this.keymasterService = _state.sent();
223
235
  return [
224
- 3,
225
- 6
236
+ 4,
237
+ _this.keymasterService.start({
238
+ cipher: cipher,
239
+ gatekeeper: gatekeeper_sdk,
240
+ wallet: _this.config.walletDb
241
+ })
226
242
  ];
227
243
  case 6:
228
- _state.trys.push([
229
- 6,
230
- 8,
231
- ,
232
- 9
233
- ]);
244
+ _state.sent();
234
245
  return [
235
246
  4,
236
247
  _this.ensureWalletExists()
@@ -242,16 +253,69 @@ var Keymaster = /*#__PURE__*/ function() {
242
253
  9
243
254
  ];
244
255
  case 8:
245
- e2 = _state.sent();
246
- console.error("Error verifying existing wallet:", e2);
256
+ e = _state.sent();
257
+ console.error("Error starting Keymaster service:", e);
247
258
  return [
248
- 3,
249
- 9
259
+ 2,
260
+ false
250
261
  ];
251
262
  case 9:
263
+ console.log("Started integrated Keymaster service");
252
264
  return [
253
265
  2,
254
- _this._serviceStarted
266
+ true
267
+ ];
268
+ }
269
+ });
270
+ })();
271
+ }
272
+ },
273
+ {
274
+ key: "startExternalKeymaster",
275
+ value: function startExternalKeymaster() {
276
+ var _this = this;
277
+ return _async_to_generator(function() {
278
+ var e;
279
+ return _ts_generator(this, function(_state) {
280
+ switch(_state.label){
281
+ case 0:
282
+ console.log("Starting external Keymaster service");
283
+ _state.label = 1;
284
+ case 1:
285
+ _state.trys.push([
286
+ 1,
287
+ 4,
288
+ ,
289
+ 5
290
+ ]);
291
+ return [
292
+ 4,
293
+ import("@mdip/keymaster/sdk")
294
+ ];
295
+ case 2:
296
+ _this.keymasterService = _state.sent();
297
+ return [
298
+ 4,
299
+ _this.keymasterService.start(_this.config.keymasterConfig)
300
+ ];
301
+ case 3:
302
+ _state.sent();
303
+ return [
304
+ 3,
305
+ 5
306
+ ];
307
+ case 4:
308
+ e = _state.sent();
309
+ console.error("Error starting Keymaster service:", e);
310
+ return [
311
+ 2,
312
+ false
313
+ ];
314
+ case 5:
315
+ console.log("Started external Keymaster service");
316
+ return [
317
+ 2,
318
+ true
255
319
  ];
256
320
  }
257
321
  });
@@ -277,7 +341,7 @@ var Keymaster = /*#__PURE__*/ function() {
277
341
  };
278
342
  return [
279
343
  4,
280
- keymaster_lib.createChallenge(challengeSpec, options)
344
+ _this.keymasterService.createChallenge(challengeSpec, options)
281
345
  ];
282
346
  case 1:
283
347
  challenge = _state.sent();
@@ -293,6 +357,25 @@ var Keymaster = /*#__PURE__*/ function() {
293
357
  })();
294
358
  }
295
359
  },
360
+ {
361
+ key: "showMnemonic",
362
+ value: function showMnemonic() {
363
+ var _this = this;
364
+ return _async_to_generator(function() {
365
+ return _ts_generator(this, function(_state) {
366
+ if (!_this.serviceRunning()) {
367
+ return [
368
+ 2
369
+ ];
370
+ }
371
+ return [
372
+ 2,
373
+ _this.keymasterService.decryptMnemonic()
374
+ ];
375
+ });
376
+ })();
377
+ }
378
+ },
296
379
  {
297
380
  key: "verifyResponse",
298
381
  value: function verifyResponse(did, options) {
@@ -309,7 +392,7 @@ var Keymaster = /*#__PURE__*/ function() {
309
392
  }
310
393
  return [
311
394
  4,
312
- keymaster_lib.verifyResponse(did, options)
395
+ _this.keymasterService.verifyResponse(did, options)
313
396
  ];
314
397
  case 1:
315
398
  response = _state.sent();
@@ -327,13 +410,13 @@ var Keymaster = /*#__PURE__*/ function() {
327
410
  value: function ensureWalletExists() {
328
411
  var _this = this;
329
412
  return _async_to_generator(function() {
330
- var existing;
413
+ var existing, walletConfig;
331
414
  return _ts_generator(this, function(_state) {
332
415
  switch(_state.label){
333
416
  case 0:
334
417
  return [
335
418
  4,
336
- _this._walletDb.loadWallet()
419
+ _this.config.walletDb.loadWallet()
337
420
  ];
338
421
  case 1:
339
422
  existing = _state.sent();
@@ -343,14 +426,15 @@ var Keymaster = /*#__PURE__*/ function() {
343
426
  2
344
427
  ];
345
428
  }
346
- console.log("Creating wallet with ID ".concat(_this._walletConfig.id));
347
- if (!_this._walletConfig.mnemonic) return [
429
+ walletConfig = _this.config.walletConfig;
430
+ console.log("Creating wallet with ID ".concat(walletConfig.id));
431
+ if (!walletConfig.mnemonic) return [
348
432
  3,
349
433
  3
350
434
  ];
351
435
  return [
352
436
  4,
353
- keymaster_lib.newWallet(spec.mnenomic, true)
437
+ _this.keymasterService.newWallet(spec.mnenomic, true)
354
438
  ];
355
439
  case 2:
356
440
  _state.sent();
@@ -358,11 +442,11 @@ var Keymaster = /*#__PURE__*/ function() {
358
442
  case 3:
359
443
  return [
360
444
  4,
361
- keymaster_lib.createId(_this._walletConfig.id, _this._walletConfig.registry)
445
+ _this.keymasterService.createId(walletConfig.id, walletConfig.registry)
362
446
  ];
363
447
  case 4:
364
448
  _state.sent();
365
- console.log("Created wallet with ID ".concat(_this._walletConfig.id));
449
+ console.log("Created wallet with ID ".concat(walletConfig.id));
366
450
  return [
367
451
  2
368
452
  ];
@@ -380,7 +464,7 @@ var Keymaster = /*#__PURE__*/ function() {
380
464
  return _ts_generator(this, function(_state) {
381
465
  switch(_state.label){
382
466
  case 0:
383
- _tmp = _this._serviceStarted;
467
+ _tmp = _this.serviceStarted;
384
468
  if (_tmp) return [
385
469
  3,
386
470
  2
@@ -401,6 +485,26 @@ var Keymaster = /*#__PURE__*/ function() {
401
485
  });
402
486
  })();
403
487
  }
488
+ },
489
+ {
490
+ key: "validateConfig",
491
+ value: function validateConfig(config) {
492
+ if (config.gatekeeperConfig && config.keymasterConfig) {
493
+ throw new Error("Cannot provide both a Gatekeeper and Keymaster config");
494
+ }
495
+ if (config.gatekeeperConfig) {
496
+ var _config_walletDb, _config_walletDb1;
497
+ if (!config.walletConfig) {
498
+ throw new Error("Missing wallet config");
499
+ }
500
+ if (!((_config_walletDb = config.walletDb) === null || _config_walletDb === void 0 ? void 0 : _config_walletDb.loadWallet)) {
501
+ throw new Error("Missing load wallet callback");
502
+ }
503
+ if (!((_config_walletDb1 = config.walletDb) === null || _config_walletDb1 === void 0 ? void 0 : _config_walletDb1.saveWallet)) {
504
+ throw new Error("Missing save wallet callback");
505
+ }
506
+ }
507
+ }
404
508
  }
405
509
  ]);
406
510
  return Keymaster;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["/home/runner/work/siwys-js/siwys-js/packages/api-js/dist/index.js","../src/keymaster.ts"],"names":["cipher","gatekeeper_sdk","keymaster_lib","Keymaster","config","_serviceStarted","_gatekeeperConfig","url","gatekeeperConfig","waitUntilReady","intervalSeconds","chatty","_walletConfig","walletConfig","_walletDb","saveWallet","onSaveWallet","loadWallet","onLoadWallet","start","e","console","error","gatekeeper","wallet","ensureWalletExists","createChallenge","spec","options","challengeSpec","challenge","serviceRunning","challengeUrl","callback","verifyResponse","did","response","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;;gBAC7D;;;;YAEcT,KAAAA;mBAAd,SAAcA;;uBAAd,oBAAA;;;;;uCACS,MAAK1B,eAAA;0CAAL;;;;gCAAyB;;oCAAM,MAAKc,KAAA;;;uCAAX;;;gCAAhC;;;;;;gBACF;;;;;;ADtFF,SACEhB,SAAS,GACT","sourcesContent":["// src/keymaster.ts\nimport * as cipher from \"@mdip/cipher/node\";\nimport * as gatekeeper_sdk from \"@mdip/gatekeeper/sdk\";\nimport * as keymaster_lib from \"@mdip/keymaster/lib\";\nvar Keymaster = class {\n constructor(config) {\n this._serviceStarted = false;\n this._gatekeeperConfig = {\n url: config.gatekeeperConfig.url,\n waitUntilReady: true,\n intervalSeconds: 5,\n chatty: true\n };\n this._walletConfig = config.walletConfig;\n this._walletDb = {\n saveWallet: config.onSaveWallet,\n loadWallet: config.onLoadWallet\n };\n }\n async start() {\n try {\n await gatekeeper_sdk.start(this._gatekeeperConfig);\n } catch (e) {\n console.error(\"Error starting Gatekeeper service:\", e);\n }\n try {\n await keymaster_lib.start({\n gatekeeper: gatekeeper_sdk,\n wallet: this._walletDb,\n cipher\n });\n this._serviceStarted = true;\n } catch (e) {\n console.error(\"Error starting Keymaster service:\", e);\n }\n try {\n await this.ensureWalletExists();\n } catch (e) {\n console.error(\"Error verifying existing wallet:\", e);\n }\n return this._serviceStarted;\n }\n async createChallenge(spec2, options) {\n if (!this.serviceRunning()) {\n return;\n }\n const challengeSpec = { challenge: spec2 };\n const challenge = await keymaster_lib.createChallenge(\n challengeSpec,\n options\n );\n return {\n challenge,\n challengeUrl: `${spec2.callback}?challenge=${challenge}`\n };\n }\n async verifyResponse(did, options) {\n if (!this.serviceRunning()) {\n return;\n }\n const response = await keymaster_lib.verifyResponse(\n did,\n options\n );\n return response;\n }\n async ensureWalletExists() {\n const existing = await this._walletDb.loadWallet();\n if (existing?.current) {\n console.log(`Using existing wallet with ID ${existing.current}`);\n return;\n }\n console.log(`Creating wallet with ID ${this._walletConfig.id}`);\n if (this._walletConfig.mnemonic) {\n await keymaster_lib.newWallet(spec.mnenomic, true);\n }\n await keymaster_lib.createId(\n this._walletConfig.id,\n this._walletConfig.registry\n );\n console.log(`Created wallet with ID ${this._walletConfig.id}`);\n }\n async serviceRunning() {\n return this._serviceStarted || await this.start();\n }\n};\nexport {\n Keymaster\n};\n","// @ts-ignore\n// @ts-nocheck\nimport * as cipher from \"@mdip/cipher/node\";\nimport * as gatekeeper_sdk from \"@mdip/gatekeeper/sdk\";\nimport * as keymaster_lib from \"@mdip/keymaster/lib\";\n\nimport { initalizeWalletDb, DbType } from \"./db\";\nimport * as wallet_db from \"./db/test\";\n\nexport interface CreateChallengeSpec {\n callback: string;\n credentials?: [];\n}\n\nexport interface CreateChallengeOptions {\n registry?: string;\n validUntil?: string; // ISO string\n}\n\nexport interface CreateChallengeResponse {\n challenge: string; // DID\n challengeUrl: string;\n}\n\nexport interface VerifyResponseOptions {\n retries?: number;\n delay?: number;\n}\n\nexport interface VerifyResponseResponse {\n match: boolean;\n challenge: string;\n responder: string;\n}\n\nexport interface WalletSeed {\n mnemonic: string;\n hdkey: {\n xpriv: string;\n xpub: string;\n };\n}\n\nexport interface Wallet {\n seed: WalletSeed;\n counter: number;\n ids: any;\n current?: string;\n}\n\nexport interface GatekeeperConfig {\n url: string;\n waitUntilReady?: boolean;\n intervalSeconds?: number;\n chatty?: boolean;\n}\n\nexport interface WalletConfig {\n id: string;\n mnemonic?: string;\n registry?: string;\n}\n\nexport interface KeymasterConfig {\n gatekeeperConfig: GatekeeperConfig;\n walletConfig: WalletConfig;\n onSaveWallet: (w: Wallet, overwrite?: boolean) => Promise<boolean>;\n onLoadWallet: () => Promise<Wallet | null>;\n}\n\nexport class Keymaster {\n private _gatekeeperConfig;\n private _walletConfig;\n private _walletDb;\n private _serviceStarted = false;\n\n constructor(config: KeymasterConfig) {\n this._gatekeeperConfig = {\n url: config.gatekeeperConfig.url,\n waitUntilReady: true,\n intervalSeconds: 5,\n chatty: true,\n };\n this._walletConfig = config.walletConfig;\n this._walletDb = {\n saveWallet: config.onSaveWallet,\n loadWallet: config.onLoadWallet,\n };\n }\n\n public async start(): Promise<boolean> {\n try {\n await gatekeeper_sdk.start(this._gatekeeperConfig);\n } catch (e) {\n console.error(\"Error starting Gatekeeper service:\", e);\n }\n\n try {\n await keymaster_lib.start({\n gatekeeper: gatekeeper_sdk,\n wallet: this._walletDb,\n cipher: cipher,\n });\n this._serviceStarted = true;\n } catch (e) {\n console.error(\"Error starting Keymaster service:\", e);\n }\n\n try {\n await this.ensureWalletExists();\n } catch (e) {\n console.error(\"Error verifying existing wallet:\", e);\n }\n\n return this._serviceStarted;\n }\n\n async createChallenge(\n spec: CreateChallengeSpec,\n options?: CreateChallengeOptions\n ): Promise<CreateChallengeResponse> {\n if (!this.serviceRunning()) {\n return;\n }\n const challengeSpec = { challenge: spec };\n const challenge: string = await keymaster_lib.createChallenge(\n challengeSpec,\n options\n );\n return {\n challenge: challenge,\n challengeUrl: `${spec.callback}?challenge=${challenge}`,\n };\n }\n\n async verifyResponse(\n did: string,\n options?: VerifyResponseOptions\n ): Promise<VerifyResponseResponse> {\n if (!this.serviceRunning()) {\n return;\n }\n const response: VerifyResponseResponse = await keymaster_lib.verifyResponse(\n did,\n options\n );\n return response;\n }\n\n private async ensureWalletExists(): Promise<void> {\n const existing: Wallet | null = await this._walletDb.loadWallet();\n if (existing?.current) {\n console.log(`Using existing wallet with ID ${existing.current}`);\n return;\n }\n\n console.log(`Creating wallet with ID ${this._walletConfig.id}`);\n // wallet was just created, recreate based on config\n if (this._walletConfig.mnemonic) {\n // create the wallet with a custom mnemonic first\n await keymaster_lib.newWallet(spec.mnenomic, true);\n }\n // create ID and set as current, createId() will save wallet\n await keymaster_lib.createId(\n this._walletConfig.id,\n this._walletConfig.registry\n );\n console.log(`Created wallet with ID ${this._walletConfig.id}`);\n }\n\n private async serviceRunning(): Promise<boolean> {\n return this._serviceStarted || (await this.start());\n }\n}\n"]}
1
+ {"version":3,"sources":["/home/runner/work/siwys-js/siwys-js/packages/api-js/dist/index.js","../src/keymaster.ts"],"names":["Keymaster","config","serviceStarted","validateConfig","start","gatekeeperConfig","startIntegratedKeymaster","keymasterConfig","startExternalKeymaster","gatekeeper_sdk","cipher","e","console","log","keymasterService","gatekeeper","wallet","walletDb","ensureWalletExists","error","createChallenge","spec","options","challengeSpec","challenge","serviceRunning","challengeUrl","callback","showMnemonic","decryptMnemonic","verifyResponse","did","response","existing","walletConfig","loadWallet","current","id","mnemonic","newWallet","mnenomic","createId","registry","Error","saveWallet"],"mappings":"AAAA,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACyFZ,IAAMA,0BAAN;;aAAMA,UAYCC,MAAA;gCAZDD;QAUX,IAAA,CAAQE,cAAA,GAAiB;QAGvB,IAAA,CAAKC,cAAA,CAAeF;QACpB,IAAA,CAAKA,MAAA,GAASA;;;;YAGHG,KAAAA;mBAAb,SAAaA;;uBAAb,oBAAA;;;;qCACM,MAAKH,MAAA,CAAOI,gBAAA,EAAZ;;;;gCACoB;;oCAAM,MAAKC,wBAAA;;;gCAAjC,MAAKJ,cAAA,GAAiB;;;;;;gCACxB,IAAW,MAAKD,MAAA,CAAOM,eAAA,EAAiB;oCACtC,MAAKL,cAAA,GAAiB,MAAKM,sBAAA;gCAC7B,OAAO;oCACL,MAAM;gCACR;;;gCACA;;oCAAO,MAAKN,cAAA;;;;gBACd;;;;YAEcI,KAAAA;mBAAd,SAAcA;;uBAAd,oBAAA;wBAIUG,gBAGAC,QAUCC;;;;gCAhBTC,QAAQC,GAAA,CAAI;;;;;;;;;gCAGa;;oCAAM,MAAA,CAAO;;;gCAA9BJ,iBAAiB;gCACvB;;oCAAMA,eAAeL,KAAA,CAAM,MAAKH,MAAA,CAAOI,gBAAgB;;;gC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AiB;oBACrD,MAAM,IAAIoC,MAAM;gBAClB;gBAEA,IAAI1C,OAAOI,gBAAA,EAAkB;wBAItBJ,kBAGAA;oBANL,IAAI,CAACA,OAAOiC,YAAA,EAAc;wBACxB,MAAM,IAAIS,MAAM;oBAClB;oBACA,IAAI,GAAC1C,mBAAAA,OAAOgB,QAAA,cAAPhB,uCAAAA,iBAAiBkC,UAAA,GAAY;wBAChC,MAAM,IAAIQ,MAAM;oBAClB;oBACA,IAAI,GAAC1C,oBAAAA,OAAOgB,QAAA,cAAPhB,wCAAAA,kBAAiB2C,UAAA,GAAY;wBAChC,MAAM,IAAID,MAAM;oBAClB;gBACF;YACF;;;;;AD3HF,SACE3C,SAAS,GACT","sourcesContent":["// src/keymaster.ts\nvar Keymaster = class {\n constructor(config) {\n this.serviceStarted = false;\n this.validateConfig(config);\n this.config = config;\n }\n async start() {\n if (this.config.gatekeeperConfig) {\n this.serviceStarted = await this.startIntegratedKeymaster();\n } else if (this.config.keymasterConfig) {\n this.serviceStarted = this.startExternalKeymaster();\n } else {\n throw \"Missing Gatekeeper or Keymaster config\";\n }\n return this.serviceStarted;\n }\n async startIntegratedKeymaster() {\n console.log(`Starting integrated Keymaster service`);\n try {\n const gatekeeper_sdk = await import(\"@mdip/gatekeeper/sdk\");\n await gatekeeper_sdk.start(this.config.gatekeeperConfig);\n const cipher = await import(\"@mdip/cipher/node\");\n this.keymasterService = await import(\"@mdip/keymaster/lib\");\n await this.keymasterService.start({\n cipher,\n gatekeeper: gatekeeper_sdk,\n wallet: this.config.walletDb\n });\n await this.ensureWalletExists();\n } catch (e) {\n console.error(\"Error starting Keymaster service:\", e);\n return false;\n }\n console.log(`Started integrated Keymaster service`);\n return true;\n }\n async startExternalKeymaster() {\n console.log(`Starting external Keymaster service`);\n try {\n this.keymasterService = await import(\"@mdip/keymaster/sdk\");\n await this.keymasterService.start(this.config.keymasterConfig);\n } catch (e) {\n console.error(`Error starting Keymaster service:`, e);\n return false;\n }\n console.log(`Started external Keymaster service`);\n return true;\n }\n async createChallenge(spec2, options) {\n if (!this.serviceRunning()) {\n return;\n }\n const challengeSpec = { challenge: spec2 };\n const challenge = await this.keymasterService.createChallenge(\n challengeSpec,\n options\n );\n return {\n challenge,\n challengeUrl: `${spec2.callback}?challenge=${challenge}`\n };\n }\n async showMnemonic() {\n if (!this.serviceRunning()) {\n return;\n }\n return this.keymasterService.decryptMnemonic();\n }\n async verifyResponse(did, options) {\n if (!this.serviceRunning()) {\n return;\n }\n const response = await this.keymasterService.verifyResponse(did, options);\n return response;\n }\n async ensureWalletExists() {\n const existing = await this.config.walletDb.loadWallet();\n if (existing?.current) {\n console.log(`Using existing wallet with ID ${existing.current}`);\n return;\n }\n const walletConfig = this.config.walletConfig;\n console.log(`Creating wallet with ID ${walletConfig.id}`);\n if (walletConfig.mnemonic) {\n await this.keymasterService.newWallet(spec.mnenomic, true);\n }\n await this.keymasterService.createId(\n walletConfig.id,\n walletConfig.registry\n );\n console.log(`Created wallet with ID ${walletConfig.id}`);\n }\n async serviceRunning() {\n return this.serviceStarted || await this.start();\n }\n validateConfig(config) {\n if (config.gatekeeperConfig && config.keymasterConfig) {\n throw new Error(\"Cannot provide both a Gatekeeper and Keymaster config\");\n }\n if (config.gatekeeperConfig) {\n if (!config.walletConfig) {\n throw new Error(\"Missing wallet config\");\n }\n if (!config.walletDb?.loadWallet) {\n throw new Error(\"Missing load wallet callback\");\n }\n if (!config.walletDb?.saveWallet) {\n throw new Error(\"Missing save wallet callback\");\n }\n }\n }\n};\nexport {\n Keymaster\n};\n","// @ts-ignore\n// @ts-nocheck\nimport { initalizeWalletDb, DbType } from \"./db\";\nimport * as wallet_db from \"./db/test\";\n\nexport interface CreateChallengeSpec {\n callback: string;\n credentials?: [];\n}\n\nexport interface CreateChallengeOptions {\n registry?: string;\n validUntil?: string; // ISO string\n}\n\nexport interface CreateChallengeResponse {\n challenge: string; // DID\n challengeUrl: string;\n}\n\nexport interface VerifyResponseOptions {\n retries?: number;\n delay?: number;\n}\n\nexport interface VerifyResponseResponse {\n match: boolean;\n challenge: string;\n responder: string;\n}\n\nexport interface WalletSeed {\n mnemonic: string;\n hdkey: {\n xpriv: string;\n xpub: string;\n };\n}\n\nexport interface Wallet {\n seed: WalletSeed;\n counter: number;\n ids: any;\n current?: string;\n}\n\n// config for connecting to an external Gatekeeper or Keymaster\nexport interface SdkConfig {\n url: string;\n waitUntilReady?: boolean;\n intervalSeconds?: number;\n chatty?: boolean;\n}\n\nexport interface WalletConfig {\n id: string;\n mnemonic?: string;\n registry?: string;\n}\n\nexport interface WalletDb {\n loadWallet: () => Promise<Wallet | null>;\n saveWallet: (w: Wallet, overwrite?: boolean) => Promise<boolean>;\n}\n\nexport interface KeymasterConfig {\n /**\n * Config for connecting to an external Keymaster service\n * when the consuming applciation is connecting to an external Keymaster.\n */\n keymasterConfig?: SdkConfig;\n /**\n * Config for connecting to an external Keymaster service when\n * the consuming applciation is acting as its own (integrated) Keymaster service.\n */\n gatekeeperConfig?: SdkConfig;\n /**\n * Config for custom user-defined values of the underlying wallet.\n *\n * Required for an integrated Keymaster.\n */\n walletConfig?: WalletConfig;\n /**\n * Config that contains the callbacks for persisting a\n * wallet to a backing Database.\n */\n walletDb?: WalletDb;\n}\n\nexport class Keymaster {\n private config: KeymasterConfig;\n /**\n * Underlying Keymaster libray based on the use-case:\n *\n * Integrated Keymaster = keymaster_lib\n *\n * External Keymaster = keymaster_sdk\n */\n private keymasterService;\n private serviceStarted = false;\n\n constructor(config: KeymasterConfig) {\n this.validateConfig(config);\n this.config = config;\n }\n\n public async start(): Promise<boolean> {\n if (this.config.gatekeeperConfig) {\n this.serviceStarted = await this.startIntegratedKeymaster();\n } else if (this.config.keymasterConfig) {\n this.serviceStarted = this.startExternalKeymaster();\n } else {\n throw \"Missing Gatekeeper or Keymaster config\";\n }\n return this.serviceStarted;\n }\n\n private async startIntegratedKeymaster(): Promise<boolean> {\n console.log(`Starting integrated Keymaster service`);\n\n try {\n const gatekeeper_sdk = await import(\"@mdip/gatekeeper/sdk\");\n await gatekeeper_sdk.start(this.config.gatekeeperConfig);\n\n const cipher = await import(\"@mdip/cipher/node\");\n\n this.keymasterService = await import(\"@mdip/keymaster/lib\");\n await this.keymasterService.start({\n cipher: cipher,\n gatekeeper: gatekeeper_sdk,\n wallet: this.config.walletDb,\n });\n\n await this.ensureWalletExists();\n } catch (e) {\n console.error(\"Error starting Keymaster service:\", e);\n return false;\n }\n\n console.log(`Started integrated Keymaster service`);\n return true;\n }\n\n private async startExternalKeymaster(): Promise<boolean> {\n console.log(`Starting external Keymaster service`);\n try {\n this.keymasterService = await import(\"@mdip/keymaster/sdk\");\n await this.keymasterService.start(this.config.keymasterConfig);\n } catch (e) {\n console.error(`Error starting Keymaster service:`, e);\n return false;\n }\n\n console.log(`Started external Keymaster service`);\n return true;\n }\n\n async createChallenge(\n spec: CreateChallengeSpec,\n options?: CreateChallengeOptions\n ): Promise<CreateChallengeResponse> {\n if (!this.serviceRunning()) {\n return;\n }\n const challengeSpec = { challenge: spec };\n const challenge: string = await this.keymasterService.createChallenge(\n challengeSpec,\n options\n );\n return {\n challenge: challenge,\n challengeUrl: `${spec.callback}?challenge=${challenge}`,\n };\n }\n\n async showMnemonic(): Promise<string> {\n if (!this.serviceRunning()) {\n return;\n }\n return this.keymasterService.decryptMnemonic();\n }\n\n async verifyResponse(\n did: string,\n options?: VerifyResponseOptions\n ): Promise<VerifyResponseResponse> {\n if (!this.serviceRunning()) {\n return;\n }\n const response: VerifyResponseResponse =\n await this.keymasterService.verifyResponse(did, options);\n return response;\n }\n\n private async ensureWalletExists(): Promise<void> {\n const existing: Wallet | null = await this.config.walletDb.loadWallet();\n if (existing?.current) {\n console.log(`Using existing wallet with ID ${existing.current}`);\n return;\n }\n\n const walletConfig = this.config.walletConfig;\n console.log(`Creating wallet with ID ${walletConfig.id}`);\n // wallet was just created, recreate based on config\n if (walletConfig.mnemonic) {\n // create the wallet with a custom mnemonic first\n await this.keymasterService.newWallet(spec.mnenomic, true);\n }\n // create ID and set as current, createId() will save wallet\n await this.keymasterService.createId(\n walletConfig.id,\n walletConfig.registry\n );\n console.log(`Created wallet with ID ${walletConfig.id}`);\n }\n\n private async serviceRunning(): Promise<boolean> {\n return this.serviceStarted || (await this.start());\n }\n\n private validateConfig(config: KeymasterConfig): void {\n if (config.gatekeeperConfig && config.keymasterConfig) {\n throw new Error(\"Cannot provide both a Gatekeeper and Keymaster config\");\n }\n\n if (config.gatekeeperConfig) {\n if (!config.walletConfig) {\n throw new Error(\"Missing wallet config\");\n }\n if (!config.walletDb?.loadWallet) {\n throw new Error(\"Missing load wallet callback\");\n }\n if (!config.walletDb?.saveWallet) {\n throw new Error(\"Missing save wallet callback\");\n }\n }\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yourself_id/siwys-api-js",
3
- "version": "0.0.11",
3
+ "version": "0.0.13-bump-mdip.0+2f567ea",
4
4
  "description": "A React component library",
5
5
  "repository": "https://github.com/selfidhq/siwys-js",
6
6
  "main": "dist/index.js",
@@ -20,8 +20,8 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@mdip/cipher": "0.4.0",
23
- "@mdip/gatekeeper": "0.4.0",
24
- "@mdip/keymaster": "0.4.0"
23
+ "@mdip/gatekeeper": "0.4.1",
24
+ "@mdip/keymaster": "0.4.1"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@babel/preset-env": "^7.25.8",
@@ -35,5 +35,5 @@
35
35
  "tsup": "^8.3.0",
36
36
  "typescript": "4.9.4"
37
37
  },
38
- "gitHead": "7363720ff07c9621db3ad67ef782d5a5e76343ea"
38
+ "gitHead": "2f567eaf90c7c9a1957d359e67f452577c6ad08e"
39
39
  }