@enbox/dids 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/browser.mjs +1 -1
  2. package/dist/browser.mjs.map +4 -4
  3. package/dist/esm/index.js +2 -0
  4. package/dist/esm/index.js.map +1 -1
  5. package/dist/esm/methods/did-dht.js.map +1 -1
  6. package/dist/esm/methods/did-ion.js.map +1 -1
  7. package/dist/esm/methods/did-jwk.js.map +1 -1
  8. package/dist/esm/methods/did-key.js.map +1 -1
  9. package/dist/esm/resolver/resolver-cache-memory.js +77 -0
  10. package/dist/esm/resolver/resolver-cache-memory.js.map +1 -0
  11. package/dist/esm/utils.js +25 -0
  12. package/dist/esm/utils.js.map +1 -1
  13. package/dist/types/bearer-did.d.ts +4 -4
  14. package/dist/types/bearer-did.d.ts.map +1 -1
  15. package/dist/types/index.d.ts +2 -0
  16. package/dist/types/index.d.ts.map +1 -1
  17. package/dist/types/methods/did-dht.d.ts +3 -3
  18. package/dist/types/methods/did-dht.d.ts.map +1 -1
  19. package/dist/types/methods/did-ion.d.ts +3 -3
  20. package/dist/types/methods/did-ion.d.ts.map +1 -1
  21. package/dist/types/methods/did-jwk.d.ts +4 -4
  22. package/dist/types/methods/did-jwk.d.ts.map +1 -1
  23. package/dist/types/methods/did-key.d.ts +4 -4
  24. package/dist/types/methods/did-key.d.ts.map +1 -1
  25. package/dist/types/methods/did-method.d.ts +3 -3
  26. package/dist/types/methods/did-method.d.ts.map +1 -1
  27. package/dist/types/resolver/resolver-cache-memory.d.ts +58 -0
  28. package/dist/types/resolver/resolver-cache-memory.d.ts.map +1 -0
  29. package/dist/types/utils.d.ts +19 -0
  30. package/dist/types/utils.d.ts.map +1 -1
  31. package/dist/utils.js +1 -1
  32. package/dist/utils.js.map +4 -4
  33. package/package.json +11 -13
  34. package/src/bearer-did.ts +4 -4
  35. package/src/index.ts +2 -0
  36. package/src/methods/did-dht.ts +3 -3
  37. package/src/methods/did-ion.ts +10 -3
  38. package/src/methods/did-jwk.ts +4 -4
  39. package/src/methods/did-key.ts +6 -6
  40. package/src/methods/did-method.ts +3 -3
  41. package/src/resolver/resolver-cache-memory.ts +84 -0
  42. package/src/utils.ts +27 -0
  43. package/dist/browser.js +0 -73
  44. package/dist/browser.js.map +0 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enbox/dids",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "TBD DIDs library",
5
5
  "type": "module",
6
6
  "main": "./dist/esm/index.js",
@@ -9,14 +9,14 @@
9
9
  "scripts": {
10
10
  "clean": "rimraf dist",
11
11
  "build:esm": "rimraf dist/esm dist/types && bun tsc -p tsconfig.json",
12
- "build:browser": "rimraf dist/browser.mjs dist/browser.js && bun build/bundles.js",
13
- "build:tests:browser": "rimraf tests/compiled && bun build/esbuild-tests.cjs",
12
+ "build:browser": "rimraf dist/browser.mjs && bun ../../build/browser-bundle.js --extra-entry src/utils.ts:dist/utils.js",
14
13
  "build": "bun run clean && bun run build:esm && bun run build:browser",
15
14
  "lint": "eslint . --max-warnings 0",
16
15
  "lint:fix": "eslint . --fix",
17
16
  "test:node": "bun test tests/",
18
17
  "test:node:coverage": "bun test --coverage --coverage-reporter=text --coverage-reporter=lcov --coverage-dir=coverage tests/",
19
- "test:browser": "bun run build:tests:browser && bunx web-test-runner"
18
+ "test:browser": "bunx --bun vitest --config vitest.browser.config.ts --run",
19
+ "test:browser:coverage": "bunx --bun vitest --config vitest.browser.config.ts --run --coverage --coverage.provider=istanbul --coverage.reportsDirectory=./coverage-browser"
20
20
  },
