@sphereon/ssi-sdk-ext.did-resolver-key 0.28.1-feature.oyd.cmsm.improv.21 → 0.28.1-next.54

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 (54) hide show
  1. package/dist/index.cjs +527 -0
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/index.d.cts +21 -0
  4. package/dist/index.d.ts +18 -5
  5. package/dist/index.js +491 -87
  6. package/dist/index.js.map +1 -1
  7. package/package.json +23 -10
  8. package/src/__tests__/__snapshots__/secp256r1.test.ts.snap +249 -1
  9. package/src/__tests__/__snapshots__/secp384r1.test.ts.snap +249 -1
  10. package/src/__tests__/__snapshots__/secp521r1.test.ts.snap +280 -1
  11. package/src/__tests__/key_resolver.test.ts +2 -1
  12. package/src/__tests__/secp256r1.test.ts +4 -4
  13. package/src/__tests__/secp384r1.test.ts +3 -3
  14. package/src/__tests__/secp521r1.test.ts +3 -2
  15. package/src/drivers/bls12381g2.ts +3 -1
  16. package/src/drivers/ed25519.ts +6 -4
  17. package/src/drivers/secp256k1.ts +3 -1
  18. package/src/drivers/secp256r1.ts +4 -3
  19. package/src/drivers/secp384r1.ts +3 -1
  20. package/src/drivers/secp521r1.ts +3 -1
  21. package/src/index.ts +3 -1
  22. package/dist/drivers/bls12381g2.d.ts +0 -8
  23. package/dist/drivers/bls12381g2.d.ts.map +0 -1
  24. package/dist/drivers/bls12381g2.js +0 -49
  25. package/dist/drivers/bls12381g2.js.map +0 -1
  26. package/dist/drivers/ed25519.d.ts +0 -8
  27. package/dist/drivers/ed25519.d.ts.map +0 -1
  28. package/dist/drivers/ed25519.js +0 -118
  29. package/dist/drivers/ed25519.js.map +0 -1
  30. package/dist/drivers/jwk.jcs.d.ts +0 -8
  31. package/dist/drivers/jwk.jcs.d.ts.map +0 -1
  32. package/dist/drivers/jwk.jcs.js +0 -23
  33. package/dist/drivers/jwk.jcs.js.map +0 -1
  34. package/dist/drivers/secp256k1.d.ts +0 -8
  35. package/dist/drivers/secp256k1.d.ts.map +0 -1
  36. package/dist/drivers/secp256k1.js +0 -49
  37. package/dist/drivers/secp256k1.js.map +0 -1
  38. package/dist/drivers/secp256r1.d.ts +0 -24
  39. package/dist/drivers/secp256r1.d.ts.map +0 -1
  40. package/dist/drivers/secp256r1.js +0 -99
  41. package/dist/drivers/secp256r1.js.map +0 -1
  42. package/dist/drivers/secp384r1.d.ts +0 -24
  43. package/dist/drivers/secp384r1.d.ts.map +0 -1
  44. package/dist/drivers/secp384r1.js +0 -99
  45. package/dist/drivers/secp384r1.js.map +0 -1
  46. package/dist/drivers/secp521r1.d.ts +0 -23
  47. package/dist/drivers/secp521r1.d.ts.map +0 -1
  48. package/dist/drivers/secp521r1.js +0 -87
  49. package/dist/drivers/secp521r1.js.map +0 -1
  50. package/dist/index.d.ts.map +0 -1
  51. package/dist/types.d.ts +0 -14
  52. package/dist/types.d.ts.map +0 -1
  53. package/dist/types.js +0 -6
  54. package/dist/types.js.map +0 -1
