@fedify/vocab-runtime 2.4.0-dev.1655 → 2.4.0-pr.934.40

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 (57) hide show
  1. package/deno.json +1 -1
  2. package/dist/{docloader-C_dir7Xb.d.ts → docloader-DnUMWHaJ.d.cts} +0 -2
  3. package/dist/{docloader-D2DTRiyA.d.cts → docloader-xRGn1azD.d.ts} +0 -2
  4. package/dist/internal/jsonld-cache.cjs +1 -1
  5. package/dist/internal/jsonld-cache.d.cts +1 -1
  6. package/dist/internal/jsonld-cache.d.ts +1 -1
  7. package/dist/internal/jsonld-cache.js +1 -1
  8. package/dist/mod.cjs +8 -159
  9. package/dist/mod.d.cts +2 -91
  10. package/dist/mod.d.ts +2 -91
  11. package/dist/mod.js +9 -152
  12. package/dist/tests/decimal.test.cjs +5 -6
  13. package/dist/tests/decimal.test.mjs +5 -6
  14. package/dist/tests/{docloader-DVVWmXLI.cjs → docloader-BFedvemb.cjs} +6 -12
  15. package/dist/tests/{docloader-HbEvzi-t.mjs → docloader-Xi61Le8w.mjs} +6 -12
  16. package/dist/tests/docloader.test.cjs +3 -3
  17. package/dist/tests/docloader.test.mjs +3 -3
  18. package/dist/tests/jsonld-cache.test.cjs +1 -1
  19. package/dist/tests/jsonld-cache.test.mjs +1 -1
  20. package/dist/tests/{key-Drbw_2tw.mjs → key-CDGDH_vC.mjs} +1 -1
  21. package/dist/tests/{key-BcOWDLMm.cjs → key-_wXwomh_.cjs} +1 -1
  22. package/dist/tests/key.test.cjs +2 -2
  23. package/dist/tests/key.test.mjs +2 -2
  24. package/dist/tests/langstr.test.cjs +1 -1
  25. package/dist/tests/langstr.test.mjs +1 -1
  26. package/dist/tests/multibase/multibase.test.cjs +1 -1
  27. package/dist/tests/multibase/multibase.test.mjs +1 -1
  28. package/dist/tests/{multibase-Cluwl8OM.mjs → multibase-B4bvakyA.mjs} +1 -12
  29. package/dist/tests/{multibase-DcDQDNnI.cjs → multibase-Bz_UUDtL.cjs} +0 -17
  30. package/dist/tests/{request-B6s_F7bi.mjs → request-BOS-hNaf.mjs} +1 -1
  31. package/dist/tests/{request-DvEyU5Zf.cjs → request-cCPgOxYG.cjs} +1 -1
  32. package/dist/tests/request.test.cjs +1 -1
  33. package/dist/tests/request.test.mjs +1 -1
  34. package/dist/tests/{url-CsOV_B_P.cjs → url-BvjYQdxL.cjs} +0 -26
  35. package/dist/tests/{url-Du7RQQgP.mjs → url-a2D8NAgh.mjs} +1 -15
  36. package/dist/tests/url.test.cjs +1 -18
  37. package/dist/tests/url.test.mjs +1 -18
  38. package/dist/{url-DD4F0ULf.cjs → url-Ck3dGEwH.cjs} +0 -26
  39. package/dist/{url-DGVbSVVi.js → url-m1YxGNZ0.js} +1 -15
  40. package/package.json +1 -1
  41. package/src/contexts/security-data-integrity-v1.json +4 -0
  42. package/src/contexts.ts +0 -2
  43. package/src/docloader.ts +0 -2
  44. package/src/mod.ts +0 -12
  45. package/src/url.test.ts +0 -23
  46. package/src/url.ts +0 -23
  47. package/dist/tests/digest-BJlumPNJ.mjs +0 -140
  48. package/dist/tests/digest-DLwUyHCK.cjs +0 -175
  49. package/dist/tests/digest.test.cjs +0 -103
  50. package/dist/tests/digest.test.d.cts +0 -1
  51. package/dist/tests/digest.test.d.mts +0 -1
  52. package/dist/tests/digest.test.mjs +0 -103
  53. package/src/contexts/fep-ef61.json +0 -10
  54. package/src/digest.test.ts +0 -220
  55. package/src/digest.ts +0 -229
  56. /package/dist/tests/{langstr-BOQHfJDr.cjs → langstr-CbAxaeEZ.cjs} +0 -0
  57. /package/dist/tests/{langstr-DDLc4833.mjs → langstr-Di5AvKpB.mjs} +0 -0
