cojson 0.7.0-alpha.36 → 0.7.0-alpha.38
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/.eslintrc.cjs +3 -2
- package/.prettierrc.js +9 -0
- package/.turbo/turbo-build.log +3 -36
- package/.turbo/turbo-lint.log +4 -0
- package/.turbo/turbo-test.log +1106 -0
- package/CHANGELOG.md +12 -0
- package/README.md +3 -1
- package/dist/base64url.test.js +25 -0
- package/dist/base64url.test.js.map +1 -0
- package/dist/coValueCore.js +16 -15
- package/dist/coValueCore.js.map +1 -1
- package/dist/coValues/account.js +16 -15
- package/dist/coValues/account.js.map +1 -1
- package/dist/coValues/group.js +13 -14
- package/dist/coValues/group.js.map +1 -1
- package/dist/coreToCoValue.js.map +1 -1
- package/dist/crypto/PureJSCrypto.js +89 -0
- package/dist/crypto/PureJSCrypto.js.map +1 -0
- package/dist/crypto/WasmCrypto.js +127 -0
- package/dist/crypto/WasmCrypto.js.map +1 -0
- package/dist/crypto/crypto.js +151 -0
- package/dist/crypto/crypto.js.map +1 -0
- package/dist/ids.js +4 -2
- package/dist/ids.js.map +1 -1
- package/dist/index.js +5 -9
- package/dist/index.js.map +1 -1
- package/dist/jsonStringify.js.map +1 -1
- package/dist/localNode.js +24 -24
- package/dist/localNode.js.map +1 -1
- package/dist/permissions.js.map +1 -1
- package/dist/storage/FileSystem.js +2 -2
- package/dist/storage/FileSystem.js.map +1 -1
- package/dist/storage/chunksAndKnownStates.js +2 -2
- package/dist/storage/chunksAndKnownStates.js.map +1 -1
- package/dist/storage/index.js.map +1 -1
- package/dist/sync.js +6 -2
- package/dist/sync.js.map +1 -1
- package/dist/tests/account.test.js +58 -0
- package/dist/tests/account.test.js.map +1 -0
- package/dist/tests/coList.test.js +76 -0
- package/dist/tests/coList.test.js.map +1 -0
- package/dist/tests/coMap.test.js +136 -0
- package/dist/tests/coMap.test.js.map +1 -0
- package/dist/tests/coStream.test.js +172 -0
- package/dist/tests/coStream.test.js.map +1 -0
- package/dist/tests/coValueCore.test.js +114 -0
- package/dist/tests/coValueCore.test.js.map +1 -0
- package/dist/tests/crypto.test.js +118 -0
- package/dist/tests/crypto.test.js.map +1 -0
- package/dist/tests/cryptoImpl.test.js +113 -0
- package/dist/tests/cryptoImpl.test.js.map +1 -0
- package/dist/tests/group.test.js +34 -0
- package/dist/tests/group.test.js.map +1 -0
- package/dist/tests/permissions.test.js +1060 -0
- package/dist/tests/permissions.test.js.map +1 -0
- package/dist/tests/sync.test.js +816 -0
- package/dist/tests/sync.test.js.map +1 -0
- package/dist/tests/testUtils.js +10 -9
- package/dist/tests/testUtils.js.map +1 -1
- package/dist/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
- package/dist/typeUtils/isAccountID.js.map +1 -1
- package/dist/typeUtils/isCoValue.js.map +1 -1
- package/package.json +14 -28
- package/src/base64url.test.ts +6 -6
- package/src/coValue.ts +1 -1
- package/src/coValueCore.ts +87 -85
- package/src/coValues/account.ts +26 -28
- package/src/coValues/coList.ts +10 -10
- package/src/coValues/coMap.ts +10 -10
- package/src/coValues/coStream.ts +17 -17
- package/src/coValues/group.ts +93 -109
- package/src/coreToCoValue.ts +5 -2
- package/src/crypto/PureJSCrypto.ts +200 -0
- package/src/crypto/WasmCrypto.ts +259 -0
- package/src/crypto/crypto.ts +336 -0
- package/src/ids.ts +8 -7
- package/src/index.ts +14 -26
- package/src/jsonStringify.ts +6 -4
- package/src/jsonValue.ts +2 -2
- package/src/localNode.ts +86 -80
- package/src/media.ts +3 -3
- package/src/permissions.ts +14 -16
- package/src/storage/FileSystem.ts +31 -30
- package/src/storage/chunksAndKnownStates.ts +24 -17
- package/src/storage/index.ts +42 -38
- package/src/streamUtils.ts +12 -12
- package/src/sync.ts +56 -40
- package/src/tests/account.test.ts +8 -12
- package/src/tests/coList.test.ts +19 -25
- package/src/tests/coMap.test.ts +25 -30
- package/src/tests/coStream.test.ts +28 -38
- package/src/tests/coValueCore.test.ts +35 -36
- package/src/tests/crypto.test.ts +66 -72
- package/src/tests/cryptoImpl.test.ts +183 -0
- package/src/tests/group.test.ts +16 -17
- package/src/tests/permissions.test.ts +237 -254
- package/src/tests/sync.test.ts +119 -120
- package/src/tests/testUtils.ts +22 -19
- package/src/typeUtils/accountOrAgentIDfromSessionID.ts +1 -2
- package/src/typeUtils/expectGroup.ts +1 -1
- package/src/typeUtils/isAccountID.ts +0 -1
- package/src/typeUtils/isCoValue.ts +1 -2
- package/tsconfig.json +0 -1
- package/dist/crypto.js +0 -255
- package/dist/crypto.js.map +0 -1
- package/src/crypto.ts +0 -485
package/src/localNode.ts
CHANGED
|
@@ -1,14 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AgentSecret,
|
|
3
|
-
agentSecretFromSecretSeed,
|
|
4
|
-
createdNowUnique,
|
|
5
|
-
getAgentID,
|
|
6
|
-
getAgentSealerID,
|
|
7
|
-
getAgentSealerSecret,
|
|
8
|
-
newRandomAgentSecret,
|
|
9
|
-
newRandomKeySecret,
|
|
10
|
-
seal,
|
|
11
|
-
} from "./crypto.js";
|
|
1
|
+
import { AgentSecret, CryptoProvider } from "./crypto/crypto.js";
|
|
12
2
|
import {
|
|
13
3
|
CoValueCore,
|
|
14
4
|
CoValueHeader,
|
|
@@ -48,6 +38,8 @@ const { localNode } = useJazz();
|
|
|
48
38
|
```
|
|
49
39
|
*/
|
|
50
40
|
export class LocalNode {
|
|
41
|
+
/** @internal */
|
|
42
|
+
crypto: CryptoProvider;
|
|
51
43
|
/** @internal */
|
|
52
44
|
coValues: { [key: RawCoID]: CoValueState } = {};
|
|
53
45
|
/** @category 3. Low-level */
|
|
@@ -60,24 +52,28 @@ export class LocalNode {
|
|
|
60
52
|
/** @category 3. Low-level */
|
|
61
53
|
constructor(
|
|
62
54
|
account: ControlledAccountOrAgent,
|
|
63
|
-
currentSessionID: SessionID
|
|
55
|
+
currentSessionID: SessionID,
|
|
56
|
+
crypto: CryptoProvider,
|
|
64
57
|
) {
|
|
65
58
|
this.account = account;
|
|
66
59
|
this.currentSessionID = currentSessionID;
|
|
60
|
+
this.crypto = crypto;
|
|
67
61
|
}
|
|
68
62
|
|
|
69
63
|
/** @category 2. Node Creation */
|
|
70
64
|
static async withNewlyCreatedAccount<
|
|
71
|
-
Meta extends AccountMeta = AccountMeta
|
|
65
|
+
Meta extends AccountMeta = AccountMeta,
|
|
72
66
|
>({
|
|
73
67
|
creationProps,
|
|
74
68
|
peersToLoadFrom,
|
|
75
69
|
migration,
|
|
76
|
-
|
|
70
|
+
crypto,
|
|
71
|
+
initialAgentSecret = crypto.newRandomAgentSecret(),
|
|
77
72
|
}: {
|
|
78
|
-
creationProps: {name: string};
|
|
73
|
+
creationProps: { name: string };
|
|
79
74
|
peersToLoadFrom?: Peer[];
|
|
80
75
|
migration?: RawAccountMigration<Meta>;
|
|
76
|
+
crypto: CryptoProvider;
|
|
81
77
|
initialAgentSecret?: AgentSecret;
|
|
82
78
|
}): Promise<{
|
|
83
79
|
node: LocalNode;
|
|
@@ -85,17 +81,18 @@ export class LocalNode {
|
|
|
85
81
|
accountSecret: AgentSecret;
|
|
86
82
|
sessionID: SessionID;
|
|
87
83
|
}> {
|
|
88
|
-
const throwawayAgent = newRandomAgentSecret();
|
|
84
|
+
const throwawayAgent = crypto.newRandomAgentSecret();
|
|
89
85
|
const setupNode = new LocalNode(
|
|
90
|
-
new ControlledAgent(throwawayAgent),
|
|
91
|
-
newRandomSessionID(getAgentID(throwawayAgent))
|
|
86
|
+
new ControlledAgent(throwawayAgent, crypto),
|
|
87
|
+
newRandomSessionID(crypto.getAgentID(throwawayAgent)),
|
|
88
|
+
crypto,
|
|
92
89
|
);
|
|
93
90
|
|
|
94
91
|
const account = setupNode.createAccount(initialAgentSecret);
|
|
95
92
|
|
|
96
93
|
const nodeWithAccount = account.core.node.testWithDifferentAccount(
|
|
97
94
|
account,
|
|
98
|
-
newRandomSessionID(account.id)
|
|
95
|
+
newRandomSessionID(account.id),
|
|
99
96
|
);
|
|
100
97
|
|
|
101
98
|
const accountOnNodeWithAccount =
|
|
@@ -108,22 +105,26 @@ export class LocalNode {
|
|
|
108
105
|
}
|
|
109
106
|
|
|
110
107
|
if (migration) {
|
|
111
|
-
await migration(
|
|
108
|
+
await migration(
|
|
109
|
+
accountOnNodeWithAccount,
|
|
110
|
+
nodeWithAccount,
|
|
111
|
+
creationProps,
|
|
112
|
+
);
|
|
112
113
|
} else {
|
|
113
114
|
const profileGroup = accountOnNodeWithAccount.createGroup();
|
|
114
115
|
profileGroup.addMember("everyone", "reader");
|
|
115
116
|
const profile = profileGroup.createMap<Profile>({
|
|
116
117
|
name: creationProps.name,
|
|
117
118
|
});
|
|
118
|
-
accountOnNodeWithAccount.set(
|
|
119
|
+
accountOnNodeWithAccount.set("profile", profile.id, "trusting");
|
|
119
120
|
}
|
|
120
121
|
|
|
121
122
|
const controlledAccount = new RawControlledAccount(
|
|
122
123
|
accountOnNodeWithAccount.core,
|
|
123
|
-
accountOnNodeWithAccount.agentSecret
|
|
124
|
+
accountOnNodeWithAccount.agentSecret,
|
|
124
125
|
);
|
|
125
126
|
|
|
126
|
-
nodeWithAccount.account = controlledAccount
|
|
127
|
+
nodeWithAccount.account = controlledAccount;
|
|
127
128
|
nodeWithAccount.coValues[controlledAccount.id] = {
|
|
128
129
|
state: "loaded",
|
|
129
130
|
coValue: controlledAccount.core,
|
|
@@ -137,11 +138,11 @@ export class LocalNode {
|
|
|
137
138
|
// we shouldn't need this, but it fixes account data not syncing for new accounts
|
|
138
139
|
function syncAllCoValuesAfterCreateAccount() {
|
|
139
140
|
for (const coValueEntry of Object.values(
|
|
140
|
-
nodeWithAccount.coValues
|
|
141
|
+
nodeWithAccount.coValues,
|
|
141
142
|
)) {
|
|
142
143
|
if (coValueEntry.state === "loaded") {
|
|
143
144
|
void nodeWithAccount.syncManager.syncCoValue(
|
|
144
|
-
coValueEntry.coValue
|
|
145
|
+
coValueEntry.coValue,
|
|
145
146
|
);
|
|
146
147
|
}
|
|
147
148
|
}
|
|
@@ -165,17 +166,20 @@ export class LocalNode {
|
|
|
165
166
|
accountSecret,
|
|
166
167
|
sessionID,
|
|
167
168
|
peersToLoadFrom,
|
|
169
|
+
crypto,
|
|
168
170
|
migration,
|
|
169
171
|
}: {
|
|
170
172
|
accountID: AccountID;
|
|
171
173
|
accountSecret: AgentSecret;
|
|
172
174
|
sessionID: SessionID;
|
|
173
175
|
peersToLoadFrom: Peer[];
|
|
176
|
+
crypto: CryptoProvider;
|
|
174
177
|
migration?: RawAccountMigration<Meta>;
|
|
175
178
|
}): Promise<LocalNode> {
|
|
176
179
|
const loadingNode = new LocalNode(
|
|
177
|
-
new ControlledAgent(accountSecret),
|
|
178
|
-
newRandomSessionID(accountID)
|
|
180
|
+
new ControlledAgent(accountSecret, crypto),
|
|
181
|
+
newRandomSessionID(accountID),
|
|
182
|
+
crypto,
|
|
179
183
|
);
|
|
180
184
|
|
|
181
185
|
for (const peer of peersToLoadFrom) {
|
|
@@ -192,13 +196,13 @@ export class LocalNode {
|
|
|
192
196
|
|
|
193
197
|
const controlledAccount = new RawControlledAccount(
|
|
194
198
|
account.core,
|
|
195
|
-
accountSecret
|
|
199
|
+
accountSecret,
|
|
196
200
|
);
|
|
197
201
|
|
|
198
202
|
// since this is all synchronous, we can just swap out nodes for the SyncManager
|
|
199
203
|
const node = loadingNode.testWithDifferentAccount(
|
|
200
204
|
controlledAccount,
|
|
201
|
-
sessionID
|
|
205
|
+
sessionID,
|
|
202
206
|
);
|
|
203
207
|
node.syncManager = loadingNode.syncManager;
|
|
204
208
|
node.syncManager.local = node;
|
|
@@ -223,11 +227,11 @@ export class LocalNode {
|
|
|
223
227
|
if (migration) {
|
|
224
228
|
await migration(
|
|
225
229
|
controlledAccount as RawControlledAccount<Meta>,
|
|
226
|
-
node
|
|
230
|
+
node,
|
|
227
231
|
);
|
|
228
232
|
node.account = new RawControlledAccount(
|
|
229
233
|
controlledAccount.core,
|
|
230
|
-
controlledAccount.agentSecret
|
|
234
|
+
controlledAccount.agentSecret,
|
|
231
235
|
);
|
|
232
236
|
}
|
|
233
237
|
|
|
@@ -251,14 +255,14 @@ export class LocalNode {
|
|
|
251
255
|
dontLoadFrom?: PeerID;
|
|
252
256
|
dontWaitFor?: PeerID;
|
|
253
257
|
onProgress?: (progress: number) => void;
|
|
254
|
-
} = {}
|
|
258
|
+
} = {},
|
|
255
259
|
): Promise<CoValueCore | "unavailable"> {
|
|
256
260
|
let entry = this.coValues[id];
|
|
257
261
|
if (!entry) {
|
|
258
262
|
const peersToWaitFor = new Set(
|
|
259
263
|
Object.values(this.syncManager.peers)
|
|
260
264
|
.filter((peer) => peer.role === "server")
|
|
261
|
-
.map((peer) => peer.id)
|
|
265
|
+
.map((peer) => peer.id),
|
|
262
266
|
);
|
|
263
267
|
if (options.dontWaitFor) peersToWaitFor.delete(options.dontWaitFor);
|
|
264
268
|
entry = newLoadingState(peersToWaitFor, options.onProgress);
|
|
@@ -272,7 +276,7 @@ export class LocalNode {
|
|
|
272
276
|
"Error loading from peers",
|
|
273
277
|
id,
|
|
274
278
|
|
|
275
|
-
e
|
|
279
|
+
e,
|
|
276
280
|
);
|
|
277
281
|
});
|
|
278
282
|
}
|
|
@@ -291,7 +295,7 @@ export class LocalNode {
|
|
|
291
295
|
*/
|
|
292
296
|
async load<T extends RawCoValue>(
|
|
293
297
|
id: CoID<T>,
|
|
294
|
-
onProgress?: (progress: number) => void
|
|
298
|
+
onProgress?: (progress: number) => void,
|
|
295
299
|
): Promise<T | "unavailable"> {
|
|
296
300
|
const core = await this.loadCoValueCore(id, { onProgress });
|
|
297
301
|
|
|
@@ -316,7 +320,7 @@ export class LocalNode {
|
|
|
316
320
|
/** @category 3. Low-level */
|
|
317
321
|
subscribe<T extends RawCoValue>(
|
|
318
322
|
id: CoID<T>,
|
|
319
|
-
callback: (update: T | "unavailable") => void
|
|
323
|
+
callback: (update: T | "unavailable") => void,
|
|
320
324
|
): () => void {
|
|
321
325
|
let stopped = false;
|
|
322
326
|
let unsubscribe!: () => void;
|
|
@@ -348,20 +352,20 @@ export class LocalNode {
|
|
|
348
352
|
/** @deprecated Use Account.acceptInvite instead */
|
|
349
353
|
async acceptInvite<T extends RawCoValue>(
|
|
350
354
|
groupOrOwnedValueID: CoID<T>,
|
|
351
|
-
inviteSecret: InviteSecret
|
|
355
|
+
inviteSecret: InviteSecret,
|
|
352
356
|
): Promise<void> {
|
|
353
357
|
const groupOrOwnedValue = await this.load(groupOrOwnedValueID);
|
|
354
358
|
|
|
355
359
|
if (groupOrOwnedValue === "unavailable") {
|
|
356
360
|
throw new Error(
|
|
357
|
-
"Trying to accept invite: Group/owned value unavailable from all peers"
|
|
361
|
+
"Trying to accept invite: Group/owned value unavailable from all peers",
|
|
358
362
|
);
|
|
359
363
|
}
|
|
360
364
|
|
|
361
365
|
if (groupOrOwnedValue.core.header.ruleset.type === "ownedByGroup") {
|
|
362
366
|
return this.acceptInvite(
|
|
363
367
|
groupOrOwnedValue.core.header.ruleset.group as CoID<RawGroup>,
|
|
364
|
-
inviteSecret
|
|
368
|
+
inviteSecret,
|
|
365
369
|
);
|
|
366
370
|
} else if (groupOrOwnedValue.core.header.ruleset.type !== "group") {
|
|
367
371
|
throw new Error("Can only accept invites to groups");
|
|
@@ -369,10 +373,10 @@ export class LocalNode {
|
|
|
369
373
|
|
|
370
374
|
const group = expectGroup(groupOrOwnedValue);
|
|
371
375
|
|
|
372
|
-
const inviteAgentSecret = agentSecretFromSecretSeed(
|
|
373
|
-
secretSeedFromInviteSecret(inviteSecret)
|
|
376
|
+
const inviteAgentSecret = this.crypto.agentSecretFromSecretSeed(
|
|
377
|
+
secretSeedFromInviteSecret(inviteSecret),
|
|
374
378
|
);
|
|
375
|
-
const inviteAgentID = getAgentID(inviteAgentSecret);
|
|
379
|
+
const inviteAgentID = this.crypto.getAgentID(inviteAgentSecret);
|
|
376
380
|
|
|
377
381
|
const inviteRole = await new Promise((resolve, reject) => {
|
|
378
382
|
group.subscribe((groupUpdate) => {
|
|
@@ -383,7 +387,7 @@ export class LocalNode {
|
|
|
383
387
|
});
|
|
384
388
|
setTimeout(
|
|
385
389
|
() => reject(new Error("Couldn't find invite before timeout")),
|
|
386
|
-
2000
|
|
390
|
+
2000,
|
|
387
391
|
);
|
|
388
392
|
});
|
|
389
393
|
|
|
@@ -400,7 +404,7 @@ export class LocalNode {
|
|
|
400
404
|
(existingRole === "reader" && inviteRole === "readerInvite")
|
|
401
405
|
) {
|
|
402
406
|
console.debug(
|
|
403
|
-
"Not accepting invite that would replace or downgrade role"
|
|
407
|
+
"Not accepting invite that would replace or downgrade role",
|
|
404
408
|
);
|
|
405
409
|
return;
|
|
406
410
|
}
|
|
@@ -408,10 +412,10 @@ export class LocalNode {
|
|
|
408
412
|
const groupAsInvite = expectGroup(
|
|
409
413
|
group.core
|
|
410
414
|
.testWithDifferentAccount(
|
|
411
|
-
new ControlledAgent(inviteAgentSecret),
|
|
412
|
-
newRandomSessionID(inviteAgentID)
|
|
415
|
+
new ControlledAgent(inviteAgentSecret, this.crypto),
|
|
416
|
+
newRandomSessionID(inviteAgentID),
|
|
413
417
|
)
|
|
414
|
-
.getCurrentContent()
|
|
418
|
+
.getCurrentContent(),
|
|
415
419
|
);
|
|
416
420
|
|
|
417
421
|
groupAsInvite.addMemberInternal(
|
|
@@ -419,8 +423,8 @@ export class LocalNode {
|
|
|
419
423
|
inviteRole === "adminInvite"
|
|
420
424
|
? "admin"
|
|
421
425
|
: inviteRole === "writerInvite"
|
|
422
|
-
|
|
423
|
-
|
|
426
|
+
? "writer"
|
|
427
|
+
: "reader",
|
|
424
428
|
);
|
|
425
429
|
|
|
426
430
|
group.core._sessionLogs = groupAsInvite.core.sessionLogs;
|
|
@@ -436,14 +440,14 @@ export class LocalNode {
|
|
|
436
440
|
const entry = this.coValues[id];
|
|
437
441
|
if (!entry) {
|
|
438
442
|
throw new Error(
|
|
439
|
-
`${expectation ? expectation + ": " : ""}Unknown CoValue ${id}
|
|
443
|
+
`${expectation ? expectation + ": " : ""}Unknown CoValue ${id}`,
|
|
440
444
|
);
|
|
441
445
|
}
|
|
442
446
|
if (entry.state === "loading") {
|
|
443
447
|
throw new Error(
|
|
444
448
|
`${
|
|
445
449
|
expectation ? expectation + ": " : ""
|
|
446
|
-
}CoValue ${id} not yet loaded
|
|
450
|
+
}CoValue ${id} not yet loaded`,
|
|
447
451
|
);
|
|
448
452
|
}
|
|
449
453
|
return entry.coValue;
|
|
@@ -453,43 +457,45 @@ export class LocalNode {
|
|
|
453
457
|
expectProfileLoaded(id: AccountID, expectation?: string): RawProfile {
|
|
454
458
|
const account = this.expectCoValueLoaded(id, expectation);
|
|
455
459
|
const profileID = expectGroup(account.getCurrentContent()).get(
|
|
456
|
-
"profile"
|
|
460
|
+
"profile",
|
|
457
461
|
);
|
|
458
462
|
if (!profileID) {
|
|
459
463
|
throw new Error(
|
|
460
464
|
`${
|
|
461
465
|
expectation ? expectation + ": " : ""
|
|
462
|
-
}Account ${id} has no profile
|
|
466
|
+
}Account ${id} has no profile`,
|
|
463
467
|
);
|
|
464
468
|
}
|
|
465
469
|
return this.expectCoValueLoaded(
|
|
466
470
|
profileID,
|
|
467
|
-
expectation
|
|
471
|
+
expectation,
|
|
468
472
|
).getCurrentContent() as RawProfile;
|
|
469
473
|
}
|
|
470
474
|
|
|
471
475
|
/** @internal */
|
|
472
476
|
createAccount(
|
|
473
|
-
agentSecret = newRandomAgentSecret()
|
|
477
|
+
agentSecret = this.crypto.newRandomAgentSecret(),
|
|
474
478
|
): RawControlledAccount {
|
|
475
|
-
const accountAgentID = getAgentID(agentSecret);
|
|
479
|
+
const accountAgentID = this.crypto.getAgentID(agentSecret);
|
|
476
480
|
const account = expectGroup(
|
|
477
|
-
this.createCoValue(
|
|
481
|
+
this.createCoValue(
|
|
482
|
+
accountHeaderForInitialAgentSecret(agentSecret, this.crypto),
|
|
483
|
+
)
|
|
478
484
|
.testWithDifferentAccount(
|
|
479
|
-
new ControlledAgent(agentSecret),
|
|
480
|
-
newRandomSessionID(accountAgentID)
|
|
485
|
+
new ControlledAgent(agentSecret, this.crypto),
|
|
486
|
+
newRandomSessionID(accountAgentID),
|
|
481
487
|
)
|
|
482
|
-
.getCurrentContent()
|
|
488
|
+
.getCurrentContent(),
|
|
483
489
|
);
|
|
484
490
|
|
|
485
491
|
account.set(accountAgentID, "admin", "trusting");
|
|
486
492
|
|
|
487
|
-
const readKey = newRandomKeySecret();
|
|
493
|
+
const readKey = this.crypto.newRandomKeySecret();
|
|
488
494
|
|
|
489
|
-
const sealed = seal({
|
|
495
|
+
const sealed = this.crypto.seal({
|
|
490
496
|
message: readKey.secret,
|
|
491
|
-
from: getAgentSealerSecret(agentSecret),
|
|
492
|
-
to: getAgentSealerID(accountAgentID),
|
|
497
|
+
from: this.crypto.getAgentSealerSecret(agentSecret),
|
|
498
|
+
to: this.crypto.getAgentSealerID(accountAgentID),
|
|
493
499
|
nOnceMaterial: {
|
|
494
500
|
in: account.id,
|
|
495
501
|
tx: account.core.nextTransactionID(),
|
|
@@ -512,7 +518,7 @@ export class LocalNode {
|
|
|
512
518
|
/** @internal */
|
|
513
519
|
resolveAccountAgent(
|
|
514
520
|
id: AccountID | AgentID,
|
|
515
|
-
expectation?: string
|
|
521
|
+
expectation?: string,
|
|
516
522
|
): AgentID {
|
|
517
523
|
if (isAgentID(id)) {
|
|
518
524
|
return id;
|
|
@@ -530,7 +536,7 @@ export class LocalNode {
|
|
|
530
536
|
throw new Error(
|
|
531
537
|
`${
|
|
532
538
|
expectation ? expectation + ": " : ""
|
|
533
|
-
}CoValue ${id} is not an account
|
|
539
|
+
}CoValue ${id} is not an account`,
|
|
534
540
|
);
|
|
535
541
|
}
|
|
536
542
|
|
|
@@ -539,7 +545,7 @@ export class LocalNode {
|
|
|
539
545
|
|
|
540
546
|
async resolveAccountAgentAsync(
|
|
541
547
|
id: AccountID | AgentID,
|
|
542
|
-
expectation?: string
|
|
548
|
+
expectation?: string,
|
|
543
549
|
): Promise<AgentID> {
|
|
544
550
|
if (isAgentID(id)) {
|
|
545
551
|
return id;
|
|
@@ -551,7 +557,7 @@ export class LocalNode {
|
|
|
551
557
|
throw new Error(
|
|
552
558
|
`${
|
|
553
559
|
expectation ? expectation + ": " : ""
|
|
554
|
-
}Account ${id} is unavailable from all peers
|
|
560
|
+
}Account ${id} is unavailable from all peers`,
|
|
555
561
|
);
|
|
556
562
|
}
|
|
557
563
|
|
|
@@ -565,7 +571,7 @@ export class LocalNode {
|
|
|
565
571
|
throw new Error(
|
|
566
572
|
`${
|
|
567
573
|
expectation ? expectation + ": " : ""
|
|
568
|
-
}CoValue ${id} is not an account
|
|
574
|
+
}CoValue ${id} is not an account`,
|
|
569
575
|
);
|
|
570
576
|
}
|
|
571
577
|
|
|
@@ -580,18 +586,18 @@ export class LocalNode {
|
|
|
580
586
|
type: "comap",
|
|
581
587
|
ruleset: { type: "group", initialAdmin: this.account.id },
|
|
582
588
|
meta: null,
|
|
583
|
-
...createdNowUnique(),
|
|
589
|
+
...this.crypto.createdNowUnique(),
|
|
584
590
|
});
|
|
585
591
|
|
|
586
|
-
|
|
592
|
+
const group = expectGroup(groupCoValue.getCurrentContent());
|
|
587
593
|
|
|
588
594
|
group.set(this.account.id, "admin", "trusting");
|
|
589
595
|
|
|
590
|
-
const readKey = newRandomKeySecret();
|
|
596
|
+
const readKey = this.crypto.newRandomKeySecret();
|
|
591
597
|
|
|
592
598
|
group.set(
|
|
593
599
|
`${readKey.id}_for_${this.account.id}`,
|
|
594
|
-
seal({
|
|
600
|
+
this.crypto.seal({
|
|
595
601
|
message: readKey.secret,
|
|
596
602
|
from: this.account.currentSealerSecret(),
|
|
597
603
|
to: this.account.currentSealerID(),
|
|
@@ -600,7 +606,7 @@ export class LocalNode {
|
|
|
600
606
|
tx: groupCoValue.nextTransactionID(),
|
|
601
607
|
},
|
|
602
608
|
}),
|
|
603
|
-
"trusting"
|
|
609
|
+
"trusting",
|
|
604
610
|
);
|
|
605
611
|
|
|
606
612
|
group.set("readKey", readKey.id, "trusting");
|
|
@@ -611,9 +617,9 @@ export class LocalNode {
|
|
|
611
617
|
/** @internal */
|
|
612
618
|
testWithDifferentAccount(
|
|
613
619
|
account: ControlledAccountOrAgent,
|
|
614
|
-
currentSessionID: SessionID
|
|
620
|
+
currentSessionID: SessionID,
|
|
615
621
|
): LocalNode {
|
|
616
|
-
const newNode = new LocalNode(account, currentSessionID);
|
|
622
|
+
const newNode = new LocalNode(account, currentSessionID, this.crypto);
|
|
617
623
|
|
|
618
624
|
const coValuesToCopy = Object.entries(this.coValues);
|
|
619
625
|
|
|
@@ -638,7 +644,7 @@ export class LocalNode {
|
|
|
638
644
|
const newCoValue = new CoValueCore(
|
|
639
645
|
entry.coValue.header,
|
|
640
646
|
newNode,
|
|
641
|
-
new Map(entry.coValue.sessionLogs)
|
|
647
|
+
new Map(entry.coValue.sessionLogs),
|
|
642
648
|
);
|
|
643
649
|
|
|
644
650
|
newNode.coValues[coValueID as RawCoID] = {
|
|
@@ -654,7 +660,7 @@ export class LocalNode {
|
|
|
654
660
|
// To make sure that when we edit the account, we're modifying the correct sessions
|
|
655
661
|
const accountInNode = new RawControlledAccount(
|
|
656
662
|
newNode.expectCoValueLoaded(account.id),
|
|
657
|
-
account.agentSecret
|
|
663
|
+
account.agentSecret,
|
|
658
664
|
);
|
|
659
665
|
if (accountInNode.core.node !== newNode) {
|
|
660
666
|
throw new Error("Account's node is not the new node");
|
|
@@ -693,7 +699,7 @@ type CoValueState =
|
|
|
693
699
|
/** @internal */
|
|
694
700
|
export function newLoadingState(
|
|
695
701
|
currentPeerIds: Set<PeerID>,
|
|
696
|
-
onProgress?: (progress: number) => void
|
|
702
|
+
onProgress?: (progress: number) => void,
|
|
697
703
|
): CoValueState {
|
|
698
704
|
let resolve: (coValue: CoValueCore | "unavailable") => void;
|
|
699
705
|
|
|
@@ -713,7 +719,7 @@ export function newLoadingState(
|
|
|
713
719
|
resolve = r;
|
|
714
720
|
});
|
|
715
721
|
return [id, { type: "waiting", done, resolve: resolve! }];
|
|
716
|
-
})
|
|
722
|
+
}),
|
|
717
723
|
),
|
|
718
724
|
};
|
|
719
725
|
}
|
package/src/media.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { RawCoMap } from
|
|
2
|
-
import { RawBinaryCoStream } from
|
|
1
|
+
import { RawCoMap } from "./coValues/coMap.js";
|
|
2
|
+
import { RawBinaryCoStream } from "./coValues/coStream.js";
|
|
3
3
|
|
|
4
4
|
export type ImageDefinition = RawCoMap<{
|
|
5
5
|
originalSize: [number, number];
|
|
6
6
|
placeholderDataURL?: string;
|
|
7
7
|
[res: `${number}x${number}`]: RawBinaryCoStream["id"];
|
|
8
|
-
}>;
|
|
8
|
+
}>;
|
package/src/permissions.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CoID } from "./coValue.js";
|
|
2
2
|
import { MapOpPayload } from "./coValues/coMap.js";
|
|
3
3
|
import { JsonValue } from "./jsonValue.js";
|
|
4
|
-
import { KeyID } from "./crypto.js";
|
|
4
|
+
import { KeyID } from "./crypto/crypto.js";
|
|
5
5
|
import { CoValueCore, Transaction } from "./coValueCore.js";
|
|
6
6
|
import { accountOrAgentIDfromSessionID } from "./typeUtils/accountOrAgentIDfromSessionID.js";
|
|
7
7
|
import { AgentID, RawCoID, SessionID, TransactionID } from "./ids.js";
|
|
@@ -25,7 +25,7 @@ export type Role =
|
|
|
25
25
|
| "readerInvite";
|
|
26
26
|
|
|
27
27
|
export function determineValidTransactions(
|
|
28
|
-
coValue: CoValueCore
|
|
28
|
+
coValue: CoValueCore,
|
|
29
29
|
): { txID: TransactionID; tx: Transaction }[] {
|
|
30
30
|
if (coValue.header.ruleset.type === "group") {
|
|
31
31
|
const allTransactionsSorted = [
|
|
@@ -73,7 +73,7 @@ export function determineValidTransactions(
|
|
|
73
73
|
continue;
|
|
74
74
|
} else {
|
|
75
75
|
console.warn(
|
|
76
|
-
"Only admins can make private transactions in groups"
|
|
76
|
+
"Only admins can make private transactions in groups",
|
|
77
77
|
);
|
|
78
78
|
continue;
|
|
79
79
|
}
|
|
@@ -92,8 +92,8 @@ export function determineValidTransactions(
|
|
|
92
92
|
JSON.stringify(tx.changes, (k, v) =>
|
|
93
93
|
k === "changes" || k === "encryptedChanges"
|
|
94
94
|
? v.slice(0, 20) + "..."
|
|
95
|
-
: v
|
|
96
|
-
)
|
|
95
|
+
: v,
|
|
96
|
+
),
|
|
97
97
|
);
|
|
98
98
|
continue;
|
|
99
99
|
}
|
|
@@ -173,7 +173,7 @@ export function determineValidTransactions(
|
|
|
173
173
|
)
|
|
174
174
|
) {
|
|
175
175
|
console.warn(
|
|
176
|
-
"Everyone can only be set to reader, writer or revoked"
|
|
176
|
+
"Everyone can only be set to reader, writer or revoked",
|
|
177
177
|
);
|
|
178
178
|
continue;
|
|
179
179
|
}
|
|
@@ -212,7 +212,7 @@ export function determineValidTransactions(
|
|
|
212
212
|
}
|
|
213
213
|
} else {
|
|
214
214
|
console.warn(
|
|
215
|
-
"Group transaction must be made by current admin or invite"
|
|
215
|
+
"Group transaction must be made by current admin or invite",
|
|
216
216
|
);
|
|
217
217
|
continue;
|
|
218
218
|
}
|
|
@@ -230,9 +230,9 @@ export function determineValidTransactions(
|
|
|
230
230
|
coValue.node
|
|
231
231
|
.expectCoValueLoaded(
|
|
232
232
|
coValue.header.ruleset.group,
|
|
233
|
-
"Determining valid transaction in owned object but its group wasn't loaded"
|
|
233
|
+
"Determining valid transaction in owned object but its group wasn't loaded",
|
|
234
234
|
)
|
|
235
|
-
.getCurrentContent()
|
|
235
|
+
.getCurrentContent(),
|
|
236
236
|
);
|
|
237
237
|
|
|
238
238
|
if (groupContent.type !== "comap") {
|
|
@@ -264,7 +264,7 @@ export function determineValidTransactions(
|
|
|
264
264
|
txID: { sessionID: sessionID, txIndex },
|
|
265
265
|
tx,
|
|
266
266
|
}));
|
|
267
|
-
}
|
|
267
|
+
},
|
|
268
268
|
);
|
|
269
269
|
} else if (coValue.header.ruleset.type === "unsafeAllowAll") {
|
|
270
270
|
return [...coValue.sessionLogs.entries()].flatMap(
|
|
@@ -273,24 +273,22 @@ export function determineValidTransactions(
|
|
|
273
273
|
txID: { sessionID: sessionID, txIndex },
|
|
274
274
|
tx,
|
|
275
275
|
}));
|
|
276
|
-
}
|
|
276
|
+
},
|
|
277
277
|
);
|
|
278
278
|
} else {
|
|
279
279
|
throw new Error(
|
|
280
280
|
"Unknown ruleset type " +
|
|
281
|
-
(coValue.header.ruleset as { type: string }).type
|
|
281
|
+
(coValue.header.ruleset as { type: string }).type,
|
|
282
282
|
);
|
|
283
283
|
}
|
|
284
284
|
}
|
|
285
285
|
|
|
286
|
-
export function isKeyForKeyField(
|
|
287
|
-
co: string
|
|
288
|
-
): co is `${KeyID}_for_${KeyID}` {
|
|
286
|
+
export function isKeyForKeyField(co: string): co is `${KeyID}_for_${KeyID}` {
|
|
289
287
|
return co.startsWith("key_") && co.includes("_for_key");
|
|
290
288
|
}
|
|
291
289
|
|
|
292
290
|
export function isKeyForAccountField(
|
|
293
|
-
co: string
|
|
291
|
+
co: string,
|
|
294
292
|
): co is `${KeyID}_for_${AccountID | AgentID}` {
|
|
295
293
|
return (
|
|
296
294
|
(co.startsWith("key_") &&
|