21
21
  "homepage": "https://github.com/enboxorg/enbox/tree/main/packages/dids#readme",
22
22
  "bugs": "https://github.com/enboxorg/enbox/issues",
@@ -50,10 +50,12 @@
50
50
  ],
51
51
  "exports": {
52
52
  ".": {
53
+ "browser": "./dist/browser.mjs",
53
54
  "types": "./dist/types/index.d.ts",
54
55
  "import": "./dist/esm/index.js"
55
56
  },
56
57
  "./utils": {
58
+ "browser": "./dist/utils.js",
57
59
  "types": "./dist/types/utils.d.ts",
58
60
  "import": "./dist/esm/utils.js"
59
61
  }
@@ -79,28 +81,24 @@
79
81
  "@decentralized-identity/ion-sdk": "1.0.4",
80
82
  "@dnsquery/dns-packet": "6.1.1",
81
83
  "@enbox/common": "0.0.3",
82
- "@enbox/crypto": "0.0.3",
84
+ "@enbox/crypto": "0.0.4",
83
85
  "abstract-level": "1.0.4",
84
86
  "bencode": "4.0.0",
85
87
  "level": "8.0.1",
86
88
  "ms": "2.1.3"
87
89
  },
88
90
  "devDependencies": {
89
- "buffer": "6.0.3",
90
- "@playwright/test": "1.45.3",
91
91
  "@types/bencode": "2.0.4",
92
92
  "@types/ms": "0.7.34",
93
93
  "@types/node": "20.14.8",
94
94
  "@typescript-eslint/eslint-plugin": "8.32.1",
95
95
  "@typescript-eslint/parser": "8.32.1",
96
- "@web/test-runner": "0.18.2",
97
- "@web/test-runner-playwright": "0.11.0",
96
+ "@vitest/browser-playwright": "4.0.18",
97
+ "@vitest/coverage-istanbul": "4.0.18",
98
98
  "bun-types": "latest",
99
- "esbuild": "0.23.0",
100
99
  "eslint": "9.7.0",
101
- "playwright": "1.45.3",
102
100
  "rimraf": "5.0.7",
103
- "source-map-loader": "5.0.0",
104
- "typescript": "5.5.4"
101
+ "typescript": "5.5.4",
102
+ "vitest": "4.0.18"
105
103
  }
106
104
  }
