@fedify/fedify 1.7.14 → 1.7.15

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 (163) hide show
  1. package/dist/{actor-Dx5YX74F.d.ts → actor.d.ts} +2 -2
  2. package/dist/{actor-y7shp0CR.js → actor.js} +4 -4
  3. package/dist/{assert-C-mZuSQl.js → assert.js} +1 -1
  4. package/dist/{assert_instance_of-lS0Jr2iu.js → assert_instance_of.js} +1 -1
  5. package/dist/{assert_is_error-CIYFACrT.js → assert_is_error.js} +1 -1
  6. package/dist/{assert_not_equals-C1azCAB0.js → assert_not_equals.js} +1 -1
  7. package/dist/{assert_rejects-Bkh5lA1a.js → assert_rejects.js} +2 -2
  8. package/dist/{assert_throws-CmpfkWEM.js → assert_throws.js} +2 -2
  9. package/dist/{authdocloader-C0JcfxUs.js → authdocloader.js} +4 -4
  10. package/dist/{builder-DaECqbbT.js → builder.js} +5 -5
  11. package/dist/{client-wKLuY12i.d.ts → client.d.ts} +1 -1
  12. package/dist/{client-dfu57WLy.js → client.js} +2 -2
  13. package/dist/compat/{mod-Bwqav33K.d.ts → mod.d.ts} +2 -2
  14. package/dist/compat/mod.js +1 -1
  15. package/dist/compat/transformers.test.js +30 -30
  16. package/dist/{context-CW_8R4BH.d.ts → context.d.ts} +10 -10
  17. package/dist/{context-nxDPfAiA.js → context.js} +4 -4
  18. package/dist/{docloader-D_MGP37Q.d.ts → docloader.d.ts} +1 -1
  19. package/dist/{docloader-sGz4vcrK.js → docloader.js} +2 -2
  20. package/dist/{esm-BRXvTSrx.js → esm.js} +1 -1
  21. package/dist/federation/builder.test.js +17 -17
  22. package/dist/federation/collection.test.js +8 -8
  23. package/dist/federation/handler.test.js +36 -36
  24. package/dist/federation/inbox.test.js +11 -11
  25. package/dist/federation/keycache.test.js +12 -12
  26. package/dist/federation/kv.test.js +8 -8
  27. package/dist/federation/middleware.test.js +39 -39
  28. package/dist/federation/{mod-DvlLc8Ru.d.ts → mod.d.ts} +3 -3
  29. package/dist/federation/mod.js +11 -11
  30. package/dist/federation/mq.test.js +10 -10
  31. package/dist/federation/retry.test.js +5 -5
  32. package/dist/federation/router.test.js +9 -9
  33. package/dist/federation/send.test.js +23 -23
  34. package/dist/{http-DFZRNfDP.d.ts → http.d.ts} +2 -2
  35. package/dist/{http-BhES0Sg9.js → http.js} +3 -3
  36. package/dist/{inbox-Z_QlSGYN.js → inbox.js} +3 -3
  37. package/dist/key.js +16 -0
  38. package/dist/{key-bMrDyA2T.js → key2.js} +3 -3
  39. package/dist/{keycache-B5Rr8Lbc.js → keycache.js} +1 -1
  40. package/dist/{keys-DK4k1R8e.js → keys.js} +1 -1
  41. package/dist/{ld--auFISy7.js → ld.js} +4 -4
  42. package/dist/{lookup-CqI9jhXo.d.ts → lookup.d.ts} +1 -1
  43. package/dist/{lookup-Bq8vhIG4.js → lookup.js} +2 -2
  44. package/dist/{lookup-cHV4n1IB.js → lookup2.js} +4 -4
  45. package/dist/{middleware-B7p-onxQ.js → middleware.js} +20 -20
  46. package/dist/middleware2.js +32 -0
  47. package/dist/{mod-DMn4Bxyg.d.ts → mod.d.ts} +17 -17
  48. package/dist/mod.js +11 -11
  49. package/dist/{mod-DjGYGrBd.d.ts → mod2.d.ts} +2 -2
  50. package/dist/{mod-BEZy4WHZ.d.ts → mod3.d.ts} +3 -3
  51. package/dist/nodeinfo/client.test.js +12 -12
  52. package/dist/nodeinfo/handler.test.js +34 -34
  53. package/dist/nodeinfo/{mod--upgPcaX.d.ts → mod.d.ts} +1 -1
  54. package/dist/nodeinfo/mod.js +2 -2
  55. package/dist/nodeinfo/semver.test.js +8 -8
  56. package/dist/nodeinfo/types.test.js +9 -9
  57. package/dist/{owner-CewLNqWO.d.ts → owner.d.ts} +3 -3
  58. package/dist/{owner-DkTRLQAV.js → owner.js} +3 -3
  59. package/dist/{proof-DT_xxRPF.js → proof.js} +4 -4
  60. package/dist/runtime/authdocloader.test.js +20 -20
  61. package/dist/runtime/docloader.test.js +13 -13
  62. package/dist/runtime/key.test.js +15 -15
  63. package/dist/runtime/langstr.test.js +8 -8
  64. package/dist/runtime/{mod-BaH1vtHp.d.ts → mod.d.ts} +3 -3
  65. package/dist/runtime/mod.js +6 -6
  66. package/dist/runtime/multibase/multibase.test.js +8 -8
  67. package/dist/runtime/url.test.js +7 -7
  68. package/dist/{send-axIyQX2p.js → send.js} +2 -2
  69. package/dist/sig/http.test.js +20 -20
  70. package/dist/sig/key.test.js +17 -17
  71. package/dist/sig/ld.test.js +18 -18
  72. package/dist/sig/{mod-BmJQTnPT.d.ts → mod.d.ts} +3 -3
  73. package/dist/sig/mod.js +6 -6
  74. package/dist/sig/owner.test.js +20 -20
  75. package/dist/sig/proof.test.js +19 -19
  76. package/dist/{std__assert-BdP_WkD-.js → std__assert.js} +1 -1
  77. package/dist/testing/docloader.test.js +8 -8
  78. package/dist/testing/mod.js +2 -2
  79. package/dist/{testing-qaAD4B0t.js → testing.js} +1 -1
  80. package/dist/{types-CB_2uuCA.js → types.js} +1 -1
  81. package/dist/vocab/actor.test.js +16 -16
  82. package/dist/vocab/lookup.test.js +17 -17
  83. package/dist/vocab/{mod-BkXGAYkx.d.ts → mod.d.ts} +3 -3
  84. package/dist/vocab/mod.js +4 -4
  85. package/dist/vocab/type.test.js +9 -9
  86. package/dist/vocab/vocab.test.js +17 -17
  87. package/dist/{vocab-DoBcp8ow.d.ts → vocab.d.ts} +1 -1
  88. package/dist/{vocab-DCHRuBGF.js → vocab.js} +181 -181
  89. package/dist/webfinger/handler.test.js +34 -34
  90. package/dist/webfinger/lookup.test.js +11 -11
  91. package/dist/webfinger/{mod-B1UhCvlL.d.ts → mod.d.ts} +1 -1
  92. package/dist/webfinger/mod.js +2 -2
  93. package/dist/x/{cfworkers-BIcR64Sf.d.ts → cfworkers.d.ts} +2 -2
  94. package/dist/x/cfworkers.test.js +7 -7
  95. package/dist/x/{hono-DmQmp8wi.d.ts → hono.d.ts} +1 -1
  96. package/dist/x/{sveltekit-Cvh0XnN4.d.ts → sveltekit.d.ts} +1 -1
  97. package/package.json +1 -1
  98. package/dist/actor-OYVgkiZG.js +0 -34789
  99. package/dist/authdocloader-C2krvRo1.js +0 -49
  100. package/dist/docloader-BU25UQLB.js +0 -4478
  101. package/dist/http-DRePVWfE.js +0 -788
  102. package/dist/key-CJuc1GE1.js +0 -10
  103. package/dist/key-D2HZrCxh.js +0 -257
  104. package/dist/key-eP5Yk7hl.js +0 -16
  105. package/dist/lookup-4uc2jh52.js +0 -127
  106. package/dist/middleware-72Tuh6Iw.js +0 -17
  107. package/dist/middleware-DB3BUysJ.js +0 -32
  108. package/dist/middleware-QJjRfjjc.js +0 -3519
  109. package/dist/proof-D4HXHDPz.js +0 -577
  110. package/dist/types-Bk4b1bGc.js +0 -445
  111. package/dist/vocab-sMuiKIWm.js +0 -168
  112. /package/dist/{assert_equals-Dy0MG_Zw.js → assert_equals.js} +0 -0
  113. /package/dist/{chunk-DvTpRkcT.js → chunk.js} +0 -0
  114. /package/dist/{collection-XNLQhehO.js → collection.js} +0 -0
  115. /package/dist/compat/{transformers.test-DnJbd34u.d.ts → transformers.test.d.ts} +0 -0
  116. /package/dist/{denokv-NcJeZ6rP.js → denokv.js} +0 -0
  117. /package/dist/{docloader-BDSHZfTJ.js → docloader2.js} +0 -0
  118. /package/dist/federation/{builder.test-Bpt6NOZ6.d.ts → builder.test.d.ts} +0 -0
  119. /package/dist/federation/{collection.test-DKJ6JOZz.d.ts → collection.test.d.ts} +0 -0
  120. /package/dist/federation/{handler.test-BMT7uLC0.d.ts → handler.test.d.ts} +0 -0
  121. /package/dist/federation/{inbox.test-Do6i02Qp.d.ts → inbox.test.d.ts} +0 -0
  122. /package/dist/federation/{keycache.test-BT83IPZY.d.ts → keycache.test.d.ts} +0 -0
  123. /package/dist/federation/{kv.test-kFzzF2VN.d.ts → kv.test.d.ts} +0 -0
  124. /package/dist/federation/{middleware.test-B1R4_e3-.d.ts → middleware.test.d.ts} +0 -0
  125. /package/dist/federation/{mq.test-l79EQQOe.d.ts → mq.test.d.ts} +0 -0
  126. /package/dist/federation/{retry.test-BqS50VCX.d.ts → retry.test.d.ts} +0 -0
  127. /package/dist/federation/{router.test-CYQl4po-.d.ts → router.test.d.ts} +0 -0
  128. /package/dist/federation/{send.test-COUnNUzv.d.ts → send.test.d.ts} +0 -0
  129. /package/dist/{kv-CKqSUxHd.d.ts → kv.d.ts} +0 -0
  130. /package/dist/{kv-QeuZ51go.js → kv.js} +0 -0
  131. /package/dist/{langstr-pFHBDU4y.js → langstr.js} +0 -0
  132. /package/dist/{mq-Cgfbl44M.d.ts → mq.d.ts} +0 -0
  133. /package/dist/{multibase-DBcKTV2a.js → multibase.js} +0 -0
  134. /package/dist/nodeinfo/{client.test-CZLe79hL.d.ts → client.test.d.ts} +0 -0
  135. /package/dist/nodeinfo/{handler.test-B-EDZ_hK.d.ts → handler.test.d.ts} +0 -0
  136. /package/dist/nodeinfo/{semver.test-BEuuQSEM.d.ts → semver.test.d.ts} +0 -0
  137. /package/dist/nodeinfo/{types.test-B5AT89WV.d.ts → types.test.d.ts} +0 -0
  138. /package/dist/{retry-BQet39_l.js → retry.js} +0 -0
  139. /package/dist/{router-BuDkN4RQ.js → router.js} +0 -0
  140. /package/dist/runtime/{authdocloader.test-hCRKzn9v.d.ts → authdocloader.test.d.ts} +0 -0
  141. /package/dist/runtime/{docloader.test-CVd7i_5h.d.ts → docloader.test.d.ts} +0 -0
  142. /package/dist/runtime/{key.test-DBsILYSD.d.ts → key.test.d.ts} +0 -0
  143. /package/dist/runtime/{langstr.test-CiKxuuRY.d.ts → langstr.test.d.ts} +0 -0
  144. /package/dist/runtime/multibase/{multibase.test-Brh6gPBP.d.ts → multibase.test.d.ts} +0 -0
  145. /package/dist/runtime/{url.test-DlRqkU2j.d.ts → url.test.d.ts} +0 -0
  146. /package/dist/{semver-D9d-VO-_.js → semver.js} +0 -0
  147. /package/dist/sig/{http.test-BpXNAWNI.d.ts → http.test.d.ts} +0 -0
  148. /package/dist/sig/{key.test-B2iLIugy.d.ts → key.test.d.ts} +0 -0
  149. /package/dist/sig/{ld.test-D-cI70Gw.d.ts → ld.test.d.ts} +0 -0
  150. /package/dist/sig/{owner.test-B_YRjMPj.d.ts → owner.test.d.ts} +0 -0
  151. /package/dist/sig/{proof.test-BagEM_-4.d.ts → proof.test.d.ts} +0 -0
  152. /package/dist/testing/{docloader.test-lrzf6sDZ.d.ts → docloader.test.d.ts} +0 -0
  153. /package/dist/testing/{mod-3uM8ZvS7.d.ts → mod.d.ts} +0 -0
  154. /package/dist/{transformers-CFSWUhNi.js → transformers.js} +0 -0
  155. /package/dist/{type-DFsmi-p1.js → type.js} +0 -0
  156. /package/dist/{url-BdNvnK9P.js → url.js} +0 -0
  157. /package/dist/vocab/{actor.test-ClC-iVWk.d.ts → actor.test.d.ts} +0 -0
  158. /package/dist/vocab/{lookup.test-Cq1I-27w.d.ts → lookup.test.d.ts} +0 -0
  159. /package/dist/vocab/{type.test-bfFiYGcs.d.ts → type.test.d.ts} +0 -0
  160. /package/dist/vocab/{vocab.test-h-ZTisfu.d.ts → vocab.test.d.ts} +0 -0
  161. /package/dist/webfinger/{handler.test-DiUeEDDD.d.ts → handler.test.d.ts} +0 -0
  162. /package/dist/webfinger/{lookup.test-D9onm3U3.d.ts → lookup.test.d.ts} +0 -0
  163. /package/dist/x/{cfworkers.test-KXHlJ29z.d.ts → cfworkers.test.d.ts} +0 -0
