applesauce-common 6.1.0 → 7.0.0

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 (231) hide show
  1. package/README.md +1 -0
  2. package/dist/casts/_circular-import.d.ts +3 -0
  3. package/dist/casts/_circular-import.js +3 -0
  4. package/dist/casts/app-handler.d.ts +56 -0
  5. package/dist/casts/app-handler.js +105 -0
  6. package/dist/casts/app-recommendation.d.ts +26 -0
  7. package/dist/casts/app-recommendation.js +41 -0
  8. package/dist/casts/external-identities.d.ts +9 -0
  9. package/dist/casts/external-identities.js +14 -0
  10. package/dist/casts/forum-thread.d.ts +17 -0
  11. package/dist/casts/forum-thread.js +31 -0
  12. package/dist/casts/git-issue.d.ts +18 -0
  13. package/dist/casts/git-issue.js +26 -0
  14. package/dist/casts/git-patch.d.ts +49 -0
  15. package/dist/casts/git-patch.js +88 -0
  16. package/dist/casts/git-pull-request.d.ts +67 -0
  17. package/dist/casts/git-pull-request.js +122 -0
  18. package/dist/casts/git-repository.d.ts +9 -0
  19. package/dist/casts/git-repository.js +16 -0
  20. package/dist/casts/git-thread.d.ts +33 -0
  21. package/dist/casts/git-thread.js +65 -0
  22. package/dist/casts/index.d.ts +11 -0
  23. package/dist/casts/index.js +11 -0
  24. package/dist/casts/napplet.d.ts +98 -0
  25. package/dist/casts/napplet.js +196 -0
  26. package/dist/casts/note.d.ts +8 -0
  27. package/dist/casts/note.js +21 -1
  28. package/dist/casts/nsite.d.ts +134 -0
  29. package/dist/casts/nsite.js +249 -0
  30. package/dist/casts/profile.d.ts +20 -0
  31. package/dist/casts/profile.js +30 -0
  32. package/dist/casts/reaction.d.ts +1 -1
  33. package/dist/casts/relay-discovery.d.ts +4 -0
  34. package/dist/casts/relay-discovery.js +9 -1
  35. package/dist/casts/report.d.ts +28 -15
  36. package/dist/casts/report.js +42 -19
  37. package/dist/casts/share.d.ts +1 -1
  38. package/dist/casts/torrent.d.ts +5 -0
  39. package/dist/casts/torrent.js +8 -1
  40. package/dist/casts/trusted-assertions.d.ts +4 -2
  41. package/dist/casts/trusted-assertions.js +20 -19
  42. package/dist/casts/user-status.d.ts +33 -0
  43. package/dist/casts/user-status.js +69 -0
  44. package/dist/casts/user.d.ts +7 -0
  45. package/dist/casts/user.js +20 -1
  46. package/dist/factories/app-handler.d.ts +31 -0
  47. package/dist/factories/app-handler.js +50 -0
  48. package/dist/factories/app-recommendation.d.ts +21 -0
  49. package/dist/factories/app-recommendation.js +28 -0
  50. package/dist/factories/chat-message.d.ts +37 -0
  51. package/dist/factories/chat-message.js +56 -0
  52. package/dist/factories/comment.d.ts +8 -7
  53. package/dist/factories/comment.js +2 -2
  54. package/dist/factories/external-identities.d.ts +17 -0
  55. package/dist/factories/external-identities.js +28 -0
  56. package/dist/factories/forum-thread.d.ts +34 -0
  57. package/dist/factories/forum-thread.js +50 -0
  58. package/dist/factories/git-applied-status.d.ts +39 -0
  59. package/dist/factories/git-applied-status.js +53 -0
  60. package/dist/factories/git-closed-status.d.ts +18 -0
  61. package/dist/factories/git-closed-status.js +24 -0
  62. package/dist/factories/git-draft-status.d.ts +18 -0
  63. package/dist/factories/git-draft-status.js +24 -0
  64. package/dist/factories/git-issue.d.ts +19 -0
  65. package/dist/factories/git-issue.js +25 -0
  66. package/dist/factories/git-open-status.d.ts +18 -0
  67. package/dist/factories/git-open-status.js +22 -0
  68. package/dist/factories/git-patch.d.ts +59 -0
  69. package/dist/factories/git-patch.js +102 -0
  70. package/dist/factories/git-pull-request-update.d.ts +23 -0
  71. package/dist/factories/git-pull-request-update.js +33 -0
  72. package/dist/factories/git-pull-request.d.ts +39 -0
  73. package/dist/factories/git-pull-request.js +64 -0
  74. package/dist/factories/index.d.ts +23 -0
  75. package/dist/factories/index.js +23 -0
  76. package/dist/factories/internal/git-pull-request.d.ts +19 -0
  77. package/dist/factories/internal/git-pull-request.js +26 -0
  78. package/dist/factories/internal/git-status.d.ts +11 -0
  79. package/dist/factories/internal/git-status.js +12 -0
  80. package/dist/factories/internal/git-thread.d.ts +19 -0
  81. package/dist/factories/internal/git-thread.js +29 -0
  82. package/dist/factories/internal/napplet-manifest.d.ts +52 -0
  83. package/dist/factories/internal/napplet-manifest.js +85 -0
  84. package/dist/factories/internal/nsite-manifest.d.ts +45 -0
  85. package/dist/factories/internal/nsite-manifest.js +68 -0
  86. package/dist/factories/legacy-message.d.ts +2 -0
  87. package/dist/factories/legacy-message.js +9 -2
  88. package/dist/factories/named-napplet.d.ts +20 -0
  89. package/dist/factories/named-napplet.js +30 -0
  90. package/dist/factories/named-nsite.d.ts +28 -0
  91. package/dist/factories/named-nsite.js +53 -0
  92. package/dist/factories/napplet-snapshot.d.ts +18 -0
  93. package/dist/factories/napplet-snapshot.js +38 -0
  94. package/dist/factories/nostr-web-token.d.ts +39 -0
  95. package/dist/factories/nostr-web-token.js +95 -0
  96. package/dist/factories/nsite-snapshot.d.ts +18 -0
  97. package/dist/factories/nsite-snapshot.js +29 -0
  98. package/dist/factories/reaction.d.ts +3 -2
  99. package/dist/factories/report.d.ts +48 -0
  100. package/dist/factories/report.js +66 -0
  101. package/dist/factories/root-napplet.d.ts +18 -0
  102. package/dist/factories/root-napplet.js +24 -0
  103. package/dist/factories/root-nsite.d.ts +23 -0
  104. package/dist/factories/root-nsite.js +44 -0
  105. package/dist/factories/torrent.d.ts +24 -10
  106. package/dist/factories/torrent.js +48 -44
  107. package/dist/factories/trusted-provider-list.d.ts +23 -0
  108. package/dist/factories/trusted-provider-list.js +39 -0
  109. package/dist/factories/user-status.d.ts +26 -0
  110. package/dist/factories/user-status.js +52 -0
  111. package/dist/helpers/app-data.js +15 -7
  112. package/dist/helpers/app-handler.d.ts +43 -6
  113. package/dist/helpers/app-handler.js +53 -3
  114. package/dist/helpers/app-recommendation.d.ts +24 -0
  115. package/dist/helpers/app-recommendation.js +30 -0
  116. package/dist/helpers/bookmark.js +5 -2
  117. package/dist/helpers/comment.d.ts +11 -5
  118. package/dist/helpers/comment.js +7 -1
  119. package/dist/helpers/content.d.ts +3 -1
  120. package/dist/helpers/emoji-pack.js +11 -6
  121. package/dist/helpers/emoji.d.ts +2 -2
  122. package/dist/helpers/encrypted-content-cache.js +18 -1
  123. package/dist/helpers/external-id.d.ts +1 -0
  124. package/dist/helpers/external-id.js +21 -46
  125. package/dist/helpers/external-identities.d.ts +29 -0
  126. package/dist/helpers/external-identities.js +52 -0
  127. package/dist/helpers/file-metadata.js +5 -6
  128. package/dist/helpers/forum-thread.d.ts +10 -0
  129. package/dist/helpers/forum-thread.js +10 -0
  130. package/dist/helpers/gift-wrap.d.ts +40 -15
  131. package/dist/helpers/gift-wrap.js +110 -33
  132. package/dist/helpers/git-issue.d.ts +13 -0
  133. package/dist/helpers/git-issue.js +19 -0
  134. package/dist/helpers/git-patch.d.ts +41 -0
  135. package/dist/helpers/git-patch.js +73 -0
  136. package/dist/helpers/git-pull-request.d.ts +57 -0
  137. package/dist/helpers/git-pull-request.js +111 -0
  138. package/dist/helpers/git-repository.d.ts +4 -0
  139. package/dist/helpers/git-repository.js +16 -0
  140. package/dist/helpers/git-status.d.ts +44 -0
  141. package/dist/helpers/git-status.js +93 -0
  142. package/dist/helpers/git.d.ts +2 -0
  143. package/dist/helpers/git.js +4 -0
  144. package/dist/helpers/groups.d.ts +5 -2
  145. package/dist/helpers/groups.js +40 -18
  146. package/dist/helpers/hashtag.d.ts +4 -2
  147. package/dist/helpers/index.d.ts +11 -0
  148. package/dist/helpers/index.js +11 -0
  149. package/dist/helpers/lists.js +14 -2
  150. package/dist/helpers/lnurl.js +1 -1
  151. package/dist/helpers/mute.js +18 -7
  152. package/dist/helpers/napplet.d.ts +89 -0
  153. package/dist/helpers/napplet.js +144 -0
  154. package/dist/helpers/nostr-web-token.d.ts +54 -0
  155. package/dist/helpers/nostr-web-token.js +124 -0
  156. package/dist/helpers/nsite.d.ts +90 -0
  157. package/dist/helpers/nsite.js +217 -0
  158. package/dist/helpers/relay-discovery.d.ts +4 -0
  159. package/dist/helpers/relay-discovery.js +8 -0
  160. package/dist/helpers/reports.d.ts +35 -0
  161. package/dist/helpers/reports.js +36 -5
  162. package/dist/helpers/threading.d.ts +4 -2
  163. package/dist/helpers/torrent.d.ts +9 -6
  164. package/dist/helpers/torrent.js +23 -15
  165. package/dist/helpers/trusted-assertions.d.ts +7 -0
  166. package/dist/helpers/trusted-assertions.js +41 -2
  167. package/dist/helpers/user-status.d.ts +13 -1
  168. package/dist/helpers/user-status.js +15 -0
  169. package/dist/models/app-handler.d.ts +21 -0
  170. package/dist/models/app-handler.js +47 -0
  171. package/dist/models/git.d.ts +34 -0
  172. package/dist/models/git.js +144 -0
  173. package/dist/models/index.d.ts +3 -0
  174. package/dist/models/index.js +3 -0
  175. package/dist/models/nsite.d.ts +22 -0
  176. package/dist/models/nsite.js +67 -0
  177. package/dist/models/thread.d.ts +4 -1
  178. package/dist/models/thread.js +34 -5
  179. package/dist/models/user-status.d.ts +8 -4
  180. package/dist/models/user-status.js +16 -22
  181. package/dist/observable/filter-timeline-by-mutes.d.ts +1 -1
  182. package/dist/observable/filter-timeline-by-mutes.js +5 -1
  183. package/dist/operations/app-handler.d.ts +24 -0
  184. package/dist/operations/app-handler.js +58 -0
  185. package/dist/operations/app-recommendation.d.ts +13 -0
  186. package/dist/operations/app-recommendation.js +40 -0
  187. package/dist/operations/calendar-event.js +3 -2
  188. package/dist/operations/chat-message.d.ts +9 -0
  189. package/dist/operations/chat-message.js +12 -0
  190. package/dist/operations/client.d.ts +1 -4
  191. package/dist/operations/client.js +1 -23
  192. package/dist/operations/comment.d.ts +9 -3
  193. package/dist/operations/comment.js +12 -9
  194. package/dist/operations/external-identities.d.ts +8 -0
  195. package/dist/operations/external-identities.js +23 -0
  196. package/dist/operations/forum-thread.d.ts +3 -0
  197. package/dist/operations/forum-thread.js +6 -0
  198. package/dist/operations/gift-wrap.js +38 -14
  199. package/dist/operations/git-issue.d.ts +18 -0
  200. package/dist/operations/git-issue.js +31 -0
  201. package/dist/operations/git-patch.d.ts +30 -0
  202. package/dist/operations/git-patch.js +75 -0
  203. package/dist/operations/git-pull-request.d.ts +29 -0
  204. package/dist/operations/git-pull-request.js +74 -0
  205. package/dist/operations/git-repository.js +4 -4
  206. package/dist/operations/git-status.d.ts +28 -0
  207. package/dist/operations/git-status.js +116 -0
  208. package/dist/operations/git-thread.d.ts +13 -0
  209. package/dist/operations/git-thread.js +39 -0
  210. package/dist/operations/index.d.ts +16 -0
  211. package/dist/operations/index.js +16 -0
  212. package/dist/operations/internal/git-commits.d.ts +2 -0
  213. package/dist/operations/internal/git-commits.js +16 -0
  214. package/dist/operations/napplet.d.ts +51 -0
  215. package/dist/operations/napplet.js +121 -0
  216. package/dist/operations/nostr-web-token.d.ts +27 -0
  217. package/dist/operations/nostr-web-token.js +58 -0
  218. package/dist/operations/nsite.d.ts +51 -0
  219. package/dist/operations/nsite.js +112 -0
  220. package/dist/operations/reaction.d.ts +12 -2
  221. package/dist/operations/reaction.js +1 -1
  222. package/dist/operations/report.d.ts +33 -0
  223. package/dist/operations/report.js +60 -0
  224. package/dist/operations/stream.js +5 -4
  225. package/dist/operations/torrent.d.ts +1 -4
  226. package/dist/operations/torrent.js +24 -11
  227. package/dist/operations/trusted-providers.d.ts +14 -0
  228. package/dist/operations/trusted-providers.js +33 -0
  229. package/dist/operations/user-status.d.ts +11 -0
  230. package/dist/operations/user-status.js +29 -0
  231. package/package.json +10 -10