package/src/bearer-did.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import type {
2
- CryptoApi,
3
2
  EnclosedSignParams,
4
3
  EnclosedVerifyParams,
5
4
  Jwk,
6
5
  KeyIdentifier,
7
6
  KeyImporterExporter,
7
+ KeyManager,
8
8
  KmsExportKeyParams,
9
9
  KmsImportKeyParams,
10
10
  Signer,
@@ -77,13 +77,13 @@ export class BearerDid {
77
77
  *
78
78
  * Each DID method requires at least one key be present in the provided `keyManager`.
79
79
  */
80
- keyManager: CryptoApi;
80
+ keyManager: KeyManager;
81
81
 
82
82
  constructor({ uri, document, metadata, keyManager }: {
83
83
  uri: string,
84
84
  document: DidDocument,
85
85
  metadata: DidMetadata,
86
- keyManager: CryptoApi
86
+ keyManager: KeyManager
87
87
  }) {
88
88
  this.uri = uri;
89
89
  this.document = document;
@@ -238,7 +238,7 @@ export class BearerDid {
238
238
  * keys for any verification method are missing in the key manager.
239
239
  */
240
240
  public static async import({ portableDid, keyManager = new LocalKeyManager() }: {
241
- keyManager?: CryptoApi & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
241
+ keyManager?: KeyManager & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
242
242
  portableDid: PortableDid;
243
243
  }): Promise<BearerDid> {
244
244
 
package/src/index.ts CHANGED
@@ -15,7 +15,9 @@ export * from './methods/did-method.js';
15
15
  export * from './methods/did-web.js';
16
16
 
17
17
  export * from './resolver/resolver-cache-level.js';
18
+ export * from './resolver/resolver-cache-memory.js';
18
19
  export * from './resolver/resolver-cache-noop.js';
19
20
  export * from './resolver/universal-resolver.js';
20
21
 
22
+ export { isPortableDid } from './utils.js';
21
23
  export * as utils from './utils.js';
@@ -1,9 +1,9 @@
1
1
  import type {
2
2
  AsymmetricKeyConverter,
3
- CryptoApi,
4
3
  Jwk,
5
4
  KeyIdentifier,
6
5
  KeyImporterExporter,
6
+ KeyManager,
7
7
  KmsExportKeyParams,
8
8
  KmsImportKeyParams,
9
9
  Signer,
@@ -506,7 +506,7 @@ export class DidDht extends DidMethod {
506
506
  * @param params.options - Optional parameters that can be specified when creating a new DID.
507
507
  * @returns A Promise resolving to a {@link BearerDid} object representing the new DID.
508
508
  */
509
- public static async create<TKms extends CryptoApi | undefined = undefined>({
509
+ public static async create<TKms extends KeyManager | undefined = undefined>({
510
510
  keyManager = new LocalKeyManager(),
511
511
  options = {}
512
512
  }: {
@@ -648,7 +648,7 @@ export class DidDht extends DidMethod {
648
648
  * manager.
649
649
  */
650
650
  public static async import({ portableDid, keyManager = new LocalKeyManager() }: {
651
- keyManager?: CryptoApi & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
651
+ keyManager?: KeyManager & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
652
652
  portableDid: PortableDid;
653
653
  }): Promise<BearerDid> {
654
654
  // Verify the DID method is supported.
@@ -1,10 +1,17 @@
1
- import type { CryptoApi, Jwk, KeyIdentifier, KeyImporterExporter, KmsExportKeyParams, KmsImportKeyParams } from '@enbox/crypto';
2
1
  import type {
3
2
  IonDocumentModel,
4
3
  IonPublicKeyModel,
5
4
  IonPublicKeyPurpose,
6
5
  JwkEs256k,
7
6
  } from '@decentralized-identity/ion-sdk';
7
+ import type {
8
+ Jwk,
9
+ KeyIdentifier,
10
+ KeyImporterExporter,
11
+ KeyManager,
12
+ KmsExportKeyParams,
13
+ KmsImportKeyParams,
14
+ } from '@enbox/crypto';
8
15
 
9
16
  import { computeJwkThumbprint, LocalKeyManager } from '@enbox/crypto';
10
17
  import { IonDid, IonRequest } from '@decentralized-identity/ion-sdk';
@@ -364,7 +371,7 @@ export class DidIon extends DidMethod {
364
371
  * @param params.options - Optional parameters that can be specified when creating a new DID.
365
372
  * @returns A Promise resolving to a {@link BearerDid} object representing the new DID.
366
373
  */
367
- public static async create<TKms extends CryptoApi | undefined = undefined>({
374
+ public static async create<TKms extends KeyManager | undefined = undefined>({
368
375
  keyManager = new LocalKeyManager(),
369
376
  options = {}
370
377
  }: {
@@ -517,7 +524,7 @@ export class DidIon extends DidMethod {
517
524
  * any verification method are missing in the key manager.
518
525
  */
519
526
  public static async import({ portableDid, keyManager = new LocalKeyManager() }: {
520
- keyManager?: CryptoApi & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
527
+ keyManager?: KeyManager & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
521
528
  portableDid: PortableDid;
522
529
  }): Promise<BearerDid> {
523
530
  // Verify the DID method is supported.
@@ -1,9 +1,9 @@
1
1
  import type {
2
- CryptoApi,
3
2
  InferKeyGeneratorAlgorithm,
4
3
  Jwk,
5
4
  KeyIdentifier,
6
5
  KeyImporterExporter,
6
+ KeyManager,
7
7
  KmsExportKeyParams,
8
8
  KmsImportKeyParams,
9
9
  } from '@enbox/crypto';
@@ -76,7 +76,7 @@ export interface DidJwkCreateOptions<TKms> extends DidCreateOptions<TKms> {
76
76
  /**
77
77
  * Optionally specify the algorithm to be used for key generation.
78
78
  */
79
- algorithm?: TKms extends CryptoApi
79
+ algorithm?: TKms extends KeyManager
80
80
  ? InferKeyGeneratorAlgorithm<TKms>
81
81
  : InferKeyGeneratorAlgorithm<LocalKeyManager>;
82
82
 
@@ -193,7 +193,7 @@ export class DidJwk extends DidMethod {
193
193
  * @param params.options - Optional parameters that can be specified when creating a new DID.
194
194
  * @returns A Promise resolving to a {@link BearerDid} object representing the new DID.
195
195
  */
196
- public static async create<TKms extends CryptoApi | undefined = undefined>({
196
+ public static async create<TKms extends KeyManager | undefined = undefined>({
197
197
  keyManager = new LocalKeyManager(),
198
198
  options = {}
199
199
  }: {
@@ -305,7 +305,7 @@ export class DidJwk extends DidMethod {
305
305
  * @throws An error if the DID document does not contain exactly one verification method.
306
306
  */
307
307
  public static async import({ portableDid, keyManager = new LocalKeyManager() }: {
308
- keyManager?: CryptoApi & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
308
+ keyManager?: KeyManager & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
309
309
  portableDid: PortableDid;
310
310
  }): Promise<BearerDid> {
311
311
  // Verify the DID method is supported.
@@ -1,11 +1,11 @@
1
1
  import type {
2
2
  AsymmetricKeyConverter,
3
- CryptoApi,
4
3
  InferKeyGeneratorAlgorithm,
5
4
  Jwk,
6
5
  KeyCompressor,
7
6
  KeyIdentifier,
8
7
  KeyImporterExporter,
8
+ KeyManager,
9
9
  KmsExportKeyParams,
10
10
  KmsImportKeyParams,
11
11
  } from '@enbox/crypto';
@@ -88,7 +88,7 @@ export interface DidKeyCreateOptions<TKms> extends DidCreateOptions<TKms> {
88
88
  /**
89
89
  * Optionally specify the algorithm to be used for key generation.
90
90
  */
91
- algorithm?: TKms extends CryptoApi
91
+ algorithm?: TKms extends KeyManager
92
92
  ? InferKeyGeneratorAlgorithm<TKms>
93
93
  : InferKeyGeneratorAlgorithm<LocalKeyManager>;
94
94
 
@@ -295,7 +295,7 @@ export class DidKey extends DidMethod {
295
295
  * @param params.options - Optional parameters that can be specified when creating a new DID.
296
296
  * @returns A Promise resolving to a {@link BearerDid} object representing the new DID.
297
297
  */
298
- public static async create<TKms extends CryptoApi | undefined = undefined>({
298
+ public static async create<TKms extends KeyManager | undefined = undefined>({
299
299
  keyManager = new LocalKeyManager(),
300
300
  options = {}
301
301
  }: {
@@ -408,7 +408,7 @@ export class DidKey extends DidMethod {
408
408
  * @throws An error if the DID document does not contain exactly one verification method.
409
409
  */
410
410
  public static async import({ portableDid, keyManager = new LocalKeyManager() }: {
411
- keyManager?: CryptoApi & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
411
+ keyManager?: KeyManager & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
412
412
  portableDid: PortableDid;
413
413
  }): Promise<BearerDid> {
414
414
  // Verify the DID method is supported.
@@ -473,7 +473,7 @@ export class DidKey extends DidMethod {
473
473
  */
474
474
  private static async createDocument({ didUri, options = {} }: {
475
475
  didUri: string;
476
- options?: Exclude<DidKeyCreateOptions<CryptoApi>, 'algorithm' | 'verificationMethods'> | DidResolutionOptions;
476
+ options?: Exclude<DidKeyCreateOptions<KeyManager>, 'algorithm' | 'verificationMethods'> | DidResolutionOptions;
477
477
  }): Promise<DidDocument> {
478
478
  const {
479
479
  defaultContext = 'https://www.w3.org/ns/did/v1',
@@ -582,7 +582,7 @@ export class DidKey extends DidMethod {
582
582
  private static async createSignatureMethod({ didUri, multibaseValue, options }: {
583
583
  didUri: string;
584
584
  multibaseValue: string;
585
- options: Required<Pick<DidKeyCreateOptions<CryptoApi>, 'enableExperimentalPublicKeyTypes' | 'publicKeyFormat'>>
585
+ options: Required<Pick<DidKeyCreateOptions<KeyManager>, 'enableExperimentalPublicKeyTypes' | 'publicKeyFormat'>>
586
586
  }): Promise<DidVerificationMethod> {
587
587
  const { enableExperimentalPublicKeyTypes, publicKeyFormat } = options;
588
588
 
@@ -1,6 +1,6 @@
1
1
  import type {
2
- CryptoApi,
3
2
  InferKeyGeneratorAlgorithm,
3
+ KeyManager,
4
4
  LocalKeyManager,
5
5
  } from '@enbox/crypto';
6
6
 
@@ -47,7 +47,7 @@ export interface DidCreateVerificationMethod<TKms> extends Pick<Partial<DidVerif
47
47
  * };
48
48
  * ```
49
49
  */
50
- algorithm: TKms extends CryptoApi
50
+ algorithm: TKms extends KeyManager
51
51
  ? InferKeyGeneratorAlgorithm<TKms>
52
52
  : InferKeyGeneratorAlgorithm<LocalKeyManager>;
53
53
 
@@ -82,7 +82,7 @@ export interface DidCreateVerificationMethod<TKms> extends Pick<Partial<DidVerif
82
82
  * @typeparam O - The type of the options used for creating the DID.
83
83
  */
84
84
  export interface DidMethodApi<
85
- TKms extends CryptoApi | undefined = CryptoApi,
85
+ TKms extends KeyManager | undefined = KeyManager,
86
86
  TDid extends BearerDid = BearerDid,
87
87
  TOptions extends DidCreateOptions<TKms> = DidCreateOptions<TKms>
88
88
  > extends DidMethodResolver {
@@ -0,0 +1,84 @@
1
+ import ms from 'ms';
2
+ import { TtlCache } from '@enbox/common';
3
+
4
+ import type { DidResolutionResult } from '../types/did-core.js';
5
+ import type { DidResolverCache } from '../types/did-resolution.js';
6
+
7
+ /**
8
+ * Configuration parameters for creating an in-memory cache for DID resolution results.
9
+ *
10
+ * Allows customization of the cache time-to-live (TTL) setting.
11
+ */
12
+ export type DidResolverCacheMemoryParams = {
13
+ /**
14
+ * Optional. The time-to-live for cache entries, expressed as a string (e.g., '1h', '15m').
15
+ * Determines how long a cache entry should remain valid before being considered expired.
16
+ *
17
+ * Defaults to '15m' if not specified.
18
+ */
19
+ ttl?: string;
20
+ };
21
+
22
+ export class DidResolverCacheMemory implements DidResolverCache {
23
+ private cache: TtlCache<string, DidResolutionResult>;
24
+
25
+ constructor({ ttl = '15m' }: DidResolverCacheMemoryParams = {}) {
26
+ this.cache = new TtlCache({ ttl: ms(ttl) });
27
+ }
28
+
29
+ /**
30
+ * Retrieves a DID resolution result from the cache.
31
+ *
32
+ * If the cached item has exceeded its TTL, it's scheduled for deletion and undefined is returned.
33
+ *
34
+ * @param didUri - The DID string used as the key for retrieving the cached result.
35
+ * @returns The cached DID resolution result or undefined if not found or expired.
36
+ */
37
+ public async get(didUri: string): Promise<DidResolutionResult | void> {
38
+ if (!didUri) {
39
+ throw new Error('Key cannot be null or undefined');
40
+ }
41
+
42
+ return this.cache.get(didUri);
43
+ }
44
+
45
+ /**
46
+ * Stores a DID resolution result in the cache with a TTL.
47
+ *
48
+ * @param didUri - The DID string used as the key for storing the result.
49
+ * @param resolutionResult - The DID resolution result to be cached.
50
+ * @returns A promise that resolves when the operation is complete.
51
+ */
52
+ public async set(didUri: string, resolutionResult: DidResolutionResult): Promise<void> {
53
+ this.cache.set(didUri, resolutionResult);
54
+ }
55
+
56
+ /**
57
+ * Deletes a DID resolution result from the cache.
58
+ *
59
+ * @param didUri - The DID string used as the key for deletion.
60
+ * @returns A promise that resolves when the operation is complete.
61
+ */
62
+ public async delete(didUri: string): Promise<void> {
63
+ this.cache.delete(didUri);
64
+ }
65
+
66
+ /**
67
+ * Clears all entries from the cache.
68
+ *
69
+ * @returns A promise that resolves when the operation is complete.
70
+ */
71
+ public async clear(): Promise<void> {
72
+ this.cache.clear();
73
+ }
74
+
75
+ /**
76
+ * This method is a no-op but exists to be consistent with other DID Resolver Cache
77
+ * implementations.
78
+ *
79
+ * @returns A promise that resolves immediately.
80
+ */
81
+ public async close(): Promise<void> {
82
+ // No-op since there is no underlying store to close.
83
+ }
84
+ }
package/src/utils.ts CHANGED
@@ -5,6 +5,7 @@ import { computeJwkThumbprint } from '@enbox/crypto';
5
5
  import { Convert, Multicodec } from '@enbox/common';
6
6
 
7
7
  import type { KeyWithMulticodec } from './types/multibase.js';
8
+ import type { PortableDid } from './types/portable-did.js';
8
9
  import type {
9
10
  DidDocument,
10
11
  DidService,
@@ -467,6 +468,32 @@ export function isDidVerificationMethod(obj: unknown): obj is DidVerificationMet
467
468
  return true;
468
469
  }
469
470
 
471
+ /**
472
+ * Checks if a given object conforms to the {@link PortableDid} interface.
473
+ *
474
+ * A `PortableDid` is an object with `uri`, `document`, and `metadata` properties that does not
475
+ * have a `keyManager` property (or has it set to `undefined`). This distinguishes it from a
476
+ * {@link BearerDid}, which includes a `keyManager`.
477
+ *
478
+ * @example
479
+ * ```ts
480
+ * if (isPortableDid(obj)) {
481
+ * console.log('The object is a PortableDid');
482
+ * }
483
+ * ```
484
+ *
485
+ * @param obj - The object to be checked.
486
+ * @returns `true` if `obj` is a `PortableDid`; otherwise, `false`.
487
+ */
488
+ export function isPortableDid(obj: unknown): obj is PortableDid {
489
+ // Validate that the given value is an object that has the necessary properties of PortableDid.
490
+ return !(!obj || typeof obj !== 'object' || obj === null)
491
+ && 'uri' in obj
492
+ && 'document' in obj
493
+ && 'metadata' in obj
494
+ && (!('keyManager' in obj) || obj.keyManager === undefined);
495
+ }
496
+
470
497
  /**
471
498
  * Converts a cryptographic key to a multibase identifier.
472
499
  *