cojson 0.1.8 → 0.1.10

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 (76) hide show
  1. package/dist/account.d.ts +6 -3
  2. package/dist/account.js +4 -2
  3. package/dist/account.js.map +1 -1
  4. package/dist/coValue.d.ts +44 -80
  5. package/dist/coValue.js +4 -348
  6. package/dist/coValue.js.map +1 -1
  7. package/dist/coValueCore.d.ts +84 -0
  8. package/dist/coValueCore.js +356 -0
  9. package/dist/coValueCore.js.map +1 -0
  10. package/dist/coValues/coList.d.ts +114 -0
  11. package/dist/{contentTypes → coValues}/coList.js +59 -19
  12. package/dist/coValues/coList.js.map +1 -0
  13. package/dist/{contentTypes → coValues}/coMap.d.ts +25 -7
  14. package/dist/{contentTypes → coValues}/coMap.js +34 -15
  15. package/dist/coValues/coMap.js.map +1 -0
  16. package/dist/coValues/coStream.d.ts +69 -0
  17. package/dist/coValues/coStream.js +131 -0
  18. package/dist/coValues/coStream.js.map +1 -0
  19. package/dist/coValues/static.d.ts +14 -0
  20. package/dist/coValues/static.js +20 -0
  21. package/dist/coValues/static.js.map +1 -0
  22. package/dist/group.d.ts +57 -9
  23. package/dist/group.js +94 -28
  24. package/dist/group.js.map +1 -1
  25. package/dist/index.d.ts +19 -10
  26. package/dist/index.js +7 -5
  27. package/dist/index.js.map +1 -1
  28. package/dist/node.d.ts +59 -5
  29. package/dist/node.js +36 -15
  30. package/dist/node.js.map +1 -1
  31. package/dist/permissions.d.ts +2 -2
  32. package/dist/permissions.js +1 -1
  33. package/dist/permissions.js.map +1 -1
  34. package/dist/sync.d.ts +3 -3
  35. package/dist/sync.js +2 -2
  36. package/dist/sync.js.map +1 -1
  37. package/dist/testUtils.d.ts +2 -2
  38. package/dist/testUtils.js +1 -1
  39. package/dist/testUtils.js.map +1 -1
  40. package/package.json +2 -2
  41. package/src/account.test.ts +1 -1
  42. package/src/account.ts +8 -5
  43. package/src/coValue.test.ts +335 -129
  44. package/src/coValue.ts +52 -576
  45. package/src/coValueCore.test.ts +180 -0
  46. package/src/coValueCore.ts +592 -0
  47. package/src/{contentTypes → coValues}/coList.ts +91 -42
  48. package/src/{contentTypes → coValues}/coMap.ts +40 -20
  49. package/src/coValues/coStream.ts +249 -0
  50. package/src/coValues/static.ts +31 -0
  51. package/src/group.test.ts +47 -0
  52. package/src/group.ts +120 -50
  53. package/src/index.ts +43 -28
  54. package/src/node.ts +48 -27
  55. package/src/permissions.test.ts +32 -32
  56. package/src/permissions.ts +5 -5
  57. package/src/sync.test.ts +77 -77
  58. package/src/sync.ts +5 -5
  59. package/src/testUtils.ts +1 -1
  60. package/tsconfig.json +1 -2
  61. package/dist/contentType.d.ts +0 -15
  62. package/dist/contentType.js +0 -7
  63. package/dist/contentType.js.map +0 -1
  64. package/dist/contentTypes/coList.d.ts +0 -77
  65. package/dist/contentTypes/coList.js.map +0 -1
  66. package/dist/contentTypes/coMap.js.map +0 -1
  67. package/dist/contentTypes/coStream.d.ts +0 -11
  68. package/dist/contentTypes/coStream.js +0 -16
  69. package/dist/contentTypes/coStream.js.map +0 -1
  70. package/dist/contentTypes/static.d.ts +0 -11
  71. package/dist/contentTypes/static.js +0 -14
  72. package/dist/contentTypes/static.js.map +0 -1
  73. package/src/contentType.test.ts +0 -284
  74. package/src/contentType.ts +0 -26
  75. package/src/contentTypes/coStream.ts +0 -24
  76. package/src/contentTypes/static.ts +0 -22
