@hashtree/worker 0.1.26 → 0.2.1

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 (258) hide show
  1. package/README.md +16 -16
  2. package/package.json +23 -19
  3. package/src/app-runtime.ts +393 -0
  4. package/src/capabilities/blossomBandwidthTracker.ts +74 -0
  5. package/src/capabilities/blossomTransport.ts +179 -0
  6. package/src/capabilities/connectivity.ts +54 -0
  7. package/src/capabilities/idbStorage.ts +94 -0
  8. package/src/capabilities/meshRouterStore.ts +426 -0
  9. package/src/capabilities/rootResolver.ts +497 -0
  10. package/src/client-id.ts +137 -0
  11. package/src/client.ts +501 -0
  12. package/{dist/entry.js → src/entry.ts} +1 -1
  13. package/src/htree-path.ts +53 -0
  14. package/src/htree-url.ts +156 -0
  15. package/src/index.ts +76 -0
  16. package/src/mediaStreaming.ts +64 -0
  17. package/src/p2p/boundedQueue.ts +168 -0
  18. package/src/p2p/errorMessage.ts +6 -0
  19. package/src/p2p/index.ts +48 -0
  20. package/src/p2p/lruCache.ts +78 -0
  21. package/src/p2p/meshQueryRouter.ts +361 -0
  22. package/src/p2p/protocol.ts +11 -0
  23. package/src/p2p/queryForwardingMachine.ts +197 -0
  24. package/src/p2p/signaling.ts +284 -0
  25. package/src/p2p/uploadRateLimiter.ts +85 -0
  26. package/src/p2p/webrtcController.ts +1168 -0
  27. package/src/p2p/webrtcProxy.ts +519 -0
  28. package/src/privacyGuards.ts +31 -0
  29. package/src/protocol.ts +124 -0
  30. package/src/relay/identity.ts +86 -0
  31. package/src/relay/mediaHandler.ts +1633 -0
  32. package/src/relay/ndk.ts +590 -0
  33. package/{dist/iris/nostr-wasm.js → src/relay/nostr-wasm.ts} +4 -1
  34. package/src/relay/nostr.ts +249 -0
  35. package/src/relay/protocol.ts +361 -0
  36. package/src/relay/publicAssetUrl.ts +25 -0
  37. package/src/relay/rootPathResolver.ts +50 -0
  38. package/src/relay/shims.d.ts +17 -0
  39. package/src/relay/signing.ts +332 -0
  40. package/src/relay/treeRootCache.ts +354 -0
  41. package/src/relay/treeRootSubscription.ts +577 -0
  42. package/src/relay/utils/constants.ts +139 -0
  43. package/src/relay/utils/errorMessage.ts +7 -0
  44. package/src/relay/utils/lruCache.ts +79 -0
  45. package/src/relay/webrtc.ts +5 -0
  46. package/src/relay/webrtcSignaling.ts +108 -0
  47. package/src/relay/worker.ts +1787 -0
  48. package/src/relay-client.ts +265 -0
  49. package/src/relay-entry.ts +1 -0
  50. package/src/runtime-network.ts +134 -0
  51. package/src/runtime.ts +153 -0
  52. package/src/transferableBytes.ts +5 -0
  53. package/src/tree-root.ts +851 -0
  54. package/src/types.ts +8 -0
  55. package/src/worker.ts +975 -0
  56. package/LICENSE +0 -21
  57. package/dist/app-runtime.d.ts +0 -60
  58. package/dist/app-runtime.d.ts.map +0 -1
  59. package/dist/app-runtime.js +0 -271
  60. package/dist/app-runtime.js.map +0 -1
  61. package/dist/capabilities/blossomBandwidthTracker.d.ts +0 -26
  62. package/dist/capabilities/blossomBandwidthTracker.d.ts.map +0 -1
  63. package/dist/capabilities/blossomBandwidthTracker.js +0 -53
  64. package/dist/capabilities/blossomBandwidthTracker.js.map +0 -1
  65. package/dist/capabilities/blossomTransport.d.ts +0 -22
  66. package/dist/capabilities/blossomTransport.d.ts.map +0 -1
  67. package/dist/capabilities/blossomTransport.js +0 -144
  68. package/dist/capabilities/blossomTransport.js.map +0 -1
  69. package/dist/capabilities/connectivity.d.ts +0 -3
  70. package/dist/capabilities/connectivity.d.ts.map +0 -1
  71. package/dist/capabilities/connectivity.js +0 -49
  72. package/dist/capabilities/connectivity.js.map +0 -1
  73. package/dist/capabilities/idbStorage.d.ts +0 -25
  74. package/dist/capabilities/idbStorage.d.ts.map +0 -1
  75. package/dist/capabilities/idbStorage.js +0 -73
  76. package/dist/capabilities/idbStorage.js.map +0 -1
  77. package/dist/capabilities/meshRouterStore.d.ts +0 -71
  78. package/dist/capabilities/meshRouterStore.d.ts.map +0 -1
  79. package/dist/capabilities/meshRouterStore.js +0 -316
  80. package/dist/capabilities/meshRouterStore.js.map +0 -1
  81. package/dist/capabilities/rootResolver.d.ts +0 -10
  82. package/dist/capabilities/rootResolver.d.ts.map +0 -1
  83. package/dist/capabilities/rootResolver.js +0 -393
  84. package/dist/capabilities/rootResolver.js.map +0 -1
  85. package/dist/client-id.d.ts +0 -18
  86. package/dist/client-id.d.ts.map +0 -1
  87. package/dist/client-id.js +0 -98
  88. package/dist/client-id.js.map +0 -1
  89. package/dist/client.d.ts +0 -61
  90. package/dist/client.d.ts.map +0 -1
  91. package/dist/client.js +0 -417
  92. package/dist/client.js.map +0 -1
  93. package/dist/entry.d.ts +0 -2
  94. package/dist/entry.d.ts.map +0 -1
  95. package/dist/entry.js.map +0 -1
  96. package/dist/htree-path.d.ts +0 -13
  97. package/dist/htree-path.d.ts.map +0 -1
  98. package/dist/htree-path.js +0 -38
  99. package/dist/htree-path.js.map +0 -1
  100. package/dist/htree-url.d.ts +0 -22
  101. package/dist/htree-url.d.ts.map +0 -1
  102. package/dist/htree-url.js +0 -118
  103. package/dist/htree-url.js.map +0 -1
  104. package/dist/index.d.ts +0 -17
  105. package/dist/index.d.ts.map +0 -1
  106. package/dist/index.js +0 -8
  107. package/dist/index.js.map +0 -1
  108. package/dist/iris/identity.d.ts +0 -36
  109. package/dist/iris/identity.d.ts.map +0 -1
  110. package/dist/iris/identity.js +0 -78
  111. package/dist/iris/identity.js.map +0 -1
  112. package/dist/iris/mediaHandler.d.ts +0 -64
  113. package/dist/iris/mediaHandler.d.ts.map +0 -1
  114. package/dist/iris/mediaHandler.js +0 -1285
  115. package/dist/iris/mediaHandler.js.map +0 -1
  116. package/dist/iris/ndk.d.ts +0 -96
  117. package/dist/iris/ndk.d.ts.map +0 -1
  118. package/dist/iris/ndk.js +0 -502
  119. package/dist/iris/ndk.js.map +0 -1
  120. package/dist/iris/nostr-wasm.d.ts +0 -14
  121. package/dist/iris/nostr-wasm.d.ts.map +0 -1
  122. package/dist/iris/nostr-wasm.js.map +0 -1
  123. package/dist/iris/nostr.d.ts +0 -60
  124. package/dist/iris/nostr.d.ts.map +0 -1
  125. package/dist/iris/nostr.js +0 -207
  126. package/dist/iris/nostr.js.map +0 -1
  127. package/dist/iris/protocol.d.ts +0 -583
  128. package/dist/iris/protocol.d.ts.map +0 -1
  129. package/dist/iris/protocol.js +0 -16
  130. package/dist/iris/protocol.js.map +0 -1
  131. package/dist/iris/publicAssetUrl.d.ts +0 -6
  132. package/dist/iris/publicAssetUrl.d.ts.map +0 -1
  133. package/dist/iris/publicAssetUrl.js +0 -14
  134. package/dist/iris/publicAssetUrl.js.map +0 -1
  135. package/dist/iris/rootPathResolver.d.ts +0 -9
  136. package/dist/iris/rootPathResolver.d.ts.map +0 -1
  137. package/dist/iris/rootPathResolver.js +0 -32
  138. package/dist/iris/rootPathResolver.js.map +0 -1
  139. package/dist/iris/signing.d.ts +0 -50
  140. package/dist/iris/signing.d.ts.map +0 -1
  141. package/dist/iris/signing.js +0 -299
  142. package/dist/iris/signing.js.map +0 -1
  143. package/dist/iris/treeRootCache.d.ts +0 -86
  144. package/dist/iris/treeRootCache.d.ts.map +0 -1
  145. package/dist/iris/treeRootCache.js +0 -269
  146. package/dist/iris/treeRootCache.js.map +0 -1
  147. package/dist/iris/treeRootSubscription.d.ts +0 -55
  148. package/dist/iris/treeRootSubscription.d.ts.map +0 -1
  149. package/dist/iris/treeRootSubscription.js +0 -479
  150. package/dist/iris/treeRootSubscription.js.map +0 -1
  151. package/dist/iris/utils/constants.d.ts +0 -76
  152. package/dist/iris/utils/constants.d.ts.map +0 -1
  153. package/dist/iris/utils/constants.js +0 -113
  154. package/dist/iris/utils/constants.js.map +0 -1
  155. package/dist/iris/utils/errorMessage.d.ts +0 -5
  156. package/dist/iris/utils/errorMessage.d.ts.map +0 -1
  157. package/dist/iris/utils/errorMessage.js +0 -8
  158. package/dist/iris/utils/errorMessage.js.map +0 -1
  159. package/dist/iris/utils/lruCache.d.ts +0 -26
  160. package/dist/iris/utils/lruCache.d.ts.map +0 -1
  161. package/dist/iris/utils/lruCache.js +0 -66
  162. package/dist/iris/utils/lruCache.js.map +0 -1
  163. package/dist/iris/webrtc.d.ts +0 -2
  164. package/dist/iris/webrtc.d.ts.map +0 -1
  165. package/dist/iris/webrtc.js +0 -3
  166. package/dist/iris/webrtc.js.map +0 -1
  167. package/dist/iris/webrtcSignaling.d.ts +0 -37
  168. package/dist/iris/webrtcSignaling.d.ts.map +0 -1
  169. package/dist/iris/webrtcSignaling.js +0 -86
  170. package/dist/iris/webrtcSignaling.js.map +0 -1
  171. package/dist/iris/worker.d.ts +0 -12
  172. package/dist/iris/worker.d.ts.map +0 -1
  173. package/dist/iris/worker.js +0 -1529
  174. package/dist/iris/worker.js.map +0 -1
  175. package/dist/iris-client.d.ts +0 -31
  176. package/dist/iris-client.d.ts.map +0 -1
  177. package/dist/iris-client.js +0 -197
  178. package/dist/iris-client.js.map +0 -1
  179. package/dist/iris-entry.d.ts +0 -2
  180. package/dist/iris-entry.d.ts.map +0 -1
  181. package/dist/iris-entry.js +0 -2
  182. package/dist/iris-entry.js.map +0 -1
  183. package/dist/mediaStreaming.d.ts +0 -7
  184. package/dist/mediaStreaming.d.ts.map +0 -1
  185. package/dist/mediaStreaming.js +0 -48
  186. package/dist/mediaStreaming.js.map +0 -1
  187. package/dist/p2p/boundedQueue.d.ts +0 -79
  188. package/dist/p2p/boundedQueue.d.ts.map +0 -1
  189. package/dist/p2p/boundedQueue.js +0 -134
  190. package/dist/p2p/boundedQueue.js.map +0 -1
  191. package/dist/p2p/errorMessage.d.ts +0 -5
  192. package/dist/p2p/errorMessage.d.ts.map +0 -1
  193. package/dist/p2p/errorMessage.js +0 -7
  194. package/dist/p2p/errorMessage.js.map +0 -1
  195. package/dist/p2p/index.d.ts +0 -8
  196. package/dist/p2p/index.d.ts.map +0 -1
  197. package/dist/p2p/index.js +0 -6
  198. package/dist/p2p/index.js.map +0 -1
  199. package/dist/p2p/lruCache.d.ts +0 -26
  200. package/dist/p2p/lruCache.d.ts.map +0 -1
  201. package/dist/p2p/lruCache.js +0 -65
  202. package/dist/p2p/lruCache.js.map +0 -1
  203. package/dist/p2p/meshQueryRouter.d.ts +0 -44
  204. package/dist/p2p/meshQueryRouter.d.ts.map +0 -1
  205. package/dist/p2p/meshQueryRouter.js +0 -228
  206. package/dist/p2p/meshQueryRouter.js.map +0 -1
  207. package/dist/p2p/protocol.d.ts +0 -10
  208. package/dist/p2p/protocol.d.ts.map +0 -1
  209. package/dist/p2p/protocol.js +0 -2
  210. package/dist/p2p/protocol.js.map +0 -1
  211. package/dist/p2p/queryForwardingMachine.d.ts +0 -46
  212. package/dist/p2p/queryForwardingMachine.d.ts.map +0 -1
  213. package/dist/p2p/queryForwardingMachine.js +0 -144
  214. package/dist/p2p/queryForwardingMachine.js.map +0 -1
  215. package/dist/p2p/signaling.d.ts +0 -63
  216. package/dist/p2p/signaling.d.ts.map +0 -1
  217. package/dist/p2p/signaling.js +0 -185
  218. package/dist/p2p/signaling.js.map +0 -1
  219. package/dist/p2p/uploadRateLimiter.d.ts +0 -21
  220. package/dist/p2p/uploadRateLimiter.d.ts.map +0 -1
  221. package/dist/p2p/uploadRateLimiter.js +0 -62
  222. package/dist/p2p/uploadRateLimiter.js.map +0 -1
  223. package/dist/p2p/webrtcController.d.ts +0 -168
  224. package/dist/p2p/webrtcController.d.ts.map +0 -1
  225. package/dist/p2p/webrtcController.js +0 -902
  226. package/dist/p2p/webrtcController.js.map +0 -1
  227. package/dist/p2p/webrtcProxy.d.ts +0 -62
  228. package/dist/p2p/webrtcProxy.d.ts.map +0 -1
  229. package/dist/p2p/webrtcProxy.js +0 -447
  230. package/dist/p2p/webrtcProxy.js.map +0 -1
  231. package/dist/privacyGuards.d.ts +0 -14
  232. package/dist/privacyGuards.d.ts.map +0 -1
  233. package/dist/privacyGuards.js +0 -27
  234. package/dist/privacyGuards.js.map +0 -1
  235. package/dist/protocol.d.ts +0 -225
  236. package/dist/protocol.d.ts.map +0 -1
  237. package/dist/protocol.js +0 -2
  238. package/dist/protocol.js.map +0 -1
  239. package/dist/runtime-network.d.ts +0 -23
  240. package/dist/runtime-network.d.ts.map +0 -1
  241. package/dist/runtime-network.js +0 -105
  242. package/dist/runtime-network.js.map +0 -1
  243. package/dist/runtime.d.ts +0 -23
  244. package/dist/runtime.d.ts.map +0 -1
  245. package/dist/runtime.js +0 -122
  246. package/dist/runtime.js.map +0 -1
  247. package/dist/tree-root.d.ts +0 -201
  248. package/dist/tree-root.d.ts.map +0 -1
  249. package/dist/tree-root.js +0 -632
  250. package/dist/tree-root.js.map +0 -1
  251. package/dist/types.d.ts +0 -2
  252. package/dist/types.d.ts.map +0 -1
  253. package/dist/types.js +0 -2
  254. package/dist/types.js.map +0 -1
  255. package/dist/worker.d.ts +0 -9
  256. package/dist/worker.d.ts.map +0 -1
  257. package/dist/worker.js +0 -797
  258. package/dist/worker.js.map +0 -1