@@ -1,4 +1,4 @@
1
- import { a as formatIri, c as haveSameIriOrigin, d as isValidPublicIPv6Address, f as parseGatewayUrl, h as validatePublicUrl, i as expandIPv6Address, l as isGatewayUrl, m as parseJsonLdId, n as arePortableUrisEqual, o as getFe34Origin, p as parseIri, r as canonicalizePortableUri, s as haveSameFe34Origin, t as UrlError, u as isValidPublicIPv4Address } from "./url-Du7RQQgP.mjs";
1
+ import { a as formatIri, c as haveSameIriOrigin, d as parseIri, f as parseJsonLdId, i as expandIPv6Address, l as isValidPublicIPv4Address, n as arePortableUrisEqual, o as getFe34Origin, p as validatePublicUrl, r as canonicalizePortableUri, s as haveSameFe34Origin, t as UrlError, u as isValidPublicIPv6Address } from "./url-a2D8NAgh.mjs";
2
2
  import { deepStrictEqual, ok, rejects, throws } from "node:assert";
3
3
  import { test } from "node:test";
4
4
  //#region src/url.test.ts
@@ -227,23 +227,6 @@ test("parseIri() preserves encoded percent signs while decoding delimiters", ()
227
227
  deepStrictEqual(parsed, new URL("ap+ef61://did%3Aweb%3Aexample.com%2500/u/1"));
228
228
  deepStrictEqual(formatIri(parsed), "ap+ef61://did:web:example.com%00/u/1");
229
229
  });
