@enbox/dids 0.0.1

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 (104) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +1 -0
  3. package/dist/browser.js +77 -0
  4. package/dist/browser.js.map +7 -0
  5. package/dist/browser.mjs +77 -0
  6. package/dist/browser.mjs.map +7 -0
  7. package/dist/cjs/index.js +6303 -0
  8. package/dist/cjs/index.js.map +7 -0
  9. package/dist/cjs/package.json +1 -0
  10. package/dist/cjs/utils.js +245 -0
  11. package/dist/cjs/utils.js.map +7 -0
  12. package/dist/esm/bearer-did.js +201 -0
  13. package/dist/esm/bearer-did.js.map +1 -0
  14. package/dist/esm/did-error.js +62 -0
  15. package/dist/esm/did-error.js.map +1 -0
  16. package/dist/esm/did.js +114 -0
  17. package/dist/esm/did.js.map +1 -0
  18. package/dist/esm/index.js +16 -0
  19. package/dist/esm/index.js.map +1 -0
  20. package/dist/esm/methods/did-dht.js +1241 -0
  21. package/dist/esm/methods/did-dht.js.map +1 -0
  22. package/dist/esm/methods/did-ion.js +570 -0
  23. package/dist/esm/methods/did-ion.js.map +1 -0
  24. package/dist/esm/methods/did-jwk.js +298 -0
  25. package/dist/esm/methods/did-jwk.js.map +1 -0
  26. package/dist/esm/methods/did-key.js +983 -0
  27. package/dist/esm/methods/did-key.js.map +1 -0
  28. package/dist/esm/methods/did-method.js +53 -0
  29. package/dist/esm/methods/did-method.js.map +1 -0
  30. package/dist/esm/methods/did-web.js +83 -0
  31. package/dist/esm/methods/did-web.js.map +1 -0
  32. package/dist/esm/resolver/resolver-cache-level.js +101 -0
  33. package/dist/esm/resolver/resolver-cache-level.js.map +1 -0
  34. package/dist/esm/resolver/resolver-cache-noop.js +24 -0
  35. package/dist/esm/resolver/resolver-cache-noop.js.map +1 -0
  36. package/dist/esm/resolver/universal-resolver.js +187 -0
  37. package/dist/esm/resolver/universal-resolver.js.map +1 -0
  38. package/dist/esm/types/did-core.js +51 -0
  39. package/dist/esm/types/did-core.js.map +1 -0
  40. package/dist/esm/types/did-resolution.js +12 -0
  41. package/dist/esm/types/did-resolution.js.map +1 -0
  42. package/dist/esm/types/multibase.js +2 -0
  43. package/dist/esm/types/multibase.js.map +1 -0
  44. package/dist/esm/types/portable-did.js +2 -0
  45. package/dist/esm/types/portable-did.js.map +1 -0
  46. package/dist/esm/utils.js +458 -0
  47. package/dist/esm/utils.js.map +1 -0
  48. package/dist/types/bearer-did.d.ts +143 -0
  49. package/dist/types/bearer-did.d.ts.map +1 -0
  50. package/dist/types/did-error.d.ts +50 -0
  51. package/dist/types/did-error.d.ts.map +1 -0
  52. package/dist/types/did.d.ts +125 -0
  53. package/dist/types/did.d.ts.map +1 -0
  54. package/dist/types/index.d.ts +18 -0
  55. package/dist/types/index.d.ts.map +1 -0
  56. package/dist/types/methods/did-dht.d.ts +682 -0
  57. package/dist/types/methods/did-dht.d.ts.map +1 -0
  58. package/dist/types/methods/did-ion.d.ts +492 -0
  59. package/dist/types/methods/did-ion.d.ts.map +1 -0
  60. package/dist/types/methods/did-jwk.d.ts +236 -0
  61. package/dist/types/methods/did-jwk.d.ts.map +1 -0
  62. package/dist/types/methods/did-key.d.ts +499 -0
  63. package/dist/types/methods/did-key.d.ts.map +1 -0
  64. package/dist/types/methods/did-method.d.ts +238 -0
  65. package/dist/types/methods/did-method.d.ts.map +1 -0
  66. package/dist/types/methods/did-web.d.ts +37 -0
  67. package/dist/types/methods/did-web.d.ts.map +1 -0
  68. package/dist/types/resolver/resolver-cache-level.d.ts +86 -0
  69. package/dist/types/resolver/resolver-cache-level.d.ts.map +1 -0
  70. package/dist/types/resolver/resolver-cache-noop.d.ts +9 -0
  71. package/dist/types/resolver/resolver-cache-noop.d.ts.map +1 -0
  72. package/dist/types/resolver/universal-resolver.d.ts +109 -0
  73. package/dist/types/resolver/universal-resolver.d.ts.map +1 -0
  74. package/dist/types/types/did-core.d.ts +523 -0
  75. package/dist/types/types/did-core.d.ts.map +1 -0
  76. package/dist/types/types/did-resolution.d.ts +85 -0
  77. package/dist/types/types/did-resolution.d.ts.map +1 -0
  78. package/dist/types/types/multibase.d.ts +28 -0
  79. package/dist/types/types/multibase.d.ts.map +1 -0
  80. package/dist/types/types/portable-did.d.ts +59 -0
  81. package/dist/types/types/portable-did.d.ts.map +1 -0
  82. package/dist/types/utils.d.ts +378 -0
  83. package/dist/types/utils.d.ts.map +1 -0
  84. package/dist/utils.js +28 -0
  85. package/dist/utils.js.map +7 -0
  86. package/package.json +116 -0
  87. package/src/bearer-did.ts +287 -0
  88. package/src/did-error.ts +75 -0
  89. package/src/did.ts +186 -0
  90. package/src/index.ts +21 -0
  91. package/src/methods/did-dht.ts +1637 -0
  92. package/src/methods/did-ion.ts +887 -0
  93. package/src/methods/did-jwk.ts +410 -0
  94. package/src/methods/did-key.ts +1248 -0
  95. package/src/methods/did-method.ts +276 -0
  96. package/src/methods/did-web.ts +96 -0
  97. package/src/resolver/resolver-cache-level.ts +163 -0
  98. package/src/resolver/resolver-cache-noop.ts +26 -0
  99. package/src/resolver/universal-resolver.ts +238 -0
  100. package/src/types/did-core.ts +580 -0
  101. package/src/types/did-resolution.ts +93 -0
  102. package/src/types/multibase.ts +29 -0
  103. package/src/types/portable-did.ts +64 -0
  104. package/src/utils.ts +532 -0