@@ -0,0 +1,592 @@
1
+ import { randomBytes } from "@noble/hashes/utils";
2
+ import { CoValueImpl } from "./coValue.js";
3
+ import { Static } from "./coValues/static.js";
4
+ import { BinaryCoStream, CoStream } from "./coValues/coStream.js";
5
+ import { CoMap } from "./coValues/coMap.js";
6
+ import {
7
+ Encrypted,
8
+ Hash,
9
+ KeySecret,
10
+ Signature,
11
+ StreamingHash,
12
+ unseal,
13
+ shortHash,
14
+ sign,
15
+ verify,
16
+ encryptForTransaction,
17
+ decryptForTransaction,
18
+ KeyID,
19
+ decryptKeySecret,
20
+ getAgentSignerID,
21
+ getAgentSealerID,
22
+ } from "./crypto.js";
23
+ import { JsonObject, JsonValue } from "./jsonValue.js";
24
+ import { base58 } from "@scure/base";
25
+ import {
26
+ PermissionsDef as RulesetDef,
27
+ determineValidTransactions,
28
+ isKeyForKeyField,
29
+ } from "./permissions.js";
30
+ import { Group, expectGroupContent } from "./group.js";
31
+ import { LocalNode } from "./node.js";
32
+ import { CoValueKnownState, NewContentMessage } from "./sync.js";
33
+ import { AgentID, RawCoID, SessionID, TransactionID } from "./ids.js";
34
+ import { CoList } from "./coValues/coList.js";
35
+ import {
36
+ AccountID,
37
+ GeneralizedControlledAccount,
38
+ } from "./account.js";
39
+
40
+ export type CoValueHeader = {
41
+ type: CoValueImpl["type"];
42
+ ruleset: RulesetDef;
43
+ meta: JsonObject | null;
44
+ createdAt: `2${string}` | null;
45
+ uniqueness: `z${string}` | null;
46
+ };
47
+
48
+ export function idforHeader(header: CoValueHeader): RawCoID {
49
+ const hash = shortHash(header);
50
+ return `co_z${hash.slice("shortHash_z".length)}`;
51
+ }
52
+
53
+ export function accountOrAgentIDfromSessionID(
54
+ sessionID: SessionID
55
+ ): AccountID | AgentID {
56
+ return sessionID.split("_session")[0] as AccountID | AgentID;
57
+ }
58
+
59
+ export function newRandomSessionID(accountID: AccountID | AgentID): SessionID {
60
+ return `${accountID}_session_z${base58.encode(randomBytes(8))}`;
61
+ }
62
+
63
+ type SessionLog = {
64
+ transactions: Transaction[];
65
+ lastHash?: Hash;
66
+ streamingHash: StreamingHash;
67
+ lastSignature: Signature;
68
+ };
69
+
70
+ export type PrivateTransaction = {
71
+ privacy: "private";
72
+ madeAt: number;
73
+ keyUsed: KeyID;
74
+ encryptedChanges: Encrypted<
75
+ JsonValue[],
76
+ { in: RawCoID; tx: TransactionID }
77
+ >;
78
+ };
79
+
80
+ export type TrustingTransaction = {
81
+ privacy: "trusting";
82
+ madeAt: number;
83
+ changes: JsonValue[];
84
+ };
85
+
86
+ export type Transaction = PrivateTransaction | TrustingTransaction;
87
+
88
+ export type DecryptedTransaction = {
89
+ txID: TransactionID;
90
+ changes: JsonValue[];
91
+ madeAt: number;
92
+ };
93
+
94
+ const readKeyCache = new WeakMap<CoValueCore, { [id: KeyID]: KeySecret }>();
95
+
96
+ export class CoValueCore {
97
+ id: RawCoID;
98
+ node: LocalNode;
99
+ header: CoValueHeader;
100
+ _sessions: { [key: SessionID]: SessionLog };
101
+ _cachedContent?: CoValueImpl;
102
+ listeners: Set<(content?: CoValueImpl) => void> = new Set();
103
+
104
+ constructor(
105
+ header: CoValueHeader,
106
+ node: LocalNode,
107
+ internalInitSessions: { [key: SessionID]: SessionLog } = {}
108
+ ) {
109
+ this.id = idforHeader(header);
110
+ this.header = header;
111
+ this._sessions = internalInitSessions;
112
+ this.node = node;
113
+
114
+ if (header.ruleset.type == "ownedByGroup") {
115
+ this.node
116
+ .expectCoValueLoaded(header.ruleset.group)
117
+ .subscribe((_groupUpdate) => {
118
+ this._cachedContent = undefined;
119
+ const newContent = this.getCurrentContent();
120
+ for (const listener of this.listeners) {
121
+ listener(newContent);
122
+ }
123
+ });
124
+ }
125
+ }
126
+
127
+ get sessions(): Readonly<{ [key: SessionID]: SessionLog }> {
128
+ return this._sessions;
129
+ }
130
+
131
+ testWithDifferentAccount(
132
+ account: GeneralizedControlledAccount,
133
+ currentSessionID: SessionID
134
+ ): CoValueCore {
135
+ const newNode = this.node.testWithDifferentAccount(
136
+ account,
137
+ currentSessionID
138
+ );
139
+
140
+ return newNode.expectCoValueLoaded(this.id);
141
+ }
142
+
143
+ knownState(): CoValueKnownState {
144
+ return {
145
+ id: this.id,
146
+ header: true,
147
+ sessions: Object.fromEntries(
148
+ Object.entries(this.sessions).map(([k, v]) => [
149
+ k,
150
+ v.transactions.length,
151
+ ])
152
+ ),
153
+ };
154
+ }
155
+
156
+ get meta(): JsonValue {
157
+ return this.header?.meta ?? null;
158
+ }
159
+
160
+ nextTransactionID(): TransactionID {
161
+ const sessionID = this.node.currentSessionID;
162
+ return {
163
+ sessionID,
164
+ txIndex: this.sessions[sessionID]?.transactions.length || 0,
165
+ };
166
+ }
167
+
168
+ tryAddTransactions(
169
+ sessionID: SessionID,
170
+ newTransactions: Transaction[],
171
+ givenExpectedNewHash: Hash | undefined,
172
+ newSignature: Signature
173
+ ): boolean {
174
+ const signerID = getAgentSignerID(
175
+ this.node.resolveAccountAgent(
176
+ accountOrAgentIDfromSessionID(sessionID),
177
+ "Expected to know signer of transaction"
178
+ )
179
+ );
180
+
181
+ if (!signerID) {
182
+ console.warn(
183
+ "Unknown agent",
184
+ accountOrAgentIDfromSessionID(sessionID)
185
+ );
186
+ return false;
187
+ }
188
+
189
+ const { expectedNewHash, newStreamingHash } = this.expectedNewHashAfter(
190
+ sessionID,
191
+ newTransactions
192
+ );
193
+
194
+ if (givenExpectedNewHash && givenExpectedNewHash !== expectedNewHash) {
195
+ console.warn("Invalid hash", {
196
+ expectedNewHash,
197
+ givenExpectedNewHash,
198
+ });
199
+ return false;
200
+ }
201
+
202
+ if (!verify(newSignature, expectedNewHash, signerID)) {
203
+ console.warn(
204
+ "Invalid signature",
205
+ newSignature,
206
+ expectedNewHash,
207
+ signerID
208
+ );
209
+ return false;
210
+ }
211
+
212
+ const transactions = this.sessions[sessionID]?.transactions ?? [];
213
+
214
+ transactions.push(...newTransactions);
215
+
216
+ this._sessions[sessionID] = {
217
+ transactions,
218
+ lastHash: expectedNewHash,
219
+ streamingHash: newStreamingHash,
220
+ lastSignature: newSignature,
221
+ };
222
+
223
+ this._cachedContent = undefined;
224
+
225
+ const content = this.getCurrentContent();
226
+
227
+ for (const listener of this.listeners) {
228
+ listener(content);
229
+ }
230
+
231
+ return true;
232
+ }
233
+
234
+ subscribe(listener: (content?: CoValueImpl) => void): () => void {
235
+ this.listeners.add(listener);
236
+ listener(this.getCurrentContent());
237
+
238
+ return () => {
239
+ this.listeners.delete(listener);
240
+ };
241
+ }
242
+
243
+ expectedNewHashAfter(
244
+ sessionID: SessionID,
245
+ newTransactions: Transaction[]
246
+ ): { expectedNewHash: Hash; newStreamingHash: StreamingHash } {
247
+ const streamingHash =
248
+ this.sessions[sessionID]?.streamingHash.clone() ??
249
+ new StreamingHash();
250
+ for (const transaction of newTransactions) {
251
+ streamingHash.update(transaction);
252
+ }
253
+
254
+ const newStreamingHash = streamingHash.clone();
255
+
256
+ return {
257
+ expectedNewHash: streamingHash.digest(),
258
+ newStreamingHash,
259
+ };
260
+ }
261
+
262
+ makeTransaction(
263
+ changes: JsonValue[],
264
+ privacy: "private" | "trusting"
265
+ ): boolean {
266
+ const madeAt = Date.now();
267
+
268
+ let transaction: Transaction;
269
+
270
+ if (privacy === "private") {
271
+ const { secret: keySecret, id: keyID } = this.getCurrentReadKey();
272
+
273
+ if (!keySecret) {
274
+ throw new Error(
275
+ "Can't make transaction without read key secret"
276
+ );
277
+ }
278
+
279
+ transaction = {
280
+ privacy: "private",
281
+ madeAt,
282
+ keyUsed: keyID,
283
+ encryptedChanges: encryptForTransaction(changes, keySecret, {
284
+ in: this.id,
285
+ tx: this.nextTransactionID(),
286
+ }),
287
+ };
288
+ } else {
289
+ transaction = {
290
+ privacy: "trusting",
291
+ madeAt,
292
+ changes,
293
+ };
294
+ }
295
+
296
+ const sessionID = this.node.currentSessionID;
297
+
298
+ const { expectedNewHash } = this.expectedNewHashAfter(sessionID, [
299
+ transaction,
300
+ ]);
301
+
302
+ const signature = sign(
303
+ this.node.account.currentSignerSecret(),
304
+ expectedNewHash
305
+ );
306
+
307
+ const success = this.tryAddTransactions(
308
+ sessionID,
309
+ [transaction],
310
+ expectedNewHash,
311
+ signature
312
+ );
313
+
314
+ if (success) {
315
+ void this.node.sync.syncCoValue(this);
316
+ }
317
+
318
+ return success;
319
+ }
320
+
321
+ getCurrentContent(): CoValueImpl {
322
+ if (this._cachedContent) {
323
+ return this._cachedContent;
324
+ }
325
+
326
+ if (this.header.type === "comap") {
327
+ this._cachedContent = new CoMap(this);
328
+ } else if (this.header.type === "colist") {
329
+ this._cachedContent = new CoList(this);
330
+ } else if (this.header.type === "costream") {
331
+ if (this.header.meta && this.header.meta.type === "binary") {
332
+ this._cachedContent = new BinaryCoStream(this);
333
+ } else {
334
+ this._cachedContent = new CoStream(this);
335
+ }
336
+ } else if (this.header.type === "static") {
337
+ this._cachedContent = new Static(this);
338
+ } else {
339
+ throw new Error(`Unknown coValue type ${this.header.type}`);
340
+ }
341
+
342
+ return this._cachedContent;
343
+ }
344
+
345
+ getValidSortedTransactions(): DecryptedTransaction[] {
346
+ const validTransactions = determineValidTransactions(this);
347
+
348
+ const allTransactions: DecryptedTransaction[] = validTransactions
349
+ .map(({ txID, tx }) => {
350
+ if (tx.privacy === "trusting") {
351
+ return {
352
+ txID,
353
+ madeAt: tx.madeAt,
354
+ changes: tx.changes,
355
+ };
356
+ } else {
357
+ const readKey = this.getReadKey(tx.keyUsed);
358
+
359
+ if (!readKey) {
360
+ return undefined;
361
+ } else {
362
+ const decrytedChanges = decryptForTransaction(
363
+ tx.encryptedChanges,
364
+ readKey,
365
+ {
366
+ in: this.id,
367
+ tx: txID,
368
+ }
369
+ );
370
+
371
+ if (!decrytedChanges) {
372
+ console.error(
373
+ "Failed to decrypt transaction despite having key"
374
+ );
375
+ return undefined;
376
+ }
377
+ return {
378
+ txID,
379
+ madeAt: tx.madeAt,
380
+ changes: decrytedChanges,
381
+ };
382
+ }
383
+ }
384
+ })
385
+ .filter((x): x is Exclude<typeof x, undefined> => !!x);
386
+ allTransactions.sort(
387
+ (a, b) =>
388
+ a.madeAt - b.madeAt ||
389
+ (a.txID.sessionID < b.txID.sessionID ? -1 : 1) ||
390
+ a.txID.txIndex - b.txID.txIndex
391
+ );
392
+
393
+ return allTransactions;
394
+ }
395
+
396
+ getCurrentReadKey(): { secret: KeySecret | undefined; id: KeyID } {
397
+ if (this.header.ruleset.type === "group") {
398
+ const content = expectGroupContent(this.getCurrentContent());
399
+
400
+ const currentKeyId = content.get("readKey");
401
+
402
+ if (!currentKeyId) {
403
+ throw new Error("No readKey set");
404
+ }
405
+
406
+ const secret = this.getReadKey(currentKeyId);
407
+
408
+ return {
409
+ secret: secret,
410
+ id: currentKeyId,
411
+ };
412
+ } else if (this.header.ruleset.type === "ownedByGroup") {
413
+ return this.node
414
+ .expectCoValueLoaded(this.header.ruleset.group)
415
+ .getCurrentReadKey();
416
+ } else {
417
+ throw new Error(
418
+ "Only groups or values owned by groups have read secrets"
419
+ );
420
+ }
421
+ }
422
+
423
+ getReadKey(keyID: KeyID): KeySecret | undefined {
424
+ if (readKeyCache.get(this)?.[keyID]) {
425
+ return readKeyCache.get(this)?.[keyID];
426
+ }
427
+ if (this.header.ruleset.type === "group") {
428
+ const content = expectGroupContent(this.getCurrentContent());
429
+
430
+ // Try to find key revelation for us
431
+
432
+ const readKeyEntry = content.getLastEntry(
433
+ `${keyID}_for_${this.node.account.id}`
434
+ );
435
+
436
+ if (readKeyEntry) {
437
+ const revealer = accountOrAgentIDfromSessionID(
438
+ readKeyEntry.txID.sessionID
439
+ );
440
+ const revealerAgent = this.node.resolveAccountAgent(
441
+ revealer,
442
+ "Expected to know revealer"
443
+ );
444
+
445
+ const secret = unseal(
446
+ readKeyEntry.value,
447
+ this.node.account.currentSealerSecret(),
448
+ getAgentSealerID(revealerAgent),
449
+ {
450
+ in: this.id,
451
+ tx: readKeyEntry.txID,
452
+ }
453
+ );
454
+
455
+ if (secret) {
456
+ let cache = readKeyCache.get(this);
457
+ if (!cache) {
458
+ cache = {};
459
+ readKeyCache.set(this, cache);
460
+ }
461
+ cache[keyID] = secret;
462
+
463
+ return secret as KeySecret;
464
+ }
465
+ }
466
+
467
+ // Try to find indirect revelation through previousKeys
468
+
469
+ for (const field of content.keys()) {
470
+ if (isKeyForKeyField(field) && field.startsWith(keyID)) {
471
+ const encryptingKeyID = field.split("_for_")[1] as KeyID;
472
+ const encryptingKeySecret =
473
+ this.getReadKey(encryptingKeyID);
474
+
475
+ if (!encryptingKeySecret) {
476
+ continue;
477
+ }
478
+
479
+ const encryptedPreviousKey = content.get(field)!;
480
+
481
+ const secret = decryptKeySecret(
482
+ {
483
+ encryptedID: keyID,
484
+ encryptingID: encryptingKeyID,
485
+ encrypted: encryptedPreviousKey,
486
+ },
487
+ encryptingKeySecret
488
+ );
489
+
490
+ if (secret) {
491
+ let cache = readKeyCache.get(this);
492
+ if (!cache) {
493
+ cache = {};
494
+ readKeyCache.set(this, cache);
495
+ }
496
+ cache[keyID] = secret;
497
+
498
+ return secret as KeySecret;
499
+ } else {
500
+ console.error(
501
+ `Encrypting ${encryptingKeyID} key didn't decrypt ${keyID}`
502
+ );
503
+ }
504
+ }
505
+ }
506
+
507
+ return undefined;
508
+ } else if (this.header.ruleset.type === "ownedByGroup") {
509
+ return this.node
510
+ .expectCoValueLoaded(this.header.ruleset.group)
511
+ .getReadKey(keyID);
512
+ } else {
513
+ throw new Error(
514
+ "Only groups or values owned by groups have read secrets"
515
+ );
516
+ }
517
+ }
518
+
519
+ getGroup(): Group {
520
+ if (this.header.ruleset.type !== "ownedByGroup") {
521
+ throw new Error("Only values owned by groups have groups");
522
+ }
523
+
524
+ return new Group(
525
+ expectGroupContent(
526
+ this.node
527
+ .expectCoValueLoaded(this.header.ruleset.group)
528
+ .getCurrentContent()
529
+ ),
530
+ this.node
531
+ );
532
+ }
533
+
534
+ getTx(txID: TransactionID): Transaction | undefined {
535
+ return this.sessions[txID.sessionID]?.transactions[txID.txIndex];
536
+ }
537
+
538
+ newContentSince(
539
+ knownState: CoValueKnownState | undefined
540
+ ): NewContentMessage | undefined {
541
+ const newContent: NewContentMessage = {
542
+ action: "content",
543
+ id: this.id,
544
+ header: knownState?.header ? undefined : this.header,
545
+ new: Object.fromEntries(
546
+ Object.entries(this.sessions)
547
+ .map(([sessionID, log]) => {
548
+ const newTransactions = log.transactions.slice(
549
+ knownState?.sessions[sessionID as SessionID] || 0
550
+ );
551
+
552
+ if (
553
+ newTransactions.length === 0 ||
554
+ !log.lastHash ||
555
+ !log.lastSignature
556
+ ) {
557
+ return undefined;
558
+ }
559
+
560
+ return [
561
+ sessionID,
562
+ {
563
+ after:
564
+ knownState?.sessions[
565
+ sessionID as SessionID
566
+ ] || 0,
567
+ newTransactions,
568
+ lastSignature: log.lastSignature,
569
+ },
570
+ ];
571
+ })
572
+ .filter((x): x is Exclude<typeof x, undefined> => !!x)
573
+ ),
574
+ };
575
+
576
+ if (!newContent.header && Object.keys(newContent.new).length === 0) {
577
+ return undefined;
578
+ }
579
+
580
+ return newContent;
581
+ }
582
+
583
+ getDependedOnCoValues(): RawCoID[] {
584
+ return this.header.ruleset.type === "group"
585
+ ? expectGroupContent(this.getCurrentContent())
586
+ .keys()
587
+ .filter((k): k is AccountID => k.startsWith("co_"))
588
+ : this.header.ruleset.type === "ownedByGroup"
589
+ ? [this.header.ruleset.group]
590
+ : [];
591
+ }
592
+ }