@fedify/vocab-runtime 2.0.0-dev.12 → 2.0.0-dev.158

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 (43) hide show
  1. package/LICENSE +1 -1
  2. package/deno.json +7 -1
  3. package/dist/chunk-DWy1uDak.cjs +39 -0
  4. package/dist/docloader.test.cjs +5851 -0
  5. package/dist/docloader.test.d.cts +1 -0
  6. package/dist/docloader.test.d.ts +1 -0
  7. package/dist/docloader.test.js +5877 -0
  8. package/dist/key.test.cjs +272 -0
  9. package/dist/key.test.d.cts +1 -0
  10. package/dist/key.test.d.ts +1 -0
  11. package/dist/key.test.js +271 -0
  12. package/dist/langstr.test.cjs +51 -0
  13. package/dist/langstr.test.d.cts +1 -0
  14. package/dist/langstr.test.d.ts +1 -0
  15. package/dist/langstr.test.js +50 -0
  16. package/dist/link-CdFPEo9O.cjs +189 -0
  17. package/dist/link-Ck2yj4dH.js +183 -0
  18. package/dist/link.test.cjs +56 -0
  19. package/dist/link.test.d.cts +1 -0
  20. package/dist/link.test.d.ts +1 -0
  21. package/dist/link.test.js +55 -0
  22. package/dist/mod.cjs +4 -1
  23. package/dist/mod.js +4 -1
  24. package/dist/multibase/multibase.test.cjs +346 -0
  25. package/dist/multibase/multibase.test.d.cts +1 -0
  26. package/dist/multibase/multibase.test.d.ts +1 -0
  27. package/dist/multibase/multibase.test.js +345 -0
  28. package/dist/multibase-BFbBiaPE.cjs +347 -0
  29. package/dist/multibase-DStmqni9.js +311 -0
  30. package/dist/request-BMn0A6Ak.cjs +138 -0
  31. package/dist/request-BYgiXEDJ.js +108 -0
  32. package/dist/request.test.cjs +44 -0
  33. package/dist/request.test.d.cts +1 -0
  34. package/dist/request.test.d.ts +1 -0
  35. package/dist/request.test.js +43 -0
  36. package/dist/url-C5Vs9nYh.cjs +93 -0
  37. package/dist/url-fW_DHbih.js +63 -0
  38. package/dist/url.test.cjs +37 -0
  39. package/dist/url.test.d.cts +1 -0
  40. package/dist/url.test.d.ts +1 -0
  41. package/dist/url.test.js +36 -0
  42. package/package.json +3 -3
  43. package/tsdown.config.ts +18 -7
