@hashtree/worker 0.2.0 → 0.2.2
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.
- package/package.json +7 -3
- package/src/app-runtime.ts +393 -0
- package/src/capabilities/blossomBandwidthTracker.ts +74 -0
- package/src/capabilities/blossomTransport.ts +179 -0
- package/src/capabilities/connectivity.ts +54 -0
- package/src/capabilities/idbStorage.ts +94 -0
- package/src/capabilities/meshRouterStore.ts +426 -0
- package/src/capabilities/rootResolver.ts +497 -0
- package/src/client-id.ts +137 -0
- package/src/client.ts +501 -0
- package/src/entry.ts +3 -0
- package/src/htree-path.ts +53 -0
- package/src/htree-url.ts +156 -0
- package/src/index.ts +76 -0
- package/src/mediaStreaming.ts +64 -0
- package/src/p2p/boundedQueue.ts +168 -0
- package/src/p2p/errorMessage.ts +6 -0
- package/src/p2p/index.ts +48 -0
- package/src/p2p/lruCache.ts +78 -0
- package/src/p2p/meshQueryRouter.ts +361 -0
- package/src/p2p/protocol.ts +11 -0
- package/src/p2p/queryForwardingMachine.ts +197 -0
- package/src/p2p/signaling.ts +284 -0
- package/src/p2p/uploadRateLimiter.ts +85 -0
- package/src/p2p/webrtcController.ts +1168 -0
- package/src/p2p/webrtcProxy.ts +519 -0
- package/src/privacyGuards.ts +31 -0
- package/src/protocol.ts +124 -0
- package/src/relay/identity.ts +86 -0
- package/src/relay/mediaHandler.ts +1633 -0
- package/src/relay/ndk.ts +590 -0
- package/src/relay/nostr-wasm.ts +249 -0
- package/src/relay/nostr.ts +249 -0
- package/src/relay/protocol.ts +361 -0
- package/src/relay/publicAssetUrl.ts +25 -0
- package/src/relay/rootPathResolver.ts +50 -0
- package/src/relay/shims.d.ts +17 -0
- package/src/relay/signing.ts +332 -0
- package/src/relay/treeRootCache.ts +354 -0
- package/src/relay/treeRootSubscription.ts +577 -0
- package/src/relay/utils/constants.ts +139 -0
- package/src/relay/utils/errorMessage.ts +7 -0
- package/src/relay/utils/lruCache.ts +79 -0
- package/src/relay/webrtc.ts +5 -0
- package/src/relay/webrtcSignaling.ts +108 -0
- package/src/relay/worker.ts +1787 -0
- package/src/relay-client.ts +265 -0
- package/src/relay-entry.ts +1 -0
- package/src/runtime-network.ts +134 -0
- package/src/runtime.ts +153 -0
- package/src/transferableBytes.ts +5 -0
- package/src/tree-root.ts +851 -0
- package/src/types.ts +8 -0
- package/src/worker.ts +975 -0
|
@@ -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
|
+
}
|