@enbox/dids 0.0.3 → 0.0.5

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 (77) 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-dns.js +455 -0
  6. package/dist/esm/methods/did-dht-dns.js.map +1 -0
  7. package/dist/esm/methods/did-dht-pkarr.js +168 -0
  8. package/dist/esm/methods/did-dht-pkarr.js.map +1 -0
  9. package/dist/esm/methods/did-dht-types.js +116 -0
  10. package/dist/esm/methods/did-dht-types.js.map +1 -0
  11. package/dist/esm/methods/did-dht-utils.js +143 -0
  12. package/dist/esm/methods/did-dht-utils.js.map +1 -0
  13. package/dist/esm/methods/did-dht.js +65 -842
  14. package/dist/esm/methods/did-dht.js.map +1 -1
  15. package/dist/esm/methods/did-ion-utils.js +161 -0
  16. package/dist/esm/methods/did-ion-utils.js.map +1 -0
  17. package/dist/esm/methods/did-ion.js +4 -151
  18. package/dist/esm/methods/did-ion.js.map +1 -1
  19. package/dist/esm/methods/did-jwk.js.map +1 -1
  20. package/dist/esm/methods/did-key-utils.js +235 -0
  21. package/dist/esm/methods/did-key-utils.js.map +1 -0
  22. package/dist/esm/methods/did-key.js +6 -222
  23. package/dist/esm/methods/did-key.js.map +1 -1
  24. package/dist/esm/resolver/resolver-cache-memory.js +77 -0
  25. package/dist/esm/resolver/resolver-cache-memory.js.map +1 -0
  26. package/dist/esm/utils.js +25 -0
  27. package/dist/esm/utils.js.map +1 -1
  28. package/dist/types/bearer-did.d.ts +4 -4
  29. package/dist/types/bearer-did.d.ts.map +1 -1
  30. package/dist/types/index.d.ts +2 -0
  31. package/dist/types/index.d.ts.map +1 -1
  32. package/dist/types/methods/did-dht-dns.d.ts +114 -0
  33. package/dist/types/methods/did-dht-dns.d.ts.map +1 -0
  34. package/dist/types/methods/did-dht-pkarr.d.ts +56 -0
  35. package/dist/types/methods/did-dht-pkarr.d.ts.map +1 -0
  36. package/dist/types/methods/did-dht-types.d.ts +286 -0
  37. package/dist/types/methods/did-dht-types.d.ts.map +1 -0
  38. package/dist/types/methods/did-dht-utils.d.ts +54 -0
  39. package/dist/types/methods/did-dht-utils.d.ts.map +1 -0
  40. package/dist/types/methods/did-dht.d.ts +45 -460
  41. package/dist/types/methods/did-dht.d.ts.map +1 -1
  42. package/dist/types/methods/did-ion-utils.d.ts +86 -0
  43. package/dist/types/methods/did-ion-utils.d.ts.map +1 -0
  44. package/dist/types/methods/did-ion.d.ts +4 -84
  45. package/dist/types/methods/did-ion.d.ts.map +1 -1
  46. package/dist/types/methods/did-jwk.d.ts +4 -4
  47. package/dist/types/methods/did-jwk.d.ts.map +1 -1
  48. package/dist/types/methods/did-key-utils.d.ts +138 -0
  49. package/dist/types/methods/did-key-utils.d.ts.map +1 -0
  50. package/dist/types/methods/did-key.d.ts +6 -127
  51. package/dist/types/methods/did-key.d.ts.map +1 -1
  52. package/dist/types/methods/did-method.d.ts +3 -3
  53. package/dist/types/methods/did-method.d.ts.map +1 -1
  54. package/dist/types/resolver/resolver-cache-memory.d.ts +58 -0
  55. package/dist/types/resolver/resolver-cache-memory.d.ts.map +1 -0
  56. package/dist/types/utils.d.ts +19 -0
  57. package/dist/types/utils.d.ts.map +1 -1
  58. package/dist/utils.js +1 -1
  59. package/dist/utils.js.map +4 -4
  60. package/package.json +11 -13
  61. package/src/bearer-did.ts +4 -4
  62. package/src/index.ts +2 -0
  63. package/src/methods/did-dht-dns.ts +516 -0
  64. package/src/methods/did-dht-pkarr.ts +192 -0
  65. package/src/methods/did-dht-types.ts +316 -0
  66. package/src/methods/did-dht-utils.ts +157 -0
  67. package/src/methods/did-dht.ts +125 -1131
  68. package/src/methods/did-ion-utils.ts +186 -0
  69. package/src/methods/did-ion.ts +16 -185
  70. package/src/methods/did-jwk.ts +4 -4
  71. package/src/methods/did-key-utils.ts +258 -0
  72. package/src/methods/did-key.ts +20 -272
  73. package/src/methods/did-method.ts +3 -3
  74. package/src/resolver/resolver-cache-memory.ts +84 -0
  75. package/src/utils.ts +27 -0
  76. package/dist/browser.js +0 -73
  77. package/dist/browser.js.map +0 -7
@@ -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
  *