package/dist/index.js CHANGED
@@ -1,94 +1,498 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/index.ts
5
+ import varint from "varint";
6
+ import { base58btc } from "multiformats/bases/base58";
7
+
8
+ // src/drivers/ed25519.ts
9
+ import * as u8a from "uint8arrays";
10
+ import { convertPublicKeyToX25519 } from "@stablelib/ed25519";
11
+
12
+ // src/types.ts
13
+ var DID_LD_JSON = "application/did+ld+json";
14
+ var DID_JSON = "application/did+json";
15
+
16
+ // src/drivers/ed25519.ts
17
+ var { toString } = u8a;
18
+ function encodeKey(key, encodeKey2) {
19
+ const bytes = new Uint8Array(key.length + 2);
20
+ bytes[0] = encodeKey2 ?? 236;
21
+ bytes[1] = 1;
22
+ bytes.set(key, 2);
23
+ return `z${toString(bytes, "base58btc")}`;
24
+ }
25
+ __name(encodeKey, "encodeKey");
26
+ var keyToDidDoc = /* @__PURE__ */ __name((args) => {
27
+ const { options } = args;
28
+ if (!options?.publicKeyFormat) {
29
+ return keyToDidDoc2020(args);
30
+ }
31
+ switch (options.publicKeyFormat) {
32
+ case "Ed25519VerificationKey2018":
33
+ case "X25519KeyAgreementKey2019":
34
+ return keyToDidDoc2018_2019(args);
35
+ case "Ed25519VerificationKey2020":
36
+ case "X25519KeyAgreementKey2020":
37
+ case "Multikey":
38
+ return keyToDidDoc2020(args);
39
+ default:
40
+ throw Error(`${options.publicKeyFormat} not supported yet for the ed25519 driver`);
41
+ }
42
+ }, "keyToDidDoc");
43
+ var keyToDidDoc2018_2019 = /* @__PURE__ */ __name(({ pubKeyBytes, fingerprint, contentType }) => {
44
+ const did = `did:key:${fingerprint}`;
45
+ const keyId = `${did}#${fingerprint}`;
46
+ const x25519PubBytes = convertPublicKeyToX25519(pubKeyBytes);
47
+ const x25519KeyId = `${did}#${encodeKey(x25519PubBytes)}`;
48
+ return {
49
+ ...contentType === DID_LD_JSON && {
50
+ "@context": [
51
+ "https://www.w3.org/ns/did/v1",
52
+ "https://w3id.org/security/suites/ed25519-2018/v1",
53
+ "https://w3id.org/security/suites/x25519-2019/v1"
54
+ ]
55
+ },
56
+ id: did,
57
+ verificationMethod: [
58
+ {
59
+ id: keyId,
60
+ type: "Ed25519VerificationKey2018",
61
+ controller: did,
62
+ publicKeyBase58: toString(pubKeyBytes, "base58btc")
63
+ },
64
+ {
65
+ id: x25519KeyId,
66
+ type: "X25519KeyAgreementKey2019",
67
+ controller: did,
68
+ publicKeyBase58: toString(x25519PubBytes, "base58btc")
69
+ }
70
+ ],
71
+ authentication: [
72
+ keyId
73
+ ],
74
+ assertionMethod: [
75
+ keyId
76
+ ],
77
+ capabilityDelegation: [
78
+ keyId
79
+ ],
80
+ capabilityInvocation: [
81
+ keyId
82
+ ],
83
+ keyAgreement: [
84
+ x25519KeyId
85
+ ]
86
+ };
87
+ }, "keyToDidDoc2018_2019");
88
+ var keyToDidDoc2020 = /* @__PURE__ */ __name(({ pubKeyBytes, fingerprint, contentType }) => {
89
+ const did = `did:key:${fingerprint}`;
90
+ const keyId = `${did}#${fingerprint}`;
91
+ const x25519PubBytes = convertPublicKeyToX25519(pubKeyBytes);
92
+ const x25519KeyId = `${did}#${encodeKey(x25519PubBytes)}`;
93
+ return {
94
+ ...contentType === DID_LD_JSON && {
95
+ "@context": [
96
+ "https://www.w3.org/ns/did/v1",
97
+ "https://w3id.org/security/suites/ed25519-2020/v1",
98
+ "https://w3id.org/security/suites/x25519-2020/v1"
99
+ ]
100
+ },
101
+ id: did,
102
+ verificationMethod: [
103
+ {
104
+ id: keyId,
105
+ type: "Ed25519VerificationKey2020",
106
+ controller: did,
107
+ publicKeyMultibase: encodeKey(pubKeyBytes, 237)
108
+ }
109
+ ],
110
+ authentication: [
111
+ keyId
112
+ ],
113
+ assertionMethod: [
114
+ keyId
115
+ ],
116
+ capabilityDelegation: [
117
+ keyId
118
+ ],
119
+ capabilityInvocation: [
120
+ keyId
121
+ ],
122
+ keyAgreement: [
123
+ {
124
+ id: x25519KeyId,
125
+ type: "X25519KeyAgreementKey2020",
126
+ controller: did,
127
+ publicKeyMultibase: encodeKey(x25519PubBytes, 236)
128
+ }
129
+ ]
130
+ };
131
+ }, "keyToDidDoc2020");
132
+ var ed25519_default = {
133
+ keyToDidDoc
134
+ };
135
+
136
+ // src/drivers/bls12381g2.ts
137
+ import * as u8a2 from "uint8arrays";
138
+ var { toString: toString2 } = u8a2;
139
+ var keyToDidDoc2 = /* @__PURE__ */ __name(({ pubKeyBytes, fingerprint }) => {
140
+ const did = `did:key:${fingerprint}`;
141
+ const keyId = `${did}#${fingerprint}`;
142
+ return {
143
+ id: did,
144
+ verificationMethod: [
145
+ {
146
+ id: keyId,
147
+ type: "Bls12381G2Key2020",
148
+ controller: did,
149
+ publicKeyBase58: toString2(pubKeyBytes, "base58btc")
150
+ }
151
+ ],
152
+ authentication: [
153
+ keyId
154
+ ],
155
+ assertionMethod: [
156
+ keyId
157
+ ],
158
+ capabilityDelegation: [
159
+ keyId
160
+ ],
161
+ capabilityInvocation: [
162
+ keyId
163
+ ]
164
+ };
165
+ }, "keyToDidDoc");
166
+ var bls12381g2_default = {
167
+ keyToDidDoc: keyToDidDoc2
168
+ };
169
+
170
+ // src/drivers/secp256k1.ts
171
+ import * as u8a3 from "uint8arrays";
172
+ var { toString: toString3 } = u8a3;
173
+ var keyToDidDoc3 = /* @__PURE__ */ __name(({ pubKeyBytes, fingerprint }) => {
174
+ const did = `did:key:${fingerprint}`;
175
+ const keyId = `${did}#${fingerprint}`;
176
+ return {
177
+ id: did,
178
+ verificationMethod: [
179
+ {
180
+ id: keyId,
181
+ type: "Secp256k1VerificationKey2018",
182
+ controller: did,
183
+ publicKeyBase58: toString3(pubKeyBytes, "base58btc")
184
+ }
185
+ ],
186
+ authentication: [
187
+ keyId
188
+ ],
189
+ assertionMethod: [
190
+ keyId
191
+ ],
192
+ capabilityDelegation: [
193
+ keyId
194
+ ],
195
+ capabilityInvocation: [
196
+ keyId
197
+ ]
198
+ };
199
+ }, "keyToDidDoc");
200
+ var secp256k1_default = {
201
+ keyToDidDoc: keyToDidDoc3
202
+ };
203
+
204
+ // src/drivers/secp256r1.ts
205
+ import * as nist_weierstrauss from "nist-weierstrauss";
206
+ import * as u8a4 from "uint8arrays";
207
+ var { fromString } = u8a4;
208
+ function keyToDidDoc4({ pubKeyBytes, fingerprint }) {
209
+ const did = `did:key:${fingerprint}`;
210
+ const keyId = `${did}#${fingerprint}`;
211
+ const key = pubKeyBytesToXY(pubKeyBytes);
212
+ return {
213
+ id: did,
214
+ verificationMethod: [
215
+ {
216
+ id: keyId,
217
+ type: "JsonWebKey2020",
218
+ controller: did,
219
+ publicKeyJwk: {
220
+ kty: "EC",
221
+ crv: "P-256",
222
+ x: key.xm,
223
+ y: key.ym
224
+ }
225
+ }
226
+ ],
227
+ authentication: [
228
+ keyId
229
+ ],
230
+ assertionMethod: [
231
+ keyId
232
+ ],
233
+ capabilityDelegation: [
234
+ keyId
235
+ ],
236
+ capabilityInvocation: [
237
+ keyId
238
+ ]
239
+ };
240
+ }
241
+ __name(keyToDidDoc4, "keyToDidDoc");
242
+ function pubKeyBytesToXY(pubKeyBytes) {
243
+ if (!nist_weierstrauss.nist_weierstrauss_common.testUint8Array(pubKeyBytes)) {
244
+ throw new TypeError("input must be a Uint8Array");
245
+ }
246
+ const publicKeyHex = nist_weierstrauss.nist_weierstrauss_common.pubKeyBytesToHex(pubKeyBytes);
247
+ const bytesCount = publicKeyHex.length / 2;
248
+ if (bytesCount == 64) {
249
+ return nist_weierstrauss.nist_weierstrauss_common.publicKeyToXY(publicKeyHex);
250
+ }
251
+ if (bytesCount == 65) {
252
+ if (publicKeyHex.slice(0, 2) == "04") {
253
+ const publicKey = publicKeyHex.slice(2);
254
+ return nist_weierstrauss.nist_weierstrauss_common.publicKeyToXY(publicKey);
255
+ }
256
+ }
257
+ if (bytesCount == 33) {
258
+ if (publicKeyHex.slice(0, 2) == "03" || publicKeyHex.slice(0, 2) == "02") {
259
+ const publicKey = fromString(publicKeyHex, "base16");
260
+ const point = nist_weierstrauss.secp256r1.ECPointDecompress(publicKey);
261
+ return nist_weierstrauss.nist_weierstrauss_common.publicKeyIntToXY(point);
262
+ }
263
+ }
264
+ throw new Error("Unexpected pubKeyBytes");
265
+ }
266
+ __name(pubKeyBytesToXY, "pubKeyBytesToXY");
267
+ var secp256r1_default = {
268
+ keyToDidDoc: keyToDidDoc4
269
+ };
270
+
271
+ // src/drivers/secp384r1.ts
272
+ import * as u8a5 from "uint8arrays";
273
+ import * as nist_weierstrauss2 from "nist-weierstrauss";
274
+ var { fromString: fromString2 } = u8a5;
275
+ function keyToDidDoc5({ pubKeyBytes, fingerprint }) {
276
+ const did = `did:key:${fingerprint}`;
277
+ const keyId = `${did}#${fingerprint}`;
278
+ const key = pubKeyBytesToXY2(pubKeyBytes);
279
+ return {
280
+ id: did,
281
+ verificationMethod: [
282
+ {
283
+ id: keyId,
284
+ type: "JsonWebKey2020",
285
+ controller: did,
286
+ publicKeyJwk: {
287
+ kty: "EC",
288
+ crv: "P-384",
289
+ x: key.xm,
290
+ y: key.ym
291
+ }
292
+ }
293
+ ],
294
+ authentication: [
295
+ keyId
296
+ ],
297
+ assertionMethod: [
298
+ keyId
299
+ ],
300
+ capabilityDelegation: [
301
+ keyId
302
+ ],
303
+ capabilityInvocation: [
304
+ keyId
305
+ ]
306
+ };
307
+ }
308
+ __name(keyToDidDoc5, "keyToDidDoc");
309
+ function pubKeyBytesToXY2(pubKeyBytes) {
310
+ if (!nist_weierstrauss2.nist_weierstrauss_common.testUint8Array(pubKeyBytes)) {
311
+ throw new TypeError("input must be a Uint8Array");
312
+ }
313
+ const publicKeyHex = nist_weierstrauss2.nist_weierstrauss_common.pubKeyBytesToHex(pubKeyBytes);
314
+ const bytesCount = publicKeyHex.length / 2;
315
+ if (bytesCount == 96) {
316
+ return nist_weierstrauss2.nist_weierstrauss_common.publicKeyToXY(publicKeyHex);
317
+ }
318
+ if (bytesCount == 97) {
319
+ if (publicKeyHex.slice(0, 2) == "04") {
320
+ const publicKey = publicKeyHex.slice(2);
321
+ return nist_weierstrauss2.nist_weierstrauss_common.publicKeyToXY(publicKey);
322
+ }
323
+ }
324
+ if (bytesCount == 49) {
325
+ if (publicKeyHex.slice(0, 2) == "03" || publicKeyHex.slice(0, 2) == "02") {
326
+ const publicKey = fromString2(publicKeyHex, "base16");
327
+ const point = nist_weierstrauss2.secp384r1.ECPointDecompress(publicKey);
328
+ return nist_weierstrauss2.nist_weierstrauss_common.publicKeyIntToXY(point);
329
+ }
330
+ }
331
+ throw new Error("Unexpected pubKeyBytes");
332
+ }
333
+ __name(pubKeyBytesToXY2, "pubKeyBytesToXY");
334
+ var secp384r1_default = {
335
+ keyToDidDoc: keyToDidDoc5
336
+ };
337
+
338
+ // src/drivers/secp521r1.ts
339
+ import * as u8a6 from "uint8arrays";
340
+ import * as nist_weierstrauss3 from "nist-weierstrauss";
341
+ var { fromString: fromString3 } = u8a6;
342
+ function keyToDidDoc6({ pubKeyBytes, fingerprint }) {
343
+ const did = `did:key:${fingerprint}`;
344
+ const keyId = `${did}#${fingerprint}`;
345
+ const key = pubKeyBytesToXY3(pubKeyBytes);
346
+ return {
347
+ id: did,
348
+ verificationMethod: [
349
+ {
350
+ id: keyId,
351
+ type: "JsonWebKey2020",
352
+ controller: did,
353
+ publicKeyJwk: {
354
+ kty: "EC",
355
+ crv: "P-521",
356
+ x: key.xm,
357
+ y: key.ym
358
+ }
359
+ }
360
+ ],
361
+ authentication: [
362
+ keyId
363
+ ],
364
+ assertionMethod: [
365
+ keyId
366
+ ],
367
+ capabilityDelegation: [
368
+ keyId
369
+ ],
370
+ capabilityInvocation: [
371
+ keyId
372
+ ]
373
+ };
374
+ }
375
+ __name(keyToDidDoc6, "keyToDidDoc");
376
+ function pubKeyBytesToXY3(pubKeyBytes) {
377
+ if (!nist_weierstrauss3.nist_weierstrauss_common.testUint8Array(pubKeyBytes)) {
378
+ throw new TypeError("input must be a Uint8Array");
379
+ }
380
+ const publicKeyHex = nist_weierstrauss3.nist_weierstrauss_common.pubKeyBytesToHex(pubKeyBytes);
381
+ if (132 <= publicKeyHex.length && publicKeyHex.length <= 134) {
382
+ if (publicKeyHex.slice(0, 2) == "03" || publicKeyHex.slice(0, 2) == "02") {
383
+ const publicKey = fromString3(publicKeyHex, "base16");
384
+ const point = nist_weierstrauss3.secp521r1.ECPointDecompress(publicKey);
385
+ return nist_weierstrauss3.nist_weierstrauss_common.publicKeyIntToXY(point);
7
386
  }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
387
+ }
388
+ throw new Error("Unexpected pubKeyBytes");
389
+ }
390
+ __name(pubKeyBytesToXY3, "pubKeyBytesToXY");
391
+ var secp521r1_default = {
392
+ keyToDidDoc: keyToDidDoc6
15
393
  };