230
- test("parseGatewayUrl() accepts only HTTP(S) base URIs", () => {
231
- for (const url of ["https://server.example/", "http://server.example/"]) {
232
- deepStrictEqual(parseGatewayUrl(url), new URL(url));
233
- ok(isGatewayUrl(new URL(url)));
234
- }
235
- for (const url of [
236
- "ftp://server.example/",
237
- "https://user:pass@server.example/",
238
- "https://user@server.example/",
239
- "https://server.example/path",
240
- "https://server.example/?x=1",
241
- "https://server.example/#fragment"
242
- ]) {
243
- throws(() => parseGatewayUrl(url), TypeError);
244
- ok(!isGatewayUrl(new URL(url)));
245
- }
246
- });
247
230
  test("validatePublicUrl()", async () => {
248
231
  await rejects(() => validatePublicUrl("ftp://localhost"), UrlError);
249
232
  await rejects(() => validatePublicUrl("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="), UrlError);
@@ -212,20 +212,6 @@ function parseAtUri(uri) {
212
212
  return new URL("at://" + encodeURIComponent(authority) + path);
213
213
  }
214
214
  /**
215
- * Checks whether the URL is an FEP-ef61 gateway base URI.
216
- */
217
- function isGatewayUrl(url) {
218
- return (url.protocol === "http:" || url.protocol === "https:") && url.username === "" && url.password === "" && url.pathname === "/" && url.search === "" && url.hash === "";
219
- }
220
- /**
221
- * Parses and validates an FEP-ef61 gateway base URI.
222
- */
223
- function parseGatewayUrl(url) {
224
- const parsed = parseIri(url);
225
- if (!isGatewayUrl(parsed)) throw new TypeError("FEP-ef61 gateways must be HTTP(S) base URIs with no credentials, path, query, or fragment.");
226
- return parsed;
227
- }
228
- /**
229
215
  * Validates a URL to prevent SSRF attacks.
230
216
  */
231
217
  async function validatePublicUrl(url) {
@@ -439,12 +425,6 @@ Object.defineProperty(exports, "haveSameIriOrigin", {
439
425
  return haveSameIriOrigin;
440
426
  }
441
427
  });
442
- Object.defineProperty(exports, "isGatewayUrl", {
443
- enumerable: true,
444
- get: function() {
445
- return isGatewayUrl;
446
- }
447
- });
448
428
  Object.defineProperty(exports, "isValidPublicIPv4Address", {
449
429
  enumerable: true,
450
430
  get: function() {
@@ -457,12 +437,6 @@ Object.defineProperty(exports, "isValidPublicIPv6Address", {
457
437
  return isValidPublicIPv6Address;
458
438
  }
459
439
  });
460
- Object.defineProperty(exports, "parseGatewayUrl", {
461
- enumerable: true,
462
- get: function() {
463
- return parseGatewayUrl;
464
- }
465
- });
466
440
  Object.defineProperty(exports, "parseIri", {
467
441
  enumerable: true,
468
442
  get: function() {
@@ -211,20 +211,6 @@ function parseAtUri(uri) {
211
211
  return new URL("at://" + encodeURIComponent(authority) + path);
212
212
  }
213
213
  /**
214
- * Checks whether the URL is an FEP-ef61 gateway base URI.
215
- */
216
- function isGatewayUrl(url) {
217
- return (url.protocol === "http:" || url.protocol === "https:") && url.username === "" && url.password === "" && url.pathname === "/" && url.search === "" && url.hash === "";
218
- }
219
- /**
220
- * Parses and validates an FEP-ef61 gateway base URI.
221
- */
222
- function parseGatewayUrl(url) {
223
- const parsed = parseIri(url);
224
- if (!isGatewayUrl(parsed)) throw new TypeError("FEP-ef61 gateways must be HTTP(S) base URIs with no credentials, path, query, or fragment.");
225
- return parsed;
226
- }
227
- /**
228
214
  * Validates a URL to prevent SSRF attacks.
229
215
  */
230
216
  async function validatePublicUrl(url) {
@@ -390,4 +376,4 @@ function matchesIPv6Prefix(address, prefixWords, prefixLength) {
390
376
  return true;
391
377
  }
392
378
  //#endregion
393
- export { formatIri as a, haveSameIriOrigin as c, isValidPublicIPv6Address as d, parseGatewayUrl as f, validatePublicUrl as h, expandIPv6Address as i, isGatewayUrl as l, parseJsonLdId as m, arePortableUrisEqual as n, getFe34Origin as o, parseIri as p, canonicalizePortableUri as r, haveSameFe34Origin as s, UrlError as t, isValidPublicIPv4Address as u };
379
+ export { formatIri as a, haveSameIriOrigin as c, parseIri as d, parseJsonLdId as f, expandIPv6Address as i, isValidPublicIPv4Address as l, arePortableUrisEqual as n, getFe34Origin as o, validatePublicUrl as p, canonicalizePortableUri as r, haveSameFe34Origin as s, UrlError as t, isValidPublicIPv6Address as u };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/vocab-runtime",
3
- "version": "2.4.0-dev.1655+b8f3c3d5",
3
+ "version": "2.4.0-pr.934.40+3331d302",
4
4
  "homepage": "https://fedify.dev/",
5
5
  "repository": {
6
6
  "type": "git",
@@ -3,6 +3,10 @@
3
3
  "id": "@id",
4
4
  "type": "@type",
5
5
  "@protected": true,
6
+ "digestMultibase": {
7
+ "@id": "https://w3id.org/security#digestMultibase",
8
+ "@type": "https://w3id.org/security#multibase"
9
+ },
6
10
  "proof": {
7
11
  "@id": "https://w3id.org/security#proof",
8
12
  "@type": "@id",
package/src/contexts.ts CHANGED
@@ -8,7 +8,6 @@ import activitystreams from "./contexts/activitystreams.json" with {
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
10
  import fep7aa9 from "./contexts/fep-7aa9.json" with { type: "json" };
11
- import fepEf61 from "./contexts/fep-ef61.json" with { type: "json" };
12
11
  import gotosocial from "./contexts/gotosocial.json" with { type: "json" };
13
12
  import identityV1 from "./contexts/identity-v1.json" with { type: "json" };
14
13
  import joinLemmyContext from "./contexts/join-lemmy.json" with { type: "json" };
@@ -38,7 +37,6 @@ const preloadedContexts: Record<string, unknown> = {
38
37
  "https://gotosocial.org/ns": gotosocial,
39
38
  "https://w3id.org/fep/5711": fep5711,
40
39
  "https://w3id.org/fep/7aa9": fep7aa9,
41
- "https://w3id.org/fep/ef61": fepEf61,
42
40
 
43
41
  // Lemmy's context document is served as application/json without the JSON-LD
44
42
  // context Link header. The default document loader treats that as a regular
package/src/docloader.ts CHANGED
@@ -351,10 +351,8 @@ export interface GetDocumentLoaderOptions extends DocumentLoaderFactoryOptions {
351
351
  * - <https://www.w3.org/ns/activitystreams>
352
352
  * - <https://w3id.org/security/v1>
353
353
  * - <https://w3id.org/security/data-integrity/v1>
354
- * - <https://w3id.org/security/data-integrity/v2>
355
354
  * - <https://www.w3.org/ns/did/v1>
356
355
  * - <https://w3id.org/security/multikey/v1>
357
- * - <https://w3id.org/fep/ef61>
358
356
  * - <https://purl.archive.org/socialweb/webfinger>
359
357
  * - <http://schema.org/>
360
358
  * @param options Options for the document loader.
package/src/mod.ts CHANGED
@@ -34,16 +34,6 @@ export {
34
34
  isDecimal,
35
35
  parseDecimal,
36
36
  } from "./decimal.ts";
37
- export {
38
- computeDigestMultibase,
39
- createHashlink,
40
- type ParsedDigestMultibase,
41
- type ParsedHashlink,
42
- parseDigestMultibase,
43
- parseHashlink,
44
- verifyDigestMultibase,
45
- verifyHashlink,
46
- } from "./digest.ts";
47
37
  export { LanguageString } from "./langstr.ts";
48
38
  export {
49
39
  decodeMultibase,
@@ -71,10 +61,8 @@ export {
71
61
  getFe34Origin,
72
62
  haveSameFe34Origin,
73
63
  haveSameIriOrigin,
74
- isGatewayUrl,
75
64
  isValidPublicIPv4Address,
76
65
  isValidPublicIPv6Address,
77
- parseGatewayUrl,
78
66
  parseIri,
79
67
  parseJsonLdId,
80
68
  UrlError,
package/src/url.test.ts CHANGED
@@ -8,10 +8,8 @@ import {
8
8
  getFe34Origin,
9
9
  haveSameFe34Origin,
10
10
  haveSameIriOrigin,
11
- isGatewayUrl,
12
11
  isValidPublicIPv4Address,
13
12
  isValidPublicIPv6Address,
14
- parseGatewayUrl,
15
13
  parseIri,
16
14
  parseJsonLdId,
17
15
  UrlError,
@@ -640,27 +638,6 @@ test("parseIri() preserves encoded percent signs while decoding delimiters", ()
640
638
  );
641
639
  });
642
640
 
643
- test("parseGatewayUrl() accepts only HTTP(S) base URIs", () => {
644
- for (const url of ["https://server.example/", "http://server.example/"]) {
645
- deepStrictEqual(parseGatewayUrl(url), new URL(url));
646
- ok(isGatewayUrl(new URL(url)));
647
- }
648
-
649
- for (
650
- const url of [
651
- "ftp://server.example/",
652
- "https://user:pass@server.example/",
653
- "https://user@server.example/",
654
- "https://server.example/path",
655
- "https://server.example/?x=1",
656
- "https://server.example/#fragment",
657
- ]
658
- ) {
659
- throws(() => parseGatewayUrl(url), TypeError);
660
- ok(!isGatewayUrl(new URL(url)));
661
- }
662
- });
663
-
664
641
  test("validatePublicUrl()", async () => {
665
642
  await rejects(() => validatePublicUrl("ftp://localhost"), UrlError);
666
643
  await rejects(
package/src/url.ts CHANGED
@@ -323,29 +323,6 @@ function parseAtUri(uri: string): URL {
323
323
  return new URL("at://" + encodeURIComponent(authority) + path);
324
324
  }
325
325
 
326
- /**
327
- * Checks whether the URL is an FEP-ef61 gateway base URI.
328
- */
329
- export function isGatewayUrl(url: URL): boolean {
330
- return (url.protocol === "http:" || url.protocol === "https:") &&
331
- url.username === "" && url.password === "" &&
332
- url.pathname === "/" && url.search === "" && url.hash === "";
333
- }
334
-
335
- /**
336
- * Parses and validates an FEP-ef61 gateway base URI.
337
- */
338
- export function parseGatewayUrl(url: string): URL {
339
- const parsed = parseIri(url);
340
- if (!isGatewayUrl(parsed)) {
341
- throw new TypeError(
342
- "FEP-ef61 gateways must be HTTP(S) base URIs with no credentials, " +
343
- "path, query, or fragment.",
344
- );
345
- }
346
- return parsed;
347
- }
348
-
349
326
  /**
350
327
  * Validates a URL to prevent SSRF attacks.
351
328
  */
@@ -1,140 +0,0 @@
1
- import { n as getMulticodecPrefix, t as addMulticodecPrefix } from "./multicodec-CyFp54fI.mjs";
2
- import { n as encodeMultibase, r as encodingFromBaseData, t as decodeMultibase } from "./multibase-Cluwl8OM.mjs";
3
- //#region src/digest.ts
4
- const SHA2_256_MULTIHASH_CODE = 18;
5
- const SHA2_256_DIGEST_LENGTH = 32;
6
- const MAX_DIGEST_MULTIBASE_LENGTH = 1 + (SHA2_256_DIGEST_LENGTH + 2) * 8;
7
- const textDecoder = new TextDecoder();
8
- const getArrayBufferByteLength = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "byteLength")?.get;
9
- function isCanonicalVarintPrefix(data, prefix) {
10
- const canonical = addMulticodecPrefix(prefix.code, new Uint8Array());
11
- if (canonical.length !== prefix.prefixLength) return false;
12
- for (let i = 0; i < canonical.length; i++) if (data[i] !== canonical[i]) return false;
13
- return true;
14
- }
15
- function toWebCryptoBytes(bytes) {
16
- if (getArrayBufferByteLength != null) try {
17
- getArrayBufferByteLength.call(bytes.buffer);
18
- return bytes;
19
- } catch {}
20
- return new Uint8Array(bytes);
21
- }
22
- /**
23
- * Computes the SHA-256 digest of a byte sequence and encodes it as a
24
- * base58-btc multibase multihash suitable for FEP-ef61's
25
- * `digestMultibase` property.
26
- *
27
- * @param bytes The bytes to digest.
28
- * @returns The base58-btc multibase-encoded SHA-256 multihash.
29
- * @since 2.4.0
30
- */
31
- async function computeDigestMultibase(bytes) {
32
- const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", toWebCryptoBytes(bytes)));
33
- const multihash = addMulticodecPrefix(SHA2_256_MULTIHASH_CODE, addMulticodecPrefix(digest.length, digest));
34
- return textDecoder.decode(encodeMultibase("base58btc", multihash));
35
- }
36
- /**
37
- * Parses and validates a SHA-256 `digestMultibase` value.
38
- *
39
- * @param value The multibase-encoded multihash to parse.
40
- * @returns The digest algorithm and raw digest bytes.
41
- * @throws {TypeError} If the value is malformed or uses an unsupported hash
42
- * algorithm.
43
- * @since 2.4.0
44
- */
45
- function parseDigestMultibase(value) {
46
- if (value.length > MAX_DIGEST_MULTIBASE_LENGTH) throw new TypeError("Invalid digestMultibase encoding.");
47
- let multihash;
48
- try {
49
- multihash = decodeMultibase(value);
50
- } catch (error) {
51
- throw new TypeError("Invalid digestMultibase encoding.", { cause: error });
52
- }
53
- const encoding = encodingFromBaseData(value);
54
- if (value !== textDecoder.decode(encodeMultibase(encoding.name, multihash))) throw new TypeError("Invalid digestMultibase encoding.");
55
- let algorithmPrefix;
56
- try {
57
- algorithmPrefix = getMulticodecPrefix(multihash);
58
- } catch (error) {
59
- throw new TypeError("Invalid digestMultibase multihash.", { cause: error });
60
- }
61
- if (!isCanonicalVarintPrefix(multihash, algorithmPrefix)) throw new TypeError("Invalid digestMultibase multihash.");
62
- if (algorithmPrefix.code !== SHA2_256_MULTIHASH_CODE) throw new TypeError(`Unsupported digest algorithm: 0x${algorithmPrefix.code.toString(16)}`);
63
- const lengthAndDigest = multihash.subarray(algorithmPrefix.prefixLength);
64
- let lengthPrefix;
65
- try {
66
- lengthPrefix = getMulticodecPrefix(lengthAndDigest);
67
- } catch (error) {
68
- throw new TypeError("Invalid digestMultibase multihash.", { cause: error });
69
- }
70
- if (!isCanonicalVarintPrefix(lengthAndDigest, lengthPrefix)) throw new TypeError("Invalid digestMultibase multihash.");
71
- const digest = lengthAndDigest.slice(lengthPrefix.prefixLength);
72
- if (lengthPrefix.code !== SHA2_256_DIGEST_LENGTH || digest.length !== SHA2_256_DIGEST_LENGTH) throw new TypeError("Invalid SHA-256 digest length.");
73
- return {
74
- algorithm: "sha2-256",
75
- digest
76
- };
77
- }
78
- function extractDigestMultibase(value) {
79
- const hashlink = typeof value === "string" ? value : value.href;
80
- const match = /^hl:([^:\r\n\u2028\u2029]+)$/i.exec(hashlink);
81
- if (match == null || match[0].length !== hashlink.length) throw new TypeError("Invalid simple hashlink.");
82
- return match[1];
83
- }
84
- /**
85
- * Parses a metadata-free `hl:` hashlink and validates its resource digest.
86
- *
87
- * @param value The simple hashlink to parse.
88
- * @returns The `digestMultibase` value carried by the hashlink.
89
- * @throws {TypeError} If the hashlink is malformed, contains metadata, or
90
- * carries an invalid or unsupported digest.
91
- * @since 2.4.0
92
- */
93
- function parseHashlink(value) {
94
- const digestMultibase = extractDigestMultibase(value);
95
- parseDigestMultibase(digestMultibase);
96
- return { digestMultibase };
97
- }
98
- /**
99
- * Creates a metadata-free `hl:` hashlink from a `digestMultibase` value.
100
- *
101
- * @param digestMultibase The SHA-256 multibase multihash to embed.
102
- * @returns The simple hashlink.
103
- * @throws {TypeError} If the digest is malformed or unsupported.
104
- * @since 2.4.0
105
- */
106
- function createHashlink(digestMultibase) {
107
- parseDigestMultibase(digestMultibase);
108
- return `hl:${digestMultibase}`;
109
- }
110
- /**
111
- * Verifies that bytes match a SHA-256 `digestMultibase` value.
112
- *
113
- * @param bytes The bytes to verify.
114
- * @param digestMultibase The expected digest.
115
- * @returns Whether the bytes match the digest.
116
- * @throws {TypeError} If the digest is malformed or unsupported.
117
- * @since 2.4.0
118
- */
119
- async function verifyDigestMultibase(bytes, digestMultibase) {
120
- const expected = parseDigestMultibase(digestMultibase).digest;
121
- const actual = new Uint8Array(await crypto.subtle.digest("SHA-256", toWebCryptoBytes(bytes)));
122
- if (expected.length !== actual.length) return false;
123
- let difference = 0;
124
- for (let i = 0; i < expected.length; i++) difference |= expected[i] ^ actual[i];
125
- return difference === 0;
126
- }
127
- /**
128
- * Verifies that bytes match the resource digest in a simple hashlink.
129
- *
130
- * @param bytes The bytes to verify.
131
- * @param hashlink The simple hashlink containing the expected digest.
132
- * @returns Whether the bytes match the hashlink digest.
133
- * @throws {TypeError} If the hashlink or digest is malformed or unsupported.
134
- * @since 2.4.0
135
- */
136
- async function verifyHashlink(bytes, hashlink) {
137
- return await verifyDigestMultibase(bytes, extractDigestMultibase(hashlink));
138
- }
139
- //#endregion
140
- export { verifyDigestMultibase as a, parseHashlink as i, createHashlink as n, verifyHashlink as o, parseDigestMultibase as r, computeDigestMultibase as t };
@@ -1,175 +0,0 @@
1
- const require_multicodec = require("./multicodec-CxGVGa91.cjs");
2
- const require_multibase = require("./multibase-DcDQDNnI.cjs");
3
- //#region src/digest.ts
4
- const SHA2_256_MULTIHASH_CODE = 18;
5
- const SHA2_256_DIGEST_LENGTH = 32;
6
- const MAX_DIGEST_MULTIBASE_LENGTH = 1 + (SHA2_256_DIGEST_LENGTH + 2) * 8;
7
- const textDecoder = new TextDecoder();
8
- const getArrayBufferByteLength = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "byteLength")?.get;
9
- function isCanonicalVarintPrefix(data, prefix) {
10
- const canonical = require_multicodec.addMulticodecPrefix(prefix.code, new Uint8Array());
11
- if (canonical.length !== prefix.prefixLength) return false;
12
- for (let i = 0; i < canonical.length; i++) if (data[i] !== canonical[i]) return false;
13
- return true;
14
- }
15
- function toWebCryptoBytes(bytes) {
16
- if (getArrayBufferByteLength != null) try {
17
- getArrayBufferByteLength.call(bytes.buffer);
18
- return bytes;
19
- } catch {}
20
- return new Uint8Array(bytes);
21
- }
22
- /**
23
- * Computes the SHA-256 digest of a byte sequence and encodes it as a
24
- * base58-btc multibase multihash suitable for FEP-ef61's
25
- * `digestMultibase` property.
26
- *
27
- * @param bytes The bytes to digest.
28
- * @returns The base58-btc multibase-encoded SHA-256 multihash.
29
- * @since 2.4.0
30
- */
31
- async function computeDigestMultibase(bytes) {
32
- const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", toWebCryptoBytes(bytes)));
33
- const multihash = require_multicodec.addMulticodecPrefix(SHA2_256_MULTIHASH_CODE, require_multicodec.addMulticodecPrefix(digest.length, digest));
34
- return textDecoder.decode(require_multibase.encodeMultibase("base58btc", multihash));
35
- }
36
- /**
37
- * Parses and validates a SHA-256 `digestMultibase` value.
38
- *
39
- * @param value The multibase-encoded multihash to parse.
40
- * @returns The digest algorithm and raw digest bytes.
41
- * @throws {TypeError} If the value is malformed or uses an unsupported hash
42
- * algorithm.
43
- * @since 2.4.0
44
- */
45
- function parseDigestMultibase(value) {
46
- if (value.length > MAX_DIGEST_MULTIBASE_LENGTH) throw new TypeError("Invalid digestMultibase encoding.");
47
- let multihash;
48
- try {
49
- multihash = require_multibase.decodeMultibase(value);
50
- } catch (error) {
51
- throw new TypeError("Invalid digestMultibase encoding.", { cause: error });
52
- }
53
- const encoding = require_multibase.encodingFromBaseData(value);
54
- if (value !== textDecoder.decode(require_multibase.encodeMultibase(encoding.name, multihash))) throw new TypeError("Invalid digestMultibase encoding.");
55
- let algorithmPrefix;
56
- try {
57
- algorithmPrefix = require_multicodec.getMulticodecPrefix(multihash);
58
- } catch (error) {
59
- throw new TypeError("Invalid digestMultibase multihash.", { cause: error });
60
- }
61
- if (!isCanonicalVarintPrefix(multihash, algorithmPrefix)) throw new TypeError("Invalid digestMultibase multihash.");
62
- if (algorithmPrefix.code !== SHA2_256_MULTIHASH_CODE) throw new TypeError(`Unsupported digest algorithm: 0x${algorithmPrefix.code.toString(16)}`);
63
- const lengthAndDigest = multihash.subarray(algorithmPrefix.prefixLength);
64
- let lengthPrefix;
65
- try {
66
- lengthPrefix = require_multicodec.getMulticodecPrefix(lengthAndDigest);
67
- } catch (error) {
68
- throw new TypeError("Invalid digestMultibase multihash.", { cause: error });
69
- }
70
- if (!isCanonicalVarintPrefix(lengthAndDigest, lengthPrefix)) throw new TypeError("Invalid digestMultibase multihash.");
71
- const digest = lengthAndDigest.slice(lengthPrefix.prefixLength);
72
- if (lengthPrefix.code !== SHA2_256_DIGEST_LENGTH || digest.length !== SHA2_256_DIGEST_LENGTH) throw new TypeError("Invalid SHA-256 digest length.");
73
- return {
74
- algorithm: "sha2-256",
75
- digest
76
- };
77
- }
78
- function extractDigestMultibase(value) {
79
- const hashlink = typeof value === "string" ? value : value.href;
80
- const match = /^hl:([^:\r\n\u2028\u2029]+)$/i.exec(hashlink);
81
- if (match == null || match[0].length !== hashlink.length) throw new TypeError("Invalid simple hashlink.");
82
- return match[1];
83
- }
84
- /**
85
- * Parses a metadata-free `hl:` hashlink and validates its resource digest.
86
- *
87
- * @param value The simple hashlink to parse.
88
- * @returns The `digestMultibase` value carried by the hashlink.
89
- * @throws {TypeError} If the hashlink is malformed, contains metadata, or
90
- * carries an invalid or unsupported digest.
91
- * @since 2.4.0
92
- */
93
- function parseHashlink(value) {
94
- const digestMultibase = extractDigestMultibase(value);
95
- parseDigestMultibase(digestMultibase);
96
- return { digestMultibase };
97
- }
98
- /**
99
- * Creates a metadata-free `hl:` hashlink from a `digestMultibase` value.
100
- *
101
- * @param digestMultibase The SHA-256 multibase multihash to embed.
102
- * @returns The simple hashlink.
103
- * @throws {TypeError} If the digest is malformed or unsupported.
104
- * @since 2.4.0
105
- */
106
- function createHashlink(digestMultibase) {
107
- parseDigestMultibase(digestMultibase);
108
- return `hl:${digestMultibase}`;
109
- }
110
- /**
111
- * Verifies that bytes match a SHA-256 `digestMultibase` value.
112
- *
113
- * @param bytes The bytes to verify.
114
- * @param digestMultibase The expected digest.
115
- * @returns Whether the bytes match the digest.
116
- * @throws {TypeError} If the digest is malformed or unsupported.
117
- * @since 2.4.0
118
- */
119
- async function verifyDigestMultibase(bytes, digestMultibase) {
120
- const expected = parseDigestMultibase(digestMultibase).digest;
121
- const actual = new Uint8Array(await crypto.subtle.digest("SHA-256", toWebCryptoBytes(bytes)));
122
- if (expected.length !== actual.length) return false;
123
- let difference = 0;
124
- for (let i = 0; i < expected.length; i++) difference |= expected[i] ^ actual[i];
125
- return difference === 0;
126
- }
127
- /**
128
- * Verifies that bytes match the resource digest in a simple hashlink.
129
- *
130
- * @param bytes The bytes to verify.
131
- * @param hashlink The simple hashlink containing the expected digest.
132
- * @returns Whether the bytes match the hashlink digest.
133
- * @throws {TypeError} If the hashlink or digest is malformed or unsupported.
134
- * @since 2.4.0
135
- */
136
- async function verifyHashlink(bytes, hashlink) {
137
- return await verifyDigestMultibase(bytes, extractDigestMultibase(hashlink));
138
- }
139
- //#endregion
140
- Object.defineProperty(exports, "computeDigestMultibase", {
141
- enumerable: true,
142
- get: function() {
143
- return computeDigestMultibase;
144
- }
145
- });
146
- Object.defineProperty(exports, "createHashlink", {
147
- enumerable: true,
148
- get: function() {
149
- return createHashlink;
150
- }
151
- });
152
- Object.defineProperty(exports, "parseDigestMultibase", {
153
- enumerable: true,
154
- get: function() {
155
- return parseDigestMultibase;
156
- }
157
- });
158
- Object.defineProperty(exports, "parseHashlink", {
159
- enumerable: true,
160
- get: function() {
161
- return parseHashlink;
162
- }
163
- });
164
- Object.defineProperty(exports, "verifyDigestMultibase", {
165
- enumerable: true,
166
- get: function() {
167
- return verifyDigestMultibase;
168
- }
169
- });
170
- Object.defineProperty(exports, "verifyHashlink", {
171
- enumerable: true,
172
- get: function() {
173
- return verifyHashlink;
174
- }
175
- });