@fedify/fedify 2.0.36-dev.2746 → 2.0.36-dev.2779

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 (67) hide show
  1. package/dist/attachment-arrays-D43igPLB.mjs +111 -0
  2. package/dist/{builder-CnyFdlUn.mjs → builder-UXDfZSno.mjs} +3 -3
  3. package/dist/compat/attachment-arrays.test.d.mts +2 -0
  4. package/dist/compat/attachment-arrays.test.mjs +254 -0
  5. package/dist/compat/transformers.test.mjs +1 -1
  6. package/dist/{deno-DCXndlEx.mjs → deno-Bwn7hAXr.mjs} +1 -1
  7. package/dist/{docloader-7M06kit4.mjs → docloader-DSQXpyiJ.mjs} +2 -2
  8. package/dist/federation/attachment-responses.test.d.mts +2 -0
  9. package/dist/federation/attachment-responses.test.mjs +182 -0
  10. package/dist/federation/builder.test.mjs +1 -1
  11. package/dist/federation/collection.test.mjs +1 -1
  12. package/dist/federation/delivery-attachments.test.d.mts +2 -0
  13. package/dist/federation/delivery-attachments.test.mjs +280 -0
  14. package/dist/federation/handler.test.mjs +43 -14
  15. package/dist/federation/idempotency.test.mjs +2 -2
  16. package/dist/federation/inbox.test.mjs +1 -1
  17. package/dist/federation/keycache.test.mjs +1 -1
  18. package/dist/federation/middleware.test.mjs +7 -7
  19. package/dist/federation/mod.cjs +1 -1
  20. package/dist/federation/mod.js +1 -1
  21. package/dist/federation/negotiation.test.mjs +1 -1
  22. package/dist/federation/retry.test.mjs +1 -1
  23. package/dist/federation/send.test.mjs +3 -3
  24. package/dist/federation/temporal.test.mjs +1 -1
  25. package/dist/federation/webfinger.test.mjs +1 -1
  26. package/dist/{http-hjoPA7jZ.js → http-C-P8JP1h.js} +1 -1
  27. package/dist/{http-hi5IS6qw.mjs → http-DuIId6ul.mjs} +2 -2
  28. package/dist/{http-BhaAN2KG.cjs → http-aVfiO4CG.cjs} +1 -1
  29. package/dist/{inbox-Bygi511A.mjs → inbox-Cq4_-nPi.mjs} +1 -1
  30. package/dist/{key-DYyRTxPH.mjs → key-BMJlknME.mjs} +1 -1
  31. package/dist/{kv-cache-Bjnv9p9e.cjs → kv-cache-C6BYEp2D.cjs} +1 -1
  32. package/dist/{kv-cache-YZ2bpa_P.js → kv-cache-PAsTBkxX.js} +1 -1
  33. package/dist/{ld-CKUnk1Ps.mjs → ld-oemavcUi.mjs} +2 -2
  34. package/dist/{middleware-D5uql06B.js → middleware-BSMUxoX2.js} +259 -32
  35. package/dist/{middleware-dYPOyDLN.mjs → middleware-Bw59VTh0.mjs} +1 -1
  36. package/dist/{middleware-C_4rWloR.cjs → middleware-CjwdngG9.cjs} +260 -32
  37. package/dist/{middleware-yx080dbw.mjs → middleware-D8Hh8RSu.mjs} +170 -45
  38. package/dist/{middleware-BDCfPddM.cjs → middleware-sDUMisQG.cjs} +1 -1
  39. package/dist/mod.cjs +4 -4
  40. package/dist/mod.js +4 -4
  41. package/dist/nodeinfo/handler.test.mjs +1 -1
  42. package/dist/nodeinfo/types.test.mjs +1 -1
  43. package/dist/{owner-CqHBbNXl.mjs → owner-CYTWux20.mjs} +2 -2
  44. package/dist/{proof-CDalRU9r.mjs → proof-B7pdOJb5.mjs} +2 -2
  45. package/dist/{proof-D2RNLHHt.js → proof-DWny6g-g.js} +2 -2
  46. package/dist/{proof-DLqGjdaL.cjs → proof-DXp_5fqW.cjs} +3 -3
  47. package/dist/{send-XHpoZpxW.mjs → send-7rTXYfE8.mjs} +2 -2
  48. package/dist/sig/http.test.mjs +2 -2
  49. package/dist/sig/key-types.test.d.mts +1 -1
  50. package/dist/sig/key.test.mjs +2 -2
  51. package/dist/sig/ld.test.mjs +2 -2
  52. package/dist/sig/mod.cjs +2 -2
  53. package/dist/sig/mod.js +2 -2
  54. package/dist/sig/owner.test.mjs +1 -1
  55. package/dist/sig/proof.test.mjs +26 -3
  56. package/dist/{temporal-D3yEsvvS.mjs → temporal-CxaXuWOi.mjs} +1 -1
  57. package/dist/testing/mod.d.mts +1 -1
  58. package/dist/utils/docloader.test.mjs +2 -2
  59. package/dist/utils/mod.cjs +1 -1
  60. package/dist/utils/mod.js +1 -1
  61. package/package.json +6 -6
  62. /package/dist/{callback-BFMEE_9V.d.mts → callback-ySj5p3Ml.d.mts} +0 -0
  63. /package/dist/{collection-ChgDTHLz.mjs → collection-BD6-SZ6O.mjs} +0 -0
  64. /package/dist/{keycache-crDMn2HK.mjs → keycache-BhLorx33.mjs} +0 -0
  65. /package/dist/{negotiation-BehA2uul.mjs → negotiation-DnsfFF8I.mjs} +0 -0
  66. /package/dist/{retry-B_E3V_Dx.mjs → retry-CJL0poaU.mjs} +0 -0
  67. /package/dist/{types-C37hquWI.mjs → types-DCP0WLdt.mjs} +0 -0
