@fedify/vocab-runtime 2.4.0-dev.1531 → 2.4.0-dev.1567
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/deno.json +1 -1
- package/dist/mod.cjs +94 -1
- package/dist/mod.d.cts +52 -1
- package/dist/mod.d.ts +52 -1
- package/dist/mod.js +92 -2
- package/dist/tests/decimal.test.cjs +2 -2
- package/dist/tests/decimal.test.mjs +2 -2
- package/dist/tests/{docloader-Cvdl8PIZ.cjs → docloader-BcaQHb0Q.cjs} +23 -1
- package/dist/tests/{docloader-Bfj7NaT_.mjs → docloader-D61oV0K1.mjs} +23 -1
- package/dist/tests/docloader.test.cjs +2 -2
- package/dist/tests/docloader.test.mjs +2 -2
- package/dist/tests/{key-CrrK9mYh.mjs → key-CDGDH_vC.mjs} +69 -1
- package/dist/tests/{key-pMmqUKuo.cjs → key-_wXwomh_.cjs} +86 -0
- package/dist/tests/key.test.cjs +48 -1
- package/dist/tests/key.test.mjs +48 -1
- package/dist/tests/{request-B5Su2gl0.mjs → request-Cn0vP7Hj.mjs} +1 -1
- package/dist/tests/{request-DJvOZdo7.cjs → request-DnNWah97.cjs} +1 -1
- package/dist/tests/request.test.cjs +1 -1
- package/dist/tests/request.test.mjs +1 -1
- package/package.json +1 -1
- package/src/contexts/fep-7aa9.json +24 -0
- package/src/contexts.ts +2 -0
- package/src/key.test.ts +86 -0
- package/src/key.ts +125 -0
- package/src/mod.ts +4 -0
package/deno.json
CHANGED
package/dist/mod.cjs
CHANGED
|
@@ -4287,6 +4287,28 @@ const preloadedContexts = {
|
|
|
4287
4287
|
"@type": "@id"
|
|
4288
4288
|
}
|
|
4289
4289
|
} },
|
|
4290
|
+
"https://w3id.org/fep/7aa9": { "@context": {
|
|
4291
|
+
"FeaturedCollection": "https://w3id.org/fep/7aa9#FeaturedCollection",
|
|
4292
|
+
"FeaturedItem": "https://w3id.org/fep/7aa9#FeaturedItem",
|
|
4293
|
+
"FeatureRequest": "https://w3id.org/fep/7aa9#FeatureRequest",
|
|
4294
|
+
"FeatureAuthorization": "https://w3id.org/fep/7aa9#FeatureAuthorization",
|
|
4295
|
+
"topic": {
|
|
4296
|
+
"@id": "https://w3id.org/fep/7aa9#topic",
|
|
4297
|
+
"@type": "@id"
|
|
4298
|
+
},
|
|
4299
|
+
"featuredObject": {
|
|
4300
|
+
"@id": "https://w3id.org/fep/7aa9#featuredObject",
|
|
4301
|
+
"@type": "@id"
|
|
4302
|
+
},
|
|
4303
|
+
"canFeature": {
|
|
4304
|
+
"@id": "https://w3id.org/fep/7aa9#canFeature",
|
|
4305
|
+
"@type": "@id"
|
|
4306
|
+
},
|
|
4307
|
+
"featureAuthorization": {
|
|
4308
|
+
"@id": "https://w3id.org/fep/7aa9#featureAuthorization",
|
|
4309
|
+
"@type": "@id"
|
|
4310
|
+
}
|
|
4311
|
+
} },
|
|
4290
4312
|
"https://join-lemmy.org/context.json": { "@context": ["https://w3id.org/security/v1", {
|
|
4291
4313
|
"as": "https://www.w3.org/ns/activitystreams#",
|
|
4292
4314
|
"lemmy": "https://join-lemmy.org/ns#",
|
|
@@ -4345,7 +4367,7 @@ const preloadedContexts = {
|
|
|
4345
4367
|
//#endregion
|
|
4346
4368
|
//#region deno.json
|
|
4347
4369
|
var name = "@fedify/vocab-runtime";
|
|
4348
|
-
var version = "2.4.0-dev.
|
|
4370
|
+
var version = "2.4.0-dev.1567+12e6a3c0";
|
|
4349
4371
|
//#endregion
|
|
4350
4372
|
//#region src/link.ts
|
|
4351
4373
|
const parametersNeedLowerCase = ["rel", "type"];
|
|
@@ -5231,6 +5253,11 @@ const algorithms = {
|
|
|
5231
5253
|
},
|
|
5232
5254
|
"1.3.101.112": "Ed25519"
|
|
5233
5255
|
};
|
|
5256
|
+
const DID_KEY_PREFIX = "did:key:";
|
|
5257
|
+
const ED25519_PUBLIC_KEY_MULTICODEC = 237;
|
|
5258
|
+
const ED25519_PUBLIC_KEY_LENGTH = 32;
|
|
5259
|
+
const DID_KEY_PATTERN = /^did:key:([^/?#]+)$/;
|
|
5260
|
+
const DID_KEY_VERIFICATION_METHOD_PATTERN = /^did:key:([^/?#]+)#([^/?#]+)$/;
|
|
5234
5261
|
/**
|
|
5235
5262
|
* Imports a PEM-SPKI formatted public key.
|
|
5236
5263
|
* @param pem The PEM-SPKI formatted public key.
|
|
@@ -5292,6 +5319,69 @@ const PKCS1_HEADER = /^\s*-----BEGIN\s+RSA\s+PUBLIC\s+KEY-----\s*\n/;
|
|
|
5292
5319
|
function importPem(pem) {
|
|
5293
5320
|
return PKCS1_HEADER.test(pem) ? importPkcs1(pem) : importSpki(pem);
|
|
5294
5321
|
}
|
|
5322
|
+
function decodeEd25519DidKeyMultibase(multibaseKey) {
|
|
5323
|
+
if (!multibaseKey.startsWith("z")) throw new TypeError("did:key must use base58-btc Multibase encoding.");
|
|
5324
|
+
let decoded;
|
|
5325
|
+
try {
|
|
5326
|
+
decoded = decodeMultibase(multibaseKey);
|
|
5327
|
+
} catch (error) {
|
|
5328
|
+
throw new TypeError("Invalid did:key Multibase encoding.", { cause: error });
|
|
5329
|
+
}
|
|
5330
|
+
const { code } = getMulticodecPrefix(decoded);
|
|
5331
|
+
if (code !== ED25519_PUBLIC_KEY_MULTICODEC) throw new TypeError("Unsupported did:key type: 0x" + code.toString(16));
|
|
5332
|
+
const content = removeMulticodecPrefix(decoded);
|
|
5333
|
+
if (content.length !== ED25519_PUBLIC_KEY_LENGTH) throw new TypeError("Invalid Ed25519 did:key length.");
|
|
5334
|
+
return content;
|
|
5335
|
+
}
|
|
5336
|
+
/**
|
|
5337
|
+
* Imports an Ed25519 `did:key` DID.
|
|
5338
|
+
*
|
|
5339
|
+
* @param did The `did:key` DID.
|
|
5340
|
+
* @returns The imported Ed25519 public key.
|
|
5341
|
+
* @throws {TypeError} If the DID is malformed or uses an unsupported key type.
|
|
5342
|
+
* @since 2.4.0
|
|
5343
|
+
*/
|
|
5344
|
+
async function importDidKey(did) {
|
|
5345
|
+
const match = (did instanceof URL ? did.href : did).match(DID_KEY_PATTERN);
|
|
5346
|
+
if (match == null) throw new TypeError("Invalid did:key DID.");
|
|
5347
|
+
const content = decodeEd25519DidKeyMultibase(match[1]);
|
|
5348
|
+
return await crypto.subtle.importKey("raw", content.slice(), "Ed25519", true, ["verify"]);
|
|
5349
|
+
}
|
|
5350
|
+
/**
|
|
5351
|
+
* Exports an Ed25519 public key as a `did:key` DID.
|
|
5352
|
+
*
|
|
5353
|
+
* @param key The Ed25519 public key.
|
|
5354
|
+
* @returns The `did:key` DID.
|
|
5355
|
+
* @throws {TypeError} If the key is invalid or unsupported.
|
|
5356
|
+
* @since 2.4.0
|
|
5357
|
+
*/
|
|
5358
|
+
async function exportDidKey(key) {
|
|
5359
|
+
if (key.algorithm.name !== "Ed25519") throw new TypeError("Unsupported key type: " + JSON.stringify(key.algorithm));
|
|
5360
|
+
return DID_KEY_PREFIX + await exportMultibaseKey(key);
|
|
5361
|
+
}
|
|
5362
|
+
/**
|
|
5363
|
+
* Parses an Ed25519 `did:key` verification method DID URL.
|
|
5364
|
+
*
|
|
5365
|
+
* @param didUrl The `did:key` DID URL.
|
|
5366
|
+
* @returns The parsed verification method.
|
|
5367
|
+
* @throws {TypeError} If the DID URL is malformed, unsupported, or its
|
|
5368
|
+
* fragment does not identify the same key as the DID.
|
|
5369
|
+
* @since 2.4.0
|
|
5370
|
+
*/
|
|
5371
|
+
async function parseDidKeyVerificationMethod(didUrl) {
|
|
5372
|
+
const didUrlString = didUrl instanceof URL ? didUrl.href : didUrl;
|
|
5373
|
+
const match = didUrlString.match(DID_KEY_VERIFICATION_METHOD_PATTERN);
|
|
5374
|
+
if (match == null) throw new TypeError("Invalid did:key verification method.");
|
|
5375
|
+
const [, publicKeyMultibase, fragment] = match;
|
|
5376
|
+
if (publicKeyMultibase !== fragment) throw new TypeError("Invalid did:key verification method fragment.");
|
|
5377
|
+
const publicKey = await importDidKey(DID_KEY_PREFIX + publicKeyMultibase);
|
|
5378
|
+
return {
|
|
5379
|
+
id: new URL(didUrlString),
|
|
5380
|
+
controller: new URL(DID_KEY_PREFIX + publicKeyMultibase),
|
|
5381
|
+
publicKeyMultibase,
|
|
5382
|
+
publicKey
|
|
5383
|
+
};
|
|
5384
|
+
}
|
|
5295
5385
|
/**
|
|
5296
5386
|
* Imports a [Multibase]-encoded public key.
|
|
5297
5387
|
*
|
|
@@ -5467,6 +5557,7 @@ exports.decodeMultibase = decodeMultibase;
|
|
|
5467
5557
|
exports.encodeMultibase = encodeMultibase;
|
|
5468
5558
|
exports.encodingFromBaseData = encodingFromBaseData;
|
|
5469
5559
|
exports.expandIPv6Address = require_url.expandIPv6Address;
|
|
5560
|
+
exports.exportDidKey = exportDidKey;
|
|
5470
5561
|
exports.exportMultibaseKey = exportMultibaseKey;
|
|
5471
5562
|
exports.exportSpki = exportSpki;
|
|
5472
5563
|
exports.formatIri = require_url.formatIri;
|
|
@@ -5474,6 +5565,7 @@ exports.getDocumentLoader = getDocumentLoader;
|
|
|
5474
5565
|
exports.getRemoteDocument = getRemoteDocument;
|
|
5475
5566
|
exports.getUserAgent = getUserAgent;
|
|
5476
5567
|
exports.haveSameIriOrigin = require_url.haveSameIriOrigin;
|
|
5568
|
+
exports.importDidKey = importDidKey;
|
|
5477
5569
|
exports.importMultibaseKey = importMultibaseKey;
|
|
5478
5570
|
exports.importPem = importPem;
|
|
5479
5571
|
exports.importPkcs1 = importPkcs1;
|
|
@@ -5483,6 +5575,7 @@ exports.isValidPublicIPv4Address = require_url.isValidPublicIPv4Address;
|
|
|
5483
5575
|
exports.isValidPublicIPv6Address = require_url.isValidPublicIPv6Address;
|
|
5484
5576
|
exports.logRequest = logRequest;
|
|
5485
5577
|
exports.parseDecimal = parseDecimal;
|
|
5578
|
+
exports.parseDidKeyVerificationMethod = parseDidKeyVerificationMethod;
|
|
5486
5579
|
exports.parseIri = require_url.parseIri;
|
|
5487
5580
|
exports.parseJsonLdId = require_url.parseJsonLdId;
|
|
5488
5581
|
exports.preloadedContexts = preloadedContexts;
|
package/dist/mod.d.cts
CHANGED
|
@@ -7,6 +7,29 @@ declare const preloadedContexts: Record<string, unknown>;
|
|
|
7
7
|
//#endregion
|
|
8
8
|
//#region src/key.d.ts
|
|
9
9
|
/**
|
|
10
|
+
* Parsed `did:key` verification method.
|
|
11
|
+
*
|
|
12
|
+
* @since 2.4.0
|
|
13
|
+
*/
|
|
14
|
+
interface DidKeyVerificationMethod {
|
|
15
|
+
/**
|
|
16
|
+
* The DID URL identifying the verification method.
|
|
17
|
+
*/
|
|
18
|
+
readonly id: URL;
|
|
19
|
+
/**
|
|
20
|
+
* The controller DID.
|
|
21
|
+
*/
|
|
22
|
+
readonly controller: URL;
|
|
23
|
+
/**
|
|
24
|
+
* The Ed25519 public key encoded as a Multibase Multikey value.
|
|
25
|
+
*/
|
|
26
|
+
readonly publicKeyMultibase: string;
|
|
27
|
+
/**
|
|
28
|
+
* The Ed25519 public key.
|
|
29
|
+
*/
|
|
30
|
+
readonly publicKey: CryptoKey;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
10
33
|
* Imports a PEM-SPKI formatted public key.
|
|
11
34
|
* @param pem The PEM-SPKI formatted public key.
|
|
12
35
|
* @returns The imported public key.
|
|
@@ -39,6 +62,34 @@ declare function importPkcs1(pem: string): Promise<CryptoKey>;
|
|
|
39
62
|
*/
|
|
40
63
|
declare function importPem(pem: string): Promise<CryptoKey>;
|
|
41
64
|
/**
|
|
65
|
+
* Imports an Ed25519 `did:key` DID.
|
|
66
|
+
*
|
|
67
|
+
* @param did The `did:key` DID.
|
|
68
|
+
* @returns The imported Ed25519 public key.
|
|
69
|
+
* @throws {TypeError} If the DID is malformed or uses an unsupported key type.
|
|
70
|
+
* @since 2.4.0
|
|
71
|
+
*/
|
|
72
|
+
declare function importDidKey(did: string | URL): Promise<CryptoKey>;
|
|
73
|
+
/**
|
|
74
|
+
* Exports an Ed25519 public key as a `did:key` DID.
|
|
75
|
+
*
|
|
76
|
+
* @param key The Ed25519 public key.
|
|
77
|
+
* @returns The `did:key` DID.
|
|
78
|
+
* @throws {TypeError} If the key is invalid or unsupported.
|
|
79
|
+
* @since 2.4.0
|
|
80
|
+
*/
|
|
81
|
+
declare function exportDidKey(key: CryptoKey): Promise<string>;
|
|
82
|
+
/**
|
|
83
|
+
* Parses an Ed25519 `did:key` verification method DID URL.
|
|
84
|
+
*
|
|
85
|
+
* @param didUrl The `did:key` DID URL.
|
|
86
|
+
* @returns The parsed verification method.
|
|
87
|
+
* @throws {TypeError} If the DID URL is malformed, unsupported, or its
|
|
88
|
+
* fragment does not identify the same key as the DID.
|
|
89
|
+
* @since 2.4.0
|
|
90
|
+
*/
|
|
91
|
+
declare function parseDidKeyVerificationMethod(didUrl: string | URL): Promise<DidKeyVerificationMethod>;
|
|
92
|
+
/**
|
|
42
93
|
* Imports a [Multibase]-encoded public key.
|
|
43
94
|
*
|
|
44
95
|
* [Multibase]: https://www.w3.org/TR/vc-data-integrity/#multibase-0
|
|
@@ -286,4 +337,4 @@ declare function isValidPublicIPv4Address(address: string): boolean;
|
|
|
286
337
|
declare function isValidPublicIPv6Address(address: string): boolean;
|
|
287
338
|
declare function expandIPv6Address(address: string): string;
|
|
288
339
|
//#endregion
|
|
289
|
-
export { type AuthenticatedDocumentLoaderFactory, type CreateRequestOptions, type Decimal, type DocumentLoader, type DocumentLoaderFactory, type DocumentLoaderFactoryOptions, type DocumentLoaderOptions, FetchError, type GetDocumentLoaderOptions, type GetUserAgentOptions, type Json, LanguageString, type PropertyPreprocessor, type PropertyPreprocessorContext, type RemoteDocument, UrlError, canParseDecimal, createActivityPubRequest, decodeMultibase, encodeMultibase, encodingFromBaseData, expandIPv6Address, exportMultibaseKey, exportSpki, formatIri, getDocumentLoader, getRemoteDocument, getUserAgent, haveSameIriOrigin, importMultibaseKey, importPem, importPkcs1, importSpki, isDecimal, isValidPublicIPv4Address, isValidPublicIPv6Address, logRequest, parseDecimal, parseIri, parseJsonLdId, preloadedContexts, validatePublicUrl };
|
|
340
|
+
export { type AuthenticatedDocumentLoaderFactory, type CreateRequestOptions, type Decimal, type DidKeyVerificationMethod, type DocumentLoader, type DocumentLoaderFactory, type DocumentLoaderFactoryOptions, type DocumentLoaderOptions, FetchError, type GetDocumentLoaderOptions, type GetUserAgentOptions, type Json, LanguageString, type PropertyPreprocessor, type PropertyPreprocessorContext, type RemoteDocument, UrlError, canParseDecimal, createActivityPubRequest, decodeMultibase, encodeMultibase, encodingFromBaseData, expandIPv6Address, exportDidKey, exportMultibaseKey, exportSpki, formatIri, getDocumentLoader, getRemoteDocument, getUserAgent, haveSameIriOrigin, importDidKey, importMultibaseKey, importPem, importPkcs1, importSpki, isDecimal, isValidPublicIPv4Address, isValidPublicIPv6Address, logRequest, parseDecimal, parseDidKeyVerificationMethod, parseIri, parseJsonLdId, preloadedContexts, validatePublicUrl };
|
package/dist/mod.d.ts
CHANGED
|
@@ -7,6 +7,29 @@ declare const preloadedContexts: Record<string, unknown>;
|
|
|
7
7
|
//#endregion
|
|
8
8
|
//#region src/key.d.ts
|
|
9
9
|
/**
|
|
10
|
+
* Parsed `did:key` verification method.
|
|
11
|
+
*
|
|
12
|
+
* @since 2.4.0
|
|
13
|
+
*/
|
|
14
|
+
interface DidKeyVerificationMethod {
|
|
15
|
+
/**
|
|
16
|
+
* The DID URL identifying the verification method.
|
|
17
|
+
*/
|
|
18
|
+
readonly id: URL;
|
|
19
|
+
/**
|
|
20
|
+
* The controller DID.
|
|
21
|
+
*/
|
|
22
|
+
readonly controller: URL;
|
|
23
|
+
/**
|
|
24
|
+
* The Ed25519 public key encoded as a Multibase Multikey value.
|
|
25
|
+
*/
|
|
26
|
+
readonly publicKeyMultibase: string;
|
|
27
|
+
/**
|
|
28
|
+
* The Ed25519 public key.
|
|
29
|
+
*/
|
|
30
|
+
readonly publicKey: CryptoKey;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
10
33
|
* Imports a PEM-SPKI formatted public key.
|
|
11
34
|
* @param pem The PEM-SPKI formatted public key.
|
|
12
35
|
* @returns The imported public key.
|
|
@@ -39,6 +62,34 @@ declare function importPkcs1(pem: string): Promise<CryptoKey>;
|
|
|
39
62
|
*/
|
|
40
63
|
declare function importPem(pem: string): Promise<CryptoKey>;
|
|
41
64
|
/**
|
|
65
|
+
* Imports an Ed25519 `did:key` DID.
|
|
66
|
+
*
|
|
67
|
+
* @param did The `did:key` DID.
|
|
68
|
+
* @returns The imported Ed25519 public key.
|
|
69
|
+
* @throws {TypeError} If the DID is malformed or uses an unsupported key type.
|
|
70
|
+
* @since 2.4.0
|
|
71
|
+
*/
|
|
72
|
+
declare function importDidKey(did: string | URL): Promise<CryptoKey>;
|
|
73
|
+
/**
|
|
74
|
+
* Exports an Ed25519 public key as a `did:key` DID.
|
|
75
|
+
*
|
|
76
|
+
* @param key The Ed25519 public key.
|
|
77
|
+
* @returns The `did:key` DID.
|
|
78
|
+
* @throws {TypeError} If the key is invalid or unsupported.
|
|
79
|
+
* @since 2.4.0
|
|
80
|
+
*/
|
|
81
|
+
declare function exportDidKey(key: CryptoKey): Promise<string>;
|
|
82
|
+
/**
|
|
83
|
+
* Parses an Ed25519 `did:key` verification method DID URL.
|
|
84
|
+
*
|
|
85
|
+
* @param didUrl The `did:key` DID URL.
|
|
86
|
+
* @returns The parsed verification method.
|
|
87
|
+
* @throws {TypeError} If the DID URL is malformed, unsupported, or its
|
|
88
|
+
* fragment does not identify the same key as the DID.
|
|
89
|
+
* @since 2.4.0
|
|
90
|
+
*/
|
|
91
|
+
declare function parseDidKeyVerificationMethod(didUrl: string | URL): Promise<DidKeyVerificationMethod>;
|
|
92
|
+
/**
|
|
42
93
|
* Imports a [Multibase]-encoded public key.
|
|
43
94
|
*
|
|
44
95
|
* [Multibase]: https://www.w3.org/TR/vc-data-integrity/#multibase-0
|
|
@@ -286,4 +337,4 @@ declare function isValidPublicIPv4Address(address: string): boolean;
|
|
|
286
337
|
declare function isValidPublicIPv6Address(address: string): boolean;
|
|
287
338
|
declare function expandIPv6Address(address: string): string;
|
|
288
339
|
//#endregion
|
|
289
|
-
export { type AuthenticatedDocumentLoaderFactory, type CreateRequestOptions, type Decimal, type DocumentLoader, type DocumentLoaderFactory, type DocumentLoaderFactoryOptions, type DocumentLoaderOptions, FetchError, type GetDocumentLoaderOptions, type GetUserAgentOptions, type Json, LanguageString, type PropertyPreprocessor, type PropertyPreprocessorContext, type RemoteDocument, UrlError, canParseDecimal, createActivityPubRequest, decodeMultibase, encodeMultibase, encodingFromBaseData, expandIPv6Address, exportMultibaseKey, exportSpki, formatIri, getDocumentLoader, getRemoteDocument, getUserAgent, haveSameIriOrigin, importMultibaseKey, importPem, importPkcs1, importSpki, isDecimal, isValidPublicIPv4Address, isValidPublicIPv6Address, logRequest, parseDecimal, parseIri, parseJsonLdId, preloadedContexts, validatePublicUrl };
|
|
340
|
+
export { type AuthenticatedDocumentLoaderFactory, type CreateRequestOptions, type Decimal, type DidKeyVerificationMethod, type DocumentLoader, type DocumentLoaderFactory, type DocumentLoaderFactoryOptions, type DocumentLoaderOptions, FetchError, type GetDocumentLoaderOptions, type GetUserAgentOptions, type Json, LanguageString, type PropertyPreprocessor, type PropertyPreprocessorContext, type RemoteDocument, UrlError, canParseDecimal, createActivityPubRequest, decodeMultibase, encodeMultibase, encodingFromBaseData, expandIPv6Address, exportDidKey, exportMultibaseKey, exportSpki, formatIri, getDocumentLoader, getRemoteDocument, getUserAgent, haveSameIriOrigin, importDidKey, importMultibaseKey, importPem, importPkcs1, importSpki, isDecimal, isValidPublicIPv4Address, isValidPublicIPv6Address, logRequest, parseDecimal, parseDidKeyVerificationMethod, parseIri, parseJsonLdId, preloadedContexts, validatePublicUrl };
|
package/dist/mod.js
CHANGED
|
@@ -4283,6 +4283,28 @@ const preloadedContexts = {
|
|
|
4283
4283
|
"@type": "@id"
|
|
4284
4284
|
}
|
|
4285
4285
|
} },
|
|
4286
|
+
"https://w3id.org/fep/7aa9": { "@context": {
|
|
4287
|
+
"FeaturedCollection": "https://w3id.org/fep/7aa9#FeaturedCollection",
|
|
4288
|
+
"FeaturedItem": "https://w3id.org/fep/7aa9#FeaturedItem",
|
|
4289
|
+
"FeatureRequest": "https://w3id.org/fep/7aa9#FeatureRequest",
|
|
4290
|
+
"FeatureAuthorization": "https://w3id.org/fep/7aa9#FeatureAuthorization",
|
|
4291
|
+
"topic": {
|
|
4292
|
+
"@id": "https://w3id.org/fep/7aa9#topic",
|
|
4293
|
+
"@type": "@id"
|
|
4294
|
+
},
|
|
4295
|
+
"featuredObject": {
|
|
4296
|
+
"@id": "https://w3id.org/fep/7aa9#featuredObject",
|
|
4297
|
+
"@type": "@id"
|
|
4298
|
+
},
|
|
4299
|
+
"canFeature": {
|
|
4300
|
+
"@id": "https://w3id.org/fep/7aa9#canFeature",
|
|
4301
|
+
"@type": "@id"
|
|
4302
|
+
},
|
|
4303
|
+
"featureAuthorization": {
|
|
4304
|
+
"@id": "https://w3id.org/fep/7aa9#featureAuthorization",
|
|
4305
|
+
"@type": "@id"
|
|
4306
|
+
}
|
|
4307
|
+
} },
|
|
4286
4308
|
"https://join-lemmy.org/context.json": { "@context": ["https://w3id.org/security/v1", {
|
|
4287
4309
|
"as": "https://www.w3.org/ns/activitystreams#",
|
|
4288
4310
|
"lemmy": "https://join-lemmy.org/ns#",
|
|
@@ -4341,7 +4363,7 @@ const preloadedContexts = {
|
|
|
4341
4363
|
//#endregion
|
|
4342
4364
|
//#region deno.json
|
|
4343
4365
|
var name = "@fedify/vocab-runtime";
|
|
4344
|
-
var version = "2.4.0-dev.
|
|
4366
|
+
var version = "2.4.0-dev.1567+12e6a3c0";
|
|
4345
4367
|
//#endregion
|
|
4346
4368
|
//#region src/link.ts
|
|
4347
4369
|
const parametersNeedLowerCase = ["rel", "type"];
|
|
@@ -5227,6 +5249,11 @@ const algorithms = {
|
|
|
5227
5249
|
},
|
|
5228
5250
|
"1.3.101.112": "Ed25519"
|
|
5229
5251
|
};
|
|
5252
|
+
const DID_KEY_PREFIX = "did:key:";
|
|
5253
|
+
const ED25519_PUBLIC_KEY_MULTICODEC = 237;
|
|
5254
|
+
const ED25519_PUBLIC_KEY_LENGTH = 32;
|
|
5255
|
+
const DID_KEY_PATTERN = /^did:key:([^/?#]+)$/;
|
|
5256
|
+
const DID_KEY_VERIFICATION_METHOD_PATTERN = /^did:key:([^/?#]+)#([^/?#]+)$/;
|
|
5230
5257
|
/**
|
|
5231
5258
|
* Imports a PEM-SPKI formatted public key.
|
|
5232
5259
|
* @param pem The PEM-SPKI formatted public key.
|
|
@@ -5288,6 +5315,69 @@ const PKCS1_HEADER = /^\s*-----BEGIN\s+RSA\s+PUBLIC\s+KEY-----\s*\n/;
|
|
|
5288
5315
|
function importPem(pem) {
|
|
5289
5316
|
return PKCS1_HEADER.test(pem) ? importPkcs1(pem) : importSpki(pem);
|
|
5290
5317
|
}
|
|
5318
|
+
function decodeEd25519DidKeyMultibase(multibaseKey) {
|
|
5319
|
+
if (!multibaseKey.startsWith("z")) throw new TypeError("did:key must use base58-btc Multibase encoding.");
|
|
5320
|
+
let decoded;
|
|
5321
|
+
try {
|
|
5322
|
+
decoded = decodeMultibase(multibaseKey);
|
|
5323
|
+
} catch (error) {
|
|
5324
|
+
throw new TypeError("Invalid did:key Multibase encoding.", { cause: error });
|
|
5325
|
+
}
|
|
5326
|
+
const { code } = getMulticodecPrefix(decoded);
|
|
5327
|
+
if (code !== ED25519_PUBLIC_KEY_MULTICODEC) throw new TypeError("Unsupported did:key type: 0x" + code.toString(16));
|
|
5328
|
+
const content = removeMulticodecPrefix(decoded);
|
|
5329
|
+
if (content.length !== ED25519_PUBLIC_KEY_LENGTH) throw new TypeError("Invalid Ed25519 did:key length.");
|
|
5330
|
+
return content;
|
|
5331
|
+
}
|
|
5332
|
+
/**
|
|
5333
|
+
* Imports an Ed25519 `did:key` DID.
|
|
5334
|
+
*
|
|
5335
|
+
* @param did The `did:key` DID.
|
|
5336
|
+
* @returns The imported Ed25519 public key.
|
|
5337
|
+
* @throws {TypeError} If the DID is malformed or uses an unsupported key type.
|
|
5338
|
+
* @since 2.4.0
|
|
5339
|
+
*/
|
|
5340
|
+
async function importDidKey(did) {
|
|
5341
|
+
const match = (did instanceof URL ? did.href : did).match(DID_KEY_PATTERN);
|
|
5342
|
+
if (match == null) throw new TypeError("Invalid did:key DID.");
|
|
5343
|
+
const content = decodeEd25519DidKeyMultibase(match[1]);
|
|
5344
|
+
return await crypto.subtle.importKey("raw", content.slice(), "Ed25519", true, ["verify"]);
|
|
5345
|
+
}
|
|
5346
|
+
/**
|
|
5347
|
+
* Exports an Ed25519 public key as a `did:key` DID.
|
|
5348
|
+
*
|
|
5349
|
+
* @param key The Ed25519 public key.
|
|
5350
|
+
* @returns The `did:key` DID.
|
|
5351
|
+
* @throws {TypeError} If the key is invalid or unsupported.
|
|
5352
|
+
* @since 2.4.0
|
|
5353
|
+
*/
|
|
5354
|
+
async function exportDidKey(key) {
|
|
5355
|
+
if (key.algorithm.name !== "Ed25519") throw new TypeError("Unsupported key type: " + JSON.stringify(key.algorithm));
|
|
5356
|
+
return DID_KEY_PREFIX + await exportMultibaseKey(key);
|
|
5357
|
+
}
|
|
5358
|
+
/**
|
|
5359
|
+
* Parses an Ed25519 `did:key` verification method DID URL.
|
|
5360
|
+
*
|
|
5361
|
+
* @param didUrl The `did:key` DID URL.
|
|
5362
|
+
* @returns The parsed verification method.
|
|
5363
|
+
* @throws {TypeError} If the DID URL is malformed, unsupported, or its
|
|
5364
|
+
* fragment does not identify the same key as the DID.
|
|
5365
|
+
* @since 2.4.0
|
|
5366
|
+
*/
|
|
5367
|
+
async function parseDidKeyVerificationMethod(didUrl) {
|
|
5368
|
+
const didUrlString = didUrl instanceof URL ? didUrl.href : didUrl;
|
|
5369
|
+
const match = didUrlString.match(DID_KEY_VERIFICATION_METHOD_PATTERN);
|
|
5370
|
+
if (match == null) throw new TypeError("Invalid did:key verification method.");
|
|
5371
|
+
const [, publicKeyMultibase, fragment] = match;
|
|
5372
|
+
if (publicKeyMultibase !== fragment) throw new TypeError("Invalid did:key verification method fragment.");
|
|
5373
|
+
const publicKey = await importDidKey(DID_KEY_PREFIX + publicKeyMultibase);
|
|
5374
|
+
return {
|
|
5375
|
+
id: new URL(didUrlString),
|
|
5376
|
+
controller: new URL(DID_KEY_PREFIX + publicKeyMultibase),
|
|
5377
|
+
publicKeyMultibase,
|
|
5378
|
+
publicKey
|
|
5379
|
+
};
|
|
5380
|
+
}
|
|
5291
5381
|
/**
|
|
5292
5382
|
* Imports a [Multibase]-encoded public key.
|
|
5293
5383
|
*
|
|
@@ -5454,4 +5544,4 @@ LanguageString.prototype[Symbol.for("nodejs.util.inspect.custom")] = function(_d
|
|
|
5454
5544
|
return `<${this.locale.baseName}> ${inspect(this.toString(), options)}`;
|
|
5455
5545
|
};
|
|
5456
5546
|
//#endregion
|
|
5457
|
-
export { FetchError, LanguageString, UrlError, canParseDecimal, createActivityPubRequest, decodeMultibase, encodeMultibase, encodingFromBaseData, expandIPv6Address, exportMultibaseKey, exportSpki, formatIri, getDocumentLoader, getRemoteDocument, getUserAgent, haveSameIriOrigin, importMultibaseKey, importPem, importPkcs1, importSpki, isDecimal, isValidPublicIPv4Address, isValidPublicIPv6Address, logRequest, parseDecimal, parseIri, parseJsonLdId, preloadedContexts, validatePublicUrl };
|
|
5547
|
+
export { FetchError, LanguageString, UrlError, canParseDecimal, createActivityPubRequest, decodeMultibase, encodeMultibase, encodingFromBaseData, expandIPv6Address, exportDidKey, exportMultibaseKey, exportSpki, formatIri, getDocumentLoader, getRemoteDocument, getUserAgent, haveSameIriOrigin, importDidKey, importMultibaseKey, importPem, importPkcs1, importSpki, isDecimal, isValidPublicIPv4Address, isValidPublicIPv6Address, logRequest, parseDecimal, parseDidKeyVerificationMethod, parseIri, parseJsonLdId, preloadedContexts, validatePublicUrl };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require("./chunk-C2EiDwsr.cjs");
|
|
2
|
-
require("./docloader-
|
|
2
|
+
require("./docloader-BcaQHb0Q.cjs");
|
|
3
3
|
require("./url-2XwVbUS_.cjs");
|
|
4
|
-
require("./key-
|
|
4
|
+
require("./key-_wXwomh_.cjs");
|
|
5
5
|
require("./multibase-Bz_UUDtL.cjs");
|
|
6
6
|
require("./langstr-CbAxaeEZ.cjs");
|
|
7
7
|
let node_assert = require("node:assert");
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import "./docloader-
|
|
1
|
+
import "./docloader-D61oV0K1.mjs";
|
|
2
2
|
import "./url-YWJbnRlf.mjs";
|
|
3
|
-
import "./key-
|
|
3
|
+
import "./key-CDGDH_vC.mjs";
|
|
4
4
|
import "./multibase-B4bvakyA.mjs";
|
|
5
5
|
import "./langstr-Di5AvKpB.mjs";
|
|
6
6
|
import { deepStrictEqual, throws } from "node:assert";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
require("./chunk-C2EiDwsr.cjs");
|
|
2
|
-
const require_request = require("./request-
|
|
2
|
+
const require_request = require("./request-DnNWah97.cjs");
|
|
3
3
|
const require_link = require("./link-FguCydMA.cjs");
|
|
4
4
|
const require_url = require("./url-2XwVbUS_.cjs");
|
|
5
5
|
let _logtape_logtape = require("@logtape/logtape");
|
|
@@ -4278,6 +4278,28 @@ const preloadedContexts = {
|
|
|
4278
4278
|
"@type": "@id"
|
|
4279
4279
|
}
|
|
4280
4280
|
} },
|
|
4281
|
+
"https://w3id.org/fep/7aa9": { "@context": {
|
|
4282
|
+
"FeaturedCollection": "https://w3id.org/fep/7aa9#FeaturedCollection",
|
|
4283
|
+
"FeaturedItem": "https://w3id.org/fep/7aa9#FeaturedItem",
|
|
4284
|
+
"FeatureRequest": "https://w3id.org/fep/7aa9#FeatureRequest",
|
|
4285
|
+
"FeatureAuthorization": "https://w3id.org/fep/7aa9#FeatureAuthorization",
|
|
4286
|
+
"topic": {
|
|
4287
|
+
"@id": "https://w3id.org/fep/7aa9#topic",
|
|
4288
|
+
"@type": "@id"
|
|
4289
|
+
},
|
|
4290
|
+
"featuredObject": {
|
|
4291
|
+
"@id": "https://w3id.org/fep/7aa9#featuredObject",
|
|
4292
|
+
"@type": "@id"
|
|
4293
|
+
},
|
|
4294
|
+
"canFeature": {
|
|
4295
|
+
"@id": "https://w3id.org/fep/7aa9#canFeature",
|
|
4296
|
+
"@type": "@id"
|
|
4297
|
+
},
|
|
4298
|
+
"featureAuthorization": {
|
|
4299
|
+
"@id": "https://w3id.org/fep/7aa9#featureAuthorization",
|
|
4300
|
+
"@type": "@id"
|
|
4301
|
+
}
|
|
4302
|
+
} },
|
|
4281
4303
|
"https://join-lemmy.org/context.json": { "@context": ["https://w3id.org/security/v1", {
|
|
4282
4304
|
"as": "https://www.w3.org/ns/activitystreams#",
|
|
4283
4305
|
"lemmy": "https://join-lemmy.org/ns#",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as name, i as logRequest, n as createActivityPubRequest, o as version, t as FetchError } from "./request-
|
|
1
|
+
import { a as name, i as logRequest, n as createActivityPubRequest, o as version, t as FetchError } from "./request-Cn0vP7Hj.mjs";
|
|
2
2
|
import { t as HttpHeaderLink } from "./link-NUUWCdnK.mjs";
|
|
3
3
|
import { l as validatePublicUrl, t as UrlError } from "./url-YWJbnRlf.mjs";
|
|
4
4
|
import { getLogger } from "@logtape/logtape";
|
|
@@ -4277,6 +4277,28 @@ const preloadedContexts = {
|
|
|
4277
4277
|
"@type": "@id"
|
|
4278
4278
|
}
|
|
4279
4279
|
} },
|
|
4280
|
+
"https://w3id.org/fep/7aa9": { "@context": {
|
|
4281
|
+
"FeaturedCollection": "https://w3id.org/fep/7aa9#FeaturedCollection",
|
|
4282
|
+
"FeaturedItem": "https://w3id.org/fep/7aa9#FeaturedItem",
|
|
4283
|
+
"FeatureRequest": "https://w3id.org/fep/7aa9#FeatureRequest",
|
|
4284
|
+
"FeatureAuthorization": "https://w3id.org/fep/7aa9#FeatureAuthorization",
|
|
4285
|
+
"topic": {
|
|
4286
|
+
"@id": "https://w3id.org/fep/7aa9#topic",
|
|
4287
|
+
"@type": "@id"
|
|
4288
|
+
},
|
|
4289
|
+
"featuredObject": {
|
|
4290
|
+
"@id": "https://w3id.org/fep/7aa9#featuredObject",
|
|
4291
|
+
"@type": "@id"
|
|
4292
|
+
},
|
|
4293
|
+
"canFeature": {
|
|
4294
|
+
"@id": "https://w3id.org/fep/7aa9#canFeature",
|
|
4295
|
+
"@type": "@id"
|
|
4296
|
+
},
|
|
4297
|
+
"featureAuthorization": {
|
|
4298
|
+
"@id": "https://w3id.org/fep/7aa9#featureAuthorization",
|
|
4299
|
+
"@type": "@id"
|
|
4300
|
+
}
|
|
4301
|
+
} },
|
|
4280
4302
|
"https://join-lemmy.org/context.json": { "@context": ["https://w3id.org/security/v1", {
|
|
4281
4303
|
"as": "https://www.w3.org/ns/activitystreams#",
|
|
4282
4304
|
"lemmy": "https://join-lemmy.org/ns#",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const require_chunk = require("./chunk-C2EiDwsr.cjs");
|
|
2
|
-
const require_docloader = require("./docloader-
|
|
3
|
-
const require_request = require("./request-
|
|
2
|
+
const require_docloader = require("./docloader-BcaQHb0Q.cjs");
|
|
3
|
+
const require_request = require("./request-DnNWah97.cjs");
|
|
4
4
|
const require_url = require("./url-2XwVbUS_.cjs");
|
|
5
5
|
let node_assert = require("node:assert");
|
|
6
6
|
let node_test = require("node:test");
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { n as getRemoteDocument, r as preloadedContexts, t as getDocumentLoader } from "./docloader-
|
|
2
|
-
import { t as FetchError } from "./request-
|
|
1
|
+
import { n as getRemoteDocument, r as preloadedContexts, t as getDocumentLoader } from "./docloader-D61oV0K1.mjs";
|
|
2
|
+
import { t as FetchError } from "./request-Cn0vP7Hj.mjs";
|
|
3
3
|
import { t as UrlError } from "./url-YWJbnRlf.mjs";
|
|
4
4
|
import { deepStrictEqual, ok, rejects } from "node:assert";
|
|
5
5
|
import { test } from "node:test";
|
|
@@ -45,6 +45,11 @@ const algorithms = {
|
|
|
45
45
|
},
|
|
46
46
|
"1.3.101.112": "Ed25519"
|
|
47
47
|
};
|
|
48
|
+
const DID_KEY_PREFIX = "did:key:";
|
|
49
|
+
const ED25519_PUBLIC_KEY_MULTICODEC = 237;
|
|
50
|
+
const ED25519_PUBLIC_KEY_LENGTH = 32;
|
|
51
|
+
const DID_KEY_PATTERN = /^did:key:([^/?#]+)$/;
|
|
52
|
+
const DID_KEY_VERIFICATION_METHOD_PATTERN = /^did:key:([^/?#]+)#([^/?#]+)$/;
|
|
48
53
|
/**
|
|
49
54
|
* Imports a PEM-SPKI formatted public key.
|
|
50
55
|
* @param pem The PEM-SPKI formatted public key.
|
|
@@ -106,6 +111,69 @@ const PKCS1_HEADER = /^\s*-----BEGIN\s+RSA\s+PUBLIC\s+KEY-----\s*\n/;
|
|
|
106
111
|
function importPem(pem) {
|
|
107
112
|
return PKCS1_HEADER.test(pem) ? importPkcs1(pem) : importSpki(pem);
|
|
108
113
|
}
|
|
114
|
+
function decodeEd25519DidKeyMultibase(multibaseKey) {
|
|
115
|
+
if (!multibaseKey.startsWith("z")) throw new TypeError("did:key must use base58-btc Multibase encoding.");
|
|
116
|
+
let decoded;
|
|
117
|
+
try {
|
|
118
|
+
decoded = decodeMultibase(multibaseKey);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
throw new TypeError("Invalid did:key Multibase encoding.", { cause: error });
|
|
121
|
+
}
|
|
122
|
+
const { code } = getMulticodecPrefix(decoded);
|
|
123
|
+
if (code !== ED25519_PUBLIC_KEY_MULTICODEC) throw new TypeError("Unsupported did:key type: 0x" + code.toString(16));
|
|
124
|
+
const content = removeMulticodecPrefix(decoded);
|
|
125
|
+
if (content.length !== ED25519_PUBLIC_KEY_LENGTH) throw new TypeError("Invalid Ed25519 did:key length.");
|
|
126
|
+
return content;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Imports an Ed25519 `did:key` DID.
|
|
130
|
+
*
|
|
131
|
+
* @param did The `did:key` DID.
|
|
132
|
+
* @returns The imported Ed25519 public key.
|
|
133
|
+
* @throws {TypeError} If the DID is malformed or uses an unsupported key type.
|
|
134
|
+
* @since 2.4.0
|
|
135
|
+
*/
|
|
136
|
+
async function importDidKey(did) {
|
|
137
|
+
const match = (did instanceof URL ? did.href : did).match(DID_KEY_PATTERN);
|
|
138
|
+
if (match == null) throw new TypeError("Invalid did:key DID.");
|
|
139
|
+
const content = decodeEd25519DidKeyMultibase(match[1]);
|
|
140
|
+
return await crypto.subtle.importKey("raw", content.slice(), "Ed25519", true, ["verify"]);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Exports an Ed25519 public key as a `did:key` DID.
|
|
144
|
+
*
|
|
145
|
+
* @param key The Ed25519 public key.
|
|
146
|
+
* @returns The `did:key` DID.
|
|
147
|
+
* @throws {TypeError} If the key is invalid or unsupported.
|
|
148
|
+
* @since 2.4.0
|
|
149
|
+
*/
|
|
150
|
+
async function exportDidKey(key) {
|
|
151
|
+
if (key.algorithm.name !== "Ed25519") throw new TypeError("Unsupported key type: " + JSON.stringify(key.algorithm));
|
|
152
|
+
return DID_KEY_PREFIX + await exportMultibaseKey(key);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Parses an Ed25519 `did:key` verification method DID URL.
|
|
156
|
+
*
|
|
157
|
+
* @param didUrl The `did:key` DID URL.
|
|
158
|
+
* @returns The parsed verification method.
|
|
159
|
+
* @throws {TypeError} If the DID URL is malformed, unsupported, or its
|
|
160
|
+
* fragment does not identify the same key as the DID.
|
|
161
|
+
* @since 2.4.0
|
|
162
|
+
*/
|
|
163
|
+
async function parseDidKeyVerificationMethod(didUrl) {
|
|
164
|
+
const didUrlString = didUrl instanceof URL ? didUrl.href : didUrl;
|
|
165
|
+
const match = didUrlString.match(DID_KEY_VERIFICATION_METHOD_PATTERN);
|
|
166
|
+
if (match == null) throw new TypeError("Invalid did:key verification method.");
|
|
167
|
+
const [, publicKeyMultibase, fragment] = match;
|
|
168
|
+
if (publicKeyMultibase !== fragment) throw new TypeError("Invalid did:key verification method fragment.");
|
|
169
|
+
const publicKey = await importDidKey(DID_KEY_PREFIX + publicKeyMultibase);
|
|
170
|
+
return {
|
|
171
|
+
id: new URL(didUrlString),
|
|
172
|
+
controller: new URL(DID_KEY_PREFIX + publicKeyMultibase),
|
|
173
|
+
publicKeyMultibase,
|
|
174
|
+
publicKey
|
|
175
|
+
};
|
|
176
|
+
}
|
|
109
177
|
/**
|
|
110
178
|
* Imports a [Multibase]-encoded public key.
|
|
111
179
|
*
|
|
@@ -169,4 +237,4 @@ async function exportMultibaseKey(key) {
|
|
|
169
237
|
return new TextDecoder().decode(encoded);
|
|
170
238
|
}
|
|
171
239
|
//#endregion
|
|
172
|
-
export {
|
|
240
|
+
export { importMultibaseKey as a, importSpki as c, importJwk as d, importDidKey as i, parseDidKeyVerificationMethod as l, exportMultibaseKey as n, importPem as o, exportSpki as r, importPkcs1 as s, exportDidKey as t, exportJwk as u };
|
|
@@ -46,6 +46,11 @@ const algorithms = {
|
|
|
46
46
|
},
|
|
47
47
|
"1.3.101.112": "Ed25519"
|
|
48
48
|
};
|
|
49
|
+
const DID_KEY_PREFIX = "did:key:";
|
|
50
|
+
const ED25519_PUBLIC_KEY_MULTICODEC = 237;
|
|
51
|
+
const ED25519_PUBLIC_KEY_LENGTH = 32;
|
|
52
|
+
const DID_KEY_PATTERN = /^did:key:([^/?#]+)$/;
|
|
53
|
+
const DID_KEY_VERIFICATION_METHOD_PATTERN = /^did:key:([^/?#]+)#([^/?#]+)$/;
|
|
49
54
|
/**
|
|
50
55
|
* Imports a PEM-SPKI formatted public key.
|
|
51
56
|
* @param pem The PEM-SPKI formatted public key.
|
|
@@ -107,6 +112,69 @@ const PKCS1_HEADER = /^\s*-----BEGIN\s+RSA\s+PUBLIC\s+KEY-----\s*\n/;
|
|
|
107
112
|
function importPem(pem) {
|
|
108
113
|
return PKCS1_HEADER.test(pem) ? importPkcs1(pem) : importSpki(pem);
|
|
109
114
|
}
|
|
115
|
+
function decodeEd25519DidKeyMultibase(multibaseKey) {
|
|
116
|
+
if (!multibaseKey.startsWith("z")) throw new TypeError("did:key must use base58-btc Multibase encoding.");
|
|
117
|
+
let decoded;
|
|
118
|
+
try {
|
|
119
|
+
decoded = require_multibase.decodeMultibase(multibaseKey);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
throw new TypeError("Invalid did:key Multibase encoding.", { cause: error });
|
|
122
|
+
}
|
|
123
|
+
const { code } = require_multicodec.getMulticodecPrefix(decoded);
|
|
124
|
+
if (code !== ED25519_PUBLIC_KEY_MULTICODEC) throw new TypeError("Unsupported did:key type: 0x" + code.toString(16));
|
|
125
|
+
const content = require_multicodec.removeMulticodecPrefix(decoded);
|
|
126
|
+
if (content.length !== ED25519_PUBLIC_KEY_LENGTH) throw new TypeError("Invalid Ed25519 did:key length.");
|
|
127
|
+
return content;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Imports an Ed25519 `did:key` DID.
|
|
131
|
+
*
|
|
132
|
+
* @param did The `did:key` DID.
|
|
133
|
+
* @returns The imported Ed25519 public key.
|
|
134
|
+
* @throws {TypeError} If the DID is malformed or uses an unsupported key type.
|
|
135
|
+
* @since 2.4.0
|
|
136
|
+
*/
|
|
137
|
+
async function importDidKey(did) {
|
|
138
|
+
const match = (did instanceof URL ? did.href : did).match(DID_KEY_PATTERN);
|
|
139
|
+
if (match == null) throw new TypeError("Invalid did:key DID.");
|
|
140
|
+
const content = decodeEd25519DidKeyMultibase(match[1]);
|
|
141
|
+
return await crypto.subtle.importKey("raw", content.slice(), "Ed25519", true, ["verify"]);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Exports an Ed25519 public key as a `did:key` DID.
|
|
145
|
+
*
|
|
146
|
+
* @param key The Ed25519 public key.
|
|
147
|
+
* @returns The `did:key` DID.
|
|
148
|
+
* @throws {TypeError} If the key is invalid or unsupported.
|
|
149
|
+
* @since 2.4.0
|
|
150
|
+
*/
|
|
151
|
+
async function exportDidKey(key) {
|
|
152
|
+
if (key.algorithm.name !== "Ed25519") throw new TypeError("Unsupported key type: " + JSON.stringify(key.algorithm));
|
|
153
|
+
return DID_KEY_PREFIX + await exportMultibaseKey(key);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Parses an Ed25519 `did:key` verification method DID URL.
|
|
157
|
+
*
|
|
158
|
+
* @param didUrl The `did:key` DID URL.
|
|
159
|
+
* @returns The parsed verification method.
|
|
160
|
+
* @throws {TypeError} If the DID URL is malformed, unsupported, or its
|
|
161
|
+
* fragment does not identify the same key as the DID.
|
|
162
|
+
* @since 2.4.0
|
|
163
|
+
*/
|
|
164
|
+
async function parseDidKeyVerificationMethod(didUrl) {
|
|
165
|
+
const didUrlString = didUrl instanceof URL ? didUrl.href : didUrl;
|
|
166
|
+
const match = didUrlString.match(DID_KEY_VERIFICATION_METHOD_PATTERN);
|
|
167
|
+
if (match == null) throw new TypeError("Invalid did:key verification method.");
|
|
168
|
+
const [, publicKeyMultibase, fragment] = match;
|
|
169
|
+
if (publicKeyMultibase !== fragment) throw new TypeError("Invalid did:key verification method fragment.");
|
|
170
|
+
const publicKey = await importDidKey(DID_KEY_PREFIX + publicKeyMultibase);
|
|
171
|
+
return {
|
|
172
|
+
id: new URL(didUrlString),
|
|
173
|
+
controller: new URL(DID_KEY_PREFIX + publicKeyMultibase),
|
|
174
|
+
publicKeyMultibase,
|
|
175
|
+
publicKey
|
|
176
|
+
};
|
|
177
|
+
}
|
|
110
178
|
/**
|
|
111
179
|
* Imports a [Multibase]-encoded public key.
|
|
112
180
|
*
|
|
@@ -170,6 +238,12 @@ async function exportMultibaseKey(key) {
|
|
|
170
238
|
return new TextDecoder().decode(encoded);
|
|
171
239
|
}
|
|
172
240
|
//#endregion
|
|
241
|
+
Object.defineProperty(exports, "exportDidKey", {
|
|
242
|
+
enumerable: true,
|
|
243
|
+
get: function() {
|
|
244
|
+
return exportDidKey;
|
|
245
|
+
}
|
|
246
|
+
});
|
|
173
247
|
Object.defineProperty(exports, "exportJwk", {
|
|
174
248
|
enumerable: true,
|
|
175
249
|
get: function() {
|
|
@@ -188,6 +262,12 @@ Object.defineProperty(exports, "exportSpki", {
|
|
|
188
262
|
return exportSpki;
|
|
189
263
|
}
|
|
190
264
|
});
|
|
265
|
+
Object.defineProperty(exports, "importDidKey", {
|
|
266
|
+
enumerable: true,
|
|
267
|
+
get: function() {
|
|
268
|
+
return importDidKey;
|
|
269
|
+
}
|
|
270
|
+
});
|
|
191
271
|
Object.defineProperty(exports, "importJwk", {
|
|
192
272
|
enumerable: true,
|
|
193
273
|
get: function() {
|
|
@@ -218,3 +298,9 @@ Object.defineProperty(exports, "importSpki", {
|
|
|
218
298
|
return importSpki;
|
|
219
299
|
}
|
|
220
300
|
});
|
|
301
|
+
Object.defineProperty(exports, "parseDidKeyVerificationMethod", {
|
|
302
|
+
enumerable: true,
|
|
303
|
+
get: function() {
|
|
304
|
+
return parseDidKeyVerificationMethod;
|
|
305
|
+
}
|
|
306
|
+
});
|
package/dist/tests/key.test.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
require("./chunk-C2EiDwsr.cjs");
|
|
2
|
-
const
|
|
2
|
+
const require_multicodec = require("./multicodec-CxGVGa91.cjs");
|
|
3
|
+
const require_key = require("./key-_wXwomh_.cjs");
|
|
3
4
|
const require_multibase = require("./multibase-Bz_UUDtL.cjs");
|
|
4
5
|
let node_assert = require("node:assert");
|
|
5
6
|
let node_test = require("node:test");
|
|
@@ -71,4 +72,50 @@ const ed25519Multibase = "z6MksHj1MJnidCtDiyYW9ugNFftoX9fLK4bornTxmMZ6X7vq";
|
|
|
71
72
|
x: "Lm_M42cB3HkUiODQsXRcweM6TByfzEHGO9ND274JcOY"
|
|
72
73
|
}, "public")), "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK");
|
|
73
74
|
});
|
|
75
|
+
(0, node_test.test)("exportDidKey() and importDidKey()", async () => {
|
|
76
|
+
const did = await require_key.exportDidKey(await require_key.importJwk(ed25519Jwk, "public"));
|
|
77
|
+
(0, node_assert.deepStrictEqual)(did, `did:key:${ed25519Multibase}`);
|
|
78
|
+
(0, node_assert.deepStrictEqual)(await require_key.exportJwk(await require_key.importDidKey(did)), ed25519Jwk);
|
|
79
|
+
(0, node_assert.deepStrictEqual)(await require_key.exportJwk(await require_key.importDidKey(new URL(did))), ed25519Jwk);
|
|
80
|
+
const rsaKey = await require_key.importJwk(rsaJwk, "public");
|
|
81
|
+
await (0, node_assert.rejects)(() => require_key.exportDidKey(rsaKey), TypeError);
|
|
82
|
+
await (0, node_assert.rejects)(() => require_key.importDidKey(`did:key:${rsaMultibase}`), /* @__PURE__ */ new TypeError("Unsupported did:key type: 0x1205"));
|
|
83
|
+
});
|
|
84
|
+
(0, node_test.test)("parseDidKeyVerificationMethod()", async () => {
|
|
85
|
+
const verificationMethod = `did:key:${ed25519Multibase}#${ed25519Multibase}`;
|
|
86
|
+
const parsed = await require_key.parseDidKeyVerificationMethod(verificationMethod);
|
|
87
|
+
(0, node_assert.deepStrictEqual)(parsed.id, new URL(verificationMethod));
|
|
88
|
+
(0, node_assert.deepStrictEqual)(parsed.controller, new URL(`did:key:${ed25519Multibase}`));
|
|
89
|
+
(0, node_assert.deepStrictEqual)(parsed.publicKeyMultibase, ed25519Multibase);
|
|
90
|
+
(0, node_assert.deepStrictEqual)(await require_key.exportJwk(parsed.publicKey), ed25519Jwk);
|
|
91
|
+
});
|
|
92
|
+
(0, node_test.test)("did:key helpers reject malformed values", async () => {
|
|
93
|
+
const ed25519Key = await require_key.importJwk(ed25519Jwk, "public");
|
|
94
|
+
const raw = new Uint8Array(await crypto.subtle.exportKey("raw", ed25519Key));
|
|
95
|
+
const shortEd25519 = new TextDecoder().decode(require_multibase.encodeMultibase("base58btc", require_multicodec.addMulticodecPrefix(237, raw.slice(0, 31))));
|
|
96
|
+
const base64UrlEd25519 = new TextDecoder().decode(require_multibase.encodeMultibase("base64url", require_multicodec.addMulticodecPrefix(237, raw)));
|
|
97
|
+
const malformedMulticodec = new TextDecoder().decode(require_multibase.encodeMultibase("base58btc", Uint8Array.from([128])));
|
|
98
|
+
for (const value of [
|
|
99
|
+
"did:web:example.com",
|
|
100
|
+
"did:key:",
|
|
101
|
+
`did:key:${ed25519Multibase}/path`,
|
|
102
|
+
`did:key:${ed25519Multibase}?query`,
|
|
103
|
+
`did:key:${ed25519Multibase}#${ed25519Multibase}`,
|
|
104
|
+
"did:key:z0",
|
|
105
|
+
`did:key:${base64UrlEd25519}`,
|
|
106
|
+
`did:key:${shortEd25519}`,
|
|
107
|
+
`did:key:${malformedMulticodec}`
|
|
108
|
+
]) await (0, node_assert.rejects)(() => require_key.importDidKey(value), TypeError);
|
|
109
|
+
for (const value of [
|
|
110
|
+
`did:key:${ed25519Multibase}`,
|
|
111
|
+
`did:key:${ed25519Multibase}#`,
|
|
112
|
+
`did:key:${ed25519Multibase}#z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK`,
|
|
113
|
+
`did:key:${ed25519Multibase}#${base64UrlEd25519}`,
|
|
114
|
+
"did:key:z0#z0",
|
|
115
|
+
`did:key:${rsaMultibase}#${rsaMultibase}`,
|
|
116
|
+
`did:key:${shortEd25519}#${shortEd25519}`,
|
|
117
|
+
`did:key:${malformedMulticodec}#${malformedMulticodec}`,
|
|
118
|
+
"https://example.com/key"
|
|
119
|
+
]) await (0, node_assert.rejects)(() => require_key.parseDidKeyVerificationMethod(value), TypeError);
|
|
120
|
+
});
|
|
74
121
|
//#endregion
|
package/dist/tests/key.test.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as addMulticodecPrefix } from "./multicodec-CyFp54fI.mjs";
|
|
2
|
+
import { a as importMultibaseKey, c as importSpki, d as importJwk, i as importDidKey, l as parseDidKeyVerificationMethod, n as exportMultibaseKey, o as importPem, r as exportSpki, s as importPkcs1, t as exportDidKey, u as exportJwk } from "./key-CDGDH_vC.mjs";
|
|
2
3
|
import { n as encodeMultibase } from "./multibase-B4bvakyA.mjs";
|
|
3
4
|
import { deepStrictEqual, rejects } from "node:assert";
|
|
4
5
|
import { test } from "node:test";
|
|
@@ -70,5 +71,51 @@ test("exportMultibaseKey()", async () => {
|
|
|
70
71
|
x: "Lm_M42cB3HkUiODQsXRcweM6TByfzEHGO9ND274JcOY"
|
|
71
72
|
}, "public")), "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK");
|
|
72
73
|
});
|
|
74
|
+
test("exportDidKey() and importDidKey()", async () => {
|
|
75
|
+
const did = await exportDidKey(await importJwk(ed25519Jwk, "public"));
|
|
76
|
+
deepStrictEqual(did, `did:key:${ed25519Multibase}`);
|
|
77
|
+
deepStrictEqual(await exportJwk(await importDidKey(did)), ed25519Jwk);
|
|
78
|
+
deepStrictEqual(await exportJwk(await importDidKey(new URL(did))), ed25519Jwk);
|
|
79
|
+
const rsaKey = await importJwk(rsaJwk, "public");
|
|
80
|
+
await rejects(() => exportDidKey(rsaKey), TypeError);
|
|
81
|
+
await rejects(() => importDidKey(`did:key:${rsaMultibase}`), /* @__PURE__ */ new TypeError("Unsupported did:key type: 0x1205"));
|
|
82
|
+
});
|
|
83
|
+
test("parseDidKeyVerificationMethod()", async () => {
|
|
84
|
+
const verificationMethod = `did:key:${ed25519Multibase}#${ed25519Multibase}`;
|
|
85
|
+
const parsed = await parseDidKeyVerificationMethod(verificationMethod);
|
|
86
|
+
deepStrictEqual(parsed.id, new URL(verificationMethod));
|
|
87
|
+
deepStrictEqual(parsed.controller, new URL(`did:key:${ed25519Multibase}`));
|
|
88
|
+
deepStrictEqual(parsed.publicKeyMultibase, ed25519Multibase);
|
|
89
|
+
deepStrictEqual(await exportJwk(parsed.publicKey), ed25519Jwk);
|
|
90
|
+
});
|
|
91
|
+
test("did:key helpers reject malformed values", async () => {
|
|
92
|
+
const ed25519Key = await importJwk(ed25519Jwk, "public");
|
|
93
|
+
const raw = new Uint8Array(await crypto.subtle.exportKey("raw", ed25519Key));
|
|
94
|
+
const shortEd25519 = new TextDecoder().decode(encodeMultibase("base58btc", addMulticodecPrefix(237, raw.slice(0, 31))));
|
|
95
|
+
const base64UrlEd25519 = new TextDecoder().decode(encodeMultibase("base64url", addMulticodecPrefix(237, raw)));
|
|
96
|
+
const malformedMulticodec = new TextDecoder().decode(encodeMultibase("base58btc", Uint8Array.from([128])));
|
|
97
|
+
for (const value of [
|
|
98
|
+
"did:web:example.com",
|
|
99
|
+
"did:key:",
|
|
100
|
+
`did:key:${ed25519Multibase}/path`,
|
|
101
|
+
`did:key:${ed25519Multibase}?query`,
|
|
102
|
+
`did:key:${ed25519Multibase}#${ed25519Multibase}`,
|
|
103
|
+
"did:key:z0",
|
|
104
|
+
`did:key:${base64UrlEd25519}`,
|
|
105
|
+
`did:key:${shortEd25519}`,
|
|
106
|
+
`did:key:${malformedMulticodec}`
|
|
107
|
+
]) await rejects(() => importDidKey(value), TypeError);
|
|
108
|
+
for (const value of [
|
|
109
|
+
`did:key:${ed25519Multibase}`,
|
|
110
|
+
`did:key:${ed25519Multibase}#`,
|
|
111
|
+
`did:key:${ed25519Multibase}#z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK`,
|
|
112
|
+
`did:key:${ed25519Multibase}#${base64UrlEd25519}`,
|
|
113
|
+
"did:key:z0#z0",
|
|
114
|
+
`did:key:${rsaMultibase}#${rsaMultibase}`,
|
|
115
|
+
`did:key:${shortEd25519}#${shortEd25519}`,
|
|
116
|
+
`did:key:${malformedMulticodec}#${malformedMulticodec}`,
|
|
117
|
+
"https://example.com/key"
|
|
118
|
+
]) await rejects(() => parseDidKeyVerificationMethod(value), TypeError);
|
|
119
|
+
});
|
|
73
120
|
//#endregion
|
|
74
121
|
export {};
|
|
@@ -3,7 +3,7 @@ let node_process = require("node:process");
|
|
|
3
3
|
node_process = require_chunk.__toESM(node_process, 1);
|
|
4
4
|
//#region deno.json
|
|
5
5
|
var name = "@fedify/vocab-runtime";
|
|
6
|
-
var version = "2.4.0-dev.
|
|
6
|
+
var version = "2.4.0-dev.1567+12e6a3c0";
|
|
7
7
|
//#endregion
|
|
8
8
|
//#region src/request.ts
|
|
9
9
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const require_chunk = require("./chunk-C2EiDwsr.cjs");
|
|
2
|
-
const require_request = require("./request-
|
|
2
|
+
const require_request = require("./request-DnNWah97.cjs");
|
|
3
3
|
let node_assert = require("node:assert");
|
|
4
4
|
let node_test = require("node:test");
|
|
5
5
|
let node_process = require("node:process");
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { o as version, r as getUserAgent } from "./request-
|
|
1
|
+
import { o as version, r as getUserAgent } from "./request-Cn0vP7Hj.mjs";
|
|
2
2
|
import { deepStrictEqual } from "node:assert";
|
|
3
3
|
import { test } from "node:test";
|
|
4
4
|
import process from "node:process";
|
package/package.json
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"@context": {
|
|
3
|
+
"FeaturedCollection": "https://w3id.org/fep/7aa9#FeaturedCollection",
|
|
4
|
+
"FeaturedItem": "https://w3id.org/fep/7aa9#FeaturedItem",
|
|
5
|
+
"FeatureRequest": "https://w3id.org/fep/7aa9#FeatureRequest",
|
|
6
|
+
"FeatureAuthorization": "https://w3id.org/fep/7aa9#FeatureAuthorization",
|
|
7
|
+
"topic": {
|
|
8
|
+
"@id": "https://w3id.org/fep/7aa9#topic",
|
|
9
|
+
"@type": "@id"
|
|
10
|
+
},
|
|
11
|
+
"featuredObject": {
|
|
12
|
+
"@id": "https://w3id.org/fep/7aa9#featuredObject",
|
|
13
|
+
"@type": "@id"
|
|
14
|
+
},
|
|
15
|
+
"canFeature": {
|
|
16
|
+
"@id": "https://w3id.org/fep/7aa9#canFeature",
|
|
17
|
+
"@type": "@id"
|
|
18
|
+
},
|
|
19
|
+
"featureAuthorization": {
|
|
20
|
+
"@id": "https://w3id.org/fep/7aa9#featureAuthorization",
|
|
21
|
+
"@type": "@id"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/contexts.ts
CHANGED
|
@@ -7,6 +7,7 @@ import activitystreams from "./contexts/activitystreams.json" with {
|
|
|
7
7
|
};
|
|
8
8
|
import didV1 from "./contexts/did-v1.json" with { type: "json" };
|
|
9
9
|
import fep5711 from "./contexts/fep-5711.json" with { type: "json" };
|
|
10
|
+
import fep7aa9 from "./contexts/fep-7aa9.json" with { type: "json" };
|
|
10
11
|
import gotosocial from "./contexts/gotosocial.json" with { type: "json" };
|
|
11
12
|
import identityV1 from "./contexts/identity-v1.json" with { type: "json" };
|
|
12
13
|
import joinLemmyContext from "./contexts/join-lemmy.json" with { type: "json" };
|
|
@@ -35,6 +36,7 @@ const preloadedContexts: Record<string, unknown> = {
|
|
|
35
36
|
"http://schema.org/": schemaorg,
|
|
36
37
|
"https://gotosocial.org/ns": gotosocial,
|
|
37
38
|
"https://w3id.org/fep/5711": fep5711,
|
|
39
|
+
"https://w3id.org/fep/7aa9": fep7aa9,
|
|
38
40
|
|
|
39
41
|
// Lemmy's context document is served as application/json without the JSON-LD
|
|
40
42
|
// context Link header. The default document loader treats that as a regular
|
package/src/key.test.ts
CHANGED
|
@@ -2,13 +2,17 @@ import { deepStrictEqual, rejects } from "node:assert";
|
|
|
2
2
|
import { test } from "node:test";
|
|
3
3
|
import { exportJwk, importJwk } from "./jwk.ts";
|
|
4
4
|
import {
|
|
5
|
+
exportDidKey,
|
|
5
6
|
exportMultibaseKey,
|
|
6
7
|
exportSpki,
|
|
8
|
+
importDidKey,
|
|
7
9
|
importMultibaseKey,
|
|
8
10
|
importPem,
|
|
9
11
|
importPkcs1,
|
|
10
12
|
importSpki,
|
|
13
|
+
parseDidKeyVerificationMethod,
|
|
11
14
|
} from "./key.ts";
|
|
15
|
+
import { addMulticodecPrefix } from "./internal/multicodec.ts";
|
|
12
16
|
import { encodeMultibase } from "./multibase/mod.ts";
|
|
13
17
|
|
|
14
18
|
// cSpell: disable
|
|
@@ -198,3 +202,85 @@ test("exportMultibaseKey()", async () => {
|
|
|
198
202
|
// cSpell: enable
|
|
199
203
|
);
|
|
200
204
|
});
|
|
205
|
+
|
|
206
|
+
test("exportDidKey() and importDidKey()", async () => {
|
|
207
|
+
const ed25519Key = await importJwk(ed25519Jwk, "public");
|
|
208
|
+
const did = await exportDidKey(ed25519Key);
|
|
209
|
+
deepStrictEqual(did, `did:key:${ed25519Multibase}`);
|
|
210
|
+
deepStrictEqual(await exportJwk(await importDidKey(did)), ed25519Jwk);
|
|
211
|
+
deepStrictEqual(
|
|
212
|
+
await exportJwk(await importDidKey(new URL(did))),
|
|
213
|
+
ed25519Jwk,
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
const rsaKey = await importJwk(rsaJwk, "public");
|
|
217
|
+
await rejects(
|
|
218
|
+
() => exportDidKey(rsaKey),
|
|
219
|
+
TypeError,
|
|
220
|
+
);
|
|
221
|
+
await rejects(
|
|
222
|
+
() => importDidKey(`did:key:${rsaMultibase}`),
|
|
223
|
+
new TypeError("Unsupported did:key type: 0x1205"),
|
|
224
|
+
);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test("parseDidKeyVerificationMethod()", async () => {
|
|
228
|
+
const verificationMethod = `did:key:${ed25519Multibase}#${ed25519Multibase}`;
|
|
229
|
+
const parsed = await parseDidKeyVerificationMethod(verificationMethod);
|
|
230
|
+
deepStrictEqual(parsed.id, new URL(verificationMethod));
|
|
231
|
+
deepStrictEqual(parsed.controller, new URL(`did:key:${ed25519Multibase}`));
|
|
232
|
+
deepStrictEqual(parsed.publicKeyMultibase, ed25519Multibase);
|
|
233
|
+
deepStrictEqual(await exportJwk(parsed.publicKey), ed25519Jwk);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test("did:key helpers reject malformed values", async () => {
|
|
237
|
+
const ed25519Key = await importJwk(ed25519Jwk, "public");
|
|
238
|
+
const raw = new Uint8Array(await crypto.subtle.exportKey("raw", ed25519Key));
|
|
239
|
+
const shortEd25519 = new TextDecoder().decode(
|
|
240
|
+
encodeMultibase(
|
|
241
|
+
"base58btc",
|
|
242
|
+
addMulticodecPrefix(0xed, raw.slice(0, 31)),
|
|
243
|
+
),
|
|
244
|
+
);
|
|
245
|
+
const base64UrlEd25519 = new TextDecoder().decode(
|
|
246
|
+
encodeMultibase(
|
|
247
|
+
"base64url",
|
|
248
|
+
addMulticodecPrefix(0xed, raw),
|
|
249
|
+
),
|
|
250
|
+
);
|
|
251
|
+
const malformedMulticodec = new TextDecoder().decode(
|
|
252
|
+
encodeMultibase("base58btc", Uint8Array.from([0x80])),
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
for (
|
|
256
|
+
const value of [
|
|
257
|
+
"did:web:example.com",
|
|
258
|
+
"did:key:",
|
|
259
|
+
`did:key:${ed25519Multibase}/path`,
|
|
260
|
+
`did:key:${ed25519Multibase}?query`,
|
|
261
|
+
`did:key:${ed25519Multibase}#${ed25519Multibase}`,
|
|
262
|
+
"did:key:z0",
|
|
263
|
+
`did:key:${base64UrlEd25519}`,
|
|
264
|
+
`did:key:${shortEd25519}`,
|
|
265
|
+
`did:key:${malformedMulticodec}`,
|
|
266
|
+
]
|
|
267
|
+
) {
|
|
268
|
+
await rejects(() => importDidKey(value), TypeError);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
for (
|
|
272
|
+
const value of [
|
|
273
|
+
`did:key:${ed25519Multibase}`,
|
|
274
|
+
`did:key:${ed25519Multibase}#`,
|
|
275
|
+
`did:key:${ed25519Multibase}#z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK`,
|
|
276
|
+
`did:key:${ed25519Multibase}#${base64UrlEd25519}`,
|
|
277
|
+
"did:key:z0#z0",
|
|
278
|
+
`did:key:${rsaMultibase}#${rsaMultibase}`,
|
|
279
|
+
`did:key:${shortEd25519}#${shortEd25519}`,
|
|
280
|
+
`did:key:${malformedMulticodec}#${malformedMulticodec}`,
|
|
281
|
+
"https://example.com/key",
|
|
282
|
+
]
|
|
283
|
+
) {
|
|
284
|
+
await rejects(() => parseDidKeyVerificationMethod(value), TypeError);
|
|
285
|
+
}
|
|
286
|
+
});
|
package/src/key.ts
CHANGED
|
@@ -22,6 +22,39 @@ const algorithms: Record<
|
|
|
22
22
|
"1.3.101.112": "Ed25519",
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
+
const DID_KEY_PREFIX = "did:key:";
|
|
26
|
+
const ED25519_PUBLIC_KEY_MULTICODEC = 0xed;
|
|
27
|
+
const ED25519_PUBLIC_KEY_LENGTH = 32;
|
|
28
|
+
const DID_KEY_PATTERN = /^did:key:([^/?#]+)$/;
|
|
29
|
+
const DID_KEY_VERIFICATION_METHOD_PATTERN = /^did:key:([^/?#]+)#([^/?#]+)$/;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Parsed `did:key` verification method.
|
|
33
|
+
*
|
|
34
|
+
* @since 2.4.0
|
|
35
|
+
*/
|
|
36
|
+
export interface DidKeyVerificationMethod {
|
|
37
|
+
/**
|
|
38
|
+
* The DID URL identifying the verification method.
|
|
39
|
+
*/
|
|
40
|
+
readonly id: URL;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The controller DID.
|
|
44
|
+
*/
|
|
45
|
+
readonly controller: URL;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The Ed25519 public key encoded as a Multibase Multikey value.
|
|
49
|
+
*/
|
|
50
|
+
readonly publicKeyMultibase: string;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The Ed25519 public key.
|
|
54
|
+
*/
|
|
55
|
+
readonly publicKey: CryptoKey;
|
|
56
|
+
}
|
|
57
|
+
|
|
25
58
|
/**
|
|
26
59
|
* Imports a PEM-SPKI formatted public key.
|
|
27
60
|
* @param pem The PEM-SPKI formatted public key.
|
|
@@ -93,6 +126,98 @@ export function importPem(pem: string): Promise<CryptoKey> {
|
|
|
93
126
|
return PKCS1_HEADER.test(pem) ? importPkcs1(pem) : importSpki(pem);
|
|
94
127
|
}
|
|
95
128
|
|
|
129
|
+
function decodeEd25519DidKeyMultibase(multibaseKey: string): Uint8Array {
|
|
130
|
+
if (!multibaseKey.startsWith("z")) {
|
|
131
|
+
throw new TypeError("did:key must use base58-btc Multibase encoding.");
|
|
132
|
+
}
|
|
133
|
+
let decoded: Uint8Array;
|
|
134
|
+
try {
|
|
135
|
+
decoded = decodeMultibase(multibaseKey);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
throw new TypeError("Invalid did:key Multibase encoding.", {
|
|
138
|
+
cause: error,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
const { code } = getMulticodecPrefix(decoded);
|
|
142
|
+
if (code !== ED25519_PUBLIC_KEY_MULTICODEC) {
|
|
143
|
+
throw new TypeError("Unsupported did:key type: 0x" + code.toString(16));
|
|
144
|
+
}
|
|
145
|
+
const content = removeMulticodecPrefix(decoded);
|
|
146
|
+
if (content.length !== ED25519_PUBLIC_KEY_LENGTH) {
|
|
147
|
+
throw new TypeError("Invalid Ed25519 did:key length.");
|
|
148
|
+
}
|
|
149
|
+
return content;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Imports an Ed25519 `did:key` DID.
|
|
154
|
+
*
|
|
155
|
+
* @param did The `did:key` DID.
|
|
156
|
+
* @returns The imported Ed25519 public key.
|
|
157
|
+
* @throws {TypeError} If the DID is malformed or uses an unsupported key type.
|
|
158
|
+
* @since 2.4.0
|
|
159
|
+
*/
|
|
160
|
+
export async function importDidKey(did: string | URL): Promise<CryptoKey> {
|
|
161
|
+
const didString = did instanceof URL ? did.href : did;
|
|
162
|
+
const match = didString.match(DID_KEY_PATTERN);
|
|
163
|
+
if (match == null) throw new TypeError("Invalid did:key DID.");
|
|
164
|
+
const content = decodeEd25519DidKeyMultibase(match[1]);
|
|
165
|
+
return await crypto.subtle.importKey(
|
|
166
|
+
"raw",
|
|
167
|
+
content.slice(),
|
|
168
|
+
"Ed25519",
|
|
169
|
+
true,
|
|
170
|
+
["verify"],
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Exports an Ed25519 public key as a `did:key` DID.
|
|
176
|
+
*
|
|
177
|
+
* @param key The Ed25519 public key.
|
|
178
|
+
* @returns The `did:key` DID.
|
|
179
|
+
* @throws {TypeError} If the key is invalid or unsupported.
|
|
180
|
+
* @since 2.4.0
|
|
181
|
+
*/
|
|
182
|
+
export async function exportDidKey(key: CryptoKey): Promise<string> {
|
|
183
|
+
if (key.algorithm.name !== "Ed25519") {
|
|
184
|
+
throw new TypeError(
|
|
185
|
+
"Unsupported key type: " + JSON.stringify(key.algorithm),
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
return DID_KEY_PREFIX + await exportMultibaseKey(key);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Parses an Ed25519 `did:key` verification method DID URL.
|
|
193
|
+
*
|
|
194
|
+
* @param didUrl The `did:key` DID URL.
|
|
195
|
+
* @returns The parsed verification method.
|
|
196
|
+
* @throws {TypeError} If the DID URL is malformed, unsupported, or its
|
|
197
|
+
* fragment does not identify the same key as the DID.
|
|
198
|
+
* @since 2.4.0
|
|
199
|
+
*/
|
|
200
|
+
export async function parseDidKeyVerificationMethod(
|
|
201
|
+
didUrl: string | URL,
|
|
202
|
+
): Promise<DidKeyVerificationMethod> {
|
|
203
|
+
const didUrlString = didUrl instanceof URL ? didUrl.href : didUrl;
|
|
204
|
+
const match = didUrlString.match(DID_KEY_VERIFICATION_METHOD_PATTERN);
|
|
205
|
+
if (match == null) {
|
|
206
|
+
throw new TypeError("Invalid did:key verification method.");
|
|
207
|
+
}
|
|
208
|
+
const [, publicKeyMultibase, fragment] = match;
|
|
209
|
+
if (publicKeyMultibase !== fragment) {
|
|
210
|
+
throw new TypeError("Invalid did:key verification method fragment.");
|
|
211
|
+
}
|
|
212
|
+
const publicKey = await importDidKey(DID_KEY_PREFIX + publicKeyMultibase);
|
|
213
|
+
return {
|
|
214
|
+
id: new URL(didUrlString),
|
|
215
|
+
controller: new URL(DID_KEY_PREFIX + publicKeyMultibase),
|
|
216
|
+
publicKeyMultibase,
|
|
217
|
+
publicKey,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
96
221
|
/**
|
|
97
222
|
* Imports a [Multibase]-encoded public key.
|
|
98
223
|
*
|
package/src/mod.ts
CHANGED
|
@@ -17,12 +17,16 @@ export {
|
|
|
17
17
|
type RemoteDocument,
|
|
18
18
|
} from "./docloader.ts";
|
|
19
19
|
export {
|
|
20
|
+
type DidKeyVerificationMethod,
|
|
21
|
+
exportDidKey,
|
|
20
22
|
exportMultibaseKey,
|
|
21
23
|
exportSpki,
|
|
24
|
+
importDidKey,
|
|
22
25
|
importMultibaseKey,
|
|
23
26
|
importPem,
|
|
24
27
|
importPkcs1,
|
|
25
28
|
importSpki,
|
|
29
|
+
parseDidKeyVerificationMethod,
|
|
26
30
|
} from "./key.ts";
|
|
27
31
|
export {
|
|
28
32
|
canParseDecimal,
|