@@ -0,0 +1,54 @@
1
+ import { KnownEvent, NostrEvent } from "applesauce-core/helpers/event";
2
+ /** @see https://github.com/Open-Ranking/nostr-web-tokens */
3
+ export declare const NOSTR_WEB_TOKEN_KIND = 27519;
4
+ export type NostrWebTokenEvent = KnownEvent<typeof NOSTR_WEB_TOKEN_KIND>;
5
+ export type NostrWebTokenClaims = {
6
+ /** Issuer claim, defaults to the signing pubkey when omitted */
7
+ issuer: string;
8
+ /** Subject claim, defaults to the signing pubkey when omitted */
9
+ subject: string;
10
+ /** Audience claims */
11
+ audiences: string[];
12
+ /** Issued-at timestamp, defaults to the event created_at when omitted */
13
+ issuedAt: number;
14
+ /** Expiration timestamp */
15
+ expiration?: number;
16
+ /** Not-before timestamp */
17
+ notBefore?: number;
18
+ /** Additional application-defined claims */
19
+ claims: Record<string, string[]>;
20
+ };
21
+ export declare const NostrWebTokenClaimsSymbol: unique symbol;
22
+ /** Parses all registered and custom claims from a Nostr Web Token event. */
23
+ export declare function parseNostrWebTokenClaims(event: NostrWebTokenEvent): NostrWebTokenClaims;
24
+ export declare function parseNostrWebTokenClaims(event: NostrEvent): NostrWebTokenClaims | undefined;
25
+ /** Gets the issuer claim, or the signing pubkey when absent. */
26
+ export declare function getNostrWebTokenIssuer(event: NostrWebTokenEvent): string;
27
+ export declare function getNostrWebTokenIssuer(event: NostrEvent): string | undefined;
28
+ /** Gets the subject claim, or the signing pubkey when absent. */
29
+ export declare function getNostrWebTokenSubject(event: NostrWebTokenEvent): string;
30
+ export declare function getNostrWebTokenSubject(event: NostrEvent): string | undefined;
31
+ /** Gets all audience claims. */
32
+ export declare function getNostrWebTokenAudiences(event: NostrWebTokenEvent): string[];
33
+ export declare function getNostrWebTokenAudiences(event: NostrEvent): string[] | undefined;
34
+ /** Gets the issued-at claim, or the event created_at when absent. */
35
+ export declare function getNostrWebTokenIssuedAt(event: NostrWebTokenEvent): number;
36
+ export declare function getNostrWebTokenIssuedAt(event: NostrEvent): number | undefined;
37
+ /** Gets the expiration claim. */
38
+ export declare function getNostrWebTokenExpiration(event: NostrEvent): number | undefined;
39
+ /** Gets the not-before claim. */
40
+ export declare function getNostrWebTokenNotBefore(event: NostrEvent): number | undefined;
41
+ /** Checks if an event is structurally a valid Nostr Web Token. Does not verify the event signature. */
42
+ export declare function isValidNostrWebToken(event?: NostrEvent): event is NostrWebTokenEvent;
43
+ /** Checks the exp and nbf claims against a timestamp. */
44
+ export declare function isNostrWebTokenActive(event: NostrEvent, now?: number, clockSkew?: number): boolean;
45
+ /** Checks if a token contains a matching audience claim. */
46
+ export declare function hasNostrWebTokenAudience(event: NostrEvent, audience: string): boolean;
47
+ /** Encodes a Nostr Web Token event for HTTP Authorization transport. */
48
+ export declare function encodeNostrWebToken(event: NostrEvent): string;
49
+ /** Decodes a Nostr Web Token from Base64URL transport encoding. */
50
+ export declare function decodeNostrWebToken(token: string): NostrEvent | undefined;
51
+ /** Creates an HTTP Authorization header value for a Nostr Web Token event. */
52
+ export declare function createNostrWebTokenAuthorizationHeader(event: NostrEvent): string;
53
+ /** Parses an HTTP Authorization header value containing a Nostr Web Token. */
54
+ export declare function parseNostrWebTokenAuthorizationHeader(header: string | null | undefined): NostrEvent | undefined;
@@ -0,0 +1,124 @@
1
+ import { base64urlnopad, utf8 } from "@scure/base";
2
+ import { getOrComputeCachedValue } from "applesauce-core/helpers/cache";
3
+ import { getTagValue, isEvent } from "applesauce-core/helpers/event";
4
+ /** @see https://github.com/Open-Ranking/nostr-web-tokens */
5
+ export const NOSTR_WEB_TOKEN_KIND = 27519;
6
+ export const NostrWebTokenClaimsSymbol = Symbol.for("nostr-web-token-claims");
7
+ const singletonClaims = new Set(["iss", "sub", "iat", "exp", "nbf"]);
8
+ const registeredClaims = new Set(["iss", "sub", "aud", "iat", "exp", "nbf"]);
9
+ function parseTimestamp(value) {
10
+ if (value === undefined || !/^\d+$/.test(value))
11
+ return undefined;
12
+ return Number.parseInt(value, 10);
13
+ }
14
+ function isValidTimestamp(value) {
15
+ return parseTimestamp(value) !== undefined;
16
+ }
17
+ export function parseNostrWebTokenClaims(event) {
18
+ if (event.kind !== NOSTR_WEB_TOKEN_KIND)
19
+ return undefined;
20
+ return getOrComputeCachedValue(event, NostrWebTokenClaimsSymbol, () => {
21
+ const claims = {};
22
+ const audiences = [];
23
+ for (const tag of event.tags) {
24
+ const [name, value] = tag;
25
+ if (value === undefined)
26
+ continue;
27
+ if (name === "aud")
28
+ audiences.push(value);
29
+ else if (!registeredClaims.has(name))
30
+ claims[name] = claims[name] ? [...claims[name], value] : [value];
31
+ }
32
+ return {
33
+ issuer: getTagValue(event, "iss") ?? event.pubkey,
34
+ subject: getTagValue(event, "sub") ?? event.pubkey,
35
+ audiences,
36
+ issuedAt: parseTimestamp(getTagValue(event, "iat")) ?? event.created_at,
37
+ expiration: parseTimestamp(getTagValue(event, "exp")),
38
+ notBefore: parseTimestamp(getTagValue(event, "nbf")),
39
+ claims,
40
+ };
41
+ });
42
+ }
43
+ export function getNostrWebTokenIssuer(event) {
44
+ return parseNostrWebTokenClaims(event)?.issuer;
45
+ }
46
+ export function getNostrWebTokenSubject(event) {
47
+ return parseNostrWebTokenClaims(event)?.subject;
48
+ }
49
+ export function getNostrWebTokenAudiences(event) {
50
+ return parseNostrWebTokenClaims(event)?.audiences;
51
+ }
52
+ export function getNostrWebTokenIssuedAt(event) {
53
+ return parseNostrWebTokenClaims(event)?.issuedAt;
54
+ }
55
+ /** Gets the expiration claim. */
56
+ export function getNostrWebTokenExpiration(event) {
57
+ return parseNostrWebTokenClaims(event)?.expiration;
58
+ }
59
+ /** Gets the not-before claim. */
60
+ export function getNostrWebTokenNotBefore(event) {
61
+ return parseNostrWebTokenClaims(event)?.notBefore;
62
+ }
63
+ /** Checks if an event is structurally a valid Nostr Web Token. Does not verify the event signature. */
64
+ export function isValidNostrWebToken(event) {
65
+ if (!event || event.kind !== NOSTR_WEB_TOKEN_KIND)
66
+ return false;
67
+ if (!isEvent(event))
68
+ return false;
69
+ for (const name of registeredClaims) {
70
+ const tags = event.tags.filter((tag) => tag[0] === name);
71
+ if (tags.some((tag) => tag[1] === undefined))
72
+ return false;
73
+ if (singletonClaims.has(name) && tags.length > 1)
74
+ return false;
75
+ }
76
+ for (const name of ["iat", "exp", "nbf"]) {
77
+ const tag = event.tags.find((t) => t[0] === name);
78
+ if (tag && !isValidTimestamp(tag[1]))
79
+ return false;
80
+ }
81
+ return true;
82
+ }
83
+ /** Checks the exp and nbf claims against a timestamp. */
84
+ export function isNostrWebTokenActive(event, now = Math.floor(Date.now() / 1000), clockSkew = 60) {
85
+ if (!isValidNostrWebToken(event))
86
+ return false;
87
+ const claims = parseNostrWebTokenClaims(event);
88
+ if (!claims)
89
+ return false;
90
+ if (claims.expiration !== undefined && now - clockSkew >= claims.expiration)
91
+ return false;
92
+ if (claims.notBefore !== undefined && now + clockSkew < claims.notBefore)
93
+ return false;
94
+ return true;
95
+ }
96
+ /** Checks if a token contains a matching audience claim. */
97
+ export function hasNostrWebTokenAudience(event, audience) {
98
+ return parseNostrWebTokenClaims(event)?.audiences.includes(audience) ?? false;
99
+ }
100
+ /** Encodes a Nostr Web Token event for HTTP Authorization transport. */
101
+ export function encodeNostrWebToken(event) {
102
+ return base64urlnopad.encode(utf8.decode(JSON.stringify(event)));
103
+ }
104
+ /** Decodes a Nostr Web Token from Base64URL transport encoding. */
105
+ export function decodeNostrWebToken(token) {
106
+ try {
107
+ const event = JSON.parse(utf8.encode(base64urlnopad.decode(token)));
108
+ return isEvent(event) ? event : undefined;
109
+ }
110
+ catch {
111
+ return undefined;
112
+ }
113
+ }
114
+ /** Creates an HTTP Authorization header value for a Nostr Web Token event. */
115
+ export function createNostrWebTokenAuthorizationHeader(event) {
116
+ return `Nostr ${encodeNostrWebToken(event)}`;
117
+ }
118
+ /** Parses an HTTP Authorization header value containing a Nostr Web Token. */
119
+ export function parseNostrWebTokenAuthorizationHeader(header) {
120
+ const match = header?.match(/^Nostr\s+(.+)$/i);
121
+ if (!match)
122
+ return undefined;
123
+ return decodeNostrWebToken(match[1]);
124
+ }
@@ -0,0 +1,90 @@
1
+ import { KnownEvent, NostrEvent } from "applesauce-core/helpers/event";
2
+ import { AddressPointer, EventPointer } from "applesauce-core/helpers/pointers";
3
+ /** The event kind for NIP-5A manifest snapshots */
4
+ export declare const NSITE_SNAPSHOT_KIND = 5128;
5
+ /** The event kind for NIP-5A root sites */
6
+ export declare const NSITE_ROOT_KIND = 15128;
7
+ /** The event kind for NIP-5A named sites */
8
+ export declare const NSITE_KIND = 35128;
9
+ /** The event kinds of an nsite manifest */
10
+ export type NsiteKind = typeof NSITE_SNAPSHOT_KIND | typeof NSITE_ROOT_KIND | typeof NSITE_KIND;
11
+ /** All NIP-5A manifest kinds */
12
+ export declare const NSITE_KINDS: NsiteKind[];
13
+ /** A validated nsite manifest */
14
+ export type NsiteEvent = KnownEvent<NsiteKind>;
15
+ /** A root site manifest */
16
+ export type RootNsiteEvent = KnownEvent<typeof NSITE_ROOT_KIND>;
17
+ /** A named site manifest */
18
+ export type NamedNsiteEvent = KnownEvent<typeof NSITE_KIND>;
19
+ /** A manifest snapshot */
20
+ export type NsiteSnapshotEvent = KnownEvent<typeof NSITE_SNAPSHOT_KIND>;
21
+ /** A file in an nsite manifest */
22
+ export interface NsiteFile {
23
+ /** The absolute path to the file */
24
+ path: string;
25
+ /** The lowercase hexadecimal SHA-256 of the file */
26
+ sha256: string;
27
+ }
28
+ /** Checks if an identifier fits the canonical named-site DNS label */
29
+ export declare function isValidNsiteIdentifier(identifier: string): boolean;
30
+ /** Checks if a path is absolute and ends in a filename with an extension */
31
+ export declare function isValidNsitePath(path: string): boolean;
32
+ /** Checks if an address pointer references a root or named site */
33
+ export declare function isNsiteAddressPointer(pointer: AddressPointer): boolean;
34
+ /** Checks if an event is a root site */
35
+ export declare function isRootNsite(event?: NostrEvent): event is RootNsiteEvent;
36
+ /** Checks if an event is a named site */
37
+ export declare function isNamedNsite(event?: NostrEvent): event is NamedNsiteEvent;
38
+ /** Checks if an event is a manifest snapshot */
39
+ export declare function isNsiteSnapshot(event?: NostrEvent): event is NsiteSnapshotEvent;
40
+ /**
41
+ * Checks if an event is a valid NIP-5A manifest
42
+ * @see https://github.com/nostr-protocol/nips/blob/master/5A.md
43
+ */
44
+ export declare function isValidNsite(event?: NostrEvent): event is NsiteEvent;
45
+ /** Checks if an event is a valid root site */
46
+ export declare function isValidRootNsite(event?: NostrEvent): event is RootNsiteEvent;
47
+ /** Checks if an event is a valid named site */
48
+ export declare function isValidNamedNsite(event?: NostrEvent): event is NamedNsiteEvent;
49
+ /** Checks if an event is a valid manifest snapshot */
50
+ export declare function isValidNsiteSnapshot(event?: NostrEvent): event is NsiteSnapshotEvent;
51
+ /** Returns the named site's identifier */
52
+ export declare function getNsiteIdentifier(event: NostrEvent): string | undefined;
53
+ /** Returns the site's display title */
54
+ export declare function getNsiteTitle(event: NostrEvent): string | undefined;
55
+ /** Returns the site's description tag */
56
+ export declare function getNsiteDescription(event: NostrEvent): string | undefined;
57
+ /** Returns the site's source repository or archive URL */
58
+ export declare function getNsiteSource(event: NostrEvent): string | undefined;
59
+ /** Returns the valid file mappings from the manifest */
60
+ export declare function getNsiteFiles(event: NostrEvent): NsiteFile[];
61
+ /** Returns the Blossom server hints */
62
+ export declare function getNsiteServers(event: NostrEvent): string[];
63
+ /** Returns the upstream addressable app descriptor pointers */
64
+ export declare function getNsiteAppPointers(event: NostrEvent): AddressPointer[];
65
+ /** Returns the immediate parent site address */
66
+ export declare function getNsiteParentPointer(event: NostrEvent): AddressPointer | undefined;
67
+ /** Returns the origin site address of a copy lineage */
68
+ export declare function getNsiteRootPointer(event: NostrEvent): AddressPointer | undefined;
69
+ /** Returns the advertised aggregate hash */
70
+ export declare function getNsiteAggregateHash(event: NostrEvent): string | undefined;
71
+ /** Returns the deterministic SHA-256 of all path tags, including duplicates */
72
+ export declare function computeNsiteAggregateHash(manifest: Pick<NostrEvent, "tags">): string;
73
+ /** Returns the cached computed aggregate hash of a manifest */
74
+ export declare function getComputedNsiteAggregateHash(event: NostrEvent): string;
75
+ /** Checks if the advertised aggregate hash matches the manifest's path tags */
76
+ export declare function verifyNsiteAggregateHash(event: NostrEvent): boolean;
77
+ /** Resolves a request path with index.html and 404.html fallbacks */
78
+ export declare function resolveNsitePath(event: NostrEvent, path: string): NsiteFile | undefined;
79
+ /**
80
+ * Returns the canonical DNS label for an nsite manifest
81
+ * @throws if the manifest is invalid
82
+ */
83
+ export declare function getNsiteHostLabel(event: NostrEvent): string;
84
+ /** Parses a canonical nsite DNS label into a site or snapshot pointer */
85
+ export declare function parseNsiteHostLabel(label: string): AddressPointer | EventPointer | undefined;
86
+ /**
87
+ * Returns a site's URL on an nsite host
88
+ * @throws if the manifest or host URL is invalid
89
+ */
90
+ export declare function getNsiteURL(event: NostrEvent, host: string | URL): URL;
@@ -0,0 +1,217 @@
1
+ import { sha256 } from "@noble/hashes/sha2.js";
2
+ import { getOrComputeCachedValue } from "applesauce-core/helpers/cache";
3
+ import { bytesToHex, getTagValue } from "applesauce-core/helpers/event";
4
+ import { decodePointer, getAddressPointerFromATag, npubEncode, } from "applesauce-core/helpers/pointers";
5
+ import { processTags } from "applesauce-core/helpers/tags";
6
+ import { isSha256 } from "./blossom.js";
7
+ /** The event kind for NIP-5A manifest snapshots */
8
+ export const NSITE_SNAPSHOT_KIND = 5128;
9
+ /** The event kind for NIP-5A root sites */
10
+ export const NSITE_ROOT_KIND = 15128;
11
+ /** The event kind for NIP-5A named sites */
12
+ export const NSITE_KIND = 35128;
13
+ /** All NIP-5A manifest kinds */
14
+ export const NSITE_KINDS = [NSITE_SNAPSHOT_KIND, NSITE_ROOT_KIND, NSITE_KIND];
15
+ const FilesSymbol = Symbol.for("nsite-files");
16
+ const ServersSymbol = Symbol.for("nsite-servers");
17
+ const AppsSymbol = Symbol.for("nsite-apps");
18
+ const HashSymbol = Symbol.for("nsite-aggregate-hash");
19
+ /** Checks if an identifier fits the canonical named-site DNS label */
20
+ export function isValidNsiteIdentifier(identifier) {
21
+ return /^[a-z0-9-]{1,13}$/.test(identifier) && !identifier.endsWith("-");
22
+ }
23
+ /** Checks if a path is absolute and ends in a filename with an extension */
24
+ export function isValidNsitePath(path) {
25
+ return (/^\/(?:[^\r\n?#\\/]+\/)*[^\r\n?#\\/]+\.[^\r\n?#\\/.]+$/u.test(path) &&
26
+ !path.split("/").some((part) => part === "." || part === ".."));
27
+ }
28
+ /** Checks if an address pointer references a root or named site */
29
+ export function isNsiteAddressPointer(pointer) {
30
+ return (isSha256(pointer.pubkey) &&
31
+ ((pointer.kind === NSITE_ROOT_KIND && pointer.identifier === "") ||
32
+ (pointer.kind === NSITE_KIND && isValidNsiteIdentifier(pointer.identifier))));
33
+ }
34
+ /** Checks if an event is a root site */
35
+ export function isRootNsite(event) {
36
+ return event?.kind === NSITE_ROOT_KIND;
37
+ }
38
+ /** Checks if an event is a named site */
39
+ export function isNamedNsite(event) {
40
+ return event?.kind === NSITE_KIND;
41
+ }
42
+ /** Checks if an event is a manifest snapshot */
43
+ export function isNsiteSnapshot(event) {
44
+ return event?.kind === NSITE_SNAPSHOT_KIND;
45
+ }
46
+ function lineagePointer(event, name) {
47
+ const tag = event.tags.find((t) => t[0] === name);
48
+ const pointer = tag && /^(15128|35128):[0-9a-f]{64}:/.test(tag[1] ?? "") && getAddressPointerFromATag(tag);
49
+ return pointer && isNsiteAddressPointer(pointer) ? pointer : undefined;
50
+ }
51
+ /**
52
+ * Checks if an event is a valid NIP-5A manifest
53
+ * @see https://github.com/nostr-protocol/nips/blob/master/5A.md
54
+ */
55
+ export function isValidNsite(event) {
56
+ if (!event || !NSITE_KINDS.includes(event.kind))
57
+ return false;
58
+ const paths = event.tags.filter((t) => t[0] === "path");
59
+ if (!paths.length || paths.some((t) => t.length !== 3 || !isValidNsitePath(t[1]) || !isSha256(t[2])))
60
+ return false;
61
+ const d = event.tags.filter((t) => t[0] === "d");
62
+ if (isNamedNsite(event) ? d.length !== 1 || !isValidNsiteIdentifier(d[0][1] ?? "") : d.length !== 0)
63
+ return false;
64
+ const x = event.tags.filter((t) => t[0] === "x");
65
+ if (x.length > 1 || x.some((t) => t.length !== 3 || !isSha256(t[1]) || t[2] !== "aggregate"))
66
+ return false;
67
+ const a = event.tags.filter((t) => t[0] === "a");
68
+ const A = event.tags.filter((t) => t[0] === "A");
69
+ if (a.length > 1 ||
70
+ A.length > 1 ||
71
+ (a.length && !getNsiteParentPointer(event)) ||
72
+ (A.length && !getNsiteRootPointer(event)))
73
+ return false;
74
+ if (isNsiteSnapshot(event))
75
+ return x.length === 1 && a.length === 1;
76
+ return a.length === A.length;
77
+ }
78
+ /** Checks if an event is a valid root site */
79
+ export function isValidRootNsite(event) {
80
+ return isRootNsite(event) && isValidNsite(event);
81
+ }
82
+ /** Checks if an event is a valid named site */
83
+ export function isValidNamedNsite(event) {
84
+ return isNamedNsite(event) && isValidNsite(event);
85
+ }
86
+ /** Checks if an event is a valid manifest snapshot */
87
+ export function isValidNsiteSnapshot(event) {
88
+ return isNsiteSnapshot(event) && isValidNsite(event);
89
+ }
90
+ /** Returns the named site's identifier */
91
+ export function getNsiteIdentifier(event) {
92
+ return isNamedNsite(event) ? getTagValue(event, "d") : undefined;
93
+ }
94
+ /** Returns the site's display title */
95
+ export function getNsiteTitle(event) {
96
+ return getTagValue(event, "title");
97
+ }
98
+ /** Returns the site's description tag */
99
+ export function getNsiteDescription(event) {
100
+ return getTagValue(event, "description");
101
+ }
102
+ /** Returns the site's source repository or archive URL */
103
+ export function getNsiteSource(event) {
104
+ return getTagValue(event, "source");
105
+ }
106
+ /** Returns the valid file mappings from the manifest */
107
+ export function getNsiteFiles(event) {
108
+ return getOrComputeCachedValue(event, FilesSymbol, () => processTags(event.tags, (t) => t[0] === "path" && t.length === 3 && isValidNsitePath(t[1]) && isSha256(t[2])
109
+ ? { path: t[1], sha256: t[2] }
110
+ : undefined));
111
+ }
112
+ /** Returns the Blossom server hints */
113
+ export function getNsiteServers(event) {
114
+ return getOrComputeCachedValue(event, ServersSymbol, () => processTags(event.tags, (t) => (t[0] === "server" && t[1] ? t[1] : undefined)));
115
+ }
116
+ /** Returns the upstream addressable app descriptor pointers */
117
+ export function getNsiteAppPointers(event) {
118
+ return getOrComputeCachedValue(event, AppsSymbol, () => processTags(event.tags, (t) => {
119
+ if (t[0] !== "app" || !t[1])
120
+ return undefined;
121
+ const pointer = getAddressPointerFromATag(t);
122
+ return pointer && pointer.kind >= 30000 && pointer.kind < 40000 ? pointer : undefined;
123
+ }));
124
+ }
125
+ /** Returns the immediate parent site address */
126
+ export function getNsiteParentPointer(event) {
127
+ return lineagePointer(event, "a");
128
+ }
129
+ /** Returns the origin site address of a copy lineage */
130
+ export function getNsiteRootPointer(event) {
131
+ return lineagePointer(event, "A");
132
+ }
133
+ /** Returns the advertised aggregate hash */
134
+ export function getNsiteAggregateHash(event) {
135
+ const tag = event.tags.find((t) => t[0] === "x" && t[2] === "aggregate");
136
+ return tag?.[1] && isSha256(tag[1]) ? tag[1] : undefined;
137
+ }
138
+ /** Returns the deterministic SHA-256 of all path tags, including duplicates */
139
+ export function computeNsiteAggregateHash(manifest) {
140
+ const lines = manifest.tags
141
+ .filter((t) => t[0] === "path")
142
+ .map(([, path, hash]) => `${hash} ${path}\n`)
143
+ .sort()
144
+ .join("");
145
+ return bytesToHex(sha256(new TextEncoder().encode(lines)));
146
+ }
147
+ /** Returns the cached computed aggregate hash of a manifest */
148
+ export function getComputedNsiteAggregateHash(event) {
149
+ return getOrComputeCachedValue(event, HashSymbol, () => computeNsiteAggregateHash(event));
150
+ }
151
+ /** Checks if the advertised aggregate hash matches the manifest's path tags */
152
+ export function verifyNsiteAggregateHash(event) {
153
+ return getNsiteAggregateHash(event) === getComputedNsiteAggregateHash(event);
154
+ }
155
+ /** Resolves a request path with index.html and 404.html fallbacks */
156
+ export function resolveNsitePath(event, path) {
157
+ if (!path.startsWith("/"))
158
+ return undefined;
159
+ const file = path.split("/").at(-1);
160
+ const resolved = file.includes(".") ? path : `${path.replace(/\/$/, "")}/index.html`;
161
+ const files = getNsiteFiles(event);
162
+ return files.find((f) => f.path === resolved) ?? files.find((f) => f.path === "/404.html");
163
+ }
164
+ function decodeBase36(value) {
165
+ let n = 0n;
166
+ for (const char of value)
167
+ n = n * 36n + BigInt(parseInt(char, 36));
168
+ const hex = n.toString(16);
169
+ return hex.length <= 64 ? hex.padStart(64, "0") : undefined;
170
+ }
171
+ /**
172
+ * Returns the canonical DNS label for an nsite manifest
173
+ * @throws if the manifest is invalid
174
+ */
175
+ export function getNsiteHostLabel(event) {
176
+ if (!isValidNsite(event))
177
+ throw new Error("Invalid nsite manifest");
178
+ if (isRootNsite(event))
179
+ return npubEncode(event.pubkey);
180
+ const hex = isNsiteSnapshot(event) ? event.id : event.pubkey;
181
+ // Fixed width preserves leading-zero keys and the specification's 50-character routing boundary.
182
+ const value = BigInt(`0x${hex}`).toString(36).padStart(50, "0");
183
+ return isNsiteSnapshot(event) ? `v${value}` : `${value}${getNsiteIdentifier(event)}`;
184
+ }
185
+ /** Parses a canonical nsite DNS label into a site or snapshot pointer */
186
+ export function parseNsiteHostLabel(label) {
187
+ if (label.startsWith("npub1")) {
188
+ try {
189
+ const decoded = decodePointer(label);
190
+ return decoded.type === "npub" ? { kind: NSITE_ROOT_KIND, pubkey: decoded.data, identifier: "" } : undefined;
191
+ }
192
+ catch {
193
+ return undefined;
194
+ }
195
+ }
196
+ if (/^v[0-9a-z]{50}$/.test(label)) {
197
+ const id = decodeBase36(label.slice(1));
198
+ return id ? { id } : undefined;
199
+ }
200
+ if (/^[0-9a-z]{50}[a-z0-9-]{1,13}$/.test(label) && !label.endsWith("-")) {
201
+ const pubkey = decodeBase36(label.slice(0, 50));
202
+ return pubkey ? { kind: NSITE_KIND, pubkey, identifier: label.slice(50) } : undefined;
203
+ }
204
+ return undefined;
205
+ }
206
+ /**
207
+ * Returns a site's URL on an nsite host
208
+ * @throws if the manifest or host URL is invalid
209
+ */
210
+ export function getNsiteURL(event, host) {
211
+ const url = new URL(typeof host === "string" && !host.includes("://") ? `https://${host}` : host);
212
+ url.hostname = `${getNsiteHostLabel(event)}.${url.hostname}`;
213
+ url.pathname = "/";
214
+ url.search = "";
215
+ url.hash = "";
216
+ return url;
217
+ }
@@ -20,6 +20,8 @@ export declare function getRelayDiscoveryRTT(event: NostrEvent, type: "open" | "
20
20
  * Should be one of: clearnet, tor, i2p, loki
21
21
  */
22
22
  export declare function getRelayDiscoveryNetworkType(event: NostrEvent): string | undefined;
23
+ /** Gets all relay types from a relay discovery event (from `T` tags), e.g. `PublicOutbox` */
24
+ export declare function getRelayDiscoveryTypes(event: NostrEvent): string[];
23
25
  /**
24
26
  * Gets all relay attributes from a relay discovery event (from `W` tags)
25
27
  * Attributes describe the relay type/characteristics
@@ -51,6 +53,8 @@ export declare function getRelayDiscoveryAcceptedKinds(event: NostrEvent): strin
51
53
  * This is a NIP-52 geohash
52
54
  */
53
55
  export declare function getRelayDiscoveryGeohash(event: NostrEvent): string | undefined;
56
+ /** Gets all geohashes from a relay discovery event (from `g` tags) */
57
+ export declare function getRelayDiscoveryGeohashes(event: NostrEvent): string[];
54
58
  /**
55
59
  * Validates that an event is a proper relay discovery event
56
60
  * Checks that the event is kind 30166 and has the required `d` tag
@@ -26,6 +26,10 @@ export function getRelayDiscoveryRTT(event, type) {
26
26
  export function getRelayDiscoveryNetworkType(event) {
27
27
  return getTagValue(event, "n");
28
28
  }
29
+ /** Gets all relay types from a relay discovery event (from `T` tags), e.g. `PublicOutbox` */
30
+ export function getRelayDiscoveryTypes(event) {
31
+ return event.tags.filter((t) => t[0] === "T" && t[1]).map((t) => t[1]);
32
+ }
29
33
  /**
30
34
  * Gets all relay attributes from a relay discovery event (from `W` tags)
31
35
  * Attributes describe the relay type/characteristics
@@ -69,6 +73,10 @@ export function getRelayDiscoveryAcceptedKinds(event) {
69
73
  export function getRelayDiscoveryGeohash(event) {
70
74
  return getTagValue(event, "g");
71
75
  }
76
+ /** Gets all geohashes from a relay discovery event (from `g` tags) */
77
+ export function getRelayDiscoveryGeohashes(event) {
78
+ return event.tags.filter((t) => t[0] === "g" && t[1]).map((t) => t[1]);
79
+ }
72
80
  /**
73
81
  * Validates that an event is a proper relay discovery event
74
82
  * Checks that the event is kind 30166 and has the required `d` tag
@@ -1,5 +1,9 @@
1
1
  import { NostrEvent } from "applesauce-core/helpers/event";
2
+ /** The NIP-56 report event kind */
3
+ export declare const REPORT_KIND = 1984;
4
+ /** The cache key for the legacy report projection */
2
5
  export declare const ParsedReportSymbol: unique symbol;
6
+ /** The standard NIP-56 report categories */
3
7
  export declare enum ReportReason {
4
8
  nudity = "nudity",
5
9
  malware = "malware",
@@ -9,23 +13,54 @@ export declare enum ReportReason {
9
13
  impersonation = "impersonation",
10
14
  other = "other"
11
15
  }
16
+ /** A user report with its source event */
12
17
  export type ReportedUser = {
18
+ /** The report category */
13
19
  type: "user";
20
+ /** The source report */
14
21
  event: NostrEvent;
22
+ /** The reported public key */
15
23
  pubkey: string;
24
+ /** The report reason */
16
25
  reason?: ReportReason;
26
+ /** The reporter's explanation */
17
27
  comment?: string;
18
28
  };
29
+ /** An event report with its source event */
19
30
  export type ReportedEvent = {
31
+ /** The report category */
20
32
  type: "event";
33
+ /** The source report */
21
34
  event: NostrEvent;
35
+ /** The reporter's explanation */
22
36
  comment?: string;
37
+ /** The reported event ID */
23
38
  id: string;
39
+ /** The reported public key */
24
40
  pubkey: string;
41
+ /** The report reason */
25
42
  reason?: ReportReason;
43
+ /** The reported blob hashes */
26
44
  blobs?: string[];
27
45
  };
28
46
  /** Reads a report event as either a user or event report */
29
47
  export declare function getReported(report: NostrEvent): ReportedEvent | ReportedUser | null;
30
48
  /** Gets the server tags from a report event (for blob reports) */
31
49
  export declare function getReportServers(report: NostrEvent): string[];
50
+ /** A target explicitly assigned a report reason */
51
+ export type ReportTarget = {
52
+ /** The target category */
53
+ type: "user" | "event" | "blob";
54
+ /** The public key, event ID, or blob hash */
55
+ value: string;
56
+ /** The report category for this target */
57
+ reason: ReportReason;
58
+ };
59
+ /** Checks whether a value is a lowercase 32-byte hex identifier */
60
+ export declare function isReportIdentifier(value: string | undefined): value is string;
61
+ /** Returns all explicitly reported targets and their individual reasons */
62
+ export declare function getReportTargets(event: NostrEvent): ReportTarget[];
63
+ /** Checks whether an event has NIP-56 targets and required context tags */
64
+ export declare function isValidReport(event: NostrEvent): event is NostrEvent & {
65
+ kind: typeof REPORT_KIND;
66
+ };
@@ -1,6 +1,10 @@
1
1
  import { getOrComputeCachedValue } from "applesauce-core/helpers/cache";
2
2
  import { isETag, isNameValueTag, isPTag } from "applesauce-core/helpers/tags";
3
+ /** The NIP-56 report event kind */
4
+ export const REPORT_KIND = 1984;
5
+ /** The cache key for the legacy report projection */
3
6
  export const ParsedReportSymbol = Symbol("parsed-report");
7
+ /** The standard NIP-56 report categories */
4
8
  export var ReportReason;
5
9
  (function (ReportReason) {
6
10
  ReportReason["nudity"] = "nudity";
@@ -13,30 +17,57 @@ export var ReportReason;
13
17
  })(ReportReason || (ReportReason = {}));
14
18
  /** Reads a report event as either a user or event report */
15
19
  export function getReported(report) {
20
+ if (report.kind !== REPORT_KIND)
21
+ return null;
16
22
  return getOrComputeCachedValue(report, ParsedReportSymbol, () => {
17
- const pTag = report.tags.find(isPTag);
23
+ const pTag = report.tags.find((t) => isPTag(t) && isReportIdentifier(t[1]));
18
24
  if (!pTag)
19
25
  return null;
20
26
  const comment = report.content ? report.content.trim() : undefined;
21
- const eTag = report.tags.find(isETag);
27
+ const eTag = report.tags.find((t) => isETag(t) && isReportIdentifier(t[1]));
22
28
  // Event report
23
29
  if (eTag) {
24
- const blobs = report.tags.filter((t) => t[0] === "x" && t[1]).map((t) => t[1]);
30
+ const blobs = report.tags.filter((t) => t[0] === "x" && isReportIdentifier(t[1])).map((t) => t[1]);
25
31
  return {
26
32
  type: "event",
27
33
  event: report,
28
34
  comment,
29
35
  id: eTag[1],
30
36
  pubkey: pTag[1],
31
- reason: eTag[2],
37
+ reason: parseReason(eTag[2]) ?? getReportTargets(report).find((t) => t.type === "blob")?.reason,
32
38
  blobs,
33
39
  };
34
40
  }
35
41
  // User report
36
- return { type: "user", event: report, comment, pubkey: pTag[1], reason: pTag[2] };
42
+ return { type: "user", event: report, comment, pubkey: pTag[1], reason: parseReason(pTag[2]) };
37
43
  });
38
44
  }
39
45
  /** Gets the server tags from a report event (for blob reports) */
40
46
  export function getReportServers(report) {
41
47
  return report.tags.filter((t) => isNameValueTag(t, "server") && t[1]).map((t) => t[1]);
42
48
  }
49
+ /** Checks whether a value is a lowercase 32-byte hex identifier */
50
+ export function isReportIdentifier(value) {
51
+ return typeof value === "string" && /^[0-9a-f]{64}$/.test(value);
52
+ }
53
+ function parseReason(value) {
54
+ return Object.values(ReportReason).includes(value) ? value : undefined;
55
+ }
56
+ /** Returns all explicitly reported targets and their individual reasons */
57
+ export function getReportTargets(event) {
58
+ if (event.kind !== REPORT_KIND)
59
+ return [];
60
+ return event.tags.flatMap((tag) => {
61
+ const type = tag[0] === "p" ? "user" : tag[0] === "e" ? "event" : tag[0] === "x" ? "blob" : undefined;
62
+ const reason = parseReason(tag[2]);
63
+ return type && isReportIdentifier(tag[1]) && reason ? [{ type, value: tag[1], reason }] : [];
64
+ });
65
+ }
66
+ /** Checks whether an event has NIP-56 targets and required context tags */
67
+ export function isValidReport(event) {
68
+ if (event.kind !== REPORT_KIND || !event.tags.some((t) => isPTag(t) && isReportIdentifier(t[1])))
69
+ return false;
70
+ const targets = getReportTargets(event);
71
+ return (targets.length > 0 &&
72
+ (!targets.some((t) => t.type === "blob") || event.tags.some((t) => isETag(t) && isReportIdentifier(t[1]))));
73
+ }