@@ -0,0 +1,332 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * Worker Signing & Encryption
4
+ *
5
+ * Provides signing, encryption, and gift wrap functions.
6
+ * Uses nsec directly when available, delegates to main thread otherwise.
7
+ */
8
+
9
+ import { generateSecretKey, finalizeEvent, nip44 } from 'nostr-tools';
10
+ import type { EventTemplate } from 'nostr-tools';
11
+ import { getSecretKey, getPubkey, getEphemeralSecretKey } from './identity';
12
+ import type { SignedEvent, UnsignedEvent } from './protocol';
13
+
14
+ // Pending NIP-07 requests (waiting for main thread)
15
+ const pendingSignRequests = new Map<string, (event: SignedEvent | null, error?: string) => void>();
16
+ const pendingEncryptRequests = new Map<string, (ciphertext: string | null, error?: string) => void>();
17
+ const pendingDecryptRequests = new Map<string, (plaintext: string | null, error?: string) => void>();
18
+
19
+ // Response sender (set by worker.ts)
20
+ let postResponse: ((msg: unknown) => void) | null = null;
21
+
22
+ export function setResponseSender(fn: (msg: unknown) => void) {
23
+ postResponse = fn;
24
+ }
25
+
26
+ function bytesToHex(bytes: Uint8Array): string {
27
+ let hex = '';
28
+ for (const byte of bytes) {
29
+ hex += byte.toString(16).padStart(2, '0');
30
+ }
31
+ return hex;
32
+ }
33
+
34
+ function normalizeTagValue(value: unknown): string {
35
+ if (typeof value === 'string') return value;
36
+ if (typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint') {
37
+ return String(value);
38
+ }
39
+ if (value == null) return '';
40
+ if (value instanceof Uint8Array) return bytesToHex(value);
41
+ if (ArrayBuffer.isView(value)) {
42
+ return bytesToHex(new Uint8Array(value.buffer, value.byteOffset, value.byteLength));
43
+ }
44
+ if (value instanceof ArrayBuffer) return bytesToHex(new Uint8Array(value));
45
+ try {
46
+ return JSON.stringify(value);
47
+ } catch {
48
+ return String(value);
49
+ }
50
+ }
51
+
52
+ function sanitizeTags(tags: unknown): string[][] {
53
+ if (!Array.isArray(tags)) return [];
54
+
55
+ let needsSanitize = false;
56
+ for (const tag of tags) {
57
+ if (!Array.isArray(tag)) {
58
+ needsSanitize = true;
59
+ break;
60
+ }
61
+ for (const value of tag) {
62
+ if (typeof value !== 'string') {
63
+ needsSanitize = true;
64
+ break;
65
+ }
66
+ }
67
+ if (needsSanitize) break;
68
+ }
69
+
70
+ if (!needsSanitize) return tags as string[][];
71
+
72
+ const sanitized: string[][] = [];
73
+ for (const tag of tags) {
74
+ if (!Array.isArray(tag)) continue;
75
+ const normalized: string[] = [];
76
+ for (const value of tag) {
77
+ normalized.push(normalizeTagValue(value));
78
+ }
79
+ sanitized.push(normalized);
80
+ }
81
+ return sanitized;
82
+ }
83
+
84
+ // ============================================================================
85
+ // Signing
86
+ // ============================================================================
87
+
88
+ /**
89
+ * Sign an event with user's real identity.
90
+ * - For nsec login: signs directly with secret key
91
+ * - For extension login: delegates to main thread via NIP-07
92
+ */
93
+ export async function signEvent(template: EventTemplate): Promise<SignedEvent> {
94
+ template.tags = sanitizeTags(template.tags ?? []);
95
+ const secretKey = getSecretKey();
96
+ if (secretKey) {
97
+ const event = finalizeEvent(template, secretKey);
98
+ return {
99
+ id: event.id,
100
+ pubkey: event.pubkey,
101
+ kind: event.kind,
102
+ content: event.content,
103
+ tags: event.tags,
104
+ created_at: event.created_at,
105
+ sig: event.sig,
106
+ };
107
+ } else {
108
+ return requestSign({
109
+ kind: template.kind,
110
+ created_at: template.created_at,
111
+ content: template.content,
112
+ tags: template.tags,
113
+ });
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Synchronous sign (only works with nsec, falls back to ephemeral)
119
+ */
120
+ export function signEventSync(template: EventTemplate): SignedEvent {
121
+ template.tags = sanitizeTags(template.tags ?? []);
122
+ const secretKey = getSecretKey() || getEphemeralSecretKey();
123
+ if (!secretKey) {
124
+ throw new Error('No signing key available');
125
+ }
126
+ const event = finalizeEvent(template, secretKey);
127
+ return {
128
+ id: event.id,
129
+ pubkey: event.pubkey,
130
+ kind: event.kind,
131
+ content: event.content,
132
+ tags: event.tags,
133
+ created_at: event.created_at,
134
+ sig: event.sig,
135
+ };
136
+ }
137
+
138
+ // ============================================================================
139
+ // Encryption
140
+ // ============================================================================
141
+
142
+ /**
143
+ * Encrypt plaintext for a recipient using NIP-44
144
+ */
145
+ export async function encrypt(recipientPubkey: string, plaintext: string): Promise<string> {
146
+ const secretKey = getSecretKey();
147
+ if (secretKey) {
148
+ const conversationKey = nip44.v2.utils.getConversationKey(secretKey, recipientPubkey);
149
+ return nip44.v2.encrypt(plaintext, conversationKey);
150
+ } else {
151
+ return requestEncrypt(recipientPubkey, plaintext);
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Decrypt ciphertext from a sender using NIP-44
157
+ */
158
+ export async function decrypt(senderPubkey: string, ciphertext: string): Promise<string> {
159
+ const secretKey = getSecretKey();
160
+ if (secretKey) {
161
+ const conversationKey = nip44.v2.utils.getConversationKey(secretKey, senderPubkey);
162
+ return nip44.v2.decrypt(ciphertext, conversationKey);
163
+ } else {
164
+ return requestDecrypt(senderPubkey, ciphertext);
165
+ }
166
+ }
167
+
168
+ // ============================================================================
169
+ // Gift Wrap (NIP-17 style private messaging)
170
+ // ============================================================================
171
+
172
+ interface Seal {
173
+ pubkey: string;
174
+ kind: number;
175
+ content: string;
176
+ tags: string[][];
177
+ }
178
+
179
+ /**
180
+ * Gift wrap an event for private delivery.
181
+ */
182
+ export async function giftWrap(
183
+ innerEvent: { kind: number; content: string; tags: string[][] },
184
+ recipientPubkey: string
185
+ ): Promise<SignedEvent> {
186
+ const myPubkey = getPubkey();
187
+ if (!myPubkey) throw new Error('No pubkey available');
188
+
189
+ const seal: Seal = {
190
+ pubkey: myPubkey,
191
+ kind: innerEvent.kind,
192
+ content: innerEvent.content,
193
+ tags: innerEvent.tags,
194
+ };
195
+
196
+ // Generate ephemeral keypair for the wrapper
197
+ const ephemeralSk = generateSecretKey();
198
+
199
+ // Encrypt the seal for the recipient
200
+ const conversationKey = nip44.v2.utils.getConversationKey(ephemeralSk, recipientPubkey);
201
+ const encryptedContent = nip44.v2.encrypt(JSON.stringify(seal), conversationKey);
202
+
203
+ const createdAt = Math.floor(Date.now() / 1000);
204
+ const expiration = createdAt + 5 * 60;
205
+
206
+ const event = finalizeEvent({
207
+ kind: 25050,
208
+ created_at: createdAt,
209
+ tags: [
210
+ ['p', recipientPubkey],
211
+ ['expiration', expiration.toString()],
212
+ ],
213
+ content: encryptedContent,
214
+ }, ephemeralSk);
215
+
216
+ return {
217
+ id: event.id,
218
+ pubkey: event.pubkey,
219
+ kind: event.kind,
220
+ content: event.content,
221
+ tags: event.tags,
222
+ created_at: event.created_at,
223
+ sig: event.sig,
224
+ };
225
+ }
226
+
227
+ /**
228
+ * Unwrap a gift wrapped event.
229
+ */
230
+ export async function giftUnwrap(event: SignedEvent): Promise<Seal | null> {
231
+ try {
232
+ const decrypted = await decrypt(event.pubkey, event.content);
233
+ return JSON.parse(decrypted) as Seal;
234
+ } catch {
235
+ return null;
236
+ }
237
+ }
238
+
239
+ // ============================================================================
240
+ // NIP-07 Delegation (for extension login)
241
+ // ============================================================================
242
+
243
+ async function requestSign(event: UnsignedEvent): Promise<SignedEvent> {
244
+ const id = `sign_${Date.now()}_${Math.random().toString(36).slice(2)}`;
245
+
246
+ return new Promise((resolve, reject) => {
247
+ pendingSignRequests.set(id, (signed, error) => {
248
+ if (error) reject(new Error(error));
249
+ else if (signed) resolve(signed);
250
+ else reject(new Error('Signing failed'));
251
+ });
252
+
253
+ postResponse?.({ type: 'signEvent', id, event });
254
+
255
+ setTimeout(() => {
256
+ if (pendingSignRequests.has(id)) {
257
+ pendingSignRequests.delete(id);
258
+ reject(new Error('Signing timeout'));
259
+ }
260
+ }, 60000);
261
+ });
262
+ }
263
+
264
+ async function requestEncrypt(pubkey: string, plaintext: string): Promise<string> {
265
+ const id = `enc_${Date.now()}_${Math.random().toString(36).slice(2)}`;
266
+
267
+ return new Promise((resolve, reject) => {
268
+ pendingEncryptRequests.set(id, (ciphertext, error) => {
269
+ if (error) reject(new Error(error));
270
+ else if (ciphertext) resolve(ciphertext);
271
+ else reject(new Error('Encryption failed'));
272
+ });
273
+
274
+ postResponse?.({ type: 'nip44Encrypt', id, pubkey, plaintext });
275
+
276
+ setTimeout(() => {
277
+ if (pendingEncryptRequests.has(id)) {
278
+ pendingEncryptRequests.delete(id);
279
+ reject(new Error('Encryption timeout'));
280
+ }
281
+ }, 30000);
282
+ });
283
+ }
284
+
285
+ async function requestDecrypt(pubkey: string, ciphertext: string): Promise<string> {
286
+ const id = `dec_${Date.now()}_${Math.random().toString(36).slice(2)}`;
287
+
288
+ return new Promise((resolve, reject) => {
289
+ pendingDecryptRequests.set(id, (plaintext, error) => {
290
+ if (error) reject(new Error(error));
291
+ else if (plaintext) resolve(plaintext);
292
+ else reject(new Error('Decryption failed'));
293
+ });
294
+
295
+ postResponse?.({ type: 'nip44Decrypt', id, pubkey, ciphertext });
296
+
297
+ setTimeout(() => {
298
+ if (pendingDecryptRequests.has(id)) {
299
+ pendingDecryptRequests.delete(id);
300
+ reject(new Error('Decryption timeout'));
301
+ }
302
+ }, 30000);
303
+ });
304
+ }
305
+
306
+ // ============================================================================
307
+ // Response Handlers (called by worker.ts when main thread responds)
308
+ // ============================================================================
309
+
310
+ export function handleSignedResponse(id: string, event?: SignedEvent, error?: string) {
311
+ const resolver = pendingSignRequests.get(id);
312
+ if (resolver) {
313
+ pendingSignRequests.delete(id);
314
+ resolver(event || null, error);
315
+ }
316
+ }
317
+
318
+ export function handleEncryptedResponse(id: string, ciphertext?: string, error?: string) {
319
+ const resolver = pendingEncryptRequests.get(id);
320
+ if (resolver) {
321
+ pendingEncryptRequests.delete(id);
322
+ resolver(ciphertext || null, error);
323
+ }
324
+ }
325
+
326
+ export function handleDecryptedResponse(id: string, plaintext?: string, error?: string) {
327
+ const resolver = pendingDecryptRequests.get(id);
328
+ if (resolver) {
329
+ pendingDecryptRequests.delete(id);
330
+ resolver(plaintext || null, error);
331
+ }
332
+ }
@@ -0,0 +1,354 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * Tree Root Cache
4
+ *
5
+ * Persists npub/treeName → CID mappings using any Store implementation.
6
+ * This allows quick resolution of tree roots without waiting for Nostr.
7
+ *
8
+ * Storage format:
9
+ * - Key prefix: "root:" (to distinguish from content chunks)
10
+ * - Key: SHA256("root:" + npub + "/" + treeName)
11
+ * - Value: MessagePack { hash, key?, visibility, updatedAt }
12
+ */
13
+
14
+ import type { CID, Store, TreeVisibility } from '@hashtree/core';
15
+ import { sha256 } from '@hashtree/core';
16
+ import { encode, decode } from '@msgpack/msgpack';
17
+ import { LRUCache } from './utils/lruCache';
18
+
19
+ // Cached root entry
20
+ interface CachedRoot {
21
+ hash: Uint8Array; // Root hash
22
+ key?: Uint8Array; // CHK decryption key (for encrypted trees)
23
+ visibility: TreeVisibility;
24
+ labels?: string[];
25
+ updatedAt: number; // Unix timestamp
26
+ eventId?: string; // Source event id for same-second tie-breaking
27
+ snapshotNhash?: string; // Signed root-event snapshot permalink
28
+ encryptedKey?: string; // For link-visible trees
29
+ keyId?: string; // For link-visible trees
30
+ selfEncryptedKey?: string; // For private trees
31
+ selfEncryptedLinkKey?: string; // For link-visible trees
32
+ }
33
+
34
+ export interface SetCachedRootResult {
35
+ applied: boolean;
36
+ record: CachedRoot;
37
+ }
38
+
39
+ // In-memory LRU cache for fast lookups (limited to 1000 entries to prevent memory leak)
40
+ // Data is backed by persistent store so eviction is safe
41
+ const memoryCache = new LRUCache<string, CachedRoot>(1000);
42
+ const updateListeners = new Set<(npub: string, treeName: string, cid: CID | null) => void>();
43
+
44
+ // Store reference
45
+ let store: Store | null = null;
46
+
47
+ function compareReplaceableEventOrder(
48
+ candidateUpdatedAt: number,
49
+ candidateEventId: string | null | undefined,
50
+ currentUpdatedAt: number,
51
+ currentEventId: string | null | undefined,
52
+ ): number {
53
+ if (candidateUpdatedAt !== currentUpdatedAt) {
54
+ return candidateUpdatedAt - currentUpdatedAt;
55
+ }
56
+
57
+ const candidateId = candidateEventId ?? '';
58
+ const currentId = currentEventId ?? '';
59
+ if (candidateId === currentId) {
60
+ return 0;
61
+ }
62
+
63
+ if (!candidateId) return -1;
64
+ if (!currentId) return 1;
65
+ return candidateId.localeCompare(currentId);
66
+ }
67
+
68
+ function notifyUpdate(npub: string, treeName: string, cid: CID | null): void {
69
+ for (const listener of updateListeners) {
70
+ listener(npub, treeName, cid);
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Initialize the cache with a store
76
+ */
77
+ export function initTreeRootCache(storeImpl: Store): void {
78
+ store = storeImpl;
79
+ }
80
+
81
+ export function getTreeRootCacheStore(): Store | null {
82
+ return store;
83
+ }
84
+
85
+ /**
86
+ * Generate storage key for a tree root
87
+ */
88
+ async function makeStorageKey(npub: string, treeName: string): Promise<Uint8Array> {
89
+ const keyStr = `root:${npub}/${treeName}`;
90
+ return sha256(new TextEncoder().encode(keyStr));
91
+ }
92
+
93
+ /**
94
+ * Get a cached tree root
95
+ */
96
+ export async function getCachedRoot(npub: string, treeName: string): Promise<CID | null> {
97
+ const cacheKey = `${npub}/${treeName}`;
98
+
99
+ // Check memory cache first
100
+ const memCached = memoryCache.get(cacheKey);
101
+ if (memCached) {
102
+ return { hash: memCached.hash, key: memCached.key };
103
+ }
104
+
105
+ // Check persistent store
106
+ if (!store) return null;
107
+
108
+ const storageKey = await makeStorageKey(npub, treeName);
109
+ const data = await store.get(storageKey);
110
+ if (!data) return null;
111
+
112
+ try {
113
+ const cached = decode(data) as CachedRoot;
114
+ // Update memory cache
115
+ memoryCache.set(cacheKey, cached);
116
+ return { hash: cached.hash, key: cached.key };
117
+ } catch {
118
+ return null;
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Get full cached root info (including visibility)
124
+ */
125
+ export async function getCachedRootInfo(npub: string, treeName: string): Promise<CachedRoot | null> {
126
+ const cacheKey = `${npub}/${treeName}`;
127
+
128
+ // Check memory cache first
129
+ const memCached = memoryCache.get(cacheKey);
130
+ if (memCached) return memCached;
131
+
132
+ // Check persistent store
133
+ if (!store) return null;
134
+
135
+ const storageKey = await makeStorageKey(npub, treeName);
136
+ const data = await store.get(storageKey);
137
+ if (!data) return null;
138
+
139
+ try {
140
+ const cached = decode(data) as CachedRoot;
141
+ memoryCache.set(cacheKey, cached);
142
+ return cached;
143
+ } catch {
144
+ return null;
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Cache a tree root
150
+ */
151
+ export async function setCachedRoot(
152
+ npub: string,
153
+ treeName: string,
154
+ cid: CID,
155
+ visibility: TreeVisibility = 'public',
156
+ options?: {
157
+ updatedAt?: number;
158
+ eventId?: string;
159
+ labels?: string[];
160
+ snapshotNhash?: string;
161
+ encryptedKey?: string;
162
+ keyId?: string;
163
+ selfEncryptedKey?: string;
164
+ selfEncryptedLinkKey?: string;
165
+ }
166
+ ): Promise<SetCachedRootResult> {
167
+ const cacheKey = `${npub}/${treeName}`;
168
+ const existing = await getCachedRootInfo(npub, treeName);
169
+ const updatedAt = options?.updatedAt ?? Math.floor(Date.now() / 1000);
170
+ const eventId = options?.eventId;
171
+ const sameHash = !!existing && hashEquals(existing.hash, cid.hash);
172
+
173
+ if (existing && compareReplaceableEventOrder(updatedAt, eventId, existing.updatedAt, existing.eventId) < 0) {
174
+ return { applied: false, record: existing };
175
+ }
176
+
177
+ const cached: CachedRoot = {
178
+ hash: cid.hash,
179
+ key: cid.key ?? (sameHash ? existing?.key : undefined),
180
+ visibility,
181
+ labels: options?.labels ?? existing?.labels,
182
+ updatedAt,
183
+ eventId: eventId ?? (sameHash ? existing?.eventId : undefined),
184
+ snapshotNhash: options?.snapshotNhash ?? (sameHash ? existing?.snapshotNhash : undefined),
185
+ encryptedKey: options?.encryptedKey ?? (sameHash ? existing?.encryptedKey : undefined),
186
+ keyId: options?.keyId ?? (sameHash ? existing?.keyId : undefined),
187
+ selfEncryptedKey: options?.selfEncryptedKey ?? (sameHash ? existing?.selfEncryptedKey : undefined),
188
+ selfEncryptedLinkKey: options?.selfEncryptedLinkKey ?? (sameHash ? existing?.selfEncryptedLinkKey : undefined),
189
+ };
190
+
191
+ if (existing && cachedRootEquals(existing, cached)) {
192
+ return { applied: false, record: existing };
193
+ }
194
+
195
+ // Update memory cache
196
+ memoryCache.set(cacheKey, cached);
197
+ notifyUpdate(npub, treeName, { hash: cached.hash, key: cached.key });
198
+
199
+ // Persist to store
200
+ if (store) {
201
+ const storageKey = await makeStorageKey(npub, treeName);
202
+ const data = encode(cached);
203
+ await store.put(storageKey, new Uint8Array(data));
204
+ }
205
+
206
+ return { applied: true, record: cached };
207
+ }
208
+
209
+ /**
210
+ * Merge a decrypted key into an existing cache entry (if hash matches).
211
+ */
212
+ export async function mergeCachedRootKey(
213
+ npub: string,
214
+ treeName: string,
215
+ hash: Uint8Array,
216
+ key: Uint8Array
217
+ ): Promise<boolean> {
218
+ const cacheKey = `${npub}/${treeName}`;
219
+
220
+ const cached = await getCachedRootInfo(npub, treeName);
221
+ if (!cached) return false;
222
+ if (cached.key) return false;
223
+ if (!hashEquals(cached.hash, hash)) return false;
224
+
225
+ const merged: CachedRoot = {
226
+ ...cached,
227
+ key,
228
+ };
229
+
230
+ memoryCache.set(cacheKey, merged);
231
+ notifyUpdate(npub, treeName, { hash: merged.hash, key: merged.key });
232
+
233
+ if (store) {
234
+ const storageKey = await makeStorageKey(npub, treeName);
235
+ const data = encode(merged);
236
+ await store.put(storageKey, new Uint8Array(data));
237
+ }
238
+
239
+ return true;
240
+ }
241
+
242
+ /**
243
+ * Remove a cached tree root
244
+ */
245
+ export async function removeCachedRoot(npub: string, treeName: string): Promise<void> {
246
+ const cacheKey = `${npub}/${treeName}`;
247
+
248
+ // Remove from memory cache
249
+ memoryCache.delete(cacheKey);
250
+ notifyUpdate(npub, treeName, null);
251
+
252
+ // Remove from persistent store
253
+ if (store) {
254
+ const storageKey = await makeStorageKey(npub, treeName);
255
+ await store.delete(storageKey);
256
+ }
257
+ }
258
+
259
+ /**
260
+ * List all cached roots for an npub
261
+ * Note: This scans memory cache only - persistent lookup requires iteration
262
+ */
263
+ export function listCachedRoots(npub: string): Array<{
264
+ treeName: string;
265
+ cid: CID;
266
+ visibility: TreeVisibility;
267
+ updatedAt: number;
268
+ }> {
269
+ const prefix = `${npub}/`;
270
+ const results: Array<{
271
+ treeName: string;
272
+ cid: CID;
273
+ visibility: TreeVisibility;
274
+ updatedAt: number;
275
+ }> = [];
276
+
277
+ for (const [key, cached] of memoryCache) {
278
+ if (key.startsWith(prefix)) {
279
+ const treeName = key.slice(prefix.length);
280
+ results.push({
281
+ treeName,
282
+ cid: { hash: cached.hash, key: cached.key },
283
+ visibility: cached.visibility,
284
+ updatedAt: cached.updatedAt,
285
+ });
286
+ }
287
+ }
288
+
289
+ return results;
290
+ }
291
+
292
+ /**
293
+ * Clear all cached roots (memory only)
294
+ */
295
+ export function clearMemoryCache(): void {
296
+ memoryCache.clear();
297
+ }
298
+
299
+ export function onCachedRootUpdate(
300
+ listener: (npub: string, treeName: string, cid: CID | null) => void
301
+ ): () => void {
302
+ updateListeners.add(listener);
303
+ return () => {
304
+ updateListeners.delete(listener);
305
+ };
306
+ }
307
+
308
+ /**
309
+ * Get cache stats
310
+ */
311
+ export function getCacheStats(): { memoryEntries: number } {
312
+ return {
313
+ memoryEntries: memoryCache.size,
314
+ };
315
+ }
316
+
317
+ function hashEquals(a: Uint8Array, b: Uint8Array): boolean {
318
+ if (a.length !== b.length) return false;
319
+ for (let i = 0; i < a.length; i += 1) {
320
+ if (a[i] !== b[i]) return false;
321
+ }
322
+ return true;
323
+ }
324
+
325
+ function optionalHashEquals(a?: Uint8Array, b?: Uint8Array): boolean {
326
+ if (!a && !b) return true;
327
+ if (!a || !b) return false;
328
+ return hashEquals(a, b);
329
+ }
330
+
331
+ function labelsEqual(a?: string[], b?: string[]): boolean {
332
+ if (!a && !b) return true;
333
+ if (!a || !b) return false;
334
+ if (a.length !== b.length) return false;
335
+ for (let i = 0; i < a.length; i += 1) {
336
+ if (a[i] !== b[i]) return false;
337
+ }
338
+ return true;
339
+ }
340
+
341
+ function cachedRootEquals(a: CachedRoot, b: CachedRoot): boolean {
342
+ return (
343
+ hashEquals(a.hash, b.hash) &&
344
+ optionalHashEquals(a.key, b.key) &&
345
+ a.visibility === b.visibility &&
346
+ labelsEqual(a.labels, b.labels) &&
347
+ a.updatedAt === b.updatedAt &&
348
+ a.snapshotNhash === b.snapshotNhash &&
349
+ a.encryptedKey === b.encryptedKey &&
350
+ a.keyId === b.keyId &&
351
+ a.selfEncryptedKey === b.selfEncryptedKey &&
352
+ a.selfEncryptedLinkKey === b.selfEncryptedLinkKey
353
+ );
354
+ }