@fedify/fedify 2.2.2 → 2.2.3

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 (60) hide show
  1. package/dist/{builder-Bw2K7xaq.mjs → builder-CaVN56-q.mjs} +2 -2
  2. package/dist/compat/mod.d.cts +1 -1
  3. package/dist/compat/mod.d.ts +1 -1
  4. package/dist/compat/transformers.test.mjs +1 -1
  5. package/dist/{context-BPMgyX7m.d.ts → context-BU-1O90h.d.ts} +48 -6
  6. package/dist/{context-DwkhwUX9.d.cts → context-DVA8wHZ0.d.cts} +48 -6
  7. package/dist/{deno-DCXSCeB3.mjs → deno-DMg4SgCb.mjs} +1 -1
  8. package/dist/{docloader-BqX3RfO-.mjs → docloader-Da15YRxG.mjs} +2 -2
  9. package/dist/federation/builder.test.mjs +1 -1
  10. package/dist/federation/handler.test.mjs +1363 -43
  11. package/dist/federation/idempotency.test.mjs +2 -2
  12. package/dist/federation/middleware.test.mjs +1584 -80
  13. package/dist/federation/mod.cjs +1 -1
  14. package/dist/federation/mod.d.cts +2 -2
  15. package/dist/federation/mod.d.ts +2 -2
  16. package/dist/federation/mod.js +1 -1
  17. package/dist/federation/retry.test.mjs +1 -1
  18. package/dist/federation/send.test.mjs +3 -3
  19. package/dist/federation/temporal.test.d.mts +2 -0
  20. package/dist/federation/temporal.test.mjs +71 -0
  21. package/dist/federation/webfinger.test.mjs +1 -1
  22. package/dist/{http-FZD1s7yO.js → http-BPPaA2uz.js} +1 -1
  23. package/dist/{http-Bslazr8_.mjs → http-C_edJspG.mjs} +2 -2
  24. package/dist/{http-DhcDuRya.cjs → http-Cl0Q2bUO.cjs} +1 -1
  25. package/dist/{key-Dk8-s0bs.mjs → key-BAQuZEU1.mjs} +1 -1
  26. package/dist/{kv-cache-BYBaA6NP.js → kv-cache-C4DGZ_t4.js} +1 -1
  27. package/dist/{kv-cache-Csre9hbs.cjs → kv-cache-DmGi6uC-.cjs} +1 -1
  28. package/dist/ld-tusP_XxG.mjs +573 -0
  29. package/dist/{middleware-DBU9HFil.cjs → middleware-0V-9qj7m.cjs} +399 -73
  30. package/dist/{middleware-BEw9z4kK.js → middleware-Ar1QOOPG.js} +396 -71
  31. package/dist/{middleware-AO-0hf_I.mjs → middleware-D9k0Knum.mjs} +314 -78
  32. package/dist/{middleware-D5H3hLTL.cjs → middleware-OQPBzyvx.cjs} +1 -1
  33. package/dist/{middleware-CDwTn0jH.mjs → middleware-madKLp2f.mjs} +1 -1
  34. package/dist/{mod-CNAHY39V.d.ts → mod-BVt6iTmH.d.ts} +1 -1
  35. package/dist/{mod-Bi6WOdti.d.cts → mod-q-NFLW6B.d.cts} +1 -1
  36. package/dist/mod.cjs +4 -4
  37. package/dist/mod.d.cts +2 -2
  38. package/dist/mod.d.ts +2 -2
  39. package/dist/mod.js +4 -4
  40. package/dist/nodeinfo/handler.test.mjs +1 -1
  41. package/dist/{owner-SWOTNh3d.mjs → owner-DRHNR5YO.mjs} +2 -2
  42. package/dist/{proof-BMoWLd-F.mjs → proof-DLhLRv3m.mjs} +2 -2
  43. package/dist/{proof-e42uuEG9.cjs → proof-DfrItHmh.cjs} +351 -3
  44. package/dist/{proof-BT_TJd2j.js → proof-SQ4cQs3A.js} +298 -4
  45. package/dist/{send-ir10TETC.mjs → send-C7tim5U9.mjs} +2 -2
  46. package/dist/sig/http.test.mjs +2 -2
  47. package/dist/sig/key.test.mjs +1 -1
  48. package/dist/sig/ld.test.mjs +558 -2
  49. package/dist/sig/mod.cjs +2 -2
  50. package/dist/sig/mod.js +2 -2
  51. package/dist/sig/owner.test.mjs +1 -1
  52. package/dist/sig/proof.test.mjs +1 -1
  53. package/dist/temporal-LL61Ddf2.mjs +95 -0
  54. package/dist/testing/mod.d.mts +48 -6
  55. package/dist/utils/docloader.test.mjs +2 -2
  56. package/dist/utils/mod.cjs +1 -1
  57. package/dist/utils/mod.js +1 -1
  58. package/package.json +6 -6
  59. package/dist/ld-BPA4jaBc.mjs +0 -279
  60. /package/dist/{retry-bMXBL97A.mjs → retry-v_sGLH1d.mjs} +0 -0