16
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
- return new (P || (P = Promise))(function (resolve, reject) {
19
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
- step((generator = generator.apply(thisArg, _arguments || [])).next());
23
- });
394
+
395
+ // src/drivers/jwk.jcs.ts
396
+ import { jwkJcsDecode } from "@sphereon/ssi-sdk-ext.key-utils";
397
+ var keyToDidDoc7 = /* @__PURE__ */ __name(({ pubKeyBytes, fingerprint, contentType }) => {
398
+ const did = `did:key:${fingerprint}`;
399
+ const keyId = `${did}#${fingerprint}`;
400
+ const publicKeyJwk = jwkJcsDecode(pubKeyBytes);
401
+ return {
402
+ ...contentType === DID_LD_JSON && {
403
+ "@context": [
404
+ "https://www.w3.org/ns/did/v1",
405
+ "https://w3id.org/security/suites/jws-2020/v1"
406
+ ]
407
+ },
408
+ id: did,
409
+ verificationMethod: [
410
+ {
411
+ id: keyId,
412
+ type: "JsonWebKey2020",
413
+ controller: did,
414
+ publicKeyJwk
415
+ }
416
+ ],
417
+ authentication: [
418
+ keyId
419
+ ],
420
+ assertionMethod: [
421
+ keyId
422
+ ],
423
+ capabilityDelegation: [
424
+ keyId
425
+ ],
426
+ capabilityInvocation: [
427
+ keyId
428
+ ]
429
+ };
430
+ }, "keyToDidDoc");
431
+ var jwk_jcs_default = {
432
+ keyToDidDoc: keyToDidDoc7
24
433
  };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