@@ -0,0 +1,272 @@
1
+ const require_chunk = require('./chunk-DWy1uDak.cjs');
2
+ const require_multibase = require('./multibase-BFbBiaPE.cjs');
3
+ const node_assert = require_chunk.__toESM(require("node:assert"));
4
+ const node_test = require_chunk.__toESM(require("node:test"));
5
+ const asn1js = require_chunk.__toESM(require("asn1js"));
6
+ const byte_encodings_base64 = require_chunk.__toESM(require("byte-encodings/base64"));
7
+ const byte_encodings_base64url = require_chunk.__toESM(require("byte-encodings/base64url"));
8
+ const byte_encodings_hex = require_chunk.__toESM(require("byte-encodings/hex"));
9
+ const multicodec = require_chunk.__toESM(require("multicodec"));
10
+ const node_crypto = require_chunk.__toESM(require("node:crypto"));
11
+ const pkijs = require_chunk.__toESM(require("pkijs"));
12
+
13
+ //#region src/jwk.ts
14
+ function validateCryptoKey(key, type) {
15
+ if (type != null && key.type !== type) throw new TypeError(`The key is not a ${type} key.`);
16
+ if (!key.extractable) throw new TypeError("The key is not extractable.");
17
+ if (key.algorithm.name !== "RSASSA-PKCS1-v1_5" && key.algorithm.name !== "Ed25519") throw new TypeError("Currently only RSASSA-PKCS1-v1_5 and Ed25519 keys are supported. More algorithms will be added in the future!");
18
+ if (key.algorithm.name === "RSASSA-PKCS1-v1_5") {
19
+ const algorithm = key.algorithm;
20
+ if (algorithm.hash.name !== "SHA-256") throw new TypeError("For compatibility with the existing Fediverse software (e.g., Mastodon), hash algorithm for RSASSA-PKCS1-v1_5 keys must be SHA-256.");
21
+ }
22
+ }
23
+ async function exportJwk(key) {
24
+ validateCryptoKey(key);
25
+ const jwk = await crypto.subtle.exportKey("jwk", key);
26
+ if (jwk.crv === "Ed25519") jwk.alg = "Ed25519";
27
+ return jwk;
28
+ }
29
+ async function importJwk(jwk, type) {
30
+ let key;
31
+ if (jwk.kty === "RSA" && jwk.alg === "RS256") key = await crypto.subtle.importKey("jwk", jwk, {
32
+ name: "RSASSA-PKCS1-v1_5",
33
+ hash: "SHA-256"
34
+ }, true, type === "public" ? ["verify"] : ["sign"]);
35
+ else if (jwk.kty === "OKP" && jwk.crv === "Ed25519") {
36
+ if (navigator?.userAgent === "Cloudflare-Workers") {
37
+ jwk = { ...jwk };
38
+ delete jwk.alg;
39
+ }
40
+ key = await crypto.subtle.importKey("jwk", jwk, "Ed25519", true, type === "public" ? ["verify"] : ["sign"]);
41
+ } else throw new TypeError("Unsupported JWK format.");
42
+ validateCryptoKey(key, type);
43
+ return key;
44
+ }
45
+
46
+ //#endregion
47
+ //#region src/key.ts
48
+ const algorithms = {
49
+ "1.2.840.113549.1.1.1": {
50
+ name: "RSASSA-PKCS1-v1_5",
51
+ hash: "SHA-256"
52
+ },
53
+ "1.3.101.112": "Ed25519"
54
+ };
55
+ /**
56
+ * Imports a PEM-SPKI formatted public key.
57
+ * @param pem The PEM-SPKI formatted public key.
58
+ * @returns The imported public key.
59
+ * @throws {TypeError} If the key is invalid or unsupported.
60
+ * @since 0.5.0
61
+ */
62
+ async function importSpki(pem) {
63
+ pem = pem.replace(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, "");
64
+ let spki;
65
+ try {
66
+ spki = (0, byte_encodings_base64.decodeBase64)(pem);
67
+ } catch (_) {
68
+ throw new TypeError("Invalid PEM-SPKI format.");
69
+ }
70
+ const pki = pkijs.PublicKeyInfo.fromBER(spki);
71
+ const oid = pki.algorithm.algorithmId;
72
+ const algorithm = algorithms[oid];
73
+ if (algorithm == null) throw new TypeError("Unsupported algorithm: " + oid);
74
+ return await crypto.subtle.importKey("spki", spki, algorithm, true, ["verify"]);
75
+ }
76
+ /**
77
+ * Exports a public key in PEM-SPKI format.
78
+ * @param key The public key to export.
79
+ * @returns The exported public key in PEM-SPKI format.
80
+ * @throws {TypeError} If the key is invalid or unsupported.
81
+ * @since 0.5.0
82
+ */
83
+ async function exportSpki(key) {
84
+ validateCryptoKey(key);
85
+ const spki = await crypto.subtle.exportKey("spki", key);
86
+ let pem = (0, byte_encodings_base64.encodeBase64)(spki);
87
+ pem = (pem.match(/.{1,64}/g) || []).join("\n");
88
+ return `-----BEGIN PUBLIC KEY-----\n${pem}\n-----END PUBLIC KEY-----\n`;
89
+ }
90
+ /**
91
+ * Imports a PEM-PKCS#1 formatted public key.
92
+ * @param pem The PEM-PKCS#1 formatted public key.
93
+ * @returns The imported public key.
94
+ * @throws {TypeError} If the key is invalid or unsupported.
95
+ * @since 1.5.0
96
+ */
97
+ function importPkcs1(pem) {
98
+ const key = (0, node_crypto.createPublicKey)({
99
+ key: pem,
100
+ format: "pem",
101
+ type: "pkcs1"
102
+ });
103
+ const spki = key.export({
104
+ type: "spki",
105
+ format: "pem"
106
+ });
107
+ return importSpki(spki);
108
+ }
109
+ const PKCS1_HEADER = /^\s*-----BEGIN\s+RSA\s+PUBLIC\s+KEY-----\s*\n/;
110
+ /**
111
+ * Imports a PEM formatted public key (SPKI or PKCS#1).
112
+ * @param pem The PEM formatted public key to import (SPKI or PKCS#1).
113
+ * @returns The imported public key.
114
+ * @throws {TypeError} If the key is invalid or unsupported.
115
+ * @since 1.5.0
116
+ */
117
+ function importPem(pem) {
118
+ return PKCS1_HEADER.test(pem) ? importPkcs1(pem) : importSpki(pem);
119
+ }
120
+ /**
121
+ * Imports a [Multibase]-encoded public key.
122
+ *
123
+ * [Multibase]: https://www.w3.org/TR/vc-data-integrity/#multibase-0
124
+ * @param key The Multibase-encoded public key.
125
+ * @returns The imported public key.
126
+ * @throws {TypeError} If the key is invalid or unsupported.
127
+ * @since 0.10.0
128
+ */
129
+ async function importMultibaseKey(key) {
130
+ const decoded = require_multibase.decodeMultibase(key);
131
+ const code = (0, multicodec.getCodeFromData)(decoded);
132
+ const content = (0, multicodec.rmPrefix)(decoded);
133
+ if (code === 4613) {
134
+ const keyObject = (0, node_crypto.createPublicKey)({
135
+ key: content,
136
+ format: "der",
137
+ type: "pkcs1"
138
+ });
139
+ const exported = keyObject.export({
140
+ type: "spki",
141
+ format: "der"
142
+ });
143
+ const spki = exported instanceof Uint8Array ? exported : new Uint8Array(exported);
144
+ return await crypto.subtle.importKey("spki", new Uint8Array(spki), {
145
+ name: "RSASSA-PKCS1-v1_5",
146
+ hash: "SHA-256"
147
+ }, true, ["verify"]);
148
+ } else if (code === 237) return await crypto.subtle.importKey("raw", content.slice(), "Ed25519", true, ["verify"]);
149
+ else throw new TypeError("Unsupported key type: 0x" + code.toString(16));
150
+ }
151
+ /**
152
+ * Exports a public key in [Multibase] format.
153
+ *
154
+ * [Multibase]: https://www.w3.org/TR/vc-data-integrity/#multibase-0
155
+ * @param key The public key to export.
156
+ * @returns The exported public key in Multibase format.
157
+ * @throws {TypeError} If the key is invalid or unsupported.
158
+ * @since 0.10.0
159
+ */
160
+ async function exportMultibaseKey(key) {
161
+ let content;
162
+ let code;
163
+ if (key.algorithm.name === "Ed25519") {
164
+ content = await crypto.subtle.exportKey("raw", key);
165
+ code = 237;
166
+ } else if (key.algorithm.name === "RSASSA-PKCS1-v1_5" && key.algorithm.hash.name === "SHA-256") {
167
+ const jwk = await crypto.subtle.exportKey("jwk", key);
168
+ const decodedN = (0, byte_encodings_base64url.decodeBase64Url)(jwk.n);
169
+ const n = new Uint8Array(decodedN.length + 1);
170
+ n.set(decodedN, 1);
171
+ const sequence = new asn1js.Sequence({ value: [new asn1js.Integer({
172
+ isHexOnly: true,
173
+ valueHex: n
174
+ }), new asn1js.Integer({
175
+ isHexOnly: true,
176
+ valueHex: (0, byte_encodings_base64url.decodeBase64Url)(jwk.e)
177
+ })] });
178
+ content = sequence.toBER(false);
179
+ code = 4613;
180
+ } else throw new TypeError("Unsupported key type: " + JSON.stringify(key.algorithm));
181
+ const codeHex = code.toString(16);
182
+ const codeBytes = (0, byte_encodings_hex.decodeHex)(codeHex.length % 2 < 1 ? codeHex : "0" + codeHex);
183
+ const prefixed = (0, multicodec.addPrefix)(codeBytes, new Uint8Array(content));
184
+ const encoded = require_multibase.encodeMultibase("base58btc", prefixed);
185
+ return new TextDecoder().decode(encoded);
186
+ }
187
+
188
+ //#endregion
189
+ //#region src/key.test.ts
190
+ const rsaSpki = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxsRuvCkgJtflBTl4OVsm\nnt/J1mQfZasfJtN33dcZ3d1lJroxmgmMu69zjGEAwkNbMQaWNLqC4eogkJaeJ4RR\n5MHYXkL9nNilVoTkjX5BVit3puzs7XJ7WQnKQgQMI+ezn24GHsZ/v1JIo77lerX5\nk4HNwTNVt+yaZVQWaOMR3+6FwziQR6kd0VuG9/a9dgAnz2cEoORRC1i4W7IZaB1s\nZnh1WbHbevlGd72HSXll5rocPIHn8gq6xpBgpHwRphlRsgn4KHaJ6brXDIJjrnQh\nIe/YUBOGj/ImSEXhRwlFerKsoAVnZ0Hwbfa46qk44TAt8CyoPMWmpK6pt0ng4pQ2\nuwIDAQAB\n-----END PUBLIC KEY-----\n";
191
+ const rsaPkcs1 = "-----BEGIN RSA PUBLIC KEY-----\nMIIBCgKCAQEAxsRuvCkgJtflBTl4OVsmnt/J1mQfZasfJtN33dcZ3d1lJroxmgmM\nu69zjGEAwkNbMQaWNLqC4eogkJaeJ4RR5MHYXkL9nNilVoTkjX5BVit3puzs7XJ7\nWQnKQgQMI+ezn24GHsZ/v1JIo77lerX5k4HNwTNVt+yaZVQWaOMR3+6FwziQR6kd\n0VuG9/a9dgAnz2cEoORRC1i4W7IZaB1sZnh1WbHbevlGd72HSXll5rocPIHn8gq6\nxpBgpHwRphlRsgn4KHaJ6brXDIJjrnQhIe/YUBOGj/ImSEXhRwlFerKsoAVnZ0Hw\nbfa46qk44TAt8CyoPMWmpK6pt0ng4pQ2uwIDAQAB\n-----END RSA PUBLIC KEY-----\n";
192
+ const rsaJwk = {
193
+ alg: "RS256",
194
+ e: "AQAB",
195
+ ext: true,
196
+ key_ops: ["verify"],
197
+ kty: "RSA",
198
+ n: "xsRuvCkgJtflBTl4OVsmnt_J1mQfZasfJtN33dcZ3d1lJroxmgmMu69zjGEAwkNbMQaWNLqC4eogkJaeJ4RR5MHYXkL9nNilVoTkjX5BVit3puzs7XJ7WQnKQgQMI-ezn24GHsZ_v1JIo77lerX5k4HNwTNVt-yaZVQWaOMR3-6FwziQR6kd0VuG9_a9dgAnz2cEoORRC1i4W7IZaB1sZnh1WbHbevlGd72HSXll5rocPIHn8gq6xpBgpHwRphlRsgn4KHaJ6brXDIJjrnQhIe_YUBOGj_ImSEXhRwlFerKsoAVnZ0Hwbfa46qk44TAt8CyoPMWmpK6pt0ng4pQ2uw"
199
+ };
200
+ const rsaMultibase = "z4MXj1wBzi9jUstyPqYMn6Gum79JtbKFiHTibtPRoPeufjdimA24Kg8Q5N7E2eMpgVUtD61kUvmy4FaT5D5G8XU3ktxeduwEw5FHTtiLCzaruadf6rit1AUPL34UtcPuHh6GxBzTxgFKMMuzcHiUzG9wvbxn7toS4H2gbmUn1r91836ET2EVgmSdzju614Wu67ukyBGivcboncdfxPSR5JXwURBaL8K2P6yhKn3NyprFV8s6QpN4zgQMAD3Q6fjAsEvGNwXaQTZmEN2yd1NQ7uBE3RJ2XywZnehmfLQTEqD7Ad5XM3qfLLd9CtdzJGBkRfunHhkH1kz8dHL7hXwtk5EMXktY4QF5gZ1uisUV5mpPjEgqz7uDz";
201
+ const ed25519Pem = "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAvrabdlLgVI5jWl7GpF+fLFJVF4ccI8D7h+v5ulBCYwo=\n-----END PUBLIC KEY-----\n";
202
+ const ed25519Jwk = {
203
+ alg: "Ed25519",
204
+ kty: "OKP",
205
+ crv: "Ed25519",
206
+ x: "vrabdlLgVI5jWl7GpF-fLFJVF4ccI8D7h-v5ulBCYwo",
207
+ key_ops: ["verify"],
208
+ ext: true
209
+ };
210
+ const ed25519Multibase = "z6MksHj1MJnidCtDiyYW9ugNFftoX9fLK4bornTxmMZ6X7vq";
211
+ (0, node_test.test)("importSpki()", async () => {
212
+ const rsaKey = await importSpki(rsaSpki);
213
+ (0, node_assert.deepStrictEqual)(await exportJwk(rsaKey), rsaJwk);
214
+ const ed25519Key = await importSpki(ed25519Pem);
215
+ (0, node_assert.deepStrictEqual)(await exportJwk(ed25519Key), ed25519Jwk);
216
+ });
217
+ (0, node_test.test)("exportSpki()", async () => {
218
+ const rsaKey = await importJwk(rsaJwk, "public");
219
+ const rsaSpki$1 = await exportSpki(rsaKey);
220
+ (0, node_assert.deepStrictEqual)(rsaSpki$1, rsaSpki$1);
221
+ const ed25519Key = await importJwk(ed25519Jwk, "public");
222
+ const ed25519Spki = await exportSpki(ed25519Key);
223
+ (0, node_assert.deepStrictEqual)(ed25519Spki, ed25519Pem);
224
+ });
225
+ (0, node_test.test)("importPkcs1()", async () => {
226
+ const rsaKey = await importPkcs1(rsaPkcs1);
227
+ (0, node_assert.deepStrictEqual)(await exportJwk(rsaKey), rsaJwk);
228
+ });
229
+ (0, node_test.test)("importPem()", async () => {
230
+ const rsaPkcs1Key = await importPem(rsaPkcs1);
231
+ (0, node_assert.deepStrictEqual)(await exportJwk(rsaPkcs1Key), rsaJwk);
232
+ const rsaSpkiKey = await importPem(rsaSpki);
233
+ (0, node_assert.deepStrictEqual)(await exportJwk(rsaSpkiKey), rsaJwk);
234
+ const ed25519Key = await importPem(ed25519Pem);
235
+ (0, node_assert.deepStrictEqual)(await exportJwk(ed25519Key), ed25519Jwk);
236
+ });
237
+ (0, node_test.test)("importMultibase()", async () => {
238
+ const rsaKey = await importMultibaseKey(rsaMultibase);
239
+ (0, node_assert.deepStrictEqual)(await exportJwk(rsaKey), rsaJwk);
240
+ const ed25519Key = await importMultibaseKey(ed25519Multibase);
241
+ (0, node_assert.deepStrictEqual)(await exportJwk(ed25519Key), ed25519Jwk);
242
+ });
243
+ (0, node_test.test)("exportMultibaseKey()", async () => {
244
+ const rsaKey = await importJwk(rsaJwk, "public");
245
+ const rsaMb = await exportMultibaseKey(rsaKey);
246
+ (0, node_assert.deepStrictEqual)(rsaMb, rsaMultibase);
247
+ const ed25519Key = await importJwk(ed25519Jwk, "public");
248
+ const ed25519Mb = await exportMultibaseKey(ed25519Key);
249
+ (0, node_assert.deepStrictEqual)(ed25519Mb, ed25519Multibase);
250
+ const rsaKey2 = await importJwk({
251
+ alg: "RS256",
252
+ ext: true,
253
+ key_ops: ["verify"],
254
+ e: "AQAB",
255
+ kty: "RSA",
256
+ n: "sbX82NTV6IylxCh7MfV4hlyvaniCajuP97GyOqSvTmoEdBOflFvZ06kR_9D6ctt45Fk6hskfnag2GG69NALVH2o4RCR6tQiLRpKcMRtDYE_thEmfBvDzm_VVkOIYfxu-Ipuo9J_S5XDNDjczx2v-3oDh5-CIHkU46hvFeCvpUS-L8TJSbgX0kjVk_m4eIb9wh63rtmD6Uz_KBtCo5mmR4TEtcLZKYdqMp3wCjN-TlgHiz_4oVXWbHUefCEe8rFnX1iQnpDHU49_SaXQoud1jCaexFn25n-Aa8f8bc5Vm-5SeRwidHa6ErvEhTvf1dz6GoNPp2iRvm-wJ1gxwWJEYPQ"
257
+ }, "public");
258
+ const rsaMb2 = await exportMultibaseKey(rsaKey2);
259
+ (0, node_assert.deepStrictEqual)(rsaMb2, "z4MXj1wBzi9jUstyPMS4jQqB6KdJaiatPkAtVtGc6bQEQEEsKTic4G7Rou3iBf9vPmT5dbkm9qsZsuVNjq8HCuW1w24nhBFGkRE4cd2Uf2tfrB3N7h4mnyPp1BF3ZttHTYv3DLUPi1zMdkULiow3M1GfXkoC6DoxDUm1jmN6GBj22SjVsr6dxezRVQc7aj9TxE7JLbMH1wh5X3kA58H3DFW8rnYMakFGbca5CB2Jf6CnGQZmL7o5uJAdTwXfy2iiiyPxXEGerMhHwhjTA1mKYobyk2CpeEcmvynADfNZ5MBvcCS7m3XkFCMNUYBS9NQ3fze6vMSUPsNa6GVYmKx2x6JrdEjCk3qRMMmyjnjCMfR4pXbRMZa3i");
260
+ const ed25519Key2 = await importJwk({
261
+ alg: "Ed25519",
262
+ crv: "Ed25519",
263
+ ext: true,
264
+ key_ops: ["verify"],
265
+ kty: "OKP",
266
+ x: "Lm_M42cB3HkUiODQsXRcweM6TByfzEHGO9ND274JcOY"
267
+ }, "public");
268
+ const ed25519Mb2 = await exportMultibaseKey(ed25519Key2);
269
+ (0, node_assert.deepStrictEqual)(ed25519Mb2, "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK");
270
+ });
271
+
272
+ //#endregion
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,271 @@
1
+ import { decodeMultibase, encodeMultibase } from "./multibase-DStmqni9.js";
2
+ import { deepStrictEqual } from "node:assert";
3
+ import { test } from "node:test";
4
+ import { Integer, Sequence } from "asn1js";
5
+ import { decodeBase64, encodeBase64 } from "byte-encodings/base64";
6
+ import { decodeBase64Url } from "byte-encodings/base64url";
7
+ import { decodeHex } from "byte-encodings/hex";
8
+ import { addPrefix, getCodeFromData, rmPrefix } from "multicodec";
9
+ import { createPublicKey } from "node:crypto";
10
+ import { PublicKeyInfo } from "pkijs";
11
+
12
+ //#region src/jwk.ts
13
+ function validateCryptoKey(key, type) {
14
+ if (type != null && key.type !== type) throw new TypeError(`The key is not a ${type} key.`);
15
+ if (!key.extractable) throw new TypeError("The key is not extractable.");
16
+ if (key.algorithm.name !== "RSASSA-PKCS1-v1_5" && key.algorithm.name !== "Ed25519") throw new TypeError("Currently only RSASSA-PKCS1-v1_5 and Ed25519 keys are supported. More algorithms will be added in the future!");
17
+ if (key.algorithm.name === "RSASSA-PKCS1-v1_5") {
18
+ const algorithm = key.algorithm;
19
+ if (algorithm.hash.name !== "SHA-256") throw new TypeError("For compatibility with the existing Fediverse software (e.g., Mastodon), hash algorithm for RSASSA-PKCS1-v1_5 keys must be SHA-256.");
20
+ }
21
+ }
22
+ async function exportJwk(key) {
23
+ validateCryptoKey(key);
24
+ const jwk = await crypto.subtle.exportKey("jwk", key);
25
+ if (jwk.crv === "Ed25519") jwk.alg = "Ed25519";
26
+ return jwk;
27
+ }
28
+ async function importJwk(jwk, type) {
29
+ let key;
30
+ if (jwk.kty === "RSA" && jwk.alg === "RS256") key = await crypto.subtle.importKey("jwk", jwk, {
31
+ name: "RSASSA-PKCS1-v1_5",
32
+ hash: "SHA-256"
33
+ }, true, type === "public" ? ["verify"] : ["sign"]);
34
+ else if (jwk.kty === "OKP" && jwk.crv === "Ed25519") {
35
+ if (navigator?.userAgent === "Cloudflare-Workers") {
36
+ jwk = { ...jwk };
37
+ delete jwk.alg;
38
+ }
39
+ key = await crypto.subtle.importKey("jwk", jwk, "Ed25519", true, type === "public" ? ["verify"] : ["sign"]);
40
+ } else throw new TypeError("Unsupported JWK format.");
41
+ validateCryptoKey(key, type);
42
+ return key;
43
+ }
44
+
45
+ //#endregion
46
+ //#region src/key.ts
47
+ const algorithms = {
48
+ "1.2.840.113549.1.1.1": {
49
+ name: "RSASSA-PKCS1-v1_5",
50
+ hash: "SHA-256"
51
+ },
52
+ "1.3.101.112": "Ed25519"
53
+ };
54
+ /**
55
+ * Imports a PEM-SPKI formatted public key.
56
+ * @param pem The PEM-SPKI formatted public key.
57
+ * @returns The imported public key.
58
+ * @throws {TypeError} If the key is invalid or unsupported.
59
+ * @since 0.5.0
60
+ */
61
+ async function importSpki(pem) {
62
+ pem = pem.replace(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, "");
63
+ let spki;
64
+ try {
65
+ spki = decodeBase64(pem);
66
+ } catch (_) {
67
+ throw new TypeError("Invalid PEM-SPKI format.");
68
+ }
69
+ const pki = PublicKeyInfo.fromBER(spki);
70
+ const oid = pki.algorithm.algorithmId;
71
+ const algorithm = algorithms[oid];
72
+ if (algorithm == null) throw new TypeError("Unsupported algorithm: " + oid);
73
+ return await crypto.subtle.importKey("spki", spki, algorithm, true, ["verify"]);
74
+ }
75
+ /**
76
+ * Exports a public key in PEM-SPKI format.
77
+ * @param key The public key to export.
78
+ * @returns The exported public key in PEM-SPKI format.
79
+ * @throws {TypeError} If the key is invalid or unsupported.
80
+ * @since 0.5.0
81
+ */
82
+ async function exportSpki(key) {
83
+ validateCryptoKey(key);
84
+ const spki = await crypto.subtle.exportKey("spki", key);
85
+ let pem = encodeBase64(spki);
86
+ pem = (pem.match(/.{1,64}/g) || []).join("\n");
87
+ return `-----BEGIN PUBLIC KEY-----\n${pem}\n-----END PUBLIC KEY-----\n`;
88
+ }
89
+ /**
90
+ * Imports a PEM-PKCS#1 formatted public key.
91
+ * @param pem The PEM-PKCS#1 formatted public key.
92
+ * @returns The imported public key.
93
+ * @throws {TypeError} If the key is invalid or unsupported.
94
+ * @since 1.5.0
95
+ */
96
+ function importPkcs1(pem) {
97
+ const key = createPublicKey({
98
+ key: pem,
99
+ format: "pem",
100
+ type: "pkcs1"
101
+ });
102
+ const spki = key.export({
103
+ type: "spki",
104
+ format: "pem"
105
+ });
106
+ return importSpki(spki);
107
+ }
108
+ const PKCS1_HEADER = /^\s*-----BEGIN\s+RSA\s+PUBLIC\s+KEY-----\s*\n/;
109
+ /**
110
+ * Imports a PEM formatted public key (SPKI or PKCS#1).
111
+ * @param pem The PEM formatted public key to import (SPKI or PKCS#1).
112
+ * @returns The imported public key.
113
+ * @throws {TypeError} If the key is invalid or unsupported.
114
+ * @since 1.5.0
115
+ */
116
+ function importPem(pem) {
117
+ return PKCS1_HEADER.test(pem) ? importPkcs1(pem) : importSpki(pem);
118
+ }
119
+ /**
120
+ * Imports a [Multibase]-encoded public key.
121
+ *
122
+ * [Multibase]: https://www.w3.org/TR/vc-data-integrity/#multibase-0
123
+ * @param key The Multibase-encoded public key.
124
+ * @returns The imported public key.
125
+ * @throws {TypeError} If the key is invalid or unsupported.
126
+ * @since 0.10.0
127
+ */
128
+ async function importMultibaseKey(key) {
129
+ const decoded = decodeMultibase(key);
130
+ const code = getCodeFromData(decoded);
131
+ const content = rmPrefix(decoded);
132
+ if (code === 4613) {
133
+ const keyObject = createPublicKey({
134
+ key: content,
135
+ format: "der",
136
+ type: "pkcs1"
137
+ });
138
+ const exported = keyObject.export({
139
+ type: "spki",
140
+ format: "der"
141
+ });
142
+ const spki = exported instanceof Uint8Array ? exported : new Uint8Array(exported);
143
+ return await crypto.subtle.importKey("spki", new Uint8Array(spki), {
144
+ name: "RSASSA-PKCS1-v1_5",
145
+ hash: "SHA-256"
146
+ }, true, ["verify"]);
147
+ } else if (code === 237) return await crypto.subtle.importKey("raw", content.slice(), "Ed25519", true, ["verify"]);
148
+ else throw new TypeError("Unsupported key type: 0x" + code.toString(16));
149
+ }
150
+ /**
151
+ * Exports a public key in [Multibase] format.
152
+ *
153
+ * [Multibase]: https://www.w3.org/TR/vc-data-integrity/#multibase-0
154
+ * @param key The public key to export.
155
+ * @returns The exported public key in Multibase format.
156
+ * @throws {TypeError} If the key is invalid or unsupported.
157
+ * @since 0.10.0
158
+ */
159
+ async function exportMultibaseKey(key) {
160
+ let content;
161
+ let code;
162
+ if (key.algorithm.name === "Ed25519") {
163
+ content = await crypto.subtle.exportKey("raw", key);
164
+ code = 237;
165
+ } else if (key.algorithm.name === "RSASSA-PKCS1-v1_5" && key.algorithm.hash.name === "SHA-256") {
166
+ const jwk = await crypto.subtle.exportKey("jwk", key);
167
+ const decodedN = decodeBase64Url(jwk.n);
168
+ const n = new Uint8Array(decodedN.length + 1);
169
+ n.set(decodedN, 1);
170
+ const sequence = new Sequence({ value: [new Integer({
171
+ isHexOnly: true,
172
+ valueHex: n
173
+ }), new Integer({
174
+ isHexOnly: true,
175
+ valueHex: decodeBase64Url(jwk.e)
176
+ })] });
177
+ content = sequence.toBER(false);
178
+ code = 4613;
179
+ } else throw new TypeError("Unsupported key type: " + JSON.stringify(key.algorithm));
180
+ const codeHex = code.toString(16);
181
+ const codeBytes = decodeHex(codeHex.length % 2 < 1 ? codeHex : "0" + codeHex);
182
+ const prefixed = addPrefix(codeBytes, new Uint8Array(content));
183
+ const encoded = encodeMultibase("base58btc", prefixed);
184
+ return new TextDecoder().decode(encoded);
185
+ }
186
+
187
+ //#endregion
188
+ //#region src/key.test.ts
189
+ const rsaSpki = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxsRuvCkgJtflBTl4OVsm\nnt/J1mQfZasfJtN33dcZ3d1lJroxmgmMu69zjGEAwkNbMQaWNLqC4eogkJaeJ4RR\n5MHYXkL9nNilVoTkjX5BVit3puzs7XJ7WQnKQgQMI+ezn24GHsZ/v1JIo77lerX5\nk4HNwTNVt+yaZVQWaOMR3+6FwziQR6kd0VuG9/a9dgAnz2cEoORRC1i4W7IZaB1s\nZnh1WbHbevlGd72HSXll5rocPIHn8gq6xpBgpHwRphlRsgn4KHaJ6brXDIJjrnQh\nIe/YUBOGj/ImSEXhRwlFerKsoAVnZ0Hwbfa46qk44TAt8CyoPMWmpK6pt0ng4pQ2\nuwIDAQAB\n-----END PUBLIC KEY-----\n";
190
+ const rsaPkcs1 = "-----BEGIN RSA PUBLIC KEY-----\nMIIBCgKCAQEAxsRuvCkgJtflBTl4OVsmnt/J1mQfZasfJtN33dcZ3d1lJroxmgmM\nu69zjGEAwkNbMQaWNLqC4eogkJaeJ4RR5MHYXkL9nNilVoTkjX5BVit3puzs7XJ7\nWQnKQgQMI+ezn24GHsZ/v1JIo77lerX5k4HNwTNVt+yaZVQWaOMR3+6FwziQR6kd\n0VuG9/a9dgAnz2cEoORRC1i4W7IZaB1sZnh1WbHbevlGd72HSXll5rocPIHn8gq6\nxpBgpHwRphlRsgn4KHaJ6brXDIJjrnQhIe/YUBOGj/ImSEXhRwlFerKsoAVnZ0Hw\nbfa46qk44TAt8CyoPMWmpK6pt0ng4pQ2uwIDAQAB\n-----END RSA PUBLIC KEY-----\n";
191
+ const rsaJwk = {
192
+ alg: "RS256",
193
+ e: "AQAB",
194
+ ext: true,
195
+ key_ops: ["verify"],
196
+ kty: "RSA",
197
+ n: "xsRuvCkgJtflBTl4OVsmnt_J1mQfZasfJtN33dcZ3d1lJroxmgmMu69zjGEAwkNbMQaWNLqC4eogkJaeJ4RR5MHYXkL9nNilVoTkjX5BVit3puzs7XJ7WQnKQgQMI-ezn24GHsZ_v1JIo77lerX5k4HNwTNVt-yaZVQWaOMR3-6FwziQR6kd0VuG9_a9dgAnz2cEoORRC1i4W7IZaB1sZnh1WbHbevlGd72HSXll5rocPIHn8gq6xpBgpHwRphlRsgn4KHaJ6brXDIJjrnQhIe_YUBOGj_ImSEXhRwlFerKsoAVnZ0Hwbfa46qk44TAt8CyoPMWmpK6pt0ng4pQ2uw"
198
+ };
199
+ const rsaMultibase = "z4MXj1wBzi9jUstyPqYMn6Gum79JtbKFiHTibtPRoPeufjdimA24Kg8Q5N7E2eMpgVUtD61kUvmy4FaT5D5G8XU3ktxeduwEw5FHTtiLCzaruadf6rit1AUPL34UtcPuHh6GxBzTxgFKMMuzcHiUzG9wvbxn7toS4H2gbmUn1r91836ET2EVgmSdzju614Wu67ukyBGivcboncdfxPSR5JXwURBaL8K2P6yhKn3NyprFV8s6QpN4zgQMAD3Q6fjAsEvGNwXaQTZmEN2yd1NQ7uBE3RJ2XywZnehmfLQTEqD7Ad5XM3qfLLd9CtdzJGBkRfunHhkH1kz8dHL7hXwtk5EMXktY4QF5gZ1uisUV5mpPjEgqz7uDz";
200
+ const ed25519Pem = "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAvrabdlLgVI5jWl7GpF+fLFJVF4ccI8D7h+v5ulBCYwo=\n-----END PUBLIC KEY-----\n";
201
+ const ed25519Jwk = {
202
+ alg: "Ed25519",
203
+ kty: "OKP",
204
+ crv: "Ed25519",
205
+ x: "vrabdlLgVI5jWl7GpF-fLFJVF4ccI8D7h-v5ulBCYwo",
206
+ key_ops: ["verify"],
207
+ ext: true
208
+ };
209
+ const ed25519Multibase = "z6MksHj1MJnidCtDiyYW9ugNFftoX9fLK4bornTxmMZ6X7vq";
210
+ test("importSpki()", async () => {
211
+ const rsaKey = await importSpki(rsaSpki);
212
+ deepStrictEqual(await exportJwk(rsaKey), rsaJwk);
213
+ const ed25519Key = await importSpki(ed25519Pem);
214
+ deepStrictEqual(await exportJwk(ed25519Key), ed25519Jwk);
215
+ });
216
+ test("exportSpki()", async () => {
217
+ const rsaKey = await importJwk(rsaJwk, "public");
218
+ const rsaSpki$1 = await exportSpki(rsaKey);
219
+ deepStrictEqual(rsaSpki$1, rsaSpki$1);
220
+ const ed25519Key = await importJwk(ed25519Jwk, "public");
221
+ const ed25519Spki = await exportSpki(ed25519Key);
222
+ deepStrictEqual(ed25519Spki, ed25519Pem);
223
+ });
224
+ test("importPkcs1()", async () => {
225
+ const rsaKey = await importPkcs1(rsaPkcs1);
226
+ deepStrictEqual(await exportJwk(rsaKey), rsaJwk);
227
+ });
228
+ test("importPem()", async () => {
229
+ const rsaPkcs1Key = await importPem(rsaPkcs1);
230
+ deepStrictEqual(await exportJwk(rsaPkcs1Key), rsaJwk);
231
+ const rsaSpkiKey = await importPem(rsaSpki);
232
+ deepStrictEqual(await exportJwk(rsaSpkiKey), rsaJwk);
233
+ const ed25519Key = await importPem(ed25519Pem);
234
+ deepStrictEqual(await exportJwk(ed25519Key), ed25519Jwk);
235
+ });
236
+ test("importMultibase()", async () => {
237
+ const rsaKey = await importMultibaseKey(rsaMultibase);
238
+ deepStrictEqual(await exportJwk(rsaKey), rsaJwk);
239
+ const ed25519Key = await importMultibaseKey(ed25519Multibase);
240
+ deepStrictEqual(await exportJwk(ed25519Key), ed25519Jwk);
241
+ });
242
+ test("exportMultibaseKey()", async () => {
243
+ const rsaKey = await importJwk(rsaJwk, "public");
244
+ const rsaMb = await exportMultibaseKey(rsaKey);
245
+ deepStrictEqual(rsaMb, rsaMultibase);
246
+ const ed25519Key = await importJwk(ed25519Jwk, "public");
247
+ const ed25519Mb = await exportMultibaseKey(ed25519Key);
248
+ deepStrictEqual(ed25519Mb, ed25519Multibase);
249
+ const rsaKey2 = await importJwk({
250
+ alg: "RS256",
251
+ ext: true,
252
+ key_ops: ["verify"],
253
+ e: "AQAB",
254
+ kty: "RSA",
255
+ n: "sbX82NTV6IylxCh7MfV4hlyvaniCajuP97GyOqSvTmoEdBOflFvZ06kR_9D6ctt45Fk6hskfnag2GG69NALVH2o4RCR6tQiLRpKcMRtDYE_thEmfBvDzm_VVkOIYfxu-Ipuo9J_S5XDNDjczx2v-3oDh5-CIHkU46hvFeCvpUS-L8TJSbgX0kjVk_m4eIb9wh63rtmD6Uz_KBtCo5mmR4TEtcLZKYdqMp3wCjN-TlgHiz_4oVXWbHUefCEe8rFnX1iQnpDHU49_SaXQoud1jCaexFn25n-Aa8f8bc5Vm-5SeRwidHa6ErvEhTvf1dz6GoNPp2iRvm-wJ1gxwWJEYPQ"
256
+ }, "public");
257
+ const rsaMb2 = await exportMultibaseKey(rsaKey2);
258
+ deepStrictEqual(rsaMb2, "z4MXj1wBzi9jUstyPMS4jQqB6KdJaiatPkAtVtGc6bQEQEEsKTic4G7Rou3iBf9vPmT5dbkm9qsZsuVNjq8HCuW1w24nhBFGkRE4cd2Uf2tfrB3N7h4mnyPp1BF3ZttHTYv3DLUPi1zMdkULiow3M1GfXkoC6DoxDUm1jmN6GBj22SjVsr6dxezRVQc7aj9TxE7JLbMH1wh5X3kA58H3DFW8rnYMakFGbca5CB2Jf6CnGQZmL7o5uJAdTwXfy2iiiyPxXEGerMhHwhjTA1mKYobyk2CpeEcmvynADfNZ5MBvcCS7m3XkFCMNUYBS9NQ3fze6vMSUPsNa6GVYmKx2x6JrdEjCk3qRMMmyjnjCMfR4pXbRMZa3i");
259
+ const ed25519Key2 = await importJwk({
260
+ alg: "Ed25519",
261
+ crv: "Ed25519",
262
+ ext: true,
263
+ key_ops: ["verify"],
264
+ kty: "OKP",
265
+ x: "Lm_M42cB3HkUiODQsXRcweM6TByfzEHGO9ND274JcOY"
266
+ }, "public");
267
+ const ed25519Mb2 = await exportMultibaseKey(ed25519Key2);
268
+ deepStrictEqual(ed25519Mb2, "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK");
269
+ });
270
+
271
+ //#endregion
@@ -0,0 +1,51 @@
1
+ const require_chunk = require('./chunk-DWy1uDak.cjs');
2
+ const node_assert = require_chunk.__toESM(require("node:assert"));
3
+ const node_test = require_chunk.__toESM(require("node:test"));
4
+ const node_util = require_chunk.__toESM(require("node:util"));
5
+
6
+ //#region src/langstr.ts
7
+ /**
8
+ * A language-tagged string which corresponds to the `rdf:langString` type.
9
+ */
10
+ var LanguageString = class extends String {
11
+ /**
12
+ * The locale of the string.
13
+ * @since 2.0.0
14
+ */
15
+ locale;
16
+ /**
17
+ * Constructs a new `LanguageString`.
18
+ * @param value A string value written in the given language.
19
+ * @param locale The language of the string. If a string is given, it will
20
+ * be parsed as a `Intl.Locale` object.
21
+ */
22
+ constructor(value, language) {
23
+ super(value);
24
+ this.locale = typeof language === "string" ? new Intl.Locale(language) : language;
25
+ }
26
+ [Symbol.for("Deno.customInspect")](inspect, options) {
27
+ return `<${this.locale.baseName}> ${inspect(this.toString(), options)}`;
28
+ }
29
+ [Symbol.for("nodejs.util.inspect.custom")](_depth, options, inspect) {
30
+ return `<${this.locale.baseName}> ${inspect(this.toString(), options)}`;
31
+ }
32
+ };
33
+
34
+ //#endregion
35
+ //#region src/langstr.test.ts
36
+ (0, node_test.test)("new LanguageString()", () => {
37
+ const langStr = new LanguageString("Hello", "en");
38
+ (0, node_assert.deepStrictEqual)(langStr.toString(), "Hello");
39
+ (0, node_assert.deepStrictEqual)(langStr.locale, new Intl.Locale("en"));
40
+ (0, node_assert.deepStrictEqual)(new LanguageString("Hello", new Intl.Locale("en")), langStr);
41
+ });
42
+ (0, node_test.test)("Deno.inspect(LanguageString)", () => {
43
+ const langStr = new LanguageString("Hello, 'world'", "en");
44
+ (0, node_assert.deepStrictEqual)(node_util.default.inspect(langStr, { colors: false }), "<en> \"Hello, 'world'\"");
45
+ });
46
+ (0, node_test.test)("util.inspect(LanguageString)", () => {
47
+ const langStr = new LanguageString("Hello, 'world'", "en");
48
+ (0, node_assert.deepStrictEqual)(node_util.default.inspect(langStr, { colors: false }), "<en> \"Hello, 'world'\"");
49
+ });
50
+
51
+ //#endregion
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,50 @@
1
+ import { deepStrictEqual } from "node:assert";
2
+ import { test } from "node:test";
3
+ import util from "node:util";
4
+
5
+ //#region src/langstr.ts
6
+ /**
7
+ * A language-tagged string which corresponds to the `rdf:langString` type.
8
+ */
9
+ var LanguageString = class extends String {
10
+ /**
11
+ * The locale of the string.
12
+ * @since 2.0.0
13
+ */
14
+ locale;
15
+ /**
16
+ * Constructs a new `LanguageString`.
17
+ * @param value A string value written in the given language.
18
+ * @param locale The language of the string. If a string is given, it will
19
+ * be parsed as a `Intl.Locale` object.
20
+ */
21
+ constructor(value, language) {
22
+ super(value);
23
+ this.locale = typeof language === "string" ? new Intl.Locale(language) : language;
24
+ }
25
+ [Symbol.for("Deno.customInspect")](inspect, options) {
26
+ return `<${this.locale.baseName}> ${inspect(this.toString(), options)}`;
27
+ }
28
+ [Symbol.for("nodejs.util.inspect.custom")](_depth, options, inspect) {
29
+ return `<${this.locale.baseName}> ${inspect(this.toString(), options)}`;
30
+ }
31
+ };
32
+
33
+ //#endregion
34
+ //#region src/langstr.test.ts
35
+ test("new LanguageString()", () => {
36
+ const langStr = new LanguageString("Hello", "en");
37
+ deepStrictEqual(langStr.toString(), "Hello");
38
+ deepStrictEqual(langStr.locale, new Intl.Locale("en"));
39
+ deepStrictEqual(new LanguageString("Hello", new Intl.Locale("en")), langStr);
40
+ });
41
+ test("Deno.inspect(LanguageString)", () => {
42
+ const langStr = new LanguageString("Hello, 'world'", "en");
43
+ deepStrictEqual(util.inspect(langStr, { colors: false }), "<en> \"Hello, 'world'\"");
44
+ });
45
+ test("util.inspect(LanguageString)", () => {
46
+ const langStr = new LanguageString("Hello, 'world'", "en");
47
+ deepStrictEqual(util.inspect(langStr, { colors: false }), "<en> \"Hello, 'world'\"");
48
+ });
49
+
50
+ //#endregion