@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,458 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { Convert, Multicodec } from '@enbox/common';
|
|
11
|
+
import { computeJwkThumbprint } from '@enbox/crypto';
|
|
12
|
+
import { DidError, DidErrorCode } from './did-error.js';
|
|
13
|
+
import { DidVerificationRelationship, } from './types/did-core.js';
|
|
14
|
+
/**
|
|
15
|
+
* Extracts the fragment part of a Decentralized Identifier (DID) verification method identifier.
|
|
16
|
+
*
|
|
17
|
+
* This function takes any input and aims to return only the fragment of a DID identifier,
|
|
18
|
+
* which comes after the '#' symbol in a DID string. It's designed specifically for handling
|
|
19
|
+
* DID verification method identifiers. The function returns undefined for non-string inputs, inputs
|
|
20
|
+
* that do not contain a '#', or complex data structures like objects or arrays, ensuring that only
|
|
21
|
+
* the fragment part of a DID string is extracted when present.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* console.log(extractDidFragment("did:example:123#key-1")); // Output: "key-1"
|
|
26
|
+
* console.log(extractDidFragment("did:example:123")); // Output: undefined
|
|
27
|
+
* console.log(extractDidFragment({ id: "did:example:123#0", type: "JsonWebKey" })); // Output: undefined
|
|
28
|
+
* console.log(extractDidFragment(undefined)); // Output: undefined
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @param input - The input to be processed. Can be of any type, but the function is designed
|
|
32
|
+
* to work with strings that represent DID verification method identifiers.
|
|
33
|
+
* @returns The fragment part of the DID identifier if the input is a string containing a '#'.
|
|
34
|
+
* Returns an empty string for all other inputs, including non-string types, strings
|
|
35
|
+
* without a '#', and complex data structures.
|
|
36
|
+
*/
|
|
37
|
+
export function extractDidFragment(input) {
|
|
38
|
+
if (typeof input !== 'string')
|
|
39
|
+
return undefined;
|
|
40
|
+
if (input.length === 0)
|
|
41
|
+
return undefined;
|
|
42
|
+
return input.split('#').pop();
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Retrieves services from a given DID document, optionally filtered by `id` or `type`.
|
|
46
|
+
*
|
|
47
|
+
* If no `id` or `type` filters are provided, all defined services are returned.
|
|
48
|
+
*
|
|
49
|
+
* The given DID Document must adhere to the
|
|
50
|
+
* {@link https://www.w3.org/TR/did-core/ | W3C DID Core Specification}.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* const didDocument = { ... }; // W3C DID document
|
|
55
|
+
* const services = getServices({ didDocument, type: 'DecentralizedWebNode' });
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @param params - An object containing input parameters for retrieving services.
|
|
59
|
+
* @param params.didDocument - The DID document from which services are retrieved.
|
|
60
|
+
* @param params.id - Optional. A string representing the specific service ID to match. If provided, only the service with this ID will be returned.
|
|
61
|
+
* @param params.type - Optional. A string representing the specific service type to match. If provided, only the service(s) of this type will be returned.
|
|
62
|
+
* @returns An array of services. If no matching service is found, an empty array is returned.
|
|
63
|
+
*/
|
|
64
|
+
export function getServices({ didDocument, id, type }) {
|
|
65
|
+
var _a, _b;
|
|
66
|
+
return (_b = (_a = didDocument === null || didDocument === void 0 ? void 0 : didDocument.service) === null || _a === void 0 ? void 0 : _a.filter(service => {
|
|
67
|
+
if (id && service.id !== id)
|
|
68
|
+
return false;
|
|
69
|
+
if (type && service.type !== type)
|
|
70
|
+
return false;
|
|
71
|
+
return true;
|
|
72
|
+
})) !== null && _b !== void 0 ? _b : [];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Retrieves a verification method object from a DID document if there is a match for the given
|
|
76
|
+
* public key.
|
|
77
|
+
*
|
|
78
|
+
* This function searches the verification methods in a given DID document for a match with the
|
|
79
|
+
* provided public key (either in JWK or multibase format). If a matching verification method is
|
|
80
|
+
* found it is returned. If no match is found `null` is returned.
|
|
81
|
+
*
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* const didDocument = {
|
|
86
|
+
* // ... contents of a DID document ...
|
|
87
|
+
* };
|
|
88
|
+
* const publicKeyJwk = { kty: 'OKP', crv: 'Ed25519', x: '...' };
|
|
89
|
+
*
|
|
90
|
+
* const verificationMethod = await getVerificationMethodByKey({
|
|
91
|
+
* didDocument,
|
|
92
|
+
* publicKeyJwk
|
|
93
|
+
* });
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* @param params - An object containing input parameters for retrieving the verification method ID.
|
|
97
|
+
* @param params.didDocument - The DID document to search for the verification method.
|
|
98
|
+
* @param params.publicKeyJwk - The public key in JSON Web Key (JWK) format to match against the verification methods in the DID document.
|
|
99
|
+
* @param params.publicKeyMultibase - The public key as a multibase encoded string to match against the verification methods in the DID document.
|
|
100
|
+
* @returns A promise that resolves with the matching verification method, or `null` if no match is found.
|
|
101
|
+
* @throws Throws an `Error` if the `didDocument` parameter is missing or if the `didDocument` does not contain any verification methods.
|
|
102
|
+
*/
|
|
103
|
+
export function getVerificationMethodByKey(_a) {
|
|
104
|
+
return __awaiter(this, arguments, void 0, function* ({ didDocument, publicKeyJwk, publicKeyMultibase }) {
|
|
105
|
+
// Collect all verification methods from the DID document.
|
|
106
|
+
const verificationMethods = getVerificationMethods({ didDocument });
|
|
107
|
+
for (let method of verificationMethods) {
|
|
108
|
+
if (publicKeyJwk && method.publicKeyJwk) {
|
|
109
|
+
const publicKeyThumbprint = yield computeJwkThumbprint({ jwk: publicKeyJwk });
|
|
110
|
+
if (publicKeyThumbprint === (yield computeJwkThumbprint({ jwk: method.publicKeyJwk }))) {
|
|
111
|
+
return method;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else if (publicKeyMultibase && method.publicKeyMultibase) {
|
|
115
|
+
if (publicKeyMultibase === method.publicKeyMultibase) {
|
|
116
|
+
return method;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Retrieves all verification methods from a given DID document, including embedded methods.
|
|
125
|
+
*
|
|
126
|
+
* This function consolidates all verification methods into a single array for easy access and
|
|
127
|
+
* processing. It checks both the primary `verificationMethod` array and the individual verification
|
|
128
|
+
* relationship properties `authentication`, `assertionMethod`, `keyAgreement`,
|
|
129
|
+
* `capabilityInvocation`, and `capabilityDelegation` for embedded methods.
|
|
130
|
+
*
|
|
131
|
+
* The given DID Document must adhere to the
|
|
132
|
+
* {@link https://www.w3.org/TR/did-core/ | W3C DID Core Specification}.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* const didDocument = { ... }; // W3C DID document
|
|
137
|
+
* const verificationMethods = getVerificationMethods({ didDocument });
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @param params - An object containing input parameters for retrieving verification methods.
|
|
141
|
+
* @param params.didDocument - The DID document from which verification methods are retrieved.
|
|
142
|
+
* @returns An array of `DidVerificationMethod`. If no verification methods are found, an empty array is returned.
|
|
143
|
+
* @throws Throws an `TypeError` if the `didDocument` parameter is missing.
|
|
144
|
+
*/
|
|
145
|
+
export function getVerificationMethods({ didDocument }) {
|
|
146
|
+
var _a, _b;
|
|
147
|
+
if (!didDocument)
|
|
148
|
+
throw new TypeError(`Required parameter missing: 'didDocument'`);
|
|
149
|
+
const verificationMethods = [];
|
|
150
|
+
// Check the 'verificationMethod' array.
|
|
151
|
+
verificationMethods.push(...(_b = (_a = didDocument.verificationMethod) === null || _a === void 0 ? void 0 : _a.filter(isDidVerificationMethod)) !== null && _b !== void 0 ? _b : []);
|
|
152
|
+
// Check verification relationship properties for embedded verification methods.
|
|
153
|
+
Object.keys(DidVerificationRelationship).forEach((relationship) => {
|
|
154
|
+
var _a, _b;
|
|
155
|
+
verificationMethods.push(...(_b = (_a = didDocument[relationship]) === null || _a === void 0 ? void 0 : _a.filter(isDidVerificationMethod)) !== null && _b !== void 0 ? _b : []);
|
|
156
|
+
});
|
|
157
|
+
return verificationMethods;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Retrieves all DID verification method types from a given DID document.
|
|
161
|
+
*
|
|
162
|
+
* The given DID Document must adhere to the
|
|
163
|
+
* {@link https://www.w3.org/TR/did-core/ | W3C DID Core Specification}.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```ts
|
|
167
|
+
* const didDocument = {
|
|
168
|
+
* verificationMethod: [
|
|
169
|
+
* {
|
|
170
|
+
* 'id' : 'did:example:123#key-0',
|
|
171
|
+
* 'type' : 'Ed25519VerificationKey2018',
|
|
172
|
+
* 'controller' : 'did:example:123',
|
|
173
|
+
* 'publicKeyBase58' : '3M5RCDjPTWPkKSN3sxUmmMqHbmRPegYP1tjcKyrDbt9J'
|
|
174
|
+
* },
|
|
175
|
+
* {
|
|
176
|
+
* 'id' : 'did:example:123#key-1',
|
|
177
|
+
* 'type' : 'X25519KeyAgreementKey2019',
|
|
178
|
+
* 'controller' : 'did:example:123',
|
|
179
|
+
* 'publicKeyBase58' : 'FbQWLPRhTH95MCkQUeFYdiSoQt8zMwetqfWoxqPgaq7x'
|
|
180
|
+
* },
|
|
181
|
+
* {
|
|
182
|
+
* 'id' : 'did:example:123#key-3',
|
|
183
|
+
* 'type' : 'JsonWebKey2020',
|
|
184
|
+
* 'controller' : 'did:example:123',
|
|
185
|
+
* 'publicKeyJwk' : {
|
|
186
|
+
* 'kty' : 'EC',
|
|
187
|
+
* 'crv' : 'P-256',
|
|
188
|
+
* 'x' : 'Er6KSSnAjI70ObRWhlaMgqyIOQYrDJTE94ej5hybQ2M',
|
|
189
|
+
* 'y' : 'pPVzCOTJwgikPjuUE6UebfZySqEJ0ZtsWFpj7YSPGEk'
|
|
190
|
+
* }
|
|
191
|
+
* }
|
|
192
|
+
* ]
|
|
193
|
+
* },
|
|
194
|
+
* const vmTypes = getVerificationMethodTypes({ didDocument });
|
|
195
|
+
* console.log(vmTypes);
|
|
196
|
+
* // Output: ['Ed25519VerificationKey2018', 'X25519KeyAgreementKey2019', 'JsonWebKey2020']
|
|
197
|
+
* ```
|
|
198
|
+
*
|
|
199
|
+
* @param params - An object containing input parameters for retrieving types.
|
|
200
|
+
* @param params.didDocument - The DID document from which types are retrieved.
|
|
201
|
+
* @returns An array of types. If no types were found, an empty array is returned.
|
|
202
|
+
*/
|
|
203
|
+
export function getVerificationMethodTypes({ didDocument }) {
|
|
204
|
+
// Collect all verification methods from the DID document.
|
|
205
|
+
const verificationMethods = getVerificationMethods({ didDocument });
|
|
206
|
+
// Map to extract 'type' from each verification method.
|
|
207
|
+
const types = verificationMethods.map(method => method.type);
|
|
208
|
+
return [...new Set(types)]; // Return only unique types.
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Retrieves a list of DID verification relationships by a specific method ID from a DID document.
|
|
212
|
+
*
|
|
213
|
+
* This function examines the specified DID document to identify any verification relationships
|
|
214
|
+
* (e.g., `authentication`, `assertionMethod`) that reference a verification method by its method ID
|
|
215
|
+
* or contain an embedded verification method matching the method ID. The method ID is typically a
|
|
216
|
+
* fragment of a DID (e.g., `did:example:123#key-1`) that uniquely identifies a verification method
|
|
217
|
+
* within the DID document.
|
|
218
|
+
*
|
|
219
|
+
* The search considers both direct references to verification methods by their IDs and verification
|
|
220
|
+
* methods embedded within the verification relationship arrays. It returns an array of
|
|
221
|
+
* `DidVerificationRelationship` enums corresponding to the verification relationships that contain
|
|
222
|
+
* the specified method ID.
|
|
223
|
+
*
|
|
224
|
+
* @param params - An object containing input parameters for retrieving verification relationships.
|
|
225
|
+
* @param params.didDocument - The DID document to search for verification relationships.
|
|
226
|
+
* @param params.methodId - The method ID to search for within the verification relationships.
|
|
227
|
+
* @returns An array of `DidVerificationRelationship` enums representing the types of verification
|
|
228
|
+
* relationships that reference the specified method ID.
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```ts
|
|
232
|
+
* const didDocument: DidDocument = {
|
|
233
|
+
* // ...contents of a DID document...
|
|
234
|
+
* };
|
|
235
|
+
*
|
|
236
|
+
* const relationships = getVerificationRelationshipsById({
|
|
237
|
+
* didDocument,
|
|
238
|
+
* methodId: 'key-1'
|
|
239
|
+
* });
|
|
240
|
+
* console.log(relationships);
|
|
241
|
+
* // Output might include ['authentication', 'assertionMethod'] if those relationships
|
|
242
|
+
* // reference or contain the specified method ID.
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
export function getVerificationRelationshipsById({ didDocument, methodId }) {
|
|
246
|
+
const relationships = [];
|
|
247
|
+
Object.keys(DidVerificationRelationship).forEach((relationship) => {
|
|
248
|
+
if (Array.isArray(didDocument[relationship])) {
|
|
249
|
+
const relationshipMethods = didDocument[relationship];
|
|
250
|
+
const methodIdFragment = extractDidFragment(methodId);
|
|
251
|
+
// Check if the verification relationship property contains a matching method ID either
|
|
252
|
+
// directly referenced or as an embedded verification method.
|
|
253
|
+
const containsMethodId = relationshipMethods.some(method => {
|
|
254
|
+
const isByReferenceMatch = extractDidFragment(method) === methodIdFragment;
|
|
255
|
+
const isEmbeddedMethodMatch = isDidVerificationMethod(method) && extractDidFragment(method.id) === methodIdFragment;
|
|
256
|
+
return isByReferenceMatch || isEmbeddedMethodMatch;
|
|
257
|
+
});
|
|
258
|
+
if (containsMethodId) {
|
|
259
|
+
relationships.push(relationship);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
return relationships;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Checks if a given object is a {@link DidService}.
|
|
267
|
+
*
|
|
268
|
+
* A {@link DidService} in the context of DID resources must include the properties `id`, `type`,
|
|
269
|
+
* and `serviceEndpoint`. The `serviceEndpoint` can be a `DidServiceEndpoint` or an array of
|
|
270
|
+
* `DidServiceEndpoint` objects.
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```ts
|
|
274
|
+
* const service = {
|
|
275
|
+
* id: "did:example:123#service-1",
|
|
276
|
+
* type: "OidcService",
|
|
277
|
+
* serviceEndpoint: "https://example.com/oidc"
|
|
278
|
+
* };
|
|
279
|
+
*
|
|
280
|
+
* if (isDidService(service)) {
|
|
281
|
+
* console.log('The object is a DidService');
|
|
282
|
+
* } else {
|
|
283
|
+
* console.log('The object is not a DidService');
|
|
284
|
+
* }
|
|
285
|
+
* ```
|
|
286
|
+
*
|
|
287
|
+
* @param obj - The object to be checked.
|
|
288
|
+
* @returns `true` if `obj` is a `DidService`; otherwise, `false`.
|
|
289
|
+
*/
|
|
290
|
+
export function isDidService(obj) {
|
|
291
|
+
// Validate that the given value is an object.
|
|
292
|
+
if (!obj || typeof obj !== 'object' || obj === null)
|
|
293
|
+
return false;
|
|
294
|
+
// Validate that the object has the necessary properties of DidService.
|
|
295
|
+
return 'id' in obj && 'type' in obj && 'serviceEndpoint' in obj;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Checks if a given object is a {@link DwnDidService}.
|
|
299
|
+
*
|
|
300
|
+
* A {@link DwnDidService} is defined as {@link DidService} object with a `type` of
|
|
301
|
+
* "DecentralizedWebNode" and `enc` and `sig` properties, where both properties are either strings
|
|
302
|
+
* or arrays of strings.
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* ```ts
|
|
306
|
+
* const didDocument: DidDocument = {
|
|
307
|
+
* id: 'did:example:123',
|
|
308
|
+
* verificationMethod: [
|
|
309
|
+
* {
|
|
310
|
+
* id: 'did:example:123#key-1',
|
|
311
|
+
* type: 'JsonWebKey2020',
|
|
312
|
+
* controller: 'did:example:123',
|
|
313
|
+
* publicKeyJwk: { ... }
|
|
314
|
+
* },
|
|
315
|
+
* {
|
|
316
|
+
* id: 'did:example:123#key-2',
|
|
317
|
+
* type: 'JsonWebKey2020',
|
|
318
|
+
* controller: 'did:example:123',
|
|
319
|
+
* publicKeyJwk: { ... }
|
|
320
|
+
* }
|
|
321
|
+
* ],
|
|
322
|
+
* service: [
|
|
323
|
+
* {
|
|
324
|
+
* id: 'did:example:123#dwn',
|
|
325
|
+
* type: 'DecentralizedWebNode',
|
|
326
|
+
* serviceEndpoint: 'https://enbox-production.up.railway.app',
|
|
327
|
+
* enc: 'did:example:123#key-1',
|
|
328
|
+
* sig: 'did:example:123#key-2'
|
|
329
|
+
* }
|
|
330
|
+
* ]
|
|
331
|
+
* };
|
|
332
|
+
*
|
|
333
|
+
* if (isDwnService(didDocument.service[0])) {
|
|
334
|
+
* console.log('The object is a DwnDidService');
|
|
335
|
+
* } else {
|
|
336
|
+
* console.log('The object is not a DwnDidService');
|
|
337
|
+
* }
|
|
338
|
+
* ```
|
|
339
|
+
*
|
|
340
|
+
* @see {@link https://identity.foundation/decentralized-web-node/spec/ | Decentralized Web Node (DWN) Specification}
|
|
341
|
+
*
|
|
342
|
+
* @param obj - The object to be checked.
|
|
343
|
+
* @returns `true` if `obj` is a DwnDidService; otherwise, `false`.
|
|
344
|
+
*/
|
|
345
|
+
export function isDwnDidService(obj) {
|
|
346
|
+
// Validate that the given value is a {@link DidService}.
|
|
347
|
+
if (!isDidService(obj))
|
|
348
|
+
return false;
|
|
349
|
+
// Validate that the `type` property is `DecentralizedWebNode`.
|
|
350
|
+
if (obj.type !== 'DecentralizedWebNode')
|
|
351
|
+
return false;
|
|
352
|
+
// Validate that the given object has the `enc` and `sig` properties.
|
|
353
|
+
if (!('enc' in obj && 'sig' in obj))
|
|
354
|
+
return false;
|
|
355
|
+
// Validate that the `enc` and `sig` properties are either strings or arrays of strings.
|
|
356
|
+
const isStringOrStringArray = (prop) => typeof prop === 'string' || Array.isArray(prop) && prop.every(item => typeof item === 'string');
|
|
357
|
+
return (isStringOrStringArray(obj.enc)) && (isStringOrStringArray(obj.sig));
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Checks if a given object is a DID Verification Method.
|
|
361
|
+
*
|
|
362
|
+
* A {@link DidVerificationMethod} in the context of DID resources must include the properties `id`,
|
|
363
|
+
* `type`, and `controller`.
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* ```ts
|
|
367
|
+
* const resource = {
|
|
368
|
+
* id : "did:example:123#0",
|
|
369
|
+
* type : "JsonWebKey2020",
|
|
370
|
+
* controller : "did:example:123",
|
|
371
|
+
* publicKeyJwk : { ... }
|
|
372
|
+
* };
|
|
373
|
+
*
|
|
374
|
+
* if (isDidVerificationMethod(resource)) {
|
|
375
|
+
* console.log('The resource is a DidVerificationMethod');
|
|
376
|
+
* } else {
|
|
377
|
+
* console.log('The resource is not a DidVerificationMethod');
|
|
378
|
+
* }
|
|
379
|
+
* ```
|
|
380
|
+
*
|
|
381
|
+
* @param obj - The object to be checked.
|
|
382
|
+
* @returns `true` if `obj` is a `DidVerificationMethod`; otherwise, `false`.
|
|
383
|
+
*/
|
|
384
|
+
export function isDidVerificationMethod(obj) {
|
|
385
|
+
// Validate that the given value is an object.
|
|
386
|
+
if (!obj || typeof obj !== 'object' || obj === null)
|
|
387
|
+
return false;
|
|
388
|
+
// Validate that the object has the necessary properties of a DidVerificationMethod.
|
|
389
|
+
if (!('id' in obj && 'type' in obj && 'controller' in obj))
|
|
390
|
+
return false;
|
|
391
|
+
if (typeof obj.id !== 'string')
|
|
392
|
+
return false;
|
|
393
|
+
if (typeof obj.type !== 'string')
|
|
394
|
+
return false;
|
|
395
|
+
if (typeof obj.controller !== 'string')
|
|
396
|
+
return false;
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Converts a cryptographic key to a multibase identifier.
|
|
401
|
+
*
|
|
402
|
+
* @remarks
|
|
403
|
+
* This method provides a way to represent a cryptographic key as a multibase identifier.
|
|
404
|
+
* It takes a `Uint8Array` representing the key, and either the multicodec code or multicodec name
|
|
405
|
+
* as input. The method first adds the multicodec prefix to the key, then encodes it into Base58
|
|
406
|
+
* format. Finally, it converts the Base58 encoded key into a multibase identifier.
|
|
407
|
+
*
|
|
408
|
+
* @example
|
|
409
|
+
* ```ts
|
|
410
|
+
* const key = new Uint8Array([...]); // Cryptographic key as Uint8Array
|
|
411
|
+
* const multibaseId = keyBytesToMultibaseId({ key, multicodecName: 'ed25519-pub' });
|
|
412
|
+
* ```
|
|
413
|
+
*
|
|
414
|
+
* @param params - The parameters for the conversion.
|
|
415
|
+
* @returns The multibase identifier as a string.
|
|
416
|
+
*/
|
|
417
|
+
export function keyBytesToMultibaseId({ keyBytes, multicodecCode, multicodecName }) {
|
|
418
|
+
const prefixedKey = Multicodec.addPrefix({
|
|
419
|
+
code: multicodecCode,
|
|
420
|
+
data: keyBytes,
|
|
421
|
+
name: multicodecName
|
|
422
|
+
});
|
|
423
|
+
const prefixedKeyB58 = Convert.uint8Array(prefixedKey).toBase58Btc();
|
|
424
|
+
const multibaseKeyId = Convert.base58Btc(prefixedKeyB58).toMultibase();
|
|
425
|
+
return multibaseKeyId;
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Converts a multibase identifier to a cryptographic key.
|
|
429
|
+
*
|
|
430
|
+
* @remarks
|
|
431
|
+
* This function decodes a multibase identifier back into a cryptographic key. It first decodes the
|
|
432
|
+
* identifier from multibase format into Base58 format, and then converts it into a `Uint8Array`.
|
|
433
|
+
* Afterward, it removes the multicodec prefix, extracting the raw key data along with the
|
|
434
|
+
* multicodec code and name.
|
|
435
|
+
*
|
|
436
|
+
* @example
|
|
437
|
+
* ```ts
|
|
438
|
+
* const multibaseKeyId = '...'; // Multibase identifier of the key
|
|
439
|
+
* const { key, multicodecCode, multicodecName } = multibaseIdToKey({ multibaseKeyId });
|
|
440
|
+
* ```
|
|
441
|
+
*
|
|
442
|
+
* @param params - The parameters for the conversion.
|
|
443
|
+
* @param params.multibaseKeyId - The multibase identifier string of the key.
|
|
444
|
+
* @returns An object containing the key as a `Uint8Array` and its multicodec code and name.
|
|
445
|
+
* @throws `DidError` if the multibase identifier is invalid.
|
|
446
|
+
*/
|
|
447
|
+
export function multibaseIdToKeyBytes({ multibaseKeyId }) {
|
|
448
|
+
try {
|
|
449
|
+
const prefixedKeyB58 = Convert.multibase(multibaseKeyId).toBase58Btc();
|
|
450
|
+
const prefixedKey = Convert.base58Btc(prefixedKeyB58).toUint8Array();
|
|
451
|
+
const { code, data, name } = Multicodec.removePrefix({ prefixedData: prefixedKey });
|
|
452
|
+
return { keyBytes: data, multicodecCode: code, multicodecName: name };
|
|
453
|
+
}
|
|
454
|
+
catch (error) {
|
|
455
|
+
throw new DidError(DidErrorCode.InvalidDid, `Invalid multibase identifier: ${multibaseKeyId}`);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
//# sourceMappingURL=utils.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EAAE,CAAC;QACvE,MAAM,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC,YAAY,EAAE,CAAC;QACrE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,UAAU,CAAC,YAAY,CAAC,EAAE,YAAY,EAAE,WAAW,EAAE,CAAC,CAAC;QAEpF,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,cAAc,EAAE,IAAI,EAAE,cAAc,EAAE,IAAI,EAAE,CAAC;IACxE,CAAC;IAAC,OAAO,KAAU,EAAE,CAAC;QACpB,MAAM,IAAI,QAAQ,CAAC,YAAY,CAAC,UAAU,EAAE,iCAAiC,cAAc,EAAE,CAAC,CAAC;IACjG,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { Signer, CryptoApi, KeyIdentifier, KmsExportKeyParams, KmsImportKeyParams, KeyImporterExporter } from '@enbox/crypto';
|
|
2
|
+
import type { DidDocument } from './types/did-core.js';
|
|
3
|
+
import type { DidMetadata, PortableDid } from './types/portable-did.js';
|
|
4
|
+
/**
|
|
5
|
+
* A `BearerDidSigner` extends the {@link Signer} interface to include specific properties for
|
|
6
|
+
* signing with a Decentralized Identifier (DID). It encapsulates the algorithm and key identifier,
|
|
7
|
+
* which are often needed when signing JWTs, JWSs, JWEs, and other data structures.
|
|
8
|
+
*
|
|
9
|
+
* Typically, the algorithm and key identifier are used to populate the `alg` and `kid` fields of a
|
|
10
|
+
* JWT or JWS header.
|
|
11
|
+
*/
|
|
12
|
+
export interface BearerDidSigner extends Signer {
|
|
13
|
+
/**
|
|
14
|
+
* The cryptographic algorithm identifier used for signing operations.
|
|
15
|
+
*
|
|
16
|
+
* Typically, this value is used to populate the `alg` field of a JWT or JWS header. The
|
|
17
|
+
* registered algorithm names are defined in the
|
|
18
|
+
* {@link https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-algorithms | IANA JSON Web Signature and Encryption Algorithms registry}.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* "ES256" // ECDSA using P-256 and SHA-256
|
|
22
|
+
*/
|
|
23
|
+
algorithm: string;
|
|
24
|
+
/**
|
|
25
|
+
* The unique identifier of the key within the DID document that is used for signing and
|
|
26
|
+
* verification operations.
|
|
27
|
+
*
|
|
28
|
+
* This identifier must be a DID URI with a fragment (e.g., did:method:123#key-0) that references
|
|
29
|
+
* a specific verification method in the DID document. It allows users of a `BearerDidSigner` to
|
|
30
|
+
* determine the DID and key that will be used for signing and verification operations.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* "did:dht:123#key-1" // A fragment identifier referring to a key in the DID document
|
|
34
|
+
*/
|
|
35
|
+
keyId: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Represents a Decentralized Identifier (DID) along with its DID document, key manager, metadata,
|
|
39
|
+
* and convenience functions.
|
|
40
|
+
*/
|
|
41
|
+
export declare class BearerDid {
|
|
42
|
+
/** {@inheritDoc Did#uri} */
|
|
43
|
+
uri: string;
|
|
44
|
+
/**
|
|
45
|
+
* The DID document associated with this DID.
|
|
46
|
+
*
|
|
47
|
+
* @see {@link https://www.w3.org/TR/did-core/#dfn-diddocument | DID Core Specification, § DID Document}
|
|
48
|
+
*/
|
|
49
|
+
document: DidDocument;
|
|
50
|
+
/** {@inheritDoc DidMetadata} */
|
|
51
|
+
metadata: DidMetadata;
|
|
52
|
+
/**
|
|
53
|
+
* Key Management System (KMS) used to manage the DIDs keys and sign data.
|
|
54
|
+
*
|
|
55
|
+
* Each DID method requires at least one key be present in the provided `keyManager`.
|
|
56
|
+
*/
|
|
57
|
+
keyManager: CryptoApi;
|
|
58
|
+
constructor({ uri, document, metadata, keyManager }: {
|
|
59
|
+
uri: string;
|
|
60
|
+
document: DidDocument;
|
|
61
|
+
metadata: DidMetadata;
|
|
62
|
+
keyManager: CryptoApi;
|
|
63
|
+
});
|
|
64
|
+
/**
|
|
65
|
+
* Converts a `BearerDid` object to a portable format containing the URI and verification methods
|
|
66
|
+
* associated with the DID.
|
|
67
|
+
*
|
|
68
|
+
* This method is useful when you need to represent the key material and metadata associated with
|
|
69
|
+
* a DID in format that can be used independently of the specific DID method implementation. It
|
|
70
|
+
* extracts both public and private keys from the DID's key manager and organizes them into a
|
|
71
|
+
* `PortableDid` structure.
|
|
72
|
+
*
|
|
73
|
+
* @remarks
|
|
74
|
+
* If the DID's key manager does not allow private keys to be exported, the `PortableDid` returned
|
|
75
|
+
* will not contain a `privateKeys` property. This enables the importing and exporting DIDs that
|
|
76
|
+
* use the same underlying KMS even if the KMS does not support exporting private keys. Examples
|
|
77
|
+
* include hardware security modules (HSMs) and cloud-based KMS services like AWS KMS.
|
|
78
|
+
*
|
|
79
|
+
* If the DID's key manager does support exporting private keys, the resulting `PortableDid` will
|
|
80
|
+
* include a `privateKeys` property which contains the same number of entries as there are
|
|
81
|
+
* verification methods as the DID document, each with its associated private key and the
|
|
82
|
+
* purpose(s) for which the key can be used (e.g., `authentication`, `assertionMethod`, etc.).
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* // Assuming `did` is an instance of BearerDid
|
|
87
|
+
* const portableDid = await did.export();
|
|
88
|
+
* // portableDid now contains the DID URI, document, metadata, and optionally, private keys.
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @returns A `PortableDid` containing the URI, DID document, metadata, and optionally private
|
|
92
|
+
* keys associated with the `BearerDid`.
|
|
93
|
+
* @throws An error if the DID document does not contain any verification methods or the keys for
|
|
94
|
+
* any verification method are missing in the key manager.
|
|
95
|
+
*/
|
|
96
|
+
export(): Promise<PortableDid>;
|
|
97
|
+
/**
|
|
98
|
+
* Return a {@link Signer} that can be used to sign messages, credentials, or arbitrary data.
|
|
99
|
+
*
|
|
100
|
+
* If given, the `methodId` parameter is used to select a key from the verification methods
|
|
101
|
+
* present in the DID Document.
|
|
102
|
+
*
|
|
103
|
+
* If `methodID` is not given, the first verification method intended for signing claims is used.
|
|
104
|
+
*
|
|
105
|
+
* @param params - The parameters for the `getSigner` operation.
|
|
106
|
+
* @param params.methodId - ID of the verification method key that will be used for sign and
|
|
107
|
+
* verify operations. Optional.
|
|
108
|
+
* @returns An instantiated {@link Signer} that can be used to sign and verify data.
|
|
109
|
+
*/
|
|
110
|
+
getSigner(params?: {
|
|
111
|
+
methodId: string;
|
|
112
|
+
}): Promise<BearerDidSigner>;
|
|
113
|
+
/**
|
|
114
|
+
* Instantiates a {@link BearerDid} object from a given {@link PortableDid}.
|
|
115
|
+
*
|
|
116
|
+
* This method allows for the creation of a `BearerDid` object using a previously created DID's
|
|
117
|
+
* key material, DID document, and metadata.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* // Export an existing BearerDid to PortableDid format.
|
|
122
|
+
* const portableDid = await did.export();
|
|
123
|
+
* // Reconstruct a BearerDid object from the PortableDid.
|
|
124
|
+
* const did = await BearerDid.import({ portableDid });
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* @param params - The parameters for the import operation.
|
|
128
|
+
* @param params.portableDid - The PortableDid object to import.
|
|
129
|
+
* @param params.keyManager - Optionally specify an external Key Management System (KMS) used to
|
|
130
|
+
* generate keys and sign data. If not given, a new
|
|
131
|
+
* {@link LocalKeyManager} instance will be created and
|
|
132
|
+
* used.
|
|
133
|
+
* @returns A Promise resolving to a `BearerDid` object representing the DID formed from the
|
|
134
|
+
* provided PortableDid.
|
|
135
|
+
* @throws An error if the PortableDid document does not contain any verification methods or the
|
|
136
|
+
* keys for any verification method are missing in the key manager.
|
|
137
|
+
*/
|
|
138
|
+
static import({ portableDid, keyManager }: {
|
|
139
|
+
keyManager?: CryptoApi & KeyImporterExporter<KmsImportKeyParams, KeyIdentifier, KmsExportKeyParams>;
|
|
140
|
+
portableDid: PortableDid;
|
|
141
|
+
}): Promise<BearerDid>;
|
|
142
|
+
}
|
|
143
|
+
//# sourceM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AAC,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;CA6CvB"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A custom error class for DID-related errors.
|
|
3
|
+
*/
|
|
4
|
+
export declare class DidError extends Error {
|
|
5
|
+
code: DidErrorCode;
|
|
6
|
+
/**
|
|
7
|
+
* Constructs an instance of DidError, a custom error class for handling DID-related errors.
|
|
8
|
+
*
|
|
9
|
+
* @param code - A {@link DidErrorCode} representing the specific type of error encountered.
|
|
10
|
+
* @param message - A human-readable description of the error.
|
|
11
|
+
*/
|
|
12
|
+
constructor(code: DidErrorCode, message: string);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* An enumeration of possible DID error codes.
|
|
16
|
+
*/
|
|
17
|
+
export declare enum DidErrorCode {
|
|
18
|
+
/** The DID supplied does not conform to valid syntax. */
|
|
19
|
+
InvalidDid = "invalidDid",
|
|
20
|
+
/** The supplied method name is not supported by the DID method and/or DID resolver implementation. */
|
|
21
|
+
MethodNotSupported = "methodNotSupported",
|
|
22
|
+
/** An unexpected error occurred during the requested DID operation. */
|
|
23
|
+
InternalError = "internalError",
|
|
24
|
+
/** The DID document supplied does not conform to valid syntax. */
|
|
25
|
+
InvalidDidDocument = "invalidDidDocument",
|
|
26
|
+
/** The byte length of a DID document does not match the expected value. */
|
|
27
|
+
InvalidDidDocumentLength = "invalidDidDocumentLength",
|
|
28
|
+
/** The DID URL supplied to the dereferencing function does not conform to valid syntax. */
|
|
29
|
+
InvalidDidUrl = "invalidDidUrl",
|
|
30
|
+
/** The given proof of a previous DID is invalid */
|
|
31
|
+
InvalidPreviousDidProof = "invalidPreviousDidProof",
|
|
32
|
+
/** An invalid public key is detected during a DID operation. */
|
|
33
|
+
InvalidPublicKey = "invalidPublicKey",
|
|
34
|
+
/** The byte length of a public key does not match the expected value. */
|
|
35
|
+
InvalidPublicKeyLength = "invalidPublicKeyLength",
|
|
36
|
+
/** An invalid public key type was detected during a DID operation. */
|
|
37
|
+
InvalidPublicKeyType = "invalidPublicKeyType",
|
|
38
|
+
/** Verification of a signature failed during a DID operation. */
|
|
39
|
+
InvalidSignature = "invalidSignature",
|
|
40
|
+
/** The DID resolver was unable to find the DID document resulting from the resolution request. */
|
|
41
|
+
NotFound = "notFound",
|
|
42
|
+
/**
|
|
43
|
+
* The representation requested via the `accept` input metadata property is not supported by the
|
|
44
|
+
* DID method and/or DID resolver implementation.
|
|
45
|
+
*/
|
|
46
|
+
RepresentationNotSupported = "representationNotSupported",
|
|
47
|
+
/** The type of a public key is not supported by the DID method and/or DID resolver implementation. */
|
|
48
|
+
UnsupportedPublicKeyType = "unsupportedPublicKeyType"
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=did-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"did-error.d.ts","sourceRoot":"","sources":["../../src/did-error.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,QAAS,SAAQ,KAAK;IAOd,IAAI,EAAE,YAAY;IANrC;;;;;OAKG;gBACgB,IAAI,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM;CAcvD;AAED;;GAEG;AACH,oBAAY,YAAY;IACtB,yDAAyD;IACzD,UAAU,eAAe;IAEzB,sGAAsG;IACtG,kBAAkB,uBAAuB;IAEzC,uEAAuE;IACvE,aAAa,kBAAkB;IAE/B,kEAAkE;IAClE,kBAAkB,uBAAuB;IAEzC,2EAA2E;IAC3E,wBAAwB,6BAA6B;IAErD,2FAA2F;IAC3F,aAAa,kBAAkB;IAE/B,mDAAmD;IACnD,uBAAuB,4BAA4B;IAEnD,gEAAgE;IAChE,gBAAgB,qBAAqB;IAErC,yEAAyE;IACzE,sBAAsB,2BAA2B;IAEjD,sEAAsE;IACtE,oBAAoB,yBAAyB;IAE7C,iEAAiE;IACjE,gBAAgB,qBAAqB;IAErC,kGAAkG;IAClG,QAAQ,aAAa;IAErB;;;OAGG;IACH,0BAA0B,+BAA+B;IAEzD,sGAAsG;IACtG,wBAAwB,6BAA6B;CACtD"}
|