cojson 0.0.4 → 0.0.6

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.
@@ -0,0 +1,441 @@
1
+ "use strict";
2
+ import { randomBytes } from "@noble/hashes/utils";
3
+ import { Static } from "./contentTypes/static";
4
+ import { CoStream } from "./contentTypes/coStream";
5
+ import { CoMap } from "./contentTypes/coMap";
6
+ import {
7
+ StreamingHash,
8
+ getRecipientID,
9
+ getSignatoryID,
10
+ newRandomRecipient,
11
+ newRandomSignatory,
12
+ openAs,
13
+ shortHash,
14
+ sign,
15
+ verify,
16
+ encryptForTransaction,
17
+ decryptForTransaction,
18
+ unsealKeySecret,
19
+ signatorySecretToBytes,
20
+ recipientSecretToBytes,
21
+ signatorySecretFromBytes,
22
+ recipientSecretFromBytes
23
+ } from "./crypto";
24
+ import { base58 } from "@scure/base";
25
+ import {
26
+ Team,
27
+ determineValidTransactions,
28
+ expectTeamContent
29
+ } from "./permissions";
30
+ import { CoList } from "./contentTypes/coList";
31
+ function coValueIDforHeader(header) {
32
+ const hash = shortHash(header);
33
+ if (header.publicNickname) {
34
+ return `co_${header.publicNickname}_z${hash.slice(
35
+ "shortHash_z".length
36
+ )}`;
37
+ } else {
38
+ return `co_z${hash.slice("shortHash_z".length)}`;
39
+ }
40
+ }
41
+ export function agentIDfromSessionID(sessionID) {
42
+ return sessionID.split("_session")[0];
43
+ }
44
+ export function newRandomSessionID(agentID) {
45
+ return `${agentID}_session_z${base58.encode(randomBytes(8))}`;
46
+ }
47
+ export class CoValue {
48
+ id;
49
+ node;
50
+ header;
51
+ sessions;
52
+ content;
53
+ listeners = /* @__PURE__ */ new Set();
54
+ constructor(header, node) {
55
+ this.id = coValueIDforHeader(header);
56
+ this.header = header;
57
+ this.sessions = {};
58
+ this.node = node;
59
+ }
60
+ testWithDifferentCredentials(agentCredential, ownSessionID) {
61
+ const newNode = this.node.testWithDifferentCredentials(
62
+ agentCredential,
63
+ ownSessionID
64
+ );
65
+ return newNode.expectCoValueLoaded(this.id);
66
+ }
67
+ knownState() {
68
+ return {
69
+ coValueID: this.id,
70
+ header: true,
71
+ sessions: Object.fromEntries(
72
+ Object.entries(this.sessions).map(([k, v]) => [
73
+ k,
74
+ v.transactions.length
75
+ ])
76
+ )
77
+ };
78
+ }
79
+ get meta() {
80
+ var _a;
81
+ return ((_a = this.header) == null ? void 0 : _a.meta) ?? null;
82
+ }
83
+ nextTransactionID() {
84
+ var _a;
85
+ const sessionID = this.node.ownSessionID;
86
+ return {
87
+ sessionID,
88
+ txIndex: ((_a = this.sessions[sessionID]) == null ? void 0 : _a.transactions.length) || 0
89
+ };
90
+ }
91
+ tryAddTransactions(sessionID, newTransactions, newHash, newSignature) {
92
+ var _a;
93
+ const signatoryID = this.node.expectAgentLoaded(
94
+ agentIDfromSessionID(sessionID),
95
+ "Expected to know signatory of transaction"
96
+ ).signatoryID;
97
+ if (!signatoryID) {
98
+ console.warn("Unknown agent", agentIDfromSessionID(sessionID));
99
+ return false;
100
+ }
101
+ const { expectedNewHash, newStreamingHash } = this.expectedNewHashAfter(
102
+ sessionID,
103
+ newTransactions
104
+ );
105
+ if (newHash !== expectedNewHash) {
106
+ console.warn("Invalid hash", { newHash, expectedNewHash });
107
+ return false;
108
+ }
109
+ if (!verify(newSignature, newHash, signatoryID)) {
110
+ console.warn(
111
+ "Invalid signature",
112
+ newSignature,
113
+ newHash,
114
+ signatoryID
115
+ );
116
+ return false;
117
+ }
118
+ const transactions = ((_a = this.sessions[sessionID]) == null ? void 0 : _a.transactions) ?? [];
119
+ console.log("transactions before", this.id, transactions.length, this.getValidSortedTransactions().length);
120
+ transactions.push(...newTransactions);
121
+ this.sessions[sessionID] = {
122
+ transactions,
123
+ lastHash: newHash,
124
+ streamingHash: newStreamingHash,
125
+ lastSignature: newSignature
126
+ };
127
+ this.content = void 0;
128
+ console.log("transactions after", this.id, transactions.length, this.getValidSortedTransactions().length);
129
+ const content = this.getCurrentContent();
130
+ for (const listener of this.listeners) {
131
+ console.log("Calling listener (update)", this.id, content.toJSON());
132
+ listener(content);
133
+ }
134
+ return true;
135
+ }
136
+ subscribe(listener) {
137
+ this.listeners.add(listener);
138
+ console.log("Calling listener (initial)", this.id, this.getCurrentContent().toJSON());
139
+ listener(this.getCurrentContent());
140
+ return () => {
141
+ this.listeners.delete(listener);
142
+ };
143
+ }
144
+ expectedNewHashAfter(sessionID, newTransactions) {
145
+ var _a;
146
+ const streamingHash = ((_a = this.sessions[sessionID]) == null ? void 0 : _a.streamingHash.clone()) ?? new StreamingHash();
147
+ for (const transaction of newTransactions) {
148
+ streamingHash.update(transaction);
149
+ }
150
+ const newStreamingHash = streamingHash.clone();
151
+ return {
152
+ expectedNewHash: streamingHash.digest(),
153
+ newStreamingHash
154
+ };
155
+ }
156
+ makeTransaction(changes, privacy) {
157
+ const madeAt = Date.now();
158
+ let transaction;
159
+ if (privacy === "private") {
160
+ const { secret: keySecret, id: keyID } = this.getCurrentReadKey();
161
+ if (!keySecret) {
162
+ throw new Error(
163
+ "Can't make transaction without read key secret"
164
+ );
165
+ }
166
+ transaction = {
167
+ privacy: "private",
168
+ madeAt,
169
+ keyUsed: keyID,
170
+ encryptedChanges: encryptForTransaction(changes, keySecret, {
171
+ in: this.id,
172
+ tx: this.nextTransactionID()
173
+ })
174
+ };
175
+ } else {
176
+ transaction = {
177
+ privacy: "trusting",
178
+ madeAt,
179
+ changes
180
+ };
181
+ }
182
+ const sessionID = this.node.ownSessionID;
183
+ const { expectedNewHash } = this.expectedNewHashAfter(sessionID, [
184
+ transaction
185
+ ]);
186
+ const signature = sign(
187
+ this.node.agentCredential.signatorySecret,
188
+ expectedNewHash
189
+ );
190
+ const success = this.tryAddTransactions(
191
+ sessionID,
192
+ [transaction],
193
+ expectedNewHash,
194
+ signature
195
+ );
196
+ if (success) {
197
+ void this.node.sync.syncCoValue(this);
198
+ }
199
+ return success;
200
+ }
201
+ getCurrentContent() {
202
+ if (this.content) {
203
+ return this.content;
204
+ }
205
+ if (this.header.type === "comap") {
206
+ this.content = new CoMap(this);
207
+ } else if (this.header.type === "colist") {
208
+ this.content = new CoList(this);
209
+ } else if (this.header.type === "costream") {
210
+ this.content = new CoStream(this);
211
+ } else if (this.header.type === "static") {
212
+ this.content = new Static(this);
213
+ } else {
214
+ throw new Error(`Unknown coValue type ${this.header.type}`);
215
+ }
216
+ return this.content;
217
+ }
218
+ getValidSortedTransactions() {
219
+ const validTransactions = determineValidTransactions(this);
220
+ const allTransactions = validTransactions.map(({ txID, tx }) => {
221
+ if (tx.privacy === "trusting") {
222
+ return {
223
+ txID,
224
+ madeAt: tx.madeAt,
225
+ changes: tx.changes
226
+ };
227
+ } else {
228
+ const readKey = this.getReadKey(tx.keyUsed);
229
+ if (!readKey) {
230
+ return void 0;
231
+ } else {
232
+ const decrytedChanges = decryptForTransaction(
233
+ tx.encryptedChanges,
234
+ readKey,
235
+ {
236
+ in: this.id,
237
+ tx: txID
238
+ }
239
+ );
240
+ if (!decrytedChanges) {
241
+ console.error(
242
+ "Failed to decrypt transaction despite having key"
243
+ );
244
+ return void 0;
245
+ }
246
+ return {
247
+ txID,
248
+ madeAt: tx.madeAt,
249
+ changes: decrytedChanges
250
+ };
251
+ }
252
+ }
253
+ }).filter((x) => !!x);
254
+ allTransactions.sort(
255
+ (a, b) => a.madeAt - b.madeAt || (a.txID.sessionID < b.txID.sessionID ? -1 : 1) || a.txID.txIndex - b.txID.txIndex
256
+ );
257
+ return allTransactions;
258
+ }
259
+ getCurrentReadKey() {
260
+ var _a;
261
+ if (this.header.ruleset.type === "team") {
262
+ const content = expectTeamContent(this.getCurrentContent());
263
+ const currentKeyId = (_a = content.get("readKey")) == null ? void 0 : _a.keyID;
264
+ if (!currentKeyId) {
265
+ throw new Error("No readKey set");
266
+ }
267
+ const secret = this.getReadKey(currentKeyId);
268
+ return {
269
+ secret,
270
+ id: currentKeyId
271
+ };
272
+ } else if (this.header.ruleset.type === "ownedByTeam") {
273
+ return this.node.expectCoValueLoaded(this.header.ruleset.team).getCurrentReadKey();
274
+ } else {
275
+ throw new Error(
276
+ "Only teams or values owned by teams have read secrets"
277
+ );
278
+ }
279
+ }
280
+ getReadKey(keyID) {
281
+ var _a, _b, _c;
282
+ if (this.header.ruleset.type === "team") {
283
+ const content = expectTeamContent(this.getCurrentContent());
284
+ const readKeyHistory = content.getHistory("readKey");
285
+ for (const entry of readKeyHistory) {
286
+ if (((_a = entry.value) == null ? void 0 : _a.keyID) === keyID) {
287
+ const revealer = agentIDfromSessionID(entry.txID.sessionID);
288
+ const revealerAgent = this.node.expectAgentLoaded(
289
+ revealer,
290
+ "Expected to know revealer"
291
+ );
292
+ const secret = openAs(
293
+ entry.value.revelation,
294
+ this.node.agentCredential.recipientSecret,
295
+ revealerAgent.recipientID,
296
+ {
297
+ in: this.id,
298
+ tx: entry.txID
299
+ }
300
+ );
301
+ if (secret)
302
+ return secret;
303
+ }
304
+ }
305
+ for (const entry of readKeyHistory) {
306
+ const encryptedPreviousKey = (_c = (_b = entry.value) == null ? void 0 : _b.previousKeys) == null ? void 0 : _c[keyID];
307
+ if (entry.value && encryptedPreviousKey) {
308
+ const sealingKeyID = entry.value.keyID;
309
+ const sealingKeySecret = this.getReadKey(sealingKeyID);
310
+ if (!sealingKeySecret) {
311
+ continue;
312
+ }
313
+ const secret = unsealKeySecret(
314
+ {
315
+ sealed: keyID,
316
+ sealing: sealingKeyID,
317
+ encrypted: encryptedPreviousKey
318
+ },
319
+ sealingKeySecret
320
+ );
321
+ if (secret) {
322
+ return secret;
323
+ } else {
324
+ console.error(
325
+ `Sealing ${sealingKeyID} key didn't unseal ${keyID}`
326
+ );
327
+ }
328
+ }
329
+ }
330
+ return void 0;
331
+ } else if (this.header.ruleset.type === "ownedByTeam") {
332
+ return this.node.expectCoValueLoaded(this.header.ruleset.team).getReadKey(keyID);
333
+ } else {
334
+ throw new Error(
335
+ "Only teams or values owned by teams have read secrets"
336
+ );
337
+ }
338
+ }
339
+ getTeam() {
340
+ if (this.header.ruleset.type !== "ownedByTeam") {
341
+ throw new Error("Only values owned by teams have teams");
342
+ }
343
+ return new Team(
344
+ expectTeamContent(
345
+ this.node.expectCoValueLoaded(this.header.ruleset.team).getCurrentContent()
346
+ ),
347
+ this.node
348
+ );
349
+ }
350
+ getTx(txID) {
351
+ var _a;
352
+ return (_a = this.sessions[txID.sessionID]) == null ? void 0 : _a.transactions[txID.txIndex];
353
+ }
354
+ newContentSince(knownState) {
355
+ const newContent = {
356
+ action: "newContent",
357
+ coValueID: this.id,
358
+ header: (knownState == null ? void 0 : knownState.header) ? void 0 : this.header,
359
+ newContent: Object.fromEntries(
360
+ Object.entries(this.sessions).map(([sessionID, log]) => {
361
+ const newTransactions = log.transactions.slice(
362
+ (knownState == null ? void 0 : knownState.sessions[sessionID]) || 0
363
+ );
364
+ if (newTransactions.length === 0 || !log.lastHash || !log.lastSignature) {
365
+ return void 0;
366
+ }
367
+ return [
368
+ sessionID,
369
+ {
370
+ after: (knownState == null ? void 0 : knownState.sessions[sessionID]) || 0,
371
+ newTransactions,
372
+ lastHash: log.lastHash,
373
+ lastSignature: log.lastSignature
374
+ }
375
+ ];
376
+ }).filter((x) => !!x)
377
+ )
378
+ };
379
+ if (!newContent.header && Object.keys(newContent.newContent).length === 0) {
380
+ return void 0;
381
+ }
382
+ return newContent;
383
+ }
384
+ getDependedOnCoValues() {
385
+ return this.header.ruleset.type === "team" ? expectTeamContent(this.getCurrentContent()).keys().filter((k) => k.startsWith("co_agent")) : this.header.ruleset.type === "ownedByTeam" ? [this.header.ruleset.team] : [];
386
+ }
387
+ }
388
+ export function getAgent(agentCredential) {
389
+ return {
390
+ signatoryID: getSignatoryID(agentCredential.signatorySecret),
391
+ recipientID: getRecipientID(agentCredential.recipientSecret),
392
+ publicNickname: agentCredential.publicNickname
393
+ };
394
+ }
395
+ export function getAgentCoValueHeader(agent) {
396
+ return {
397
+ type: "comap",
398
+ ruleset: {
399
+ type: "agent",
400
+ initialSignatoryID: agent.signatoryID,
401
+ initialRecipientID: agent.recipientID
402
+ },
403
+ meta: null,
404
+ createdAt: null,
405
+ uniqueness: null,
406
+ publicNickname: "agent" + (agent.publicNickname ? `-${agent.publicNickname}` : "")
407
+ };
408
+ }
409
+ export function getAgentID(agent) {
410
+ return coValueIDforHeader(getAgentCoValueHeader(agent));
411
+ }
412
+ export function newRandomAgentCredential(publicNickname) {
413
+ const signatorySecret = newRandomSignatory();
414
+ const recipientSecret = newRandomRecipient();
415
+ return { signatorySecret, recipientSecret, publicNickname };
416
+ }
417
+ export function agentCredentialToBytes(cred) {
418
+ if (cred.publicNickname) {
419
+ throw new Error("Can't convert agent credential with publicNickname");
420
+ }
421
+ const bytes = new Uint8Array(64);
422
+ const signatorySecretBytes = signatorySecretToBytes(cred.signatorySecret);
423
+ if (signatorySecretBytes.length !== 32) {
424
+ throw new Error("Invalid signatorySecret length");
425
+ }
426
+ bytes.set(signatorySecretBytes);
427
+ const recipientSecretBytes = recipientSecretToBytes(cred.recipientSecret);
428
+ if (recipientSecretBytes.length !== 32) {
429
+ throw new Error("Invalid recipientSecret length");
430
+ }
431
+ bytes.set(recipientSecretBytes, 32);
432
+ return bytes;
433
+ }
434
+ export function agentCredentialFromBytes(bytes) {
435
+ if (bytes.length !== 64) {
436
+ return void 0;
437
+ }
438
+ const signatorySecret = signatorySecretFromBytes(bytes.slice(0, 32));
439
+ const recipientSecret = recipientSecretFromBytes(bytes.slice(32));
440
+ return { signatorySecret, recipientSecret };
441
+ }
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ import {
3
+ getAgent,
4
+ getAgentID,
5
+ newRandomAgentCredential,
6
+ newRandomSessionID
7
+ } from "./coValue";
8
+ import { LocalNode } from "./node";
9
+ import { createdNowUnique, sign } from "./crypto";
10
+ test("Can create coValue with new agent credentials and add transaction to it", () => {
11
+ const agentCredential = newRandomAgentCredential("agent1");
12
+ const node = new LocalNode(
13
+ agentCredential,
14
+ newRandomSessionID(getAgentID(getAgent(agentCredential)))
15
+ );
16
+ const coValue = node.createCoValue({
17
+ type: "costream",
18
+ ruleset: { type: "unsafeAllowAll" },
19
+ meta: null,
20
+ ...createdNowUnique()
21
+ });
22
+ const transaction = {
23
+ privacy: "trusting",
24
+ madeAt: Date.now(),
25
+ changes: [
26
+ {
27
+ hello: "world"
28
+ }
29
+ ]
30
+ };
31
+ const { expectedNewHash } = coValue.expectedNewHashAfter(
32
+ node.ownSessionID,
33
+ [transaction]
34
+ );
35
+ expect(
36
+ coValue.tryAddTransactions(
37
+ node.ownSessionID,
38
+ [transaction],
39
+ expectedNewHash,
40
+ sign(agentCredential.signatorySecret, expectedNewHash)
41
+ )
42
+ ).toBe(true);
43
+ });
44
+ test("transactions with wrong signature are rejected", () => {
45
+ const wrongAgent = newRandomAgentCredential("wrongAgent");
46
+ const agentCredential = newRandomAgentCredential("agent1");
47
+ const node = new LocalNode(
48
+ agentCredential,
49
+ newRandomSessionID(getAgentID(getAgent(agentCredential)))
50
+ );
51
+ const coValue = node.createCoValue({
52
+ type: "costream",
53
+ ruleset: { type: "unsafeAllowAll" },
54
+ meta: null,
55
+ ...createdNowUnique()
56
+ });
57
+ const transaction = {
58
+ privacy: "trusting",
59
+ madeAt: Date.now(),
60
+ changes: [
61
+ {
62
+ hello: "world"
63
+ }
64
+ ]
65
+ };
66
+ const { expectedNewHash } = coValue.expectedNewHashAfter(
67
+ node.ownSessionID,
68
+ [transaction]
69
+ );
70
+ expect(
71
+ coValue.tryAddTransactions(
72
+ node.ownSessionID,
73
+ [transaction],
74
+ expectedNewHash,
75
+ sign(wrongAgent.signatorySecret, expectedNewHash)
76
+ )
77
+ ).toBe(false);
78
+ });
79
+ test("transactions with correctly signed, but wrong hash are rejected", () => {
80
+ const agentCredential = newRandomAgentCredential("agent1");
81
+ const node = new LocalNode(
82
+ agentCredential,
83
+ newRandomSessionID(getAgentID(getAgent(agentCredential)))
84
+ );
85
+ const coValue = node.createCoValue({
86
+ type: "costream",
87
+ ruleset: { type: "unsafeAllowAll" },
88
+ meta: null,
89
+ ...createdNowUnique()
90
+ });
91
+ const transaction = {
92
+ privacy: "trusting",
93
+ madeAt: Date.now(),
94
+ changes: [
95
+ {
96
+ hello: "world"
97
+ }
98
+ ]
99
+ };
100
+ const { expectedNewHash } = coValue.expectedNewHashAfter(
101
+ node.ownSessionID,
102
+ [
103
+ {
104
+ privacy: "trusting",
105
+ madeAt: Date.now(),
106
+ changes: [
107
+ {
108
+ hello: "wrong"
109
+ }
110
+ ]
111
+ }
112
+ ]
113
+ );
114
+ expect(
115
+ coValue.tryAddTransactions(
116
+ node.ownSessionID,
117
+ [transaction],
118
+ expectedNewHash,
119
+ sign(agentCredential.signatorySecret, expectedNewHash)
120
+ )
121
+ ).toBe(false);
122
+ });
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ export function expectMap(content) {
3
+ if (content.type !== "comap") {
4
+ throw new Error("Expected map");
5
+ }
6
+ return content;
7
+ }