@@ -0,0 +1,573 @@
1
+ import "@js-temporal/polyfill";
2
+ import "urlpattern-polyfill";
3
+ globalThis.addEventListener = () => {};
4
+ import { n as version, t as name } from "./deno-DMg4SgCb.mjs";
5
+ import { n as fetchKey, o as validateCryptoKey } from "./key-BAQuZEU1.mjs";
6
+ import { Activity, CryptographicKey, Object as Object$1, getTypeId } from "@fedify/vocab";
7
+ import { SpanStatusCode, trace } from "@opentelemetry/api";
8
+ import { getDocumentLoader } from "@fedify/vocab-runtime";
9
+ import { getLogger } from "@logtape/logtape";
10
+ import { decodeBase64, encodeBase64 } from "byte-encodings/base64";
11
+ import { encodeHex } from "byte-encodings/hex";
12
+ import jsonld from "@fedify/vocab-runtime/jsonld";
13
+ //#region src/sig/ld.ts
14
+ const logger = getLogger([
15
+ "fedify",
16
+ "sig",
17
+ "ld"
18
+ ]);
19
+ const localContext = [
20
+ "https://w3id.org/identity/v1",
21
+ "https://www.w3.org/ns/activitystreams",
22
+ "https://w3id.org/security/v1",
23
+ "https://w3id.org/security/data-integrity/v1"
24
+ ];
25
+ const localContextUrls = new Set(localContext);
26
+ const builtInContextLoader = getDocumentLoader();
27
+ const disallowedJsonLdKeywords = new Set([
28
+ "@graph",
29
+ "@included",
30
+ "@reverse"
31
+ ]);
32
+ /** @internal */
33
+ var UnsafeJsonLdError = class extends TypeError {
34
+ constructor(keyword) {
35
+ super(`Unsupported JSON-LD keyword: ${keyword}.`);
36
+ this.keyword = keyword;
37
+ this.name = "UnsafeJsonLdError";
38
+ }
39
+ };
40
+ /** @internal */
41
+ var InvalidContextReferenceError = class extends TypeError {
42
+ constructor(reference) {
43
+ super(`Invalid JSON-LD context reference: ${reference}.`);
44
+ this.reference = reference;
45
+ this.name = "InvalidContextReferenceError";
46
+ }
47
+ };
48
+ function createLoadingRemoteContextFailedError(reference, cause) {
49
+ const message = cause instanceof Error ? cause.message : String(cause);
50
+ const error = /* @__PURE__ */ new Error(`Dereferencing a URL did not result in a valid JSON-LD context: ${reference}. ${message}`);
51
+ error.name = "jsonld.InvalidUrl";
52
+ error.details = {
53
+ code: "loading remote context failed",
54
+ url: reference
55
+ };
56
+ error.cause = cause;
57
+ return error;
58
+ }
59
+ /** @internal */
60
+ function isClearlyMalformedContextReference(reference) {
61
+ for (const char of reference) {
62
+ const code = char.charCodeAt(0);
63
+ if (code <= 32 || code === 127) return true;
64
+ }
65
+ if (/^[A-Za-z][A-Za-z0-9+.-]*:/.test(reference) && !URL.canParse(reference)) return true;
66
+ for (let i = 0; i < reference.length; i++) {
67
+ if (reference[i] !== "%") continue;
68
+ if (i + 2 >= reference.length || !/[0-9A-Fa-f]/.test(reference[i + 1]) || !/[0-9A-Fa-f]/.test(reference[i + 2])) return true;
69
+ i += 2;
70
+ }
71
+ if (reference.startsWith("./") || reference.startsWith("../") || reference.startsWith("/") || reference.startsWith("//")) {
72
+ for (const char of reference) if ("[]<>\"\\^`{|}".includes(char)) return true;
73
+ }
74
+ return false;
75
+ }
76
+ function cloneRemoteDocument(remoteDocument) {
77
+ return structuredClone(remoteDocument);
78
+ }
79
+ function createMemoizedDocumentLoader(documentLoader) {
80
+ const cache = /* @__PURE__ */ new Map();
81
+ return async (url, options) => {
82
+ const cacheKey = URL.canParse(url) ? new URL(url).href : url;
83
+ let remoteDocument = cache.get(cacheKey);
84
+ if (remoteDocument == null) {
85
+ remoteDocument = Promise.resolve(documentLoader(url, options)).then(cloneRemoteDocument);
86
+ remoteDocument.catch(() => {
87
+ if (cache.get(cacheKey) === remoteDocument) cache.delete(cacheKey);
88
+ });
89
+ cache.set(cacheKey, remoteDocument);
90
+ }
91
+ return cloneRemoteDocument(await remoteDocument);
92
+ };
93
+ }
94
+ /** @internal */
95
+ function wrapContextLoaderForJsonLd(contextLoader) {
96
+ const loader = contextLoader ?? builtInContextLoader;
97
+ return async (url, options) => {
98
+ try {
99
+ return await loader(url, options);
100
+ } catch (error) {
101
+ if (!isInvalidUrlTypeError(error)) throw error;
102
+ if (isClearlyMalformedContextReference(url)) throw new InvalidContextReferenceError(url);
103
+ throw createLoadingRemoteContextFailedError(url, error);
104
+ }
105
+ };
106
+ }
107
+ /** @internal */
108
+ function getNormalizationContextLoader(contextLoader) {
109
+ const loader = wrapContextLoaderForJsonLd(contextLoader);
110
+ return createMemoizedDocumentLoader(async (url, options) => {
111
+ if (URL.canParse(url)) {
112
+ const normalizedUrl = new URL(url).href;
113
+ if (localContextUrls.has(normalizedUrl)) return await builtInContextLoader(normalizedUrl, options);
114
+ }
115
+ return await loader(url, options);
116
+ });
117
+ }
118
+ /** @internal */
119
+ async function compactJsonLd(jsonLd, contextLoader) {
120
+ const hasLds = typeof jsonLd === "object" && jsonLd != null && "signature" in jsonLd;
121
+ const signature = hasLds ? jsonLd.signature : void 0;
122
+ const normalizationContextLoader = getNormalizationContextLoader(contextLoader);
123
+ const document = hasLds ? detachSignature(jsonLd) : jsonLd;
124
+ await assertNoGraphBeforeCompaction(document, normalizationContextLoader);
125
+ const compacted = await jsonld.compact(document, localContext, { documentLoader: normalizationContextLoader });
126
+ if (hasLds && typeof compacted === "object" && compacted != null) compacted.signature = signature;
127
+ assertSafeJsonLd(compacted);
128
+ return compacted;
129
+ }
130
+ function createInvalidRemoteContextError(reference) {
131
+ const error = /* @__PURE__ */ new Error(`Dereferencing a URL did not result in a JSON object. The response was valid JSON, but it was not a JSON object. URL: "${reference}".`);
132
+ error.name = "jsonld.InvalidUrl";
133
+ error.details = {
134
+ code: "invalid remote context",
135
+ url: reference
136
+ };
137
+ return error;
138
+ }
139
+ function getRemoteContext(remoteDocument, reference) {
140
+ const { contextUrl, documentUrl } = remoteDocument;
141
+ let { document } = remoteDocument;
142
+ if (typeof document === "string") document = JSON.parse(document);
143
+ if (typeof document !== "object" || document == null || Array.isArray(document)) throw createInvalidRemoteContextError(reference);
144
+ let context = "@context" in document ? document["@context"] : {};
145
+ if (contextUrl != null) context = Array.isArray(context) ? [...context, contextUrl] : [context, contextUrl];
146
+ return {
147
+ context,
148
+ baseUrl: documentUrl ?? reference
149
+ };
150
+ }
151
+ function createGraphAliasContextState() {
152
+ return {
153
+ graphTerms: /* @__PURE__ */ new Set(),
154
+ jsonTerms: /* @__PURE__ */ new Set(),
155
+ propertyContexts: /* @__PURE__ */ new Map(),
156
+ termTargets: /* @__PURE__ */ new Map()
157
+ };
158
+ }
159
+ function cloneGraphAliasContextState(state) {
160
+ return {
161
+ graphTerms: new Set(state.graphTerms),
162
+ jsonTerms: new Set(state.jsonTerms),
163
+ propertyContexts: new Map(state.propertyContexts),
164
+ termTargets: new Map(state.termTargets)
165
+ };
166
+ }
167
+ function resolveContextTarget(target, state) {
168
+ if (target === "@graph") return target;
169
+ const mapped = state.termTargets.get(target);
170
+ if (mapped == null) return target;
171
+ return mapped;
172
+ }
173
+ function getDirectContextTarget(definition) {
174
+ if (definition === null) return null;
175
+ if (typeof definition === "string") return definition;
176
+ if (typeof definition === "object" && definition != null && "@id" in definition) {
177
+ const id = definition["@id"];
178
+ if (id === null) return null;
179
+ if (typeof id === "string") return id;
180
+ }
181
+ }
182
+ function isJsonTypedDefinition(definition) {
183
+ return typeof definition === "object" && definition != null && "@type" in definition && definition["@type"] === "@json";
184
+ }
185
+ function resolveLocalContextTarget(target, state, localTargets, seen = /* @__PURE__ */ new Set()) {
186
+ if (target === "@graph") return target;
187
+ if (seen.has(target)) return target;
188
+ seen.add(target);
189
+ if (localTargets.has(target)) {
190
+ const localTarget = localTargets.get(target);
191
+ return localTarget == null ? target : resolveLocalContextTarget(localTarget, state, localTargets, seen);
192
+ }
193
+ return resolveContextTarget(target, state);
194
+ }
195
+ function refreshGraphAliases(state) {
196
+ state.graphTerms.clear();
197
+ for (const [term, target] of state.termTargets) if (target === "@graph") state.graphTerms.add(term);
198
+ }
199
+ function normalizeContextReference(reference, baseUrl) {
200
+ if (baseUrl != null) return new URL(reference, baseUrl).href;
201
+ return URL.canParse(reference) ? new URL(reference).href : reference;
202
+ }
203
+ /** @internal */
204
+ function isInvalidUrlTypeError(error) {
205
+ const code = error.code;
206
+ return error instanceof TypeError && (code === "ERR_INVALID_URL" || /^Invalid URL(?::|$)/.test(error.message) || / cannot be parsed as a URL\.?$/.test(error.message));
207
+ }
208
+ async function applyGraphAliasContext(state, context, documentLoader, remoteContextCache, baseUrl = null, processingContexts = /* @__PURE__ */ new Set()) {
209
+ if (context === null) return createGraphAliasContextState();
210
+ let nextState = cloneGraphAliasContextState(state);
211
+ if (Array.isArray(context)) {
212
+ for (const item of context) nextState = await applyGraphAliasContext(nextState, item, documentLoader, remoteContextCache, baseUrl, processingContexts);
213
+ return nextState;
214
+ }
215
+ if (typeof context === "string") {
216
+ const reference = normalizeContextReference(context, baseUrl);
217
+ const cacheKey = `${baseUrl ?? ""}\n${reference}`;
218
+ if (processingContexts.has(cacheKey)) return nextState;
219
+ processingContexts.add(cacheKey);
220
+ try {
221
+ let remoteContext = remoteContextCache.get(cacheKey);
222
+ if (remoteContext == null) {
223
+ remoteContext = (async () => {
224
+ try {
225
+ return getRemoteContext(await documentLoader(reference), reference);
226
+ } catch (error) {
227
+ if (reference === context && isInvalidUrlTypeError(error) && isClearlyMalformedContextReference(context)) throw new InvalidContextReferenceError(context);
228
+ throw error;
229
+ }
230
+ })();
231
+ remoteContextCache.set(cacheKey, remoteContext);
232
+ }
233
+ const loadedRemoteContext = await remoteContext;
234
+ return await applyGraphAliasContext(nextState, loadedRemoteContext.context, documentLoader, remoteContextCache, loadedRemoteContext.baseUrl, processingContexts);
235
+ } finally {
236
+ processingContexts.delete(cacheKey);
237
+ }
238
+ }
239
+ if (typeof context === "object" && context != null) {
240
+ if ("@import" in context && typeof context["@import"] === "string") nextState = await applyGraphAliasContext(nextState, context["@import"], documentLoader, remoteContextCache, baseUrl, processingContexts);
241
+ const localTargets = /* @__PURE__ */ new Map();
242
+ for (const [term, definition] of globalThis.Object.entries(context)) {
243
+ if (term.startsWith("@")) continue;
244
+ const target = getDirectContextTarget(definition);
245
+ if (target == null) localTargets.set(term, null);
246
+ else if (typeof target === "string") localTargets.set(term, target);
247
+ else localTargets.delete(term);
248
+ }
249
+ for (const [term, definition] of globalThis.Object.entries(context)) {
250
+ if (term.startsWith("@")) continue;
251
+ if (localTargets.has(term)) {
252
+ const directTarget = localTargets.get(term);
253
+ if (directTarget == null) nextState.termTargets.set(term, null);
254
+ else nextState.termTargets.set(term, resolveLocalContextTarget(directTarget, nextState, localTargets));
255
+ } else nextState.termTargets.delete(term);
256
+ if (typeof definition === "object" && definition != null && "@context" in definition) nextState.propertyContexts.set(term, {
257
+ context: definition["@context"],
258
+ baseUrl
259
+ });
260
+ else nextState.propertyContexts.delete(term);
261
+ if (isJsonTypedDefinition(definition)) nextState.jsonTerms.add(term);
262
+ else nextState.jsonTerms.delete(term);
263
+ }
264
+ refreshGraphAliases(nextState);
265
+ }
266
+ return nextState;
267
+ }
268
+ async function assertNoGraphBeforeCompaction(jsonLd, documentLoader, inheritedState = createGraphAliasContextState(), propertyContext, remoteContextCache = /* @__PURE__ */ new Map()) {
269
+ if (Array.isArray(jsonLd)) {
270
+ for (const item of jsonLd) await assertNoGraphBeforeCompaction(item, documentLoader, inheritedState, propertyContext, remoteContextCache);
271
+ return;
272
+ }
273
+ if (typeof jsonLd !== "object" || jsonLd == null) return;
274
+ const jsonLiteralWrapper = isJsonLiteralWrapper(jsonLd);
275
+ let state = inheritedState;
276
+ if (propertyContext !== void 0) state = await applyGraphAliasContext(state, propertyContext.context, documentLoader, remoteContextCache, propertyContext.baseUrl);
277
+ if ("@context" in jsonLd) state = await applyGraphAliasContext(state, jsonLd["@context"], documentLoader, remoteContextCache);
278
+ for (const [key, value] of globalThis.Object.entries(jsonLd)) {
279
+ if (key === "@context") continue;
280
+ if (jsonLiteralWrapper && key === "@value") continue;
281
+ if (key === "@graph" || state.graphTerms.has(key)) throw new UnsafeJsonLdError("@graph");
282
+ if (state.jsonTerms.has(key)) continue;
283
+ await assertNoGraphBeforeCompaction(value, documentLoader, state, state.propertyContexts.get(key), remoteContextCache);
284
+ }
285
+ }
286
+ function isJsonLiteralWrapper(value) {
287
+ return "@value" in value && (value["@type"] === "@json" || value.type === "@json");
288
+ }
289
+ /** @internal */
290
+ function assertSafeJsonLd(jsonLd) {
291
+ if (Array.isArray(jsonLd)) for (const item of jsonLd) assertSafeJsonLd(item);
292
+ else if (typeof jsonLd === "object" && jsonLd != null) {
293
+ const jsonLiteralWrapper = isJsonLiteralWrapper(jsonLd);
294
+ for (const [key, value] of globalThis.Object.entries(jsonLd)) {
295
+ if (disallowedJsonLdKeywords.has(key)) throw new UnsafeJsonLdError(key);
296
+ if (jsonLiteralWrapper && key === "@value") continue;
297
+ assertSafeJsonLd(value);
298
+ }
299
+ }
300
+ }
301
+ /**
302
+ * Attaches a LD signature to the given JSON-LD document.
303
+ * @param jsonLd The JSON-LD document to attach the signature to. It is not
304
+ * modified.
305
+ * @param signature The signature to attach.
306
+ * @returns The JSON-LD document with the attached signature.
307
+ * @throws {TypeError} If the input document is not a valid JSON-LD document.
308
+ * @since 1.0.0
309
+ */
310
+ function attachSignature(jsonLd, signature) {
311
+ if (typeof jsonLd !== "object" || jsonLd == null) throw new TypeError("Failed to attach signature; invalid JSON-LD document.");
312
+ return {
313
+ ...jsonLd,
314
+ signature
315
+ };
316
+ }
317
+ /**
318
+ * Creates a LD signature for the given JSON-LD document.
319
+ * @param jsonLd The JSON-LD document to sign.
320
+ * @param privateKey The private key to sign the document.
321
+ * @param keyId The ID of the public key that corresponds to the private key.
322
+ * @param options Additional options for creating the signature.
323
+ * See also {@link CreateSignatureOptions}.
324
+ * @return The created signature.
325
+ * @throws {TypeError} If the private key is invalid or unsupported.
326
+ * @since 1.0.0
327
+ */
328
+ async function createSignature(jsonLd, privateKey, keyId, { contextLoader, created } = {}) {
329
+ validateCryptoKey(privateKey, "private");
330
+ if (privateKey.algorithm.name !== "RSASSA-PKCS1-v1_5") throw new TypeError("Unsupported algorithm: " + privateKey.algorithm.name);
331
+ const options = {
332
+ "@context": "https://w3id.org/identity/v1",
333
+ creator: keyId.href,
334
+ created: created?.toString() ?? (/* @__PURE__ */ new Date()).toISOString()
335
+ };
336
+ const message = await hashJsonLd(options, contextLoader) + await hashJsonLd(jsonLd, contextLoader);
337
+ const messageBytes = new TextEncoder().encode(message);
338
+ const signature = await crypto.subtle.sign("RSASSA-PKCS1-v1_5", privateKey, messageBytes);
339
+ return {
340
+ ...options,
341
+ type: "RsaSignature2017",
342
+ signatureValue: encodeBase64(signature)
343
+ };
344
+ }
345
+ /**
346
+ * Signs the given JSON-LD document with the private key and returns the signed
347
+ * JSON-LD document.
348
+ * @param jsonLd The JSON-LD document to sign.
349
+ * @param privateKey The private key to sign the document.
350
+ * @param keyId The key ID to use in the signature. It will be used by the
351
+ * verifier to fetch the corresponding public key.
352
+ * @param options Additional options for signing the document.
353
+ * See also {@link SignJsonLdOptions}.
354
+ * @returns The signed JSON-LD document.
355
+ * @throws {TypeError} If the private key is invalid or unsupported.
356
+ * @since 1.0.0
357
+ */
358
+ async function signJsonLd(jsonLd, privateKey, keyId, options) {
359
+ return await (options.tracerProvider ?? trace.getTracerProvider()).getTracer(name, version).startActiveSpan("ld_signatures.sign", { attributes: { "ld_signatures.key_id": keyId.href } }, async (span) => {
360
+ try {
361
+ const signature = await createSignature(jsonLd, privateKey, keyId, options);
362
+ if (span.isRecording()) {
363
+ span.setAttribute("ld_signatures.type", signature.type);
364
+ span.setAttribute("ld_signatures.signature", encodeHex(decodeBase64(signature.signatureValue)));
365
+ }
366
+ return attachSignature(jsonLd, signature);
367
+ } catch (error) {
368
+ span.setStatus({
369
+ code: SpanStatusCode.ERROR,
370
+ message: String(error)
371
+ });
372
+ throw error;
373
+ } finally {
374
+ span.end();
375
+ }
376
+ });
377
+ }
378
+ /**
379
+ * Checks if the given JSON-LD document has a Linked Data Signature-like
380
+ * object, without restricting it to a single suite-specific shape.
381
+ * @param jsonLd The JSON-LD document to check.
382
+ * @returns `true` if the document has a signature-like object; `false`
383
+ * otherwise.
384
+ * @since 2.2.0
385
+ */
386
+ function hasSignatureLike(jsonLd) {
387
+ if (typeof jsonLd !== "object" || jsonLd == null) return false;
388
+ const signature = jsonLd.signature;
389
+ const hasReference = (value) => {
390
+ if (typeof value === "string") return true;
391
+ if (Array.isArray(value)) return value.some(hasReference);
392
+ return typeof value === "object" && value != null && ("id" in value && typeof value.id === "string" || "@id" in value && typeof value["@id"] === "string");
393
+ };
394
+ const hasSignatureObject = (value) => {
395
+ if (typeof value !== "object" || value == null) return false;
396
+ const signatureRecord = value;
397
+ return (typeof signatureRecord.type === "string" || Array.isArray(signatureRecord.type) && signatureRecord.type.some((item) => typeof item === "string")) && (hasReference(signatureRecord.creator) || hasReference(signatureRecord.verificationMethod)) && (typeof signatureRecord.signatureValue === "string" || typeof signatureRecord.jws === "string");
398
+ };
399
+ return Array.isArray(signature) ? signature.some(hasSignatureObject) : hasSignatureObject(signature);
400
+ }
401
+ /**
402
+ * Checks if the given JSON-LD document has a Linked Data Signature.
403
+ * @param jsonLd The JSON-LD document to check.
404
+ * @returns `true` if the document has a signature; `false` otherwise.
405
+ * @since 1.0.0
406
+ */
407
+ function hasSignature(jsonLd) {
408
+ if (typeof jsonLd !== "object" || jsonLd == null) return false;
409
+ if ("signature" in jsonLd) {
410
+ const signature = jsonLd.signature;
411
+ if (typeof signature !== "object" || signature == null) return false;
412
+ return "type" in signature && signature.type === "RsaSignature2017" && "creator" in signature && typeof signature.creator === "string" && "created" in signature && typeof signature.created === "string" && "signatureValue" in signature && typeof signature.signatureValue === "string";
413
+ }
414
+ return false;
415
+ }
416
+ /**
417
+ * Detaches Linked Data Signatures from the given JSON-LD document.
418
+ * @param jsonLd The JSON-LD document to modify.
419
+ * @returns The modified JSON-LD document. If the input document does not
420
+ * contain a signature, the original document is returned.
421
+ * @since 1.0.0
422
+ */
423
+ function detachSignature(jsonLd) {
424
+ if (typeof jsonLd !== "object" || jsonLd == null) return jsonLd;
425
+ const doc = { ...jsonLd };
426
+ delete doc.signature;
427
+ return doc;
428
+ }
429
+ /**
430
+ * Verifies Linked Data Signatures of the given JSON-LD document.
431
+ * @param jsonLd The JSON-LD document to verify.
432
+ * @param options Options for verifying the signature.
433
+ * @returns The public key that signed the document or `null` if the signature
434
+ * is invalid or the key is not found.
435
+ * @since 1.0.0
436
+ */
437
+ async function verifySignature(jsonLd, options = {}) {
438
+ if (!hasSignature(jsonLd)) return null;
439
+ const sig = jsonLd.signature;
440
+ let signature;
441
+ try {
442
+ signature = decodeBase64(sig.signatureValue);
443
+ } catch (error) {
444
+ logger.debug("Failed to verify; invalid base64 signatureValue: {signatureValue}", {
445
+ ...sig,
446
+ error
447
+ });
448
+ return null;
449
+ }
450
+ const { key, cached } = await fetchKey(new URL(sig.creator), CryptographicKey, options);
451
+ if (key == null) return null;
452
+ const sigOpts = {
453
+ ...sig,
454
+ "@context": "https://w3id.org/identity/v1"
455
+ };
456
+ delete sigOpts.type;
457
+ delete sigOpts.id;
458
+ delete sigOpts.signatureValue;
459
+ let sigOptsHash;
460
+ try {
461
+ sigOptsHash = await hashJsonLd(sigOpts, options.contextLoader);
462
+ } catch (error) {
463
+ logger.warn("Failed to verify; failed to hash the signature options: {signatureOptions}\n{error}", {
464
+ signatureOptions: sigOpts,
465
+ error
466
+ });
467
+ return null;
468
+ }
469
+ const document = { ...jsonLd };
470
+ delete document.signature;
471
+ let docHash;
472
+ try {
473
+ docHash = await hashJsonLd(document, options.contextLoader);
474
+ } catch (error) {
475
+ logger.warn("Failed to verify; failed to hash the document: {document}\n{error}", {
476
+ document,
477
+ error
478
+ });
479
+ return null;
480
+ }
481
+ const encoder = new TextEncoder();
482
+ const message = sigOptsHash + docHash;
483
+ const messageBytes = encoder.encode(message);
484
+ if (await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key.publicKey, signature.slice(), messageBytes)) return key;
485
+ if (cached) {
486
+ logger.debug("Failed to verify with the cached key {keyId}; signature {signatureValue} is invalid. Retrying with the freshly fetched key...", {
487
+ keyId: sig.creator,
488
+ ...sig
489
+ });
490
+ const { key } = await fetchKey(new URL(sig.creator), CryptographicKey, {
491
+ ...options,
492
+ keyCache: {
493
+ get: () => Promise.resolve(void 0),
494
+ set: async (keyId, key) => await options.keyCache?.set(keyId, key)
495
+ }
496
+ });
497
+ if (key == null) return null;
498
+ return await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key.publicKey, signature.slice(), messageBytes) ? key : null;
499
+ }
500
+ logger.debug("Failed to verify with the fetched key {keyId}; signature {signatureValue} is invalid. Check if the key is correct or if the signed message is correct. The message to sign is:\n{message}", {
501
+ keyId: sig.creator,
502
+ ...sig,
503
+ message
504
+ });
505
+ return null;
506
+ }
507
+ /**
508
+ * Verify the authenticity of the given JSON-LD document using Linked Data
509
+ * Signatures. If the document is signed, this function verifies the signature
510
+ * and checks if the document is attributed to the owner of the public key.
511
+ * If the document is not signed, this function returns `false`.
512
+ * @param jsonLd The JSON-LD document to verify.
513
+ * @param options Options for verifying the document.
514
+ * @returns `true` if the document is authentic; `false` otherwise.
515
+ */
516
+ async function verifyJsonLd(jsonLd, options = {}) {
517
+ return await verifyJsonLdInternal(jsonLd, options, true);
518
+ }
519
+ /** @internal */
520
+ async function verifyCompactJsonLd(jsonLd, options = {}) {
521
+ return await verifyJsonLdInternal(jsonLd, options, false);
522
+ }
523
+ async function verifyJsonLdInternal(jsonLd, options, compact) {
524
+ return await (options.tracerProvider ?? trace.getTracerProvider()).getTracer(name, version).startActiveSpan("ld_signatures.verify", async (span) => {
525
+ try {
526
+ const verificationOptions = hasSignature(jsonLd) ? {
527
+ ...options,
528
+ contextLoader: getNormalizationContextLoader(options.contextLoader)
529
+ } : options;
530
+ const compacted = compact ? hasSignature(jsonLd) ? await compactJsonLd(jsonLd, options.contextLoader) : jsonLd : jsonLd;
531
+ const object = await Object$1.fromJsonLd(compacted, verificationOptions);
532
+ if (object.id != null) span.setAttribute("activitypub.object.id", object.id.href);
533
+ span.setAttribute("activitypub.object.type", getTypeId(object).href);
534
+ if (typeof jsonLd === "object" && jsonLd != null && "signature" in jsonLd && typeof jsonLd.signature === "object" && jsonLd.signature != null) {
535
+ if ("creator" in jsonLd.signature && typeof jsonLd.signature.creator === "string") span.setAttribute("ld_signatures.key_id", jsonLd.signature.creator);
536
+ if ("signatureValue" in jsonLd.signature && typeof jsonLd.signature.signatureValue === "string") span.setAttribute("ld_signatures.signature", jsonLd.signature.signatureValue);
537
+ if ("type" in jsonLd.signature && typeof jsonLd.signature.type === "string") span.setAttribute("ld_signatures.type", jsonLd.signature.type);
538
+ }
539
+ const attributions = new Set(object.attributionIds.map((uri) => uri.href));
540
+ if (object instanceof Activity) for (const uri of object.actorIds) attributions.add(uri.href);
541
+ const key = await verifySignature(compacted, verificationOptions);
542
+ if (key == null) return false;
543
+ if (key.ownerId == null) {
544
+ logger.debug("Key {keyId} has no owner.", { keyId: key.id?.href });
545
+ return false;
546
+ }
547
+ attributions.delete(key.ownerId.href);
548
+ if (attributions.size > 0) {
549
+ logger.debug("Some attributions are not authenticated by the Linked Data Signatures: {attributions}.", { attributions: [...attributions] });
550
+ return false;
551
+ }
552
+ return true;
553
+ } catch (error) {
554
+ span.setStatus({
555
+ code: SpanStatusCode.ERROR,
556
+ message: String(error)
557
+ });
558
+ throw error;
559
+ } finally {
560
+ span.end();
561
+ }
562
+ });
563
+ }
564
+ async function hashJsonLd(jsonLd, contextLoader) {
565
+ const canon = await jsonld.canonize(jsonLd, {
566
+ format: "application/n-quads",
567
+ documentLoader: contextLoader ?? getDocumentLoader()
568
+ });
569
+ const encoder = new TextEncoder();
570
+ return encodeHex(await crypto.subtle.digest("SHA-256", encoder.encode(canon)));
571
+ }
572
+ //#endregion
573
+ export { wrapContextLoaderForJsonLd as _, compactJsonLd as a, getNormalizationContextLoader as c, isClearlyMalformedContextReference as d, isInvalidUrlTypeError as f, verifySignature as g, verifyJsonLd as h, attachSignature as i, hasSignature as l, verifyCompactJsonLd as m, UnsafeJsonLdError as n, createSignature as o, signJsonLd as p, assertSafeJsonLd as r, detachSignature as s, InvalidContextReferenceError as t, hasSignatureLike as u };