@@ -0,0 +1,236 @@
1
+ import type { CryptoApi, KeyIdentifier, KmsExportKeyParams, KmsImportKeyParams, KeyImporterExporter, InferKeyGeneratorAlgorithm } from '@enbox/crypto';
2
+ import { LocalKeyManager } from '@enbox/crypto';
3
+ import type { PortableDid } from '../types/portable-did.js';
4
+ import type { DidCreateOptions, DidCreateVerificationMethod } from './did-method.js';
5
+ import type { DidDocument, DidResolutionOptions, DidResolutionResult, DidVerificationMethod } from '../types/did-core.js';
6
+ import { DidMethod } from './did-method.js';
7
+ import { BearerDid } from '../bearer-did.js';
8
+ /**
9
+ * Defines the set of options available when creating a new Decentralized Identifier (DID) with the
10
+ * 'did:jwk' method.
11
+ *
12
+ * Either the `algorithm` or `verificationMethods` option can be specified, but not both.
13
+ * - A new key will be generated using the algorithm identifier specified in either the `algorithm`
14
+ * property or the `verificationMethods` object's `algorithm` property.
15
+ * - If `verificationMethods` is given, it must contain exactly one entry since DID JWK only
16
+ * supports a single verification method.
17
+ * - If neither is given, the default is to generate a new Ed25519 key.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * // DID Creation
22
+ *
23
+ * // By default, when no options are given, a new Ed25519 key will be generated.
24
+ * const did = await DidJwk.create();
25
+ *
26
+ * // The algorithm to use for key generation can be specified as a top-level option.
27
+ * const did = await DidJwk.create({
28
+ * options: { algorithm = 'ES256K' }
29
+ * });
30
+ *
31
+ * // Or, alternatively as a property of the verification method.
32
+ * const did = await DidJwk.create({
33
+ * options: {
34
+ * verificationMethods: [{ algorithm = 'ES256K' }]
35
+ * }
36
+ * });
37
+ *
38
+ * // DID Creation with a KMS
39
+ * const keyManager = new LocalKeyManager();
40
+ * const did = await DidJwk.create({ keyManager });
41
+ *
42
+ * // DID Resolution
43
+ * const resolutionResult = await DidJwk.resolve({ did: did.uri });
44
+ *
45
+ * // Signature Operations
46
+ * const signer = await did.getSigner();
47
+ * const signature = await signer.sign({ data: new TextEncoder().encode('Message') });
48
+ * const isValid = await signer.verify({ data: new TextEncoder().encode('Message'), signature });
49
+ *
50
+ * // Import / Export
51
+ *
52
+ * // Export a BearerDid object to the PortableDid format.
53
+ * const portableDid = await did.export();
54
+ *
55
+ * // Reconstruct a BearerDid object from a PortableDid
56
+ * const did = await DidJwk.import(portableDid);
57
+ * ```
58
+ */
59
+ export interface DidJwkCreateOptions<TKms> extends DidCreateOptions<TKms> {
60
+ /**
61
+ * Optionally specify the algorithm to be used for key generation.
62
+ */
63
+ algorithm?: TKms extends CryptoApi ? InferKeyGeneratorAlgorithm<TKms> : InferKeyGeneratorAlgorithm<LocalKeyManager>;
64
+ /**
65
+ * Alternatively, specify the algorithm to be used for key generation of the single verification
66
+ * method in the DID Document.
67
+ */
68
+ verificationMethods?: DidCreateVerificationMethod<TKms>[];
69
+ }
70
+ /**
71
+ * The `DidJwk` class provides an implementation of the `did:jwk` DID method.
72
+ *
73
+ * Features:
74
+ * - DID Creation: Create new `did:jwk` DIDs.
75
+ * - DID Key Management: Instantiate a DID object from an existing verification method key set or
76
+ * or a key in a Key Management System (KMS). If supported by the KMS, a DID's
77
+ * key can be exported to a portable DID format.
78
+ * - DID Resolution: Resolve a `did:jwk` to its corresponding DID Document.
79
+ * - Signature Operations: Sign and verify messages using keys associated with a DID.
80
+ *
81
+ * @remarks
82
+ * The `did:jwk` DID method uses a single JSON Web Key (JWK) to generate a DID and does not rely
83
+ * on any external system such as a blockchain or centralized database. This characteristic makes
84
+ * it suitable for use cases where a assertions about a DID Subject can be self-verifiable by
85
+ * third parties.
86
+ *
87
+ * The DID URI is formed by Base64URL-encoding the JWK and prefixing with `did:jwk:`. The DID
88
+ * Document of a `did:jwk` DID contains a single verification method, which is the JWK used
89
+ * to generate the DID. The verification method is identified by the key ID `#0`.
90
+ *
91
+ * @see {@link https://github.com/quartzjer/did-jwk/blob/main/spec.md | DID JWK Specification}
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * // DID Creation
96
+ * const did = await DidJwk.create();
97
+ *
98
+ * // DID Creation with a KMS
99
+ * const keyManager = new LocalKeyManager();
100
+ * const did = await DidJwk.create({ keyManager });
101
+ *
102
+ * // DID Resolution
103
+ * const resolutionResult = await DidJwk.resolve({ did: did.uri });
104
+ *
105
+ * // Signature Operations
106
+ * const signer = await did.getSigner();
107
+ * const signature = await signer.sign({ data: new TextEncoder().encode('Message') });
108
+ * const isValid = await signer.verify({ data: new TextEncoder().encode('Message'), signature });
109
+ *
110
+ * // Key Management
111
+ *
112
+ * // Instantiate a DID object from an existing key in a KMS
113
+ * const did = await DidJwk.fromKeyManager({
114
+ * didUri: 'did:jwk:eyJrIjoiT0tQIiwidCI6IkV1c2UyNTYifQ',
115
+ * keyManager
116
+ * });
117
+ *
118
+ * // Instantiate a DID object from an existing verification method key
119
+ * const did = await DidJwk.fromKeys({
120
+ * verificationMethods: [{
121
+ * publicKeyJwk: {
122
+ * kty: 'OKP',
123
+ * crv: 'Ed25519',
124
+ * x: 'cHs7YMLQ3gCWjkacMURBsnEJBcEsvlsE5DfnsfTNDP4'
125
+ * },
126
+ * privateKeyJwk: {
127
+ * kty: 'OKP',
128
+ * crv: 'Ed25519',
129
+ * x: 'cHs7YMLQ3gCWjkacMURBsnEJBcEsvlsE5DfnsfTNDP4',
130
+ * d: 'bdcGE4KzEaekOwoa-ee3gAm1a991WvNj_Eq3WKyqTnE'
131
+ * }
132
+ * }]
133
+ * });
134
+ *
135
+ * // Convert a DID object to a portable format
136
+ * const portableDid = await DidJwk.toKeys({ did });
137
+ *
138
+ * // Reconstruct a DID object from a portable format
139
+ * const did = await DidJwk.fromKeys(portableDid);
140
+ * ```
141
+ */
142
+ export declare class DidJwk extends DidMethod {
143
+ /**
144
+ * Name of the DID method, as defined in the DID JWK specification.
145
+ */
146
+ static methodName: string;
147
+ /**
148
+ * Creates a new DID using the `did:jwk` method formed from a newly generated key.
149
+ *
150
+ * @remarks
151
+ * The DID URI is formed by Base64URL-encoding the JWK and prefixing with `did:jwk:`.
152
+ *
153
+ * Notes:
154
+ * - If no `options` are given, by default a new Ed25519 key will be generated.
155
+ * - The `algorithm` and `verificationMethods` options are mutually exclusive. If both are given,
156
+ * an error will be thrown.
157
+ *
158
+ * @example
159
+ * ```ts
160
+ * // DID Creation
161
+ * const did = await DidJwk.create();
162
+ *
163
+ * // DID Creation with a KMS
164
+ * const keyManager = new LocalKeyManager();
165
+ * const did = await DidJwk.create({ keyManager });
166
+ * ```
167
+ *
168
+ * @param params - The parameters for the create operation.
169
+ * @param params.keyManager - Optionally specify a Key Management System (KMS) used to generate
170
+ * keys and sign data.
171
+ * @param params.options - Optional parameters that can be specified when creating a new DID.
172
+ * @returns A Promise resolving to a {@link BearerDid} object representing the new DID.
173
+ */
174
+ static create<TKms extends CryptoApi | undefined = undefined>({ keyManager, options }?: {
175
+ keyManager?: TKms;
176
+ options?: DidJwkCreateOptions<TKms>;
177
+ }): Promise<BearerDid>;
178
+ /**
179
+ * Given the W3C DID Document of a `did:jwk` DID, return the verification method that will be used
180
+ * for signing messages and credentials. If given, the `methodId` parameter is used to select the
181
+ * verification method. If not given, the first verification method in the DID Document is used.
182
+ *
183
+ * Note that for DID JWK, only one verification method can exist so specifying `methodId` could be
184
+ * considered redundant or unnecessary. The option is provided for consistency with other DID
185
+ * method implementations.
186
+ *
187
+ * @param params - The parameters for the `getSigningMethod` operation.
188
+ * @param params.didDocument - DID Document to get the verification method from.
189
+ * @param params.methodId - ID of the verification method to use for signing.
190
+ * @returns Verification method to use for signing.
191
+ */
192
+ static getSigningMethod({ didDocument }: {
193
+ didDocument: DidDocument;
194
+ methodId?: string;
195
+ }): Promise<DidVerificationMethod>;
196
+ /**
197
+ * Instantiates a {@link BearerDid} object for the DID JWK method from a given {@link PortableDid}.
198
+ *
199
+ * This method allows for the creation of a `BearerDid` object using a previously created DID's
200
+ * key material, DID document, and metadata.
201
+ *
202
+ * @remarks
203
+ * The `verificationMethod` array of the DID document must contain exactly one key since the
204
+ * `did:jwk` method only supports a single verification method.
205
+ *
206
+ * @example
207
+ * ```ts
208
+ * // Export an existing BearerDid to PortableDid format.
209
+ * const portableDid = await did.export();
210
+ * // Reconstruct a BearerDid object from the PortableDid.
211
+ * const did = await DidJwk.import({ portableDid });
212
+ * ```
213
+ *
214
+ * @param params - The parameters for the import operation.
215
+ * @param params.portableDid - The PortableDid object to import.
216
+ * @param params.keyManager - Optionally specify an external Key Management System (KMS) used to
217
+ * generate keys and sign data. If not given, a new
218
+ * {@link LocalKeyManager} instance will be created and
219
+ * used.
220
+ * @returns A Promise resolving to a `BearerDid` object representing the DID formed from the provided keys.
221
+ * @throws An error if the DID document does not contain exactly one verification method.
222
+ */
223
+ static import({ portableDid, keyManager }: {
224
+ keyManager?: CryptoApi & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
225
+ portableDid: PortableDid;
226
+ }): Promise<BearerDid>;
227
+ /**
228
+ * Resolves a `did:jwk` identifier to a DID Document.
229
+ *
230
+ * @param didUri - The DID to be resolved.
231
+ * @param _options - Optional parameters for resolving the DID. Unused by this DID method.
232
+ * @returns A Promise resolving to a {@link DidResolutionResult} object representing the result of the resolution.
233
+ */
234
+ static resolve(didUri: string, _options?: DidResolutionOptions): Promise<DidResolutionResult>;
235
+ }
236
+ //# sourceMappingURL=did-jwk.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"did-jwk.d.ts","sourceRoot":"","sources":["../../../src/methods/did-jwk.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,SAAS,EACT,aAAa,EACb,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,0BAA0B,EAC3B,MAAM,eAAe,CAAC;AAGvB,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEhD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,KAAK,EAAE,gBAAgB,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAC;AACrF,OAAO,KAAK,EAAE,WAAW,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAG1H,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAI7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,MAAM,WAAW,mBAAmB,CAAC,IAAI,CAAE,SAAQ,gBAAgB,CAAC,IAAI,CAAC;IACvE;;OAEG;IACH,SAAS,CAAC,EAAE,IAAI,SAAS,SAAS,GAC9B,0BAA0B,CAAC,IAAI,CAAC,GAChC,0BAA0B,CAAC,eAAe,CAAC,CAAC;IAEhD;;;OAGG;IACH,mBAAmB,CAAC,EAAE,2BAA2B,CAAC,IAAI,CAAC,EAAE,CAAC;CAC3D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuEG;AACH,qBAAa,MAAO,SAAQ,SAAS;IAEnC;;OAEG;IACH,OAAc,UAAU,SAAS;IAEjC;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;WACiB,MAAM,CAAC,IAAI,SAAS,SAAS,GAAG,SAAS,GAAG,SAAS,EAAE,EACzE,UAAkC,EAClC,OAAY,EACb,GAAE;QACD,UAAU,CAAC,EAAE,IAAI,CAAC;QAClB,OAAO,CAAC,EAAE,mBAAmB,CAAC,IAAI,CAAC,CAAC;KAChC,GAAG,OAAO,CAAC,SAAS,CAAC;IA4C3B;;;;;;;;;;;;;OAaG;WACiB,gBAAgB,CAAC,EAAE,WAAW,EAAE,EAAE;QACpD,WAAW,EAAE,WAAW,CAAC;QACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB,GAAG,OAAO,CAAC,qBAAqB,CAAC;IAiBlC;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;WACiB,MAAM,CAAC,EAAE,WAAW,EAAE,UAAkC,EAAE,EAAE;QAC9E,UAAU,CAAC,EAAE,SAAS,GAAG,mBAAmB,CAAC,kBAAkB,EAAE,aAAa,EAAE,kBAAkB,CAAC,CAAC;QACpG,WAAW,EAAE,WAAW,CAAC;KAC1B,GAAG,OAAO,CAAC,SAAS,CAAC;IAoBtB;;;;;;OAMG;WACiB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,oBAAoB,GAAG,OAAO,CAAC,mBAAmB,CAAC;CAyE3G"}