@@ -2,10 +2,10 @@ const { Temporal } = require("@js-temporal/polyfill");
2
2
  const { URLPattern } = require("urlpattern-polyfill");
3
3
  const require_chunk = require("./chunk-DDcVe30Y.cjs");
4
4
  const require_transformers = require("./transformers-NeAONrAq.cjs");
5
- const require_http = require("./http-BhaAN2KG.cjs");
5
+ const require_http = require("./http-aVfiO4CG.cjs");
6
6
  const require_types = require("./types-DOvyymM4.cjs");
7
- const require_proof = require("./proof-DLqGjdaL.cjs");
8
- const require_kv_cache = require("./kv-cache-Bjnv9p9e.cjs");
7
+ const require_proof = require("./proof-DXp_5fqW.cjs");
8
+ const require_kv_cache = require("./kv-cache-C6BYEp2D.cjs");
9
9
  let _logtape_logtape = require("@logtape/logtape");
10
10
  let _fedify_vocab = require("@fedify/vocab");
11
11
  let _opentelemetry_api = require("@opentelemetry/api");
@@ -14,10 +14,12 @@ let uri_template_router = require("uri-template-router");
14
14
  let url_template = require("url-template");
15
15
  let byte_encodings_hex = require("byte-encodings/hex");
16
16
  let _fedify_vocab_runtime = require("@fedify/vocab-runtime");
17
- let _fedify_vocab_runtime_internal = require("@fedify/vocab-runtime/internal");
18
- let _opentelemetry_semantic_conventions = require("@opentelemetry/semantic-conventions");
19
17
  let _fedify_vocab_runtime_jsonld = require("@fedify/vocab-runtime/jsonld");
20
18
  _fedify_vocab_runtime_jsonld = require_chunk.__toESM(_fedify_vocab_runtime_jsonld);
19
+ let _fedify_vocab_runtime_internal = require("@fedify/vocab-runtime/internal");
20
+ let _opentelemetry_semantic_conventions = require("@opentelemetry/semantic-conventions");
21
+ let json_canon = require("json-canon");
22
+ json_canon = require_chunk.__toESM(json_canon);
21
23
  let _fedify_vocab_runtime_temporal = require("@fedify/vocab-runtime/temporal");
22
24
  let _fedify_webfinger = require("@fedify/webfinger");