@@ -1,577 +0,0 @@
1
-
2
- import { Temporal } from "@js-temporal/polyfill";
3
- import { URLPattern } from "urlpattern-polyfill";
4
-
5
- import { d as name, f as version, i as getDocumentLoader } from "./docloader-BU25UQLB.js";
6
- import { G as Multikey, Tt as getTypeId, b as DataIntegrityProof, i as isActor, q as Object$1, s as Activity, y as CryptographicKey } from "./actor-OYVgkiZG.js";
7
- import { a as validateCryptoKey, n as fetchKey } from "./key-D2HZrCxh.js";
8
- import { getLogger } from "@logtape/logtape";
9
- import { SpanStatusCode, trace } from "@opentelemetry/api";
10
- import jsonld from "jsonld";
11
- import { decodeBase64, encodeBase64 } from "byte-encodings/base64";
12
- import { encodeHex } from "byte-encodings/hex";
13
- import serialize from "json-canon";
14
-
15
- //#region sig/ld.ts
16
- const logger$1 = getLogger([
17
- "fedify",
18
- "sig",
19
- "ld"
20
- ]);
21
- /**
22
- * Attaches a LD signature to the given JSON-LD document.
23
- * @param jsonLd The JSON-LD document to attach the signature to. It is not
24
- * modified.
25
- * @param signature The signature to attach.
26
- * @returns The JSON-LD document with the attached signature.
27
- * @throws {TypeError} If the input document is not a valid JSON-LD document.
28
- * @since 1.0.0
29
- */
30
- function attachSignature(jsonLd, signature) {
31
- if (typeof jsonLd !== "object" || jsonLd == null) throw new TypeError("Failed to attach signature; invalid JSON-LD document.");
32
- return {
33
- ...jsonLd,
34
- signature
35
- };
36
- }
37
- /**
38
- * Creates a LD signature for the given JSON-LD document.
39
- * @param jsonLd The JSON-LD document to sign.
40
- * @param privateKey The private key to sign the document.
41
- * @param keyId The ID of the public key that corresponds to the private key.
42
- * @param options Additional options for creating the signature.
43
- * See also {@link CreateSignatureOptions}.
44
- * @return The created signature.
45
- * @throws {TypeError} If the private key is invalid or unsupported.
46
- * @since 1.0.0
47
- */
48
- async function createSignature(jsonLd, privateKey, keyId, { contextLoader, created } = {}) {
49
- validateCryptoKey(privateKey, "private");
50
- if (privateKey.algorithm.name !== "RSASSA-PKCS1-v1_5") throw new TypeError("Unsupported algorithm: " + privateKey.algorithm.name);
51
- const options = {
52
- "@context": "https://w3id.org/identity/v1",
53
- creator: keyId.href,
54
- created: created?.toString() ?? (/* @__PURE__ */ new Date()).toISOString()
55
- };
56
- const message = await hashJsonLd(options, contextLoader) + await hashJsonLd(jsonLd, contextLoader);
57
- const messageBytes = new TextEncoder().encode(message);
58
- const signature = await crypto.subtle.sign("RSASSA-PKCS1-v1_5", privateKey, messageBytes);
59
- return {
60
- ...options,
61
- type: "RsaSignature2017",
62
- signatureValue: encodeBase64(signature)
63
- };
64
- }
65
- /**
66
- * Signs the given JSON-LD document with the private key and returns the signed
67
- * JSON-LD document.
68
- * @param jsonLd The JSON-LD document to sign.
69
- * @param privateKey The private key to sign the document.
70
- * @param keyId The key ID to use in the signature. It will be used by the
71
- * verifier to fetch the corresponding public key.
72
- * @param options Additional options for signing the document.
73
- * See also {@link SignJsonLdOptions}.
74
- * @returns The signed JSON-LD document.
75
- * @throws {TypeError} If the private key is invalid or unsupported.
76
- * @since 1.0.0
77
- */
78
- async function signJsonLd(jsonLd, privateKey, keyId, options) {
79
- return await (options.tracerProvider ?? trace.getTracerProvider()).getTracer(name, version).startActiveSpan("ld_signatures.sign", { attributes: { "ld_signatures.key_id": keyId.href } }, async (span) => {
80
- try {
81
- const signature = await createSignature(jsonLd, privateKey, keyId, options);
82
- if (span.isRecording()) {
83
- span.setAttribute("ld_signatures.type", signature.type);
84
- span.setAttribute("ld_signatures.signature", encodeHex(decodeBase64(signature.signatureValue)));
85
- }
86
- return attachSignature(jsonLd, signature);
87
- } catch (error) {
88
- span.setStatus({
89
- code: SpanStatusCode.ERROR,
90
- message: String(error)
91
- });
92
- throw error;
93
- } finally {
94
- span.end();
95
- }
96
- });
97
- }
98
- /**
99
- * Checks if the given JSON-LD document has a Linked Data Signature.
100
- * @param jsonLd The JSON-LD document to check.
101
- * @returns `true` if the document has a signature; `false` otherwise.
102
- * @since 1.0.0
103
- */
104
- function hasSignature(jsonLd) {
105
- if (typeof jsonLd !== "object" || jsonLd == null) return false;
106
- if ("signature" in jsonLd) {
107
- const signature = jsonLd.signature;
108
- if (typeof signature !== "object" || signature == null) return false;
109
- 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";
110
- }
111
- return false;
112
- }
113
- /**
114
- * Detaches Linked Data Signatures from the given JSON-LD document.
115
- * @param jsonLd The JSON-LD document to modify.
116
- * @returns The modified JSON-LD document. If the input document does not
117
- * contain a signature, the original document is returned.
118
- * @since 1.0.0
119
- */
120
- function detachSignature(jsonLd) {
121
- if (typeof jsonLd !== "object" || jsonLd == null) return jsonLd;
122
- const doc = { ...jsonLd };
123
- delete doc.signature;
124
- return doc;
125
- }
126
- /**
127
- * Verifies Linked Data Signatures of the given JSON-LD document.
128
- * @param jsonLd The JSON-LD document to verify.
129
- * @param options Options for verifying the signature.
130
- * @returns The public key that signed the document or `null` if the signature
131
- * is invalid or the key is not found.
132
- * @since 1.0.0
133
- */
134
- async function verifySignature(jsonLd, options = {}) {
135
- if (!hasSignature(jsonLd)) return null;
136
- const sig = jsonLd.signature;
137
- let signature;
138
- try {
139
- signature = decodeBase64(sig.signatureValue);
140
- } catch (error) {
141
- logger$1.debug("Failed to verify; invalid base64 signatureValue: {signatureValue}", {
142
- ...sig,
143
- error
144
- });
145
- return null;
146
- }
147
- const { key, cached } = await fetchKey(new URL(sig.creator), CryptographicKey, options);
148
- if (key == null) return null;
149
- const sigOpts = {
150
- ...sig,
151
- "@context": "https://w3id.org/identity/v1"
152
- };
153
- delete sigOpts.type;
154
- delete sigOpts.id;
155
- delete sigOpts.signatureValue;
156
- let sigOptsHash;
157
- try {
158
- sigOptsHash = await hashJsonLd(sigOpts, options.contextLoader);
159
- } catch (error) {
160
- logger$1.warn("Failed to verify; failed to hash the signature options: {signatureOptions}\n{error}", {
161
- signatureOptions: sigOpts,
162
- error
163
- });
164
- return null;
165
- }
166
- const document = { ...jsonLd };
167
- delete document.signature;
168
- let docHash;
169
- try {
170
- docHash = await hashJsonLd(document, options.contextLoader);
171
- } catch (error) {
172
- logger$1.warn("Failed to verify; failed to hash the document: {document}\n{error}", {
173
- document,
174
- error
175
- });
176
- return null;
177
- }
178
- const encoder = new TextEncoder();
179
- const message = sigOptsHash + docHash;
180
- const messageBytes = encoder.encode(message);
181
- if (await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key.publicKey, signature.slice(), messageBytes)) return key;
182
- if (cached) {
183
- logger$1.debug("Failed to verify with the cached key {keyId}; signature {signatureValue} is invalid. Retrying with the freshly fetched key...", {
184
- keyId: sig.creator,
185
- ...sig
186
- });
187
- const { key: key$1 } = await fetchKey(new URL(sig.creator), CryptographicKey, {
188
- ...options,
189
- keyCache: {
190
- get: () => Promise.resolve(void 0),
191
- set: async (keyId, key$2) => await options.keyCache?.set(keyId, key$2)
192
- }
193
- });
194
- if (key$1 == null) return null;
195
- return await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key$1.publicKey, signature.slice(), messageBytes) ? key$1 : null;
196
- }
197
- logger$1.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}", {
198
- keyId: sig.creator,
199
- ...sig,
200
- message
201
- });
202
- return null;
203
- }
204
- /**
205
- * Verify the authenticity of the given JSON-LD document using Linked Data
206
- * Signatures. If the document is signed, this function verifies the signature
207
- * and checks if the document is attributed to the owner of the public key.
208
- * If the document is not signed, this function returns `false`.
209
- * @param jsonLd The JSON-LD document to verify.
210
- * @param options Options for verifying the document.
211
- * @returns `true` if the document is authentic; `false` otherwise.
212
- */
213
- async function verifyJsonLd(jsonLd, options = {}) {
214
- return await (options.tracerProvider ?? trace.getTracerProvider()).getTracer(name, version).startActiveSpan("ld_signatures.verify", async (span) => {
215
- try {
216
- const object = await Object$1.fromJsonLd(jsonLd, options);
217
- if (object.id != null) span.setAttribute("activitypub.object.id", object.id.href);
218
- span.setAttribute("activitypub.object.type", getTypeId(object).href);
219
- if (typeof jsonLd === "object" && jsonLd != null && "signature" in jsonLd && typeof jsonLd.signature === "object" && jsonLd.signature != null) {
220
- if ("creator" in jsonLd.signature && typeof jsonLd.signature.creator === "string") span.setAttribute("ld_signatures.key_id", jsonLd.signature.creator);
221
- if ("signatureValue" in jsonLd.signature && typeof jsonLd.signature.signatureValue === "string") span.setAttribute("ld_signatures.signature", jsonLd.signature.signatureValue);
222
- if ("type" in jsonLd.signature && typeof jsonLd.signature.type === "string") span.setAttribute("ld_signatures.type", jsonLd.signature.type);
223
- }
224
- const attributions = new Set(object.attributionIds.map((uri) => uri.href));
225
- if (object instanceof Activity) for (const uri of object.actorIds) attributions.add(uri.href);
226
- const key = await verifySignature(jsonLd, options);
227
- if (key == null) return false;
228
- if (key.ownerId == null) {
229
- logger$1.debug("Key {keyId} has no owner.", { keyId: key.id?.href });
230
- return false;
231
- }
232
- attributions.delete(key.ownerId.href);
233
- if (attributions.size > 0) {
234
- logger$1.debug("Some attributions are not authenticated by the Linked Data Signatures: {attributions}.", { attributions: [...attributions] });
235
- return false;
236
- }
237
- return true;
238
- } catch (error) {
239
- span.setStatus({
240
- code: SpanStatusCode.ERROR,
241
- message: String(error)
242
- });
243
- throw error;
244
- } finally {
245
- span.end();
246
- }
247
- });
248
- }
249
- async function hashJsonLd(jsonLd, contextLoader) {
250
- const canon = await jsonld.canonize(jsonLd, {
251
- format: "application/n-quads",
252
- documentLoader: contextLoader ?? getDocumentLoader()
253
- });
254
- const encoder = new TextEncoder();
255
- return encodeHex(await crypto.subtle.digest("SHA-256", encoder.encode(canon)));
256
- }
257
-
258
- //#endregion
259
- //#region sig/owner.ts
260
- /**
261
- * Checks if the actor of the given activity owns the specified key.
262
- * @param activity The activity to check.
263
- * @param key The public key to check.
264
- * @param options Options for checking the key ownership.
265
- * @returns Whether the actor is the owner of the key.
266
- */
267
- async function doesActorOwnKey(activity, key, options) {
268
- if (key.ownerId != null) return key.ownerId.href === activity.actorId?.href;
269
- const actor = await activity.getActor(options);
270
- if (actor == null || !isActor(actor)) return false;
271
- for (const publicKeyId of actor.publicKeyIds) if (key.id != null && publicKeyId.href === key.id.href) return true;
272
- return false;
273
- }
274
- /**
275
- * Gets the actor that owns the specified key. Returns `null` if the key has no
276
- * known owner.
277
- *
278
- * @param keyId The ID of the key to check, or the key itself.
279
- * @param options Options for getting the key owner.
280
- * @returns The actor that owns the key, or `null` if the key has no known
281
- * owner.
282
- * @since 0.7.0
283
- */
284
- async function getKeyOwner(keyId, options) {
285
- const tracerProvider = options.tracerProvider ?? trace.getTracerProvider();
286
- const documentLoader = options.documentLoader ?? getDocumentLoader();
287
- const contextLoader = options.contextLoader ?? getDocumentLoader();
288
- let object;
289
- if (keyId instanceof CryptographicKey) {
290
- object = keyId;
291
- if (object.id == null) return null;
292
- keyId = object.id;
293
- } else {
294
- let keyDoc;
295
- try {
296
- const { document } = await documentLoader(keyId.href);
297
- keyDoc = document;
298
- } catch (_) {
299
- return null;
300
- }
301
- try {
302
- object = await Object$1.fromJsonLd(keyDoc, {
303
- documentLoader,
304
- contextLoader,
305
- tracerProvider
306
- });
307
- } catch (e) {
308
- if (!(e instanceof TypeError)) throw e;
309
- try {
310
- object = await CryptographicKey.fromJsonLd(keyDoc, {
311
- documentLoader,
312
- contextLoader,
313
- tracerProvider
314
- });
315
- } catch (e$1) {
316
- if (e$1 instanceof TypeError) return null;
317
- throw e$1;
318
- }
319
- }
320
- }
321
- let owner = null;
322
- if (object instanceof CryptographicKey) {
323
- if (object.ownerId == null) return null;
324
- owner = await object.getOwner({
325
- documentLoader,
326
- contextLoader,
327
- tracerProvider
328
- });
329
- } else if (isActor(object)) owner = object;
330
- else return null;
331
- if (owner == null) return null;
332
- for (const kid of owner.publicKeyIds) if (kid.href === keyId.href) return owner;
333
- return null;
334
- }
335
-
336
- //#endregion
337
- //#region sig/proof.ts
338
- const logger = getLogger([
339
- "fedify",
340
- "sig",
341
- "proof"
342
- ]);
343
- /**
344
- * Creates a proof for the given object.
345
- * @param object The object to create a proof for.
346
- * @param privateKey The private key to sign the proof with.
347
- * @param keyId The key ID to use in the proof. It will be used by the verifier.
348
- * @param options Additional options. See also {@link CreateProofOptions}.
349
- * @returns The created proof.
350
- * @throws {TypeError} If the private key is invalid or unsupported.
351
- * @since 0.10.0
352
- */
353
- async function createProof(object, privateKey, keyId, { contextLoader, context: context$1, created } = {}) {
354
- validateCryptoKey(privateKey, "private");
355
- if (privateKey.algorithm.name !== "Ed25519") throw new TypeError("Unsupported algorithm: " + privateKey.algorithm.name);
356
- const compactMsg = await object.clone({ proofs: [] }).toJsonLd({
357
- format: "compact",
358
- contextLoader,
359
- context: context$1
360
- });
361
- const msgCanon = serialize(compactMsg);
362
- const encoder = new TextEncoder();
363
- const msgBytes = encoder.encode(msgCanon);
364
- const msgDigest = await crypto.subtle.digest("SHA-256", msgBytes);
365
- created ??= Temporal.Now.instant();
366
- const proofCanon = serialize({
367
- "@context": compactMsg["@context"],
368
- type: "DataIntegrityProof",
369
- cryptosuite: "eddsa-jcs-2022",
370
- verificationMethod: keyId.href,
371
- proofPurpose: "assertionMethod",
372
- created: created.toString()
373
- });
374
- const proofBytes = encoder.encode(proofCanon);
375
- const proofDigest = await crypto.subtle.digest("SHA-256", proofBytes);
376
- const digest = new Uint8Array(proofDigest.byteLength + msgDigest.byteLength);
377
- digest.set(new Uint8Array(proofDigest), 0);
378
- digest.set(new Uint8Array(msgDigest), proofDigest.byteLength);
379
- const sig = await crypto.subtle.sign("Ed25519", privateKey, digest);
380
- return new DataIntegrityProof({
381
- cryptosuite: "eddsa-jcs-2022",
382
- verificationMethod: keyId,
383
- proofPurpose: "assertionMethod",
384
- created: created ?? Temporal.Now.instant(),
385
- proofValue: new Uint8Array(sig)
386
- });
387
- }
388
- /**
389
- * Signs the given object with the private key and returns the signed object.
390
- * @param object The object to create a proof for.
391
- * @param privateKey The private key to sign the proof with.
392
- * @param keyId The key ID to use in the proof. It will be used by the verifier.
393
- * @param options Additional options. See also {@link SignObjectOptions}.
394
- * @returns The signed object.
395
- * @throws {TypeError} If the private key is invalid or unsupported.
396
- * @since 0.10.0
397
- */
398
- async function signObject(object, privateKey, keyId, options = {}) {
399
- return await (options.tracerProvider ?? trace.getTracerProvider()).getTracer(name, version).startActiveSpan("object_integrity_proofs.sign", { attributes: { "activitypub.object.type": getTypeId(object).href } }, async (span) => {
400
- try {
401
- if (object.id != null) span.setAttribute("activitypub.object.id", object.id.href);
402
- const existingProofs = [];
403
- for await (const proof$1 of object.getProofs(options)) existingProofs.push(proof$1);
404
- const proof = await createProof(object, privateKey, keyId, options);
405
- if (span.isRecording()) {
406
- if (proof.cryptosuite != null) span.setAttribute("object_integrity_proofs.cryptosuite", proof.cryptosuite);
407
- if (proof.verificationMethodId != null) span.setAttribute("object_integrity_proofs.key_id", proof.verificationMethodId.href);
408
- if (proof.proofValue != null) span.setAttribute("object_integrity_proofs.signature", encodeHex(proof.proofValue));
409
- }
410
- return object.clone({ proofs: [...existingProofs, proof] });
411
- } catch (error) {
412
- span.setStatus({
413
- code: SpanStatusCode.ERROR,
414
- message: String(error)
415
- });
416
- throw error;
417
- } finally {
418
- span.end();
419
- }
420
- });
421
- }
422
- /**
423
- * Verifies the given proof for the object.
424
- * @param jsonLd The JSON-LD object to verify the proof for. If it contains
425
- * any proofs, they will be ignored.
426
- * @param proof The proof to verify.
427
- * @param options Additional options. See also {@link VerifyProofOptions}.
428
- * @returns The public key that was used to sign the proof, or `null` if the
429
- * proof is invalid.
430
- * @since 0.10.0
431
- */
432
- async function verifyProof(jsonLd, proof, options = {}) {
433
- return await (options.tracerProvider ?? trace.getTracerProvider()).getTracer(name, version).startActiveSpan("object_integrity_proofs.verify", async (span) => {
434
- if (span.isRecording()) {
435
- if (proof.cryptosuite != null) span.setAttribute("object_integrity_proofs.cryptosuite", proof.cryptosuite);
436
- if (proof.verificationMethodId != null) span.setAttribute("object_integrity_proofs.key_id", proof.verificationMethodId.href);
437
- if (proof.proofValue != null) span.setAttribute("object_integrity_proofs.signature", encodeHex(proof.proofValue));
438
- }
439
- try {
440
- const key = await verifyProofInternal(jsonLd, proof, options);
441
- if (key == null) span.setStatus({ code: SpanStatusCode.ERROR });
442
- return key;
443
- } catch (error) {
444
- span.setStatus({
445
- code: SpanStatusCode.ERROR,
446
- message: String(error)
447
- });
448
- throw error;
449
- } finally {
450
- span.end();
451
- }
452
- });
453
- }
454
- async function verifyProofInternal(jsonLd, proof, options) {
455
- if (typeof jsonLd !== "object" || proof.cryptosuite !== "eddsa-jcs-2022" || proof.verificationMethodId == null || proof.proofPurpose !== "assertionMethod" || proof.proofValue == null || proof.created == null) return null;
456
- const publicKeyPromise = fetchKey(proof.verificationMethodId, Multikey, options);
457
- const proofCanon = serialize({
458
- "@context": jsonLd["@context"],
459
- type: "DataIntegrityProof",
460
- cryptosuite: proof.cryptosuite,
461
- verificationMethod: proof.verificationMethodId.href,
462
- proofPurpose: proof.proofPurpose,
463
- created: proof.created.toString()
464
- });
465
- const encoder = new TextEncoder();
466
- const proofBytes = encoder.encode(proofCanon);
467
- const proofDigest = await crypto.subtle.digest("SHA-256", proofBytes);
468
- const msg = { ...jsonLd };
469
- if ("proof" in msg) delete msg.proof;
470
- const msgCanon = serialize(msg);
471
- const msgBytes = encoder.encode(msgCanon);
472
- const msgDigest = await crypto.subtle.digest("SHA-256", msgBytes);
473
- const digest = new Uint8Array(proofDigest.byteLength + msgDigest.byteLength);
474
- digest.set(new Uint8Array(proofDigest), 0);
475
- digest.set(new Uint8Array(msgDigest), proofDigest.byteLength);
476
- let fetchedKey;
477
- try {
478
- fetchedKey = await publicKeyPromise;
479
- } catch (error) {
480
- logger.debug("Failed to get the key (verificationMethod) for the proof:\n{proof}", {
481
- proof,
482
- keyId: proof.verificationMethodId.href,
483
- error
484
- });
485
- return null;
486
- }
487
- const publicKey = fetchedKey.key;
488
- if (publicKey == null) {
489
- logger.debug("Failed to get the key (verificationMethod) for the proof:\n{proof}", {
490
- proof,
491
- keyId: proof.verificationMethodId.href
492
- });
493
- return null;
494
- }
495
- if (publicKey.publicKey.algorithm.name !== "Ed25519") {
496
- if (fetchedKey.cached) {
497
- logger.debug("The cached key (verificationMethod) for the proof is not a valid Ed25519 key:\n{keyId}; retrying with the freshly fetched key...", {
498
- proof,
499
- keyId: proof.verificationMethodId.href
500
- });
501
- return await verifyProof(jsonLd, proof, {
502
- ...options,
503
- keyCache: {
504
- get: () => Promise.resolve(null),
505
- set: async (keyId, key) => await options.keyCache?.set(keyId, key)
506
- }
507
- });
508
- }
509
- logger.debug("The fetched key (verificationMethod) for the proof is not a valid Ed25519 key:\n{keyId}", {
510
- proof,
511
- keyId: proof.verificationMethodId.href
512
- });
513
- return null;
514
- }
515
- if (!await crypto.subtle.verify("Ed25519", publicKey.publicKey, proof.proofValue.slice(), digest)) {
516
- if (fetchedKey.cached) {
517
- logger.debug("Failed to verify the proof with the cached key {keyId}; retrying with the freshly fetched key...", {
518
- keyId: proof.verificationMethodId.href,
519
- proof
520
- });
521
- return await verifyProof(jsonLd, proof, {
522
- ...options,
523
- keyCache: {
524
- get: () => Promise.resolve(void 0),
525
- set: async (keyId, key) => await options.keyCache?.set(keyId, key)
526
- }
527
- });
528
- }
529
- logger.debug("Failed to verify the proof with the fetched key {keyId}:\n{proof}", {
530
- keyId: proof.verificationMethodId.href,
531
- proof
532
- });
533
- return null;
534
- }
535
- return publicKey;
536
- }
537
- /**
538
- * Verifies the given object. It will verify all the proofs in the object,
539
- * and succeed only if all the proofs are valid and all attributions and
540
- * actors are authenticated by the proofs.
541
- * @typeParam T The type of the object to verify.
542
- * @param cls The class of the object to verify. It must be a subclass of
543
- * the {@link Object}.
544
- * @param jsonLd The JSON-LD object to verify. It's assumed that the object
545
- * is a compacted JSON-LD representation of a `T` with `@context`.
546
- * @param options Additional options. See also {@link VerifyObjectOptions}.
547
- * @returns The object if it's verified, or `null` if it's not.
548
- * @throws {TypeError} If the object is invalid or unsupported.
549
- * @since 0.10.0
550
- */
551
- async function verifyObject(cls, jsonLd, options = {}) {
552
- const logger$2 = getLogger([
553
- "fedify",
554
- "sig",
555
- "proof"
556
- ]);
557
- const object = await cls.fromJsonLd(jsonLd, options);
558
- const attributions = new Set(object.attributionIds.map((uri) => uri.href));
559
- if (object instanceof Activity) for (const uri of object.actorIds) attributions.add(uri.href);
560
- for await (const proof of object.getProofs(options)) {
561
- const key = await verifyProof(jsonLd, proof, options);
562
- if (key === null) return null;
563
- if (key.controllerId == null) {
564
- logger$2.debug("Key {keyId} does not have a controller.", { keyId: key.id?.href });
565
- continue;
566
- }
567
- attributions.delete(key.controllerId.href);
568
- }
569
- if (attributions.size > 0) {
570
- logger$2.debug("Some attributions are not authenticated by the proofs: {attributions}.", { attributions: [...attributions] });
571
- return null;
572
- }
573
- return object;
574
- }
575
-
576
- //#endregion
577
- export { doesActorOwnKey as a, createSignature as c, signJsonLd as d, verifyJsonLd as f, verifyProof as i, detachSignature as l, signObject as n, getKeyOwner as o, verifySignature as p, verifyObject as r, attachSignature as s, createProof as t, hasSignature as u };