434
+
435
+ // src/index.ts
436
+ var { decode } = varint;
437
+ var prefixToDriverMap = {
438
+ 231: secp256k1_default,
439
+ 237: ed25519_default,
440
+ 4608: secp256r1_default,
441
+ 4609: secp384r1_default,
442
+ 4610: secp521r1_default,
443
+ 235: bls12381g2_default,
444
+ 60241: jwk_jcs_default
27
445
  };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.getResolver = void 0;
30
- const varint_1 = require("varint");
31
- const base58_1 = require("multiformats/bases/base58");
32
- const ed25519_1 = __importDefault(require("./drivers/ed25519"));
33
- const bls12381g2_1 = __importDefault(require("./drivers/bls12381g2"));
34
- const secp256k1_1 = __importDefault(require("./drivers/secp256k1"));
35
- const secp256r1_1 = __importDefault(require("./drivers/secp256r1"));
36
- const secp384r1_1 = __importDefault(require("./drivers/secp384r1"));
37
- const secp521r1_1 = __importDefault(require("./drivers/secp521r1"));
38
- const jwk_jcs_1 = __importDefault(require("./drivers/jwk.jcs"));
39
- const types_1 = require("./types");
40
- __exportStar(require("./types"), exports);
41
- const prefixToDriverMap = {
42
- 0xe7: secp256k1_1.default,
43
- 0xed: ed25519_1.default,
44
- 0x1200: secp256r1_1.default,
45
- 0x1201: secp384r1_1.default,
46
- 0x1202: secp521r1_1.default,
47
- 0xeb: bls12381g2_1.default,
48
- 0xeb51: jwk_jcs_1.default,
446
+ var getResolver = /* @__PURE__ */ __name(() => {
447
+ return {
448
+ key: /* @__PURE__ */ __name(async (did, parsed, r, options) => {
449
+ const contentType = options.accept || DID_LD_JSON;
450
+ const response = {
451
+ didResolutionMetadata: {
452
+ contentType
453
+ },
454
+ didDocument: null,
455
+ didDocumentMetadata: {}
456
+ };
457
+ try {
458
+ const multicodecPubKey = base58btc.decode(parsed.id);
459
+ const keyType = decode(multicodecPubKey);
460
+ const pubKeyBytes = multicodecPubKey.slice(decode.bytes);
461
+ const args = {
462
+ pubKeyBytes,
463
+ fingerprint: parsed.id,
464
+ contentType,
465
+ options
466
+ };
467
+ const doc = await prefixToDriverMap[keyType].keyToDidDoc(args);
468
+ if (contentType === DID_LD_JSON) {
469
+ if (!doc["@context"]) {
470
+ doc["@context"] = "https://w3id.org/did/v1";
471
+ } else if (Array.isArray(doc["@context"]) && !doc["@context"].includes("https://w3id.org/did/v1") && !doc["@context"].includes("https://www.w3.org/ns/did/v1")) {
472
+ doc["@context"].push("https://w3id.org/did/v1");
473
+ }
474
+ response.didDocument = doc;
475
+ } else if (contentType === DID_JSON) {
476
+ response.didDocument = doc;
477
+ } else {
478
+ delete response.didResolutionMetadata.contentType;
479
+ response.didResolutionMetadata.error = "representationNotSupported";
480
+ }
481
+ } catch (e) {
482
+ response.didResolutionMetadata.error = "invalidDid";
483
+ response.didResolutionMetadata.message = e.toString();
484
+ }
485
+ return response;
486
+ }, "key")
487
+ };
488
+ }, "getResolver");
489
+ var index_default = {
490
+ getResolver
49
491
  };
50
- const getResolver = () => {
51
- return {
52
- key: (did, parsed, r, options) => __awaiter(void 0, void 0, void 0, function* () {
53
- const contentType = options.accept || types_1.DID_LD_JSON;
54
- const response = {
55
- didResolutionMetadata: { contentType },
56
- didDocument: null,
57
- didDocumentMetadata: {},
58
- };
59
- try {
60
- const multicodecPubKey = base58_1.base58btc.decode(parsed.id);
61
- const keyType = (0, varint_1.decode)(multicodecPubKey);
62
- const pubKeyBytes = multicodecPubKey.slice(varint_1.decode.bytes);
63
- const args = { pubKeyBytes, fingerprint: parsed.id, contentType, options };
64
- const doc = yield prefixToDriverMap[keyType].keyToDidDoc(args);
65
- if (contentType === types_1.DID_LD_JSON) {
66
- if (!doc['@context']) {
67
- doc['@context'] = 'https://w3id.org/did/v1';
68
- }
69
- else if (Array.isArray(doc['@context']) &&
70
- !doc['@context'].includes('https://w3id.org/did/v1') &&
71
- !doc['@context'].includes('https://www.w3.org/ns/did/v1')) {
72
- doc['@context'].push('https://w3id.org/did/v1');
73
- }
74
- response.didDocument = doc;
75
- }
76
- else if (contentType === types_1.DID_JSON) {
77
- response.didDocument = doc;
78
- }
79
- else {
80
- delete response.didResolutionMetadata.contentType;
81
- response.didResolutionMetadata.error = 'representationNotSupported';
82
- }
83
- }
84
- catch (e) {
85
- response.didResolutionMetadata.error = 'invalidDid';
86
- response.didResolutionMetadata.message = e.toString();
87
- }
88
- return response;
89
- }),
90
- };
492
+ export {
493
+ DID_JSON,
494
+ DID_LD_JSON,
495
+ index_default as default,
496
+ getResolver
91
497
  };
92
- exports.getResolver = getResolver;
93
- exports.default = { getResolver: exports.getResolver };
94
498
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mCAA+B;AAC/B,sDAAqD;AACrD,gEAAuC;AACvC,sEAA6C;AAC7C,oEAA2C;AAC3C,oEAA2C;AAC3C,oEAA2C;AAC3C,oEAA2C;AAE3C,gEAAsC;AACtC,mCAAyF;AAEzF,0CAAuB;AAEvB,MAAM,iBAAiB,GAAQ;IAC7B,IAAI,EAAE,mBAAS;IACf,IAAI,EAAE,iBAAO;IACb,MAAM,EAAE,mBAAS;IACjB,MAAM,EAAE,mBAAS;IACjB,MAAM,EAAE,mBAAS;IACjB,IAAI,EAAE,oBAAU;IAChB,MAAM,EAAE,iBAAM;CACf,CAAA;AAEM,MAAM,WAAW,GAAG,GAAqB,EAAE;IAChD,OAAO;QACL,GAAG,EAAE,CAAO,GAAW,EAAE,MAAiB,EAAE,CAAa,EAAE,OAAgC,EAAE,EAAE;YAC7F,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,IAAI,mBAAW,CAAA;YACjD,MAAM,QAAQ,GAAwB;gBACpC,qBAAqB,EAAE,EAAE,WAAW,EAAE;gBACtC,WAAW,EAAE,IAAI;gBACjB,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4B,CAAA;gBACrE,CAAC;YACH,CAAC;YAAC,OAAO,CAAM,EAAE,CAAC;gBAChB,QAAQ,CAAC,qBAAqB,CAAC,KAAK,GAAG,YAAY,CAAA;gBACnD,QAAQ,CAAC,qBAAqB,CAAC,OAAO,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAA;YACvD,CAAC;YACD,OAAO,QAAQ,CAAA;QACjB,CAAC,CAAA;KACF,CAAA;AACH,CAAC,CAAA;AAvCY,QAAA,WAAW,eAuCvB;AACD,kBAAe,EAAE,WAAW,EAAX,mBAAW,EAAE,CAAA"}
1
+ {"version":3,"sources":["../src/index.ts","../src/drivers/ed25519.ts","../src/types.ts","../src/drivers/bls12381g2.ts","../src/drivers/secp256k1.ts","../src/drivers/secp256r1.ts","../src/drivers/secp384r1.ts","../src/drivers/secp521r1.ts","../src/drivers/jwk.jcs.ts"],"sourcesContent":["import varint from 'varint'\nconst { decode } = varint\n// @ts-ignore\nimport { base58btc } from 'multiformats/bases/base58'\nimport ed25519 from './drivers/ed25519'\nimport bls12381g2 from './drivers/bls12381g2'\nimport secp256k1 from './drivers/secp256k1'\nimport secp256r1 from './drivers/secp256r1'\nimport secp384r1 from './drivers/secp384r1'\nimport secp521r1 from './drivers/secp521r1'\nimport { DIDResolutionResult, ParsedDID, Resolvable, ResolverRegistry } from 'did-resolver'\nimport jwkJcs from './drivers/jwk.jcs'\nimport { DID_JSON, DID_LD_JSON, DIDKeyResolutionOptions, KeyToDidDocArgs } from './types'\n\nexport * from './types'\n\nconst prefixToDriverMap: any = {\n 0xe7: secp256k1,\n 0xed: ed25519,\n 0x1200: secp256r1,\n 0x1201: secp384r1,\n 0x1202: secp521r1,\n 0xeb: bls12381g2,\n 0xeb51: jwkJcs,\n}\n\nexport const getResolver = (): ResolverRegistry => {\n return {\n key: async (did: string, parsed: ParsedDID, r: Resolvable, options: DIDKeyResolutionOptions) => {\n const contentType = options.accept || DID_LD_JSON\n const response: DIDResolutionResult = {\n didResolutionMetadata: { contentType },\n didDocument: null,\n didDocumentMetadata: {},\n }\n try {\n const multicodecPubKey = base58btc.decode(parsed.id)\n const keyType = decode(multicodecPubKey)\n const pubKeyBytes = multicodecPubKey.slice(decode.bytes)\n const args: KeyToDidDocArgs = { pubKeyBytes, fingerprint: parsed.id, contentType, options }\n const doc = await prefixToDriverMap[keyType].keyToDidDoc(args)\n if (contentType === DID_LD_JSON) {\n if (!doc['@context']) {\n doc['@context'] = 'https://w3id.org/did/v1'\n } else if (\n Array.isArray(doc['@context']) &&\n !doc['@context'].includes('https://w3id.org/did/v1') &&\n !doc['@context'].includes('https://www.w3.org/ns/did/v1')\n ) {\n doc['@context'].push('https://w3id.org/did/v1')\n }\n response.didDocument = doc\n } else if (contentType === DID_JSON) {\n response.didDocument = doc\n } else {\n delete response.didResolutionMetadata.contentType\n response.didResolutionMetadata.error = 'representationNotSupported'\n }\n } catch (e: any) {\n response.didResolutionMetadata.error = 'invalidDid'\n response.didResolutionMetadata.message = e.toString()\n }\n return response\n },\n }\n}\nexport default { getResolver }\n","// @ts-ignore\nimport * as u8a from 'uint8arrays'\nconst { toString } = u8a\nimport { DIDDocument } from 'did-resolver'\n// import { edwardsToMontgomery } from '@noble/curves/ed25519'\nimport { convertPublicKeyToX25519 } from '@stablelib/ed25519'\nimport { DID_LD_JSON, KeyToDidDocArgs } from '../types'\n\nfunction encodeKey(key: Uint8Array, encodeKey?: number) {\n const bytes = new Uint8Array(key.length + 2)\n bytes[0] = encodeKey ?? 0xec\n // The multicodec is encoded as a varint so we need to add this.\n // See js-multicodec for a general implementation\n bytes[1] = 0x01\n bytes.set(key, 2)\n return `z${toString(bytes, 'base58btc')}`\n}\n\nexport const keyToDidDoc = (args: KeyToDidDocArgs) => {\n const { options } = args\n if (!options?.publicKeyFormat) {\n return keyToDidDoc2020(args)\n }\n switch (options.publicKeyFormat) {\n case 'Ed25519VerificationKey2018':\n case 'X25519KeyAgreementKey2019':\n return keyToDidDoc2018_2019(args)\n case 'Ed25519VerificationKey2020':\n case 'X25519KeyAgreementKey2020':\n case 'Multikey':\n return keyToDidDoc2020(args)\n default:\n throw Error(`${options.publicKeyFormat} not supported yet for the ed25519 driver`)\n }\n}\nconst keyToDidDoc2018_2019 = ({ pubKeyBytes, fingerprint, contentType }: KeyToDidDocArgs): DIDDocument => {\n const did = `did:key:${fingerprint}`\n const keyId = `${did}#${fingerprint}`\n\n //todo: Move to noble lib. x25519 values differ between below methods. Current implementation is correct according to DID:key spec\n // const pubKeyHex = toString(pubKeyBytes, 'base16')\n // const x25519PubBytes = edwardsToMontgomery(pubKeyHex)\n const x25519PubBytes = convertPublicKeyToX25519(pubKeyBytes)\n\n const x25519KeyId = `${did}#${encodeKey(x25519PubBytes)}`\n return {\n ...(contentType === DID_LD_JSON && {\n '@context': [\n 'https://www.w3.org/ns/did/v1',\n 'https://w3id.org/security/suites/ed25519-2018/v1',\n 'https://w3id.org/security/suites/x25519-2019/v1',\n ],\n }),\n id: did,\n verificationMethod: [\n {\n id: keyId,\n type: 'Ed25519VerificationKey2018',\n controller: did,\n publicKeyBase58: toString(pubKeyBytes, 'base58btc'),\n },\n {\n id: x25519KeyId,\n type: 'X25519KeyAgreementKey2019',\n controller: did,\n publicKeyBase58: toString(x25519PubBytes, 'base58btc'),\n },\n ],\n authentication: [keyId],\n assertionMethod: [keyId],\n capabilityDelegation: [keyId],\n capabilityInvocation: [keyId],\n keyAgreement: [x25519KeyId],\n }\n}\n\nconst keyToDidDoc2020 = ({ pubKeyBytes, fingerprint, contentType }: KeyToDidDocArgs): DIDDocument => {\n const did = `did:key:${fingerprint}`\n const keyId = `${did}#${fingerprint}`\n //todo: Move to noble lib. x25519 values differ between below methods. Current implementation is correct according to DID:key spec\n // const pubKeyHex = u8a.toString(pubKeyBytes, 'base16')\n // const x25519PubBytes = edwardsToMontgomery(pubKeyBytes)\n const x25519PubBytes = convertPublicKeyToX25519(pubKeyBytes)\n\n const x25519KeyId = `${did}#${encodeKey(x25519PubBytes)}`\n return {\n ...(contentType === DID_LD_JSON && {\n '@context': [\n 'https://www.w3.org/ns/did/v1',\n 'https://w3id.org/security/suites/ed25519-2020/v1',\n 'https://w3id.org/security/suites/x25519-2020/v1',\n ],\n }),\n id: did,\n verificationMethod: [\n {\n id: keyId,\n type: 'Ed25519VerificationKey2020',\n controller: did,\n publicKeyMultibase: encodeKey(pubKeyBytes, 0xed),\n },\n ],\n authentication: [keyId],\n assertionMethod: [keyId],\n capabilityDelegation: [keyId],\n capabilityInvocation: [keyId],\n keyAgreement: [\n {\n id: x25519KeyId,\n type: 'X25519KeyAgreementKey2020',\n controller: did,\n publicKeyMultibase: encodeKey(x25519PubBytes, 0xec),\n },\n ],\n }\n}\nexport default { keyToDidDoc }\n","import { DIDResolutionOptions } from 'did-resolver'\n\nexport const DID_LD_JSON = 'application/did+ld+json'\nexport const DID_JSON = 'application/did+json'\n\nexport type PublicKeyFormat =\n | 'JsonWebKey2020'\n | 'Ed25519VerificationKey2018'\n | 'X25519KeyAgreementKey2019'\n | 'Ed25519VerificationKey2020'\n | 'X25519KeyAgreementKey2020'\n | 'Multikey'\nexport interface KeyToDidDocArgs {\n pubKeyBytes: Uint8Array\n fingerprint: string\n contentType?: string\n options?: DIDKeyResolutionOptions\n}\n\nexport interface DIDKeyResolutionOptions extends DIDResolutionOptions {\n publicKeyFormat?: PublicKeyFormat\n}\n","import { DIDDocument } from 'did-resolver'\n// @ts-ignore\nimport * as u8a from 'uint8arrays'\nconst { toString } = u8a\nimport { KeyToDidDocArgs } from '../index'\n\nexport const keyToDidDoc = ({ pubKeyBytes, fingerprint }: KeyToDidDocArgs): DIDDocument => {\n const did = `did:key:${fingerprint}`\n const keyId = `${did}#${fingerprint}`\n return {\n id: did,\n verificationMethod: [\n {\n id: keyId,\n type: 'Bls12381G2Key2020',\n controller: did,\n publicKeyBase58: toString(pubKeyBytes, 'base58btc'),\n },\n ],\n authentication: [keyId],\n assertionMethod: [keyId],\n capabilityDelegation: [keyId],\n capabilityInvocation: [keyId],\n }\n}\nexport default { keyToDidDoc }\n","// @ts-ignore\nimport * as u8a from 'uint8arrays'\nconst { toString } = u8a\nimport { DIDDocument } from 'did-resolver'\nimport { KeyToDidDocArgs } from '../types'\n\nexport const keyToDidDoc = ({ pubKeyBytes, fingerprint }: KeyToDidDocArgs): DIDDocument => {\n const did = `did:key:${fingerprint}`\n const keyId = `${did}#${fingerprint}`\n return {\n id: did,\n verificationMethod: [\n {\n id: keyId,\n type: 'Secp256k1VerificationKey2018',\n controller: did,\n publicKeyBase58: toString(pubKeyBytes, 'base58btc'),\n },\n ],\n authentication: [keyId],\n assertionMethod: [keyId],\n capabilityDelegation: [keyId],\n capabilityInvocation: [keyId],\n }\n}\n\nexport default { keyToDidDoc }\n","// Brent Shambaugh <brent.shambaugh@gmail.com>. 2021.\n\nimport * as nist_weierstrauss from 'nist-weierstrauss'\nimport { base64urlPoint } from 'nist-weierstrauss'\n// @ts-ignore\nimport * as u8a from 'uint8arrays'\nconst { fromString } = u8a\nimport { KeyToDidDocArgs } from '../types'\n\n/**\n * Constructs the document based on the method key\n */\nexport function keyToDidDoc({ pubKeyBytes, fingerprint }: KeyToDidDocArgs): any {\n const did = `did:key:${fingerprint}`\n const keyId = `${did}#${fingerprint}`\n const key = pubKeyBytesToXY(pubKeyBytes)\n return {\n id: did,\n verificationMethod: [\n {\n id: keyId,\n type: 'JsonWebKey2020',\n controller: did,\n publicKeyJwk: {\n kty: 'EC',\n crv: 'P-256',\n x: key.xm,\n y: key.ym,\n },\n },\n ],\n authentication: [keyId],\n assertionMethod: [keyId],\n capabilityDelegation: [keyId],\n capabilityInvocation: [keyId],\n }\n}\n\n/**\n *\n * @param pubKeyBytes - public key as uncompressed byte array with no prefix (raw key),\n * uncompressed with 0x04 prefix, or compressed with 0x02 prefix if even and 0x03 prefix if odd.\n * @returns point x,y with coordinates as multibase encoded base64urls\n *\n * See the the did:key specification: https://w3c-ccg.github.io/did-method-key/#p-256.\n * At present only raw p-256 keys are covered in the specification.\n * @throws TypeError: input cannot be null or undefined.\n * @throws Error: Unexpected pubKeyBytes\n * @internal\n */\nexport function pubKeyBytesToXY(pubKeyBytes: Uint8Array): base64urlPoint {\n if (!nist_weierstrauss.nist_weierstrauss_common.testUint8Array(pubKeyBytes)) {\n throw new TypeError('input must be a Uint8Array')\n }\n const publicKeyHex = nist_weierstrauss.nist_weierstrauss_common.pubKeyBytesToHex(pubKeyBytes)\n const bytesCount = publicKeyHex.length / 2\n\n // raw p-256 key\n if (bytesCount == 64) {\n return nist_weierstrauss.nist_weierstrauss_common.publicKeyToXY(publicKeyHex)\n }\n\n // uncompressed p-256 key, SEC format\n if (bytesCount == 65) {\n if (publicKeyHex.slice(0, 2) == '04') {\n const publicKey = publicKeyHex.slice(2)\n return nist_weierstrauss.nist_weierstrauss_common.publicKeyToXY(publicKey)\n }\n }\n\n // compressed p-256 key, SEC format\n if (bytesCount == 33) {\n if (publicKeyHex.slice(0, 2) == '03' || publicKeyHex.slice(0, 2) == '02') {\n const publicKey = fromString(publicKeyHex, 'base16')\n const point = nist_weierstrauss.secp256r1.ECPointDecompress(publicKey)\n return nist_weierstrauss.nist_weierstrauss_common.publicKeyIntToXY(point)\n }\n }\n\n throw new Error('Unexpected pubKeyBytes')\n}\n\nexport default { keyToDidDoc }\n","// Brent Shambaugh <brent.shambaugh@gmail.com>. 2021.\n\n// @ts-ignore\nimport * as u8a from 'uint8arrays'\nconst { fromString } = u8a\n\nimport * as nist_weierstrauss from 'nist-weierstrauss'\nimport { base64urlPoint } from 'nist-weierstrauss'\nimport { KeyToDidDocArgs } from '../types'\n\n/**\n * Constructs the document based on the method key\n */\nexport function keyToDidDoc({ pubKeyBytes, fingerprint }: KeyToDidDocArgs): any {\n const did = `did:key:${fingerprint}`\n const keyId = `${did}#${fingerprint}`\n const key = pubKeyBytesToXY(pubKeyBytes)\n return {\n id: did,\n verificationMethod: [\n {\n id: keyId,\n type: 'JsonWebKey2020',\n controller: did,\n publicKeyJwk: {\n kty: 'EC',\n crv: 'P-384',\n x: key.xm,\n y: key.ym,\n },\n },\n ],\n authentication: [keyId],\n assertionMethod: [keyId],\n capabilityDelegation: [keyId],\n capabilityInvocation: [keyId],\n }\n}\n\n/**\n *\n * @param pubKeyBytes - public key as uncompressed byte array with no prefix (raw key),\n * uncompressed with 0x04 prefix, or compressed with 0x02 prefix if even and 0x03 prefix if odd.\n * @returns point x,y with coordinates as multibase encoded base64urls\n *\n * See the the did:key specification: https://w3c-ccg.github.io/did-method-key/#p-384.\n * At present only raw p-384 keys are covered in the specification.\n * @throws TypeError: input cannot be null or undefined.\n * @throws Error: Unexpected pubKeyBytes\n * @internal\n */\nexport function pubKeyBytesToXY(pubKeyBytes: Uint8Array): base64urlPoint {\n if (!nist_weierstrauss.nist_weierstrauss_common.testUint8Array(pubKeyBytes)) {\n throw new TypeError('input must be a Uint8Array')\n }\n const publicKeyHex = nist_weierstrauss.nist_weierstrauss_common.pubKeyBytesToHex(pubKeyBytes)\n const bytesCount = publicKeyHex.length / 2\n\n // raw p-384 key\n if (bytesCount == 96) {\n return nist_weierstrauss.nist_weierstrauss_common.publicKeyToXY(publicKeyHex)\n }\n\n // uncompressed p-384 key, SEC format\n if (bytesCount == 97) {\n if (publicKeyHex.slice(0, 2) == '04') {\n const publicKey = publicKeyHex.slice(2)\n return nist_weierstrauss.nist_weierstrauss_common.publicKeyToXY(publicKey)\n }\n }\n\n // compressed p-384 key, SEC format\n if (bytesCount == 49) {\n if (publicKeyHex.slice(0, 2) == '03' || publicKeyHex.slice(0, 2) == '02') {\n const publicKey = fromString(publicKeyHex, 'base16')\n const point = nist_weierstrauss.secp384r1.ECPointDecompress(publicKey)\n return nist_weierstrauss.nist_weierstrauss_common.publicKeyIntToXY(point)\n }\n }\n\n throw new Error('Unexpected pubKeyBytes')\n}\n\nexport default { keyToDidDoc }\n","// Brent Shambaugh <brent.shambaugh@gmail.com>. 2021.\n\n// @ts-ignore\nimport * as u8a from 'uint8arrays'\nconst { fromString } = u8a\n\nimport * as nist_weierstrauss from 'nist-weierstrauss'\nimport { base64urlPoint } from 'nist-weierstrauss'\nimport { KeyToDidDocArgs } from '../types'\n\n/**\n * Constructs the document based on the method key\n */\nexport function keyToDidDoc({ pubKeyBytes, fingerprint }: KeyToDidDocArgs): any {\n const did = `did:key:${fingerprint}`\n const keyId = `${did}#${fingerprint}`\n const key = pubKeyBytesToXY(pubKeyBytes)\n return {\n id: did,\n verificationMethod: [\n {\n id: keyId,\n type: 'JsonWebKey2020',\n controller: did,\n publicKeyJwk: {\n kty: 'EC',\n crv: 'P-521',\n x: key.xm,\n y: key.ym,\n },\n },\n ],\n authentication: [keyId],\n assertionMethod: [keyId],\n capabilityDelegation: [keyId],\n capabilityInvocation: [keyId],\n }\n}\n\n/**\n *\n * @param pubKeyBytes - public key as compressed with 0x02 prefix if even and 0x03 prefix if odd.\n * @returns point x,y with coordinates as multibase encoded base64urls\n *\n * See the the did:key specification: https://w3c-ccg.github.io/did-method-key/#p-521.\n * For compression see: https://tools.ietf.org/id/draft-jivsov-ecc-compact-05.html#rfc.section.3\n * @throws TypeError: input cannot be null or undefined.\n * @throws Error: Unexpected pubKeyBytes\n * @internal\n */\nexport function pubKeyBytesToXY(pubKeyBytes: Uint8Array): base64urlPoint {\n if (!nist_weierstrauss.nist_weierstrauss_common.testUint8Array(pubKeyBytes)) {\n throw new TypeError('input must be a Uint8Array')\n }\n const publicKeyHex = nist_weierstrauss.nist_weierstrauss_common.pubKeyBytesToHex(pubKeyBytes)\n\n // compressed p-521 key, SEC format\n // publicKeyHex.length / 2.0 = 67.0 bytes\n if (132 <= publicKeyHex.length && publicKeyHex.length <= 134) {\n if (publicKeyHex.slice(0, 2) == '03' || publicKeyHex.slice(0, 2) == '02') {\n const publicKey = fromString(publicKeyHex, 'base16')\n const point = nist_weierstrauss.secp521r1.ECPointDecompress(publicKey)\n return nist_weierstrauss.nist_weierstrauss_common.publicKeyIntToXY(point)\n }\n }\n\n throw new Error('Unexpected pubKeyBytes')\n}\n\nexport default { keyToDidDoc }\n","import { DIDDocument, JsonWebKey as DIFJWK } from 'did-resolver'\nimport { DID_LD_JSON, KeyToDidDocArgs } from '../index'\nimport { jwkJcsDecode } from '@sphereon/ssi-sdk-ext.key-utils'\n\nexport const keyToDidDoc = ({ pubKeyBytes, fingerprint, contentType }: KeyToDidDocArgs): DIDDocument => {\n const did = `did:key:${fingerprint}`\n const keyId = `${did}#${fingerprint}`\n const publicKeyJwk = jwkJcsDecode(pubKeyBytes) as DIFJWK\n return {\n ...(contentType === DID_LD_JSON && {\n '@context': ['https://www.w3.org/ns/did/v1', 'https://w3id.org/security/suites/jws-2020/v1'],\n }),\n id: did,\n verificationMethod: [\n {\n id: keyId,\n type: 'JsonWebKey2020',\n controller: did,\n publicKeyJwk,\n },\n ],\n authentication: [keyId],\n assertionMethod: [keyId],\n capabilityDelegation: [keyId],\n capabilityInvocation: [keyId],\n }\n}\nexport default { keyToDidDoc }\n"],"mappings":";;;;AAAA,OAAOA,YAAY;AAGnB,SAASC,iBAAiB;;;ACF1B,YAAYC,SAAS;AAIrB,SAASC,gCAAgC;;;ACHlC,IAAMC,cAAc;AACpB,IAAMC,WAAW;;;ADDxB,IAAM,EAAEC,SAAQ,IAAKC;AAMrB,SAASC,UAAUC,KAAiBD,YAAkB;AACpD,QAAME,QAAQ,IAAIC,WAAWF,IAAIG,SAAS,CAAA;AAC1CF,QAAM,CAAA,IAAKF,cAAa;AAGxBE,QAAM,CAAA,IAAK;AACXA,QAAMG,IAAIJ,KAAK,CAAA;AACf,SAAO,IAAIH,SAASI,OAAO,WAAA,CAAA;AAC7B;AARSF;AAUF,IAAMM,cAAc,wBAACC,SAAAA;AAC1B,QAAM,EAAEC,QAAO,IAAKD;AACpB,MAAI,CAACC,SAASC,iBAAiB;AAC7B,WAAOC,gBAAgBH,IAAAA;EACzB;AACA,UAAQC,QAAQC,iBAAe;IAC7B,KAAK;IACL,KAAK;AACH,aAAOE,qBAAqBJ,IAAAA;IAC9B,KAAK;IACL,KAAK;IACL,KAAK;A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B,kBAAkBwB,OAAAA,EAASO,YAAYH,IAAAA;AACzD,YAAId,gBAAgBE,aAAa;AAC/B,cAAI,CAACc,IAAI,UAAA,GAAa;AACpBA,gBAAI,UAAA,IAAc;UACpB,WACEE,MAAMC,QAAQH,IAAI,UAAA,CAAW,KAC7B,CAACA,IAAI,UAAA,EAAYI,SAAS,yBAAA,KAC1B,CAACJ,IAAI,UAAA,EAAYI,SAAS,8BAAA,GAC1B;AACAJ,gBAAI,UAAA,EAAYK,KAAK,yBAAA;UACvB;AACAlB,mBAASE,cAAcW;QACzB,WAAWhB,gBAAgBsB,UAAU;AACnCnB,mBAASE,cAAcW;QACzB,OAAO;AACL,iBAAOb,SAASC,sBAAsBJ;AACtCG,mBAASC,sBAAsBmB,QAAQ;QACzC;MACF,SAASC,GAAQ;AACfrB,iBAASC,sBAAsBmB,QAAQ;AACvCpB,iBAASC,sBAAsBqB,UAAUD,EAAEE,SAAQ;MACrD;AACA,aAAOvB;IACT,GAnCK;EAoCP;AACF,GAvC2B;AAwC3B,IAAA,gBAAe;EAAET;AAAY;","names":["varint","base58btc","u8a","convertPublicKeyToX25519","DID_LD_JSON","DID_JSON","toString","u8a","encodeKey","key","bytes","Uint8Array","length","set","keyToDidDoc","args","options","publicKeyFormat","keyToDidDoc2020","keyToDidDoc2018_2019","Error","pubKeyBytes","fingerprint","contentType","did","keyId","x25519PubBytes","convertPublicKeyToX25519","x25519KeyId","DID_LD_JSON","id","verificationMethod","type","controller","publicKeyBase58","authentication","assertionMethod","capabilityDelegation","capabilityInvocation","keyAgreement","publicKeyMultibase","u8a","toString","u8a","keyToDidDoc","pubKeyBytes","fingerprint","did","keyId","id","verificationMethod","type","controller","publicKeyBase58","authentication","assertionMethod","capabilityDelegation","capabilityInvocation","u8a","toString","u8a","keyToDidDoc","pubKeyBytes","fingerprint","did","keyId","id","verificationMethod","type","controller","publicKeyBase58","authentication","assertionMethod","capabilityDelegation","capabilityInvocation","nist_weierstrauss","u8a","fromString","u8a","keyToDidDoc","pubKeyBytes","fingerprint","did","keyId","key","pubKeyBytesToXY","id","verificationMethod","type","controller","publicKeyJwk","kty","crv","x","xm","y","ym","authentication","assertionMethod","capabilityDelegation","capabilityInvocation","nist_weierstrauss_common","testUint8Array","TypeError","publicKeyHex","pubKeyBytesToHex","bytesCount","length","publicKeyToXY","slice","publicKey","point","secp256r1","ECPointDecompress","publicKeyIntToXY","Error","u8a","nist_weierstrauss","fromString","u8a","keyToDidDoc","pubKeyBytes","fingerprint","did","keyId","key","pubKeyBytesToXY","id","verificationMethod","type","controller","publicKeyJwk","kty","crv","x","xm","y","ym","authentication","assertionMethod","capabilityDelegation","capabilityInvocation","nist_weierstrauss_common","testUint8Array","TypeError","publicKeyHex","pubKeyBytesToHex","bytesCount","length","publicKeyToXY","slice","publicKey","point","secp384r1","ECPointDecompress","publicKeyIntToXY","Error","u8a","nist_weierstrauss","fromString","u8a","keyToDidDoc","pubKeyBytes","fingerprint","did","keyId","key","pubKeyBytesToXY","id","verificationMethod","type","controller","publicKeyJwk","kty","crv","x","xm","y","ym","authentication","assertionMethod","capabilityDelegation","capabilityInvocation","nist_weierstrauss_common","testUint8Array","TypeError","publicKeyHex","pubKeyBytesToHex","length","slice","publicKey","point","secp521r1","ECPointDecompress","publicKeyIntToXY","Error","jwkJcsDecode","keyToDidDoc","pubKeyBytes","fingerprint","contentType","did","keyId","publicKeyJwk","jwkJcsDecode","DID_LD_JSON","id","verificationMethod","type","controller","authentication","assertionMethod","capabilityDelegation","capabilityInvocation","decode","varint","prefixToDriverMap","secp256k1","ed25519","secp256r1","secp384r1","secp521r1","bls12381g2","jwkJcs","getResolver","key","did","parsed","r","options","contentType","accept","DID_LD_JSON","response","didResolutionMetadata","didDocument","didDocumentMetadata","multicodecPubKey","base58btc","id","keyType","pubKeyBytes","slice","bytes","args","fingerprint","doc","keyToDidDoc","Array","isArray","includes","push","DID_JSON","error","e","message","toString"]}