@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.
- package/LICENSE +201 -0
- package/README.md +1 -0
- package/dist/browser.js +77 -0
- package/dist/browser.js.map +7 -0
- package/dist/browser.mjs +77 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/cjs/index.js +6303 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/utils.js +245 -0
- package/dist/cjs/utils.js.map +7 -0
- package/dist/esm/bearer-did.js +201 -0
- package/dist/esm/bearer-did.js.map +1 -0
- package/dist/esm/did-error.js +62 -0
- package/dist/esm/did-error.js.map +1 -0
- package/dist/esm/did.js +114 -0
- package/dist/esm/did.js.map +1 -0
- package/dist/esm/index.js +16 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/methods/did-dht.js +1241 -0
- package/dist/esm/methods/did-dht.js.map +1 -0
- package/dist/esm/methods/did-ion.js +570 -0
- package/dist/esm/methods/did-ion.js.map +1 -0
- package/dist/esm/methods/did-jwk.js +298 -0
- package/dist/esm/methods/did-jwk.js.map +1 -0
- package/dist/esm/methods/did-key.js +983 -0
- package/dist/esm/methods/did-key.js.map +1 -0
- package/dist/esm/methods/did-method.js +53 -0
- package/dist/esm/methods/did-method.js.map +1 -0
- package/dist/esm/methods/did-web.js +83 -0
- package/dist/esm/methods/did-web.js.map +1 -0
- package/dist/esm/resolver/resolver-cache-level.js +101 -0
- package/dist/esm/resolver/resolver-cache-level.js.map +1 -0
- package/dist/esm/resolver/resolver-cache-noop.js +24 -0
- package/dist/esm/resolver/resolver-cache-noop.js.map +1 -0
- package/dist/esm/resolver/universal-resolver.js +187 -0
- package/dist/esm/resolver/universal-resolver.js.map +1 -0
- package/dist/esm/types/did-core.js +51 -0
- package/dist/esm/types/did-core.js.map +1 -0
- package/dist/esm/types/did-resolution.js +12 -0
- package/dist/esm/types/did-resolution.js.map +1 -0
- package/dist/esm/types/multibase.js +2 -0
- package/dist/esm/types/multibase.js.map +1 -0
- package/dist/esm/types/portable-did.js +2 -0
- package/dist/esm/types/portable-did.js.map +1 -0
- package/dist/esm/utils.js +458 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/types/bearer-did.d.ts +143 -0
- package/dist/types/bearer-did.d.ts.map +1 -0
- package/dist/types/did-error.d.ts +50 -0
- package/dist/types/did-error.d.ts.map +1 -0
- package/dist/types/did.d.ts +125 -0
- package/dist/types/did.d.ts.map +1 -0
- package/dist/types/index.d.ts +18 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/methods/did-dht.d.ts +682 -0
- package/dist/types/methods/did-dht.d.ts.map +1 -0
- package/dist/types/methods/did-ion.d.ts +492 -0
- package/dist/types/methods/did-ion.d.ts.map +1 -0
- package/dist/types/methods/did-jwk.d.ts +236 -0
- package/dist/types/methods/did-jwk.d.ts.map +1 -0
- package/dist/types/methods/did-key.d.ts +499 -0
- package/dist/types/methods/did-key.d.ts.map +1 -0
- package/dist/types/methods/did-method.d.ts +238 -0
- package/dist/types/methods/did-method.d.ts.map +1 -0
- package/dist/types/methods/did-web.d.ts +37 -0
- package/dist/types/methods/did-web.d.ts.map +1 -0
- package/dist/types/resolver/resolver-cache-level.d.ts +86 -0
- package/dist/types/resolver/resolver-cache-level.d.ts.map +1 -0
- package/dist/types/resolver/resolver-cache-noop.d.ts +9 -0
- package/dist/types/resolver/resolver-cache-noop.d.ts.map +1 -0
- package/dist/types/resolver/universal-resolver.d.ts +109 -0
- package/dist/types/resolver/universal-resolver.d.ts.map +1 -0
- package/dist/types/types/did-core.d.ts +523 -0
- package/dist/types/types/did-core.d.ts.map +1 -0
- package/dist/types/types/did-resolution.d.ts +85 -0
- package/dist/types/types/did-resolution.d.ts.map +1 -0
- package/dist/types/types/multibase.d.ts +28 -0
- package/dist/types/types/multibase.d.ts.map +1 -0
- package/dist/types/types/portable-did.d.ts +59 -0
- package/dist/types/types/portable-did.d.ts.map +1 -0
- package/dist/types/utils.d.ts +378 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/utils.js +28 -0
- package/dist/utils.js.map +7 -0
- package/package.json +116 -0
- package/src/bearer-did.ts +287 -0
- package/src/did-error.ts +75 -0
- package/src/did.ts +186 -0
- package/src/index.ts +21 -0
- package/src/methods/did-dht.ts +1637 -0
- package/src/methods/did-ion.ts +887 -0
- package/src/methods/did-jwk.ts +410 -0
- package/src/methods/did-key.ts +1248 -0
- package/src/methods/did-method.ts +276 -0
- package/src/methods/did-web.ts +96 -0
- package/src/resolver/resolver-cache-level.ts +163 -0
- package/src/resolver/resolver-cache-noop.ts +26 -0
- package/src/resolver/universal-resolver.ts +238 -0
- package/src/types/did-core.ts +580 -0
- package/src/types/did-resolution.ts +93 -0
- package/src/types/multibase.ts +29 -0
- package/src/types/portable-did.ts +64 -0
- package/src/utils.ts +532 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import type { DidMethodResolver } from '../methods/did-method.js';
|
|
2
|
+
import type { DidResolver, DidResolverCache, DidUrlDereferencer } from '../types/did-resolution.js';
|
|
3
|
+
import type { DidDereferencingOptions, DidDereferencingResult, DidResolutionOptions, DidResolutionResult, DidResource } from '../types/did-core.js';
|
|
4
|
+
|
|
5
|
+
import { Did } from '../did.js';
|
|
6
|
+
import { DidErrorCode } from '../did-error.js';
|
|
7
|
+
import { DidResolverCacheNoop } from './resolver-cache-noop.js';
|
|
8
|
+
import { EMPTY_DID_RESOLUTION_RESULT } from '../types/did-resolution.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Parameters for configuring the `UniversalResolver` class, which is responsible for resolving
|
|
12
|
+
* decentralized identifiers (DIDs) to their corresponding DID documents.
|
|
13
|
+
*
|
|
14
|
+
* This type specifies the essential components required by the `UniversalResolver` to perform
|
|
15
|
+
* DID resolution and dereferencing. It includes an array of `DidMethodResolver` instances,
|
|
16
|
+
* each capable of resolving DIDs for a specific method, and optionally, a cache for storing
|
|
17
|
+
* resolved DID documents to improve resolution efficiency.
|
|
18
|
+
*/
|
|
19
|
+
export type UniversalResolverParams = {
|
|
20
|
+
/**
|
|
21
|
+
* An array of `DidMethodResolver` instances.
|
|
22
|
+
*
|
|
23
|
+
* Each resolver in this array is designed to handle a specific DID method, enabling the
|
|
24
|
+
* `DidResolver` to support multiple DID methods simultaneously.
|
|
25
|
+
*/
|
|
26
|
+
didResolvers: DidMethodResolver[];
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* An optional `DidResolverCache` instance used for caching resolved DID documents.
|
|
30
|
+
*
|
|
31
|
+
* Providing a cache implementation can significantly enhance resolution performance by avoiding
|
|
32
|
+
* redundant resolutions for previously resolved DIDs. If omitted, a no-operation cache is used,
|
|
33
|
+
* which effectively disables caching.
|
|
34
|
+
*/
|
|
35
|
+
cache?: DidResolverCache;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The `DidResolver` class provides mechanisms for resolving Decentralized Identifiers (DIDs) to
|
|
40
|
+
* their corresponding DID documents.
|
|
41
|
+
*
|
|
42
|
+
* The class is designed to handle various DID methods by utilizing an array of `DidMethodResolver`
|
|
43
|
+
* instances, each responsible for a specific DID method.
|
|
44
|
+
*
|
|
45
|
+
* Providing a cache implementation can significantly enhance resolution performance by avoiding
|
|
46
|
+
* redundant resolutions for previously resolved DIDs. If omitted, a no-operation cache is used,
|
|
47
|
+
* which effectively disables caching.
|
|
48
|
+
*
|
|
49
|
+
* Usage:
|
|
50
|
+
* - Construct the `DidResolver` with an array of `DidMethodResolver` instances and an optional cache.
|
|
51
|
+
* - Use `resolve` to resolve a DID to its DID Resolution Result.
|
|
52
|
+
* - Use `dereference` to extract specific resources from a DID URL, like service endpoints or verification methods.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* const resolver = new DidResolver({
|
|
57
|
+
* didResolvers: [<array of DidMethodResolver instances>],
|
|
58
|
+
* cache: new DidResolverCacheNoop()
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* const resolutionResult = await resolver.resolve('did:example:123456');
|
|
62
|
+
* const dereferenceResult = await resolver.dereference({ didUri: 'did:example:123456#key-1' });
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export class UniversalResolver implements DidResolver, DidUrlDereferencer {
|
|
66
|
+
/**
|
|
67
|
+
* A cache for storing resolved DID documents.
|
|
68
|
+
*/
|
|
69
|
+
protected cache: DidResolverCache;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A map to store method resolvers against method names.
|
|
73
|
+
*/
|
|
74
|
+
private didResolvers: Map<string, DidMethodResolver> = new Map();
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Constructs a new `DidResolver`.
|
|
78
|
+
*
|
|
79
|
+
* @param params - The parameters for constructing the `DidResolver`.
|
|
80
|
+
*/
|
|
81
|
+
constructor({ cache, didResolvers }: UniversalResolverParams) {
|
|
82
|
+
this.cache = cache || DidResolverCacheNoop;
|
|
83
|
+
|
|
84
|
+
for (const resolver of didResolvers) {
|
|
85
|
+
this.didResolvers.set(resolver.methodName, resolver);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Resolves a DID to a DID Resolution Result.
|
|
91
|
+
*
|
|
92
|
+
* If the DID Resolution Result is present in the cache, it returns the cached result. Otherwise,
|
|
93
|
+
* it uses the appropriate method resolver to resolve the DID, stores the resolution result in the
|
|
94
|
+
* cache, and returns the resolultion result.
|
|
95
|
+
*
|
|
96
|
+
* @param didUri - The DID or DID URL to resolve.
|
|
97
|
+
* @returns A promise that resolves to the DID Resolution Result.
|
|
98
|
+
*/
|
|
99
|
+
public async resolve(didUri: string, options?: DidResolutionOptions): Promise<DidResolutionResult> {
|
|
100
|
+
|
|
101
|
+
const parsedDid = Did.parse(didUri);
|
|
102
|
+
if (!parsedDid) {
|
|
103
|
+
return {
|
|
104
|
+
...EMPTY_DID_RESOLUTION_RESULT,
|
|
105
|
+
didResolutionMetadata: {
|
|
106
|
+
error : DidErrorCode.InvalidDid,
|
|
107
|
+
errorMessage : `Invalid DID URI: ${didUri}`
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const resolver = this.didResolvers.get(parsedDid.method);
|
|
113
|
+
if (!resolver) {
|
|
114
|
+
return {
|
|
115
|
+
...EMPTY_DID_RESOLUTION_RESULT,
|
|
116
|
+
didResolutionMetadata: {
|
|
117
|
+
error : DidErrorCode.MethodNotSupported,
|
|
118
|
+
errorMessage : `Method not supported: ${parsedDid.method}`
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const cachedResolutionResult = await this.cache.get(parsedDid.uri);
|
|
124
|
+
|
|
125
|
+
if (cachedResolutionResult) {
|
|
126
|
+
return cachedResolutionResult;
|
|
127
|
+
} else {
|
|
128
|
+
const resolutionResult = await resolver.resolve(parsedDid.uri, options);
|
|
129
|
+
if (!resolutionResult.didResolutionMetadata.error) {
|
|
130
|
+
// Cache the resolution result if it was successful.
|
|
131
|
+
await this.cache.set(parsedDid.uri, resolutionResult);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return resolutionResult;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Dereferences a DID (Decentralized Identifier) URL to a corresponding DID resource.
|
|
140
|
+
*
|
|
141
|
+
* This method interprets the DID URL's components, which include the DID method, method-specific
|
|
142
|
+
* identifier, path, query, and fragment, and retrieves the related resource as per the DID Core
|
|
143
|
+
* specifications.
|
|
144
|
+
*
|
|
145
|
+
* The dereferencing process involves resolving the DID contained in the DID URL to a DID document,
|
|
146
|
+
* and then extracting the specific part of the document identified by the fragment in the DID URL.
|
|
147
|
+
* If no fragment is specified, the entire DID document is returned.
|
|
148
|
+
*
|
|
149
|
+
* This method supports resolution of different components within a DID document such as service
|
|
150
|
+
* endpoints and verification methods, based on their IDs. It accommodates both full and
|
|
151
|
+
* DID URLs as specified in the DID Core specification.
|
|
152
|
+
*
|
|
153
|
+
* More information on DID URL dereferencing can be found in the
|
|
154
|
+
* {@link https://www.w3.org/TR/did-core/#did-url-dereferencing | DID Core specification}.
|
|
155
|
+
*
|
|
156
|
+
* TODO: This is a partial implementation and does not fully implement DID URL dereferencing. (https://github.com/TBD54566975/web5-js/issues/387)
|
|
157
|
+
*
|
|
158
|
+
* @param didUrl - The DID URL string to dereference.
|
|
159
|
+
* @param [_options] - Input options to the dereference function. Optional.
|
|
160
|
+
* @returns a {@link DidDereferencingResult}
|
|
161
|
+
*/
|
|
162
|
+
async dereference(
|
|
163
|
+
didUrl: string,
|
|
164
|
+
_options?: DidDereferencingOptions
|
|
165
|
+
): Promise<DidDereferencingResult> {
|
|
166
|
+
|
|
167
|
+
// Validate the given `didUrl` confirms to the DID URL syntax.
|
|
168
|
+
const parsedDidUrl = Did.parse(didUrl);
|
|
169
|
+
|
|
170
|
+
if (!parsedDidUrl) {
|
|
171
|
+
return {
|
|
172
|
+
dereferencingMetadata : { error: DidErrorCode.InvalidDidUrl },
|
|
173
|
+
contentStream : null,
|
|
174
|
+
contentMetadata : {}
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Obtain the DID document for the input DID by executing DID resolution.
|
|
179
|
+
const { didDocument, didResolutionMetadata, didDocumentMetadata } = await this.resolve(parsedDidUrl.uri);
|
|
180
|
+
|
|
181
|
+
if (!didDocument) {
|
|
182
|
+
return {
|
|
183
|
+
dereferencingMetadata : { error: didResolutionMetadata.error },
|
|
184
|
+
contentStream : null,
|
|
185
|
+
contentMetadata : {}
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Return the entire DID Document if no query or fragment is present on the DID URL.
|
|
190
|
+
if (!parsedDidUrl.fragment || parsedDidUrl.query) {
|
|
191
|
+
return {
|
|
192
|
+
dereferencingMetadata : { contentType: 'application/did+json' },
|
|
193
|
+
contentStream : didDocument,
|
|
194
|
+
contentMetadata : didDocumentMetadata
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const { service = [], verificationMethod = [] } = didDocument;
|
|
199
|
+
|
|
200
|
+
// Create a set of possible id matches. The DID spec allows for an id to be the entire
|
|
201
|
+
// did#fragment or just #fragment.
|
|
202
|
+
// @see {@link }https://www.w3.org/TR/did-core/#relative-did-urls | Section 3.2.2, Relative DID URLs}.
|
|
203
|
+
// Using a Set for fast string comparison since some DID methods have long identifiers.
|
|
204
|
+
const idSet = new Set([didUrl, parsedDidUrl.fragment, `#${parsedDidUrl.fragment}`]);
|
|
205
|
+
|
|
206
|
+
let didResource: DidResource | undefined;
|
|
207
|
+
|
|
208
|
+
// Find the first matching verification method in the DID document.
|
|
209
|
+
for (let vm of verificationMethod) {
|
|
210
|
+
if (idSet.has(vm.id)) {
|
|
211
|
+
didResource = vm;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Find the first matching service in the DID document.
|
|
217
|
+
for (let svc of service) {
|
|
218
|
+
if (idSet.has(svc.id)) {
|
|
219
|
+
didResource = svc;
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (didResource) {
|
|
225
|
+
return {
|
|
226
|
+
dereferencingMetadata : { contentType: 'application/did+json' },
|
|
227
|
+
contentStream : didResource,
|
|
228
|
+
contentMetadata : didResolutionMetadata
|
|
229
|
+
};
|
|
230
|
+
} else {
|
|
231
|
+
return {
|
|
232
|
+
dereferencingMetadata : { error: DidErrorCode.NotFound },
|
|
233
|
+
contentStream : null,
|
|
234
|
+
contentMetadata : {},
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|