23
25
  let node_url = require("node:url");
@@ -351,7 +353,7 @@ var FederationBuilderImpl = class {
351
353
  this.collectionTypeIds = {};
352
354
  }
353
355
  async build(options) {
354
- const { FederationImpl } = await Promise.resolve().then(() => require("./middleware-BDCfPddM.cjs"));
356
+ const { FederationImpl } = await Promise.resolve().then(() => require("./middleware-sDUMisQG.cjs"));
355
357
  const f = new FederationImpl(options);
356
358
  const trailingSlashInsensitiveValue = f.router.trailingSlashInsensitive;
357
359
  f.router = this.router.clone();
@@ -878,6 +880,110 @@ async function buildCollectionSynchronizationHeader(collectionId, actorIds) {
878
880
  return `collectionId="${collectionId}", url="${url}", digest="${(0, byte_encodings_hex.encodeHex)(await digest(actorIds))}"`;
879
881
  }
880
882
  //#endregion
883
+ //#region src/compat/attachment-arrays.ts
884
+ const logger$1 = (0, _logtape_logtape.getLogger)([
885
+ "fedify",
886
+ "compat",
887
+ "attachment-arrays"
888
+ ]);
889
+ const MAX_DEPTH = 64;
890
+ const ATTACHMENT = "https://www.w3.org/ns/activitystreams#attachment";
891
+ const PROOF = "https://w3id.org/security#proof";
892
+ /** Recognizes JSON-LD maps without accepting arrays or scalar values. */
893
+ function isMap(value) {
894
+ return typeof value === "object" && value != null && !Array.isArray(value);
895
+ }
896
+ /** Copies only changed branches; cached vocabulary JSON belongs to its owner. */
897
+ function walk(value, depth = 0) {
898
+ if (depth >= MAX_DEPTH) throw new RangeError("JSON-LD nesting limit exceeded.");
899
+ if (Array.isArray(value)) {
900
+ const items = value.map((item) => walk(item, depth + 1));
901
+ return items.some((item, i) => item !== value[i]) ? items : value;
902
+ }
903
+ if (!isMap(value)) return value;
904
+ if (Object.hasOwn(value, "@value")) return value;
905
+ let result;
906
+ for (const [key, item] of Object.entries(value)) {
907
+ let next = key === "@context" ? item : walk(item, depth + 1);
908
+ if ((key === "attachment" || key === ATTACHMENT) && next != null && !Array.isArray(next) && !(isMap(next) && Object.hasOwn(next, "@list"))) next = [next];
909
+ if (next !== item) {
910
+ result ??= Object.assign(Object.create(null), value);
911
+ result[key] = next;
912
+ }
913
+ }
914
+ return result ?? value;
915
+ }
916
+ const preloadedLoader = (url) => {
917
+ if (!Object.hasOwn(_fedify_vocab_runtime.preloadedContexts, url)) return Promise.reject(/* @__PURE__ */ new Error("Unknown JSON-LD context: " + url));
918
+ return Promise.resolve({
919
+ contextUrl: null,
920
+ documentUrl: url,
921
+ document: _fedify_vocab_runtime.preloadedContexts[url]
922
+ });
923
+ };
924
+ /** Reuses each context resolution across the complete equivalence check. */
925
+ function memoizeLoader(loader) {
926
+ const pending = /* @__PURE__ */ new Map();
927
+ return (url, options) => {
928
+ let result = pending.get(url);
929
+ if (result == null) {
930
+ result = loader(url, options);
931
+ pending.set(url, result);
932
+ }
933
+ return result;
934
+ };
935
+ }
936
+ /** Finds security predicates in expanded nodes, never inside JSON literals. */
937
+ function hasSecurityPredicate(value) {
938
+ if (Array.isArray(value)) return value.some(hasSecurityPredicate);
939
+ if (!isMap(value) || Object.hasOwn(value, "@value")) return false;
940
+ for (const [key, child] of Object.entries(value)) {
941
+ if (key === PROOF || key === "https://w3id.org/security#signature") return true;
942
+ if (hasSecurityPredicate(child)) return true;
943
+ }
944
+ return false;
945
+ }
946
+ /**
947
+ * Keeps singleton attachments in arrays for raw-JSON ActivityPub consumers.
948
+ * Mastodon's property_values() ignores valid compact JSON-LD scalar values,
949
+ * and its caller clears the profile fields in that case:
950
+ * https://github.com/mastodon/mastodon/blob/594166519a623584920c7e696bae9a009d9183ab/app/services/activitypub/process_account_service.rb#L465-L472
951
+ * Pixelfed likewise rejects singleton media attachments:
952
+ * https://github.com/pixelfed/pixelfed/issues/6588
953
+ *
954
+ * This workaround changes only attachment representation, after serialization.
955
+ * RDF equality protects custom contexts, including JSON literals; it does not
956
+ * protect JCS signatures. Any semantic proof or Linked Data signature leaves
957
+ * the whole serialized document unchanged, as does an unresolved context.
958
+ * This layer does not compose signed objects or repair vocabulary serialization.
959
+ * Remove this
960
+ * workaround only when the supported consumer versions, including deployed
961
+ * older releases, accept scalar attachments. Do not replace it with a
962
+ * vocabulary transformer: subsequent compaction collapses arrays.
963
+ * @internal
964
+ */
965
+ async function normalizeAttachmentArrays(document, contextLoader) {
966
+ try {
967
+ const normalized = walk(document);
968
+ if (normalized === document) return document;
969
+ const loader = memoizeLoader(contextLoader ?? preloadedLoader);
970
+ if (hasSecurityPredicate(await _fedify_vocab_runtime_jsonld.default.expand(document, { documentLoader: loader }))) return document;
971
+ const [before, after] = await Promise.allSettled([_fedify_vocab_runtime_jsonld.default.canonize(document, {
972
+ format: "application/n-quads",
973
+ documentLoader: loader
974
+ }), _fedify_vocab_runtime_jsonld.default.canonize(normalized, {
975
+ format: "application/n-quads",
976
+ documentLoader: loader
977
+ })]);
978
+ if (before.status === "rejected") throw before.reason;
979
+ if (after.status === "rejected") throw after.reason;
980
+ if (before.value === after.value) return normalized;
981
+ } catch (error) {
982
+ logger$1.debug("Could not safely normalize attachment arrays; preserving the document: {error}", { error });
983
+ }
984
+ return document;
985
+ }
986
+ //#endregion
881
987
  //#region src/federation/keycache.ts
882
988
  const NULL_KEY_CACHE_VALUE = { _fedify: "key-unavailable" };
883
989
  const NULL_KEY_CACHE_TTL = Temporal.Duration.from({ minutes: 5 });
@@ -1136,7 +1242,7 @@ async function handleActor(request, { identifier, context, actorDispatcher, auth
1136
1242
  if (authorizePredicate != null) {
1137
1243
  if (!await authorizePredicate(context, identifier)) return await onUnauthorized(request);
1138
1244
  }
1139
- const jsonLd = await actor.toJsonLd(context);
1245
+ const jsonLd = await serializeForResponse(actor, context);
1140
1246
  return new Response(JSON.stringify(jsonLd), { headers: {
1141
1247
  "Content-Type": "application/activity+json",
1142
1248
  Vary: "Accept"
@@ -1156,7 +1262,7 @@ async function handleObject(request, { values, context, objectDispatcher, author
1156
1262
  if (authorizePredicate != null) {
1157
1263
  if (!await authorizePredicate(context, values)) return await onUnauthorized(request);
1158
1264
  }
1159
- const jsonLd = await object.toJsonLd(context);
1265
+ const jsonLd = await serializeForResponse(object, context);
1160
1266
  return new Response(JSON.stringify(jsonLd), { headers: {
1161
1267
  "Content-Type": "application/activity+json",
1162
1268
  Vary: "Accept"
@@ -1287,7 +1393,7 @@ async function handleCollection(request, { name: name$1, identifier, uriGetter,
1287
1393
  if (collectionCallbacks.authorizePredicate != null) {
1288
1394
  if (!await collectionCallbacks.authorizePredicate(context, identifier)) return await onUnauthorized(request);
1289
1395
  }
1290
- const jsonLd = await collection.toJsonLd(context);
1396
+ const jsonLd = await serializeForResponse(collection, context);
1291
1397
  return new Response(JSON.stringify(jsonLd), { headers: {
1292
1398
  "Content-Type": "application/activity+json",
1293
1399
  Vary: "Accept"
@@ -1847,7 +1953,7 @@ var CustomCollectionHandler = class {
1847
1953
  * @returns A promise that resolves to the JSON-LD representation.
1848
1954
  */
1849
1955
  async toJsonLd() {
1850
- return (await this.collection).toJsonLd(this.context);
1956
+ return serializeForResponse(await this.collection, this.context);
1851
1957
  }
1852
1958
  /**
1853
1959
  * Fetches the collection with optional cursor for pagination.
@@ -2150,7 +2256,7 @@ var UnauthorizedError = class extends HandlerError {
2150
2256
  * @since 0.3.0
2151
2257
  */
2152
2258
  async function respondWithObject(object, options) {
2153
- const jsonLd = await object.toJsonLd(options);
2259
+ const jsonLd = await serializeForResponse(object, options);
2154
2260
  return new Response(JSON.stringify(jsonLd), { headers: { "Content-Type": "application/activity+json" } });
2155
2261
  }
2156
2262
  /**
@@ -2168,6 +2274,106 @@ async function respondWithObjectIfAcceptable(object, request, options) {
2168
2274
  response.headers.set("Vary", "Accept");
2169
2275
  return response;
2170
2276
  }
2277
+ /** Applies compatibility changes only after ordinary vocabulary serialization. */
2278
+ async function serializeForResponse(object, options) {
2279
+ return await normalizeAttachmentArrays(await object.toJsonLd(options), options?.contextLoader);
2280
+ }
2281
+ //#endregion
2282
+ //#region src/sig/proof-creation.ts
2283
+ /** Signs the exact serialized message supplied by an internal producer. */
2284
+ async function createProofForJsonLd(jsonLd, privateKey, keyId, created) {
2285
+ require_http.validateCryptoKey(privateKey, "private");
2286
+ if (privateKey.algorithm.name !== "Ed25519") throw new TypeError("Unsupported algorithm: " + privateKey.algorithm.name);
2287
+ const encoder = new TextEncoder();
2288
+ const msgDigest = await crypto.subtle.digest("SHA-256", encoder.encode((0, json_canon.default)(jsonLd)));
2289
+ created ??= Temporal.Now.instant();
2290
+ const proofConfig = {
2291
+ "@context": jsonLd["@context"],
2292
+ type: "DataIntegrityProof",
2293
+ cryptosuite: "eddsa-jcs-2022",
2294
+ verificationMethod: keyId.href,
2295
+ proofPurpose: "assertionMethod",
2296
+ created: created.toString()
2297
+ };
2298
+ const proofDigest = await crypto.subtle.digest("SHA-256", encoder.encode((0, json_canon.default)(proofConfig)));
2299
+ const digest = new Uint8Array(proofDigest.byteLength + msgDigest.byteLength);
2300
+ digest.set(new Uint8Array(proofDigest), 0);
2301
+ digest.set(new Uint8Array(msgDigest), proofDigest.byteLength);
2302
+ const sig = await crypto.subtle.sign("Ed25519", (0, _fedify_vocab_runtime_internal.asCryptoKey)(privateKey), digest);
2303
+ return new _fedify_vocab.DataIntegrityProof({
2304
+ cryptosuite: "eddsa-jcs-2022",
2305
+ verificationMethod: keyId,
2306
+ proofPurpose: "assertionMethod",
2307
+ created,
2308
+ proofValue: new Uint8Array(sig)
2309
+ });
2310
+ }
2311
+ //#endregion
2312
+ //#region src/compat/delivery-jsonld.ts
2313
+ /**
2314
+ * Returns changed attachment JSON only; unchanged documents keep the existing
2315
+ * delivery signing path. This uses ordinary vocabulary serialization, without
2316
+ * retaining or repairing independently signed child representations.
2317
+ */
2318
+ async function prepareActivityJsonLd(activity, contextLoader) {
2319
+ if (activity.proofIds.length > 0) return void 0;
2320
+ for await (const _ of activity.getProofs({ contextLoader })) return void 0;
2321
+ const source = await activity.clone().toJsonLd({
2322
+ format: "compact",
2323
+ contextLoader
2324
+ });
2325
+ const prepared = await normalizeAttachmentArrays(source, contextLoader);
2326
+ return prepared !== source && typeof prepared === "object" && prepared != null && !Array.isArray(prepared) ? prepared : void 0;
2327
+ }
2328
+ /** Signs a changed, proof-free delivery map without serializing it again. */
2329
+ async function signActivityJsonLd(activity, unsigned, keys, options) {
2330
+ const proofs = [];
2331
+ const serializedProofs = [];
2332
+ const tracer = (options.tracerProvider ?? _opentelemetry_api.trace.getTracerProvider()).getTracer(require_http.name, require_http.version);
2333
+ for (const { privateKey, keyId } of keys) {
2334
+ const proof = await tracer.startActiveSpan("object_integrity_proofs.sign", { attributes: { "activitypub.object.type": (0, _fedify_vocab.getTypeId)(activity).href } }, async (span) => {
2335
+ try {
2336
+ if (activity.id != null) span.setAttribute("activitypub.object.id", activity.id.href);
2337
+ const proof = await createProofForJsonLd(unsigned, privateKey, keyId);
2338
+ if (span.isRecording()) {
2339
+ span.setAttribute("object_integrity_proofs.cryptosuite", "eddsa-jcs-2022");
2340
+ span.setAttribute("object_integrity_proofs.key_id", keyId.href);
2341
+ span.setAttribute("object_integrity_proofs.signature", (0, byte_encodings_hex.encodeHex)(proof.proofValue));
2342
+ }
2343
+ return proof;
2344
+ } catch (error) {
2345
+ span.setStatus({
2346
+ code: _opentelemetry_api.SpanStatusCode.ERROR,
2347
+ message: String(error)
2348
+ });
2349
+ throw error;
2350
+ } finally {
2351
+ span.end();
2352
+ }
2353
+ });
2354
+ proofs.push(proof);
2355
+ serializedProofs.push({
2356
+ "@context": unsigned["@context"],
2357
+ type: "DataIntegrityProof",
2358
+ cryptosuite: proof.cryptosuite,
2359
+ verificationMethod: keyId.href,
2360
+ proofPurpose: "assertionMethod",
2361
+ created: proof.created.toString(),
2362
+ proofValue: new TextDecoder().decode((0, _fedify_vocab_runtime.encodeMultibase)("base58btc", proof.proofValue))
2363
+ });
2364
+ }
2365
+ if (proofs.length === 0) return {
2366
+ activity,
2367
+ jsonLd: unsigned
2368
+ };
2369
+ return {
2370
+ activity: activity.clone({ proofs }),
2371
+ jsonLd: {
2372
+ ...unsigned,
2373
+ proof: serializedProofs.length === 1 ? serializedProofs[0] : serializedProofs
2374
+ }
2375
+ };
2376
+ }
2171
2377
  //#endregion
2172
2378
  //#region src/nodeinfo/handler.ts
2173
2379
  /**
@@ -3434,19 +3640,29 @@ var FederationImpl = class extends FederationBuilderImpl {
3434
3640
  proofCreated = true;
3435
3641
  break;
3436
3642
  }
3437
- if (!proofCreated) {
3438
- for (const { keyId, privateKey } of keys) if (privateKey.algorithm.name === "Ed25519") {
3439
- activity = await require_proof.signObject(activity, privateKey, keyId, {
3440
- contextLoader,
3441
- tracerProvider: this.tracerProvider
3442
- });
3443
- proofCreated = true;
3643
+ const prepared = !proofCreated && options.activityJsonLd == null ? await prepareActivityJsonLd(activity, contextLoader) : void 0;
3644
+ let jsonLd;
3645
+ if (prepared != null) {
3646
+ const signingKeys = keys.filter(({ privateKey }) => privateKey.algorithm.name === "Ed25519");
3647
+ const signed = await signActivityJsonLd(activity, prepared, signingKeys, { tracerProvider: this.tracerProvider });
3648
+ activity = signed.activity;
3649
+ jsonLd = signed.jsonLd;
3650
+ proofCreated = signingKeys.length > 0;
3651
+ } else {
3652
+ if (!proofCreated) {
3653
+ for (const { keyId, privateKey } of keys) if (privateKey.algorithm.name === "Ed25519") {
3654
+ activity = await require_proof.signObject(activity, privateKey, keyId, {
3655
+ contextLoader,
3656
+ tracerProvider: this.tracerProvider
3657
+ });
3658
+ proofCreated = true;
3659
+ }
3444
3660
  }
3661
+ jsonLd = options.activityJsonLd != null ? require_proof.detachSignature(options.activityJsonLd) : await activity.toJsonLd({
3662
+ format: "compact",
3663
+ contextLoader
3664
+ });
3445
3665
  }
3446
- let jsonLd = options.activityJsonLd != null ? require_proof.detachSignature(options.activityJsonLd) : await activity.toJsonLd({
3447
- format: "compact",
3448
- contextLoader
3449
- });
3450
3666
  if (rsaKey == null) logger.warn("No supported key found to create a Linked Data signature for the activity {activityId}. The activity will be sent without a Linked Data signature. In order to create a Linked Data signature, at least one RSASSA-PKCS1-v1_5 key must be provided.", {
3451
3667
  activityId,
3452
3668
  keys: keys.map((pair) => ({
@@ -4285,19 +4501,31 @@ var ContextImpl = class ContextImpl {
4285
4501
  }
4286
4502
  if (actorKeyPairs != null) {
4287
4503
  const contextLoader = this.contextLoader;
4288
- if (actorKeyPairs.some((kp) => kp.privateKey.algorithm.name === "Ed25519" && kp.multikey.id != null)) {
4504
+ const willSign = actorKeyPairs.some((kp) => kp.privateKey.algorithm.name === "Ed25519" && kp.multikey.id != null);
4505
+ if (willSign) {
4289
4506
  if (activityJsonLd != null && !contextMatches) activity = activity.clone({ proofs: [] });
4290
4507
  }
4291
- let proofAdded = false;
4292
- for (const kp of actorKeyPairs) {
4293
- if (kp.privateKey.algorithm.name !== "Ed25519" || kp.multikey.id == null) continue;
4294
- activity = await require_proof.signObject(activity, kp.privateKey, kp.multikey.id, {
4295
- contextLoader,
4296
- tracerProvider: this.tracerProvider
4297
- });
4298
- proofAdded = true;
4508
+ const prepared = willSign ? await prepareActivityJsonLd(activity, contextLoader) : void 0;
4509
+ if (prepared != null) {
4510
+ const signingKeys = actorKeyPairs.flatMap((kp) => kp.privateKey.algorithm.name === "Ed25519" && kp.multikey.id != null ? [{
4511
+ privateKey: kp.privateKey,
4512
+ keyId: kp.multikey.id
4513
+ }] : []);
4514
+ const signed = await signActivityJsonLd(activity, prepared, signingKeys, { tracerProvider: this.tracerProvider });
4515
+ activity = signed.activity;
4516
+ activityJsonLd = signed.jsonLd;
4517
+ } else {
4518
+ let proofAdded = false;
4519
+ for (const kp of actorKeyPairs) {
4520
+ if (kp.privateKey.algorithm.name !== "Ed25519" || kp.multikey.id == null) continue;
4521
+ activity = await require_proof.signObject(activity, kp.privateKey, kp.multikey.id, {
4522
+ contextLoader,
4523
+ tracerProvider: this.tracerProvider
4524
+ });
4525
+ proofAdded = true;
4526
+ }
4527
+ if (proofAdded) activityJsonLd = void 0;
4299
4528
  }
4300
- if (proofAdded) activityJsonLd = void 0;
4301
4529
  }
4302
4530
  const opts = {
4303
4531
  context: this,