cojson 0.7.0-alpha.37 → 0.7.0-alpha.39

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 (106) hide show
  1. package/.eslintrc.cjs +3 -2
  2. package/.prettierrc.js +9 -0
  3. package/.turbo/turbo-build.log +1 -34
  4. package/.turbo/turbo-lint.log +4 -0
  5. package/.turbo/turbo-test.log +1106 -0
  6. package/CHANGELOG.md +12 -0
  7. package/README.md +3 -1
  8. package/dist/base64url.test.js +25 -0
  9. package/dist/base64url.test.js.map +1 -0
  10. package/dist/coValueCore.js +16 -15
  11. package/dist/coValueCore.js.map +1 -1
  12. package/dist/coValues/account.js +16 -15
  13. package/dist/coValues/account.js.map +1 -1
  14. package/dist/coValues/group.js +13 -14
  15. package/dist/coValues/group.js.map +1 -1
  16. package/dist/coreToCoValue.js.map +1 -1
  17. package/dist/crypto/PureJSCrypto.js +89 -0
  18. package/dist/crypto/PureJSCrypto.js.map +1 -0
  19. package/dist/crypto/WasmCrypto.js +127 -0
  20. package/dist/crypto/WasmCrypto.js.map +1 -0
  21. package/dist/crypto/crypto.js +151 -0
  22. package/dist/crypto/crypto.js.map +1 -0
  23. package/dist/ids.js +4 -2
  24. package/dist/ids.js.map +1 -1
  25. package/dist/index.js +4 -8
  26. package/dist/index.js.map +1 -1
  27. package/dist/jsonStringify.js.map +1 -1
  28. package/dist/localNode.js +24 -24
  29. package/dist/localNode.js.map +1 -1
  30. package/dist/permissions.js.map +1 -1
  31. package/dist/storage/FileSystem.js +2 -2
  32. package/dist/storage/FileSystem.js.map +1 -1
  33. package/dist/storage/chunksAndKnownStates.js +2 -2
  34. package/dist/storage/chunksAndKnownStates.js.map +1 -1
  35. package/dist/storage/index.js.map +1 -1
  36. package/dist/sync.js +6 -2
  37. package/dist/sync.js.map +1 -1
  38. package/dist/tests/account.test.js +58 -0
  39. package/dist/tests/account.test.js.map +1 -0
  40. package/dist/tests/coList.test.js +76 -0
  41. package/dist/tests/coList.test.js.map +1 -0
  42. package/dist/tests/coMap.test.js +136 -0
  43. package/dist/tests/coMap.test.js.map +1 -0
  44. package/dist/tests/coStream.test.js +172 -0
  45. package/dist/tests/coStream.test.js.map +1 -0
  46. package/dist/tests/coValueCore.test.js +114 -0
  47. package/dist/tests/coValueCore.test.js.map +1 -0
  48. package/dist/tests/crypto.test.js +118 -0
  49. package/dist/tests/crypto.test.js.map +1 -0
  50. package/dist/tests/cryptoImpl.test.js +113 -0
  51. package/dist/tests/cryptoImpl.test.js.map +1 -0
  52. package/dist/tests/group.test.js +34 -0
  53. package/dist/tests/group.test.js.map +1 -0
  54. package/dist/tests/permissions.test.js +1060 -0
  55. package/dist/tests/permissions.test.js.map +1 -0
  56. package/dist/tests/sync.test.js +816 -0
  57. package/dist/tests/sync.test.js.map +1 -0
  58. package/dist/tests/testUtils.js +10 -9
  59. package/dist/tests/testUtils.js.map +1 -1
  60. package/dist/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
  61. package/dist/typeUtils/isAccountID.js.map +1 -1
  62. package/dist/typeUtils/isCoValue.js.map +1 -1
  63. package/package.json +14 -28
  64. package/src/base64url.test.ts +6 -6
  65. package/src/coValue.ts +1 -1
  66. package/src/coValueCore.ts +87 -85
  67. package/src/coValues/account.ts +26 -28
  68. package/src/coValues/coList.ts +10 -10
  69. package/src/coValues/coMap.ts +10 -10
  70. package/src/coValues/coStream.ts +17 -17
  71. package/src/coValues/group.ts +93 -109
  72. package/src/coreToCoValue.ts +5 -2
  73. package/src/crypto/PureJSCrypto.ts +200 -0
  74. package/src/crypto/WasmCrypto.ts +259 -0
  75. package/src/crypto/crypto.ts +336 -0
  76. package/src/ids.ts +8 -7
  77. package/src/index.ts +11 -19
  78. package/src/jsonStringify.ts +6 -4
  79. package/src/jsonValue.ts +2 -2
  80. package/src/localNode.ts +86 -80
  81. package/src/media.ts +3 -3
  82. package/src/permissions.ts +14 -16
  83. package/src/storage/FileSystem.ts +31 -30
  84. package/src/storage/chunksAndKnownStates.ts +24 -17
  85. package/src/storage/index.ts +41 -37
  86. package/src/streamUtils.ts +12 -12
  87. package/src/sync.ts +56 -40
  88. package/src/tests/account.test.ts +8 -12
  89. package/src/tests/coList.test.ts +19 -25
  90. package/src/tests/coMap.test.ts +25 -30
  91. package/src/tests/coStream.test.ts +28 -38
  92. package/src/tests/coValueCore.test.ts +35 -36
  93. package/src/tests/crypto.test.ts +66 -72
  94. package/src/tests/cryptoImpl.test.ts +183 -0
  95. package/src/tests/group.test.ts +16 -17
  96. package/src/tests/permissions.test.ts +237 -254
  97. package/src/tests/sync.test.ts +119 -120
  98. package/src/tests/testUtils.ts +22 -19
  99. package/src/typeUtils/accountOrAgentIDfromSessionID.ts +1 -2
  100. package/src/typeUtils/expectGroup.ts +1 -1
  101. package/src/typeUtils/isAccountID.ts +0 -1
  102. package/src/typeUtils/isCoValue.ts +1 -2
  103. package/tsconfig.json +0 -1
  104. package/dist/crypto.js +0 -255
  105. package/dist/crypto.js.map +0 -1
  106. package/src/crypto.ts +0 -485
@@ -5,17 +5,9 @@ import {
5
5
  KeySecret,
6
6
  Signature,
7
7
  StreamingHash,
8
- unseal,
9
- shortHash,
10
- sign,
11
- verify,
12
- encryptForTransaction,
13
8
  KeyID,
14
- decryptKeySecret,
15
- getAgentSignerID,
16
- getAgentSealerID,
17
- decryptRawForTransaction,
18
- } from "./crypto.js";
9
+ CryptoProvider,
10
+ } from "./crypto/crypto.js";
19
11
  import { JsonObject, JsonValue } from "./jsonValue.js";
20
12
  import { base58 } from "@scure/base";
21
13
  import {
@@ -44,15 +36,18 @@ export type CoValueHeader = {
44
36
  uniqueness: `z${string}` | null;
45
37
  };
46
38
 
47
- export function idforHeader(header: CoValueHeader): RawCoID {
48
- const hash = shortHash(header);
39
+ export function idforHeader(
40
+ header: CoValueHeader,
41
+ crypto: CryptoProvider,
42
+ ): RawCoID {
43
+ const hash = crypto.shortHash(header);
49
44
  return `co_z${hash.slice("shortHash_z".length)}`;
50
45
  }
51
46
 
52
47
  export function newRandomSessionID(accountID: AccountID | AgentID): SessionID {
53
48
  return `${accountID}_session_z${base58.encode(
54
49
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
- (globalThis as any).crypto.getRandomValues(new Uint8Array(8))
50
+ (globalThis as any).crypto.getRandomValues(new Uint8Array(8)),
56
51
  )}`;
57
52
  }
58
53
 
@@ -93,6 +88,7 @@ const readKeyCache = new WeakMap<CoValueCore, { [id: KeyID]: KeySecret }>();
93
88
  export class CoValueCore {
94
89
  id: RawCoID;
95
90
  node: LocalNode;
91
+ crypto: CryptoProvider;
96
92
  header: CoValueHeader;
97
93
  _sessionLogs: Map<SessionID, SessionLog>;
98
94
  _cachedContent?: RawCoValue;
@@ -108,9 +104,10 @@ export class CoValueCore {
108
104
  constructor(
109
105
  header: CoValueHeader,
110
106
  node: LocalNode,
111
- internalInitSessions: Map<SessionID, SessionLog> = new Map()
107
+ internalInitSessions: Map<SessionID, SessionLog> = new Map(),
112
108
  ) {
113
- this.id = idforHeader(header);
109
+ this.crypto = node.crypto;
110
+ this.id = idforHeader(header, node.crypto);
114
111
  this.header = header;
115
112
  this._sessionLogs = internalInitSessions;
116
113
  this.node = node;
@@ -134,11 +131,11 @@ export class CoValueCore {
134
131
 
135
132
  testWithDifferentAccount(
136
133
  account: ControlledAccountOrAgent,
137
- currentSessionID: SessionID
134
+ currentSessionID: SessionID,
138
135
  ): CoValueCore {
139
136
  const newNode = this.node.testWithDifferentAccount(
140
137
  account,
141
- currentSessionID
138
+ currentSessionID,
142
139
  );
143
140
 
144
141
  return newNode.expectCoValueLoaded(this.id);
@@ -163,7 +160,7 @@ export class CoValueCore {
163
160
  [...this.sessionLogs.entries()].map(([k, v]) => [
164
161
  k,
165
162
  v.transactions.length,
166
- ])
163
+ ]),
167
164
  ),
168
165
  };
169
166
  }
@@ -178,7 +175,7 @@ export class CoValueCore {
178
175
  this.header.meta?.type === "account"
179
176
  ? (this.node.currentSessionID.replace(
180
177
  this.node.account.id,
181
- this.node.account.currentAgentID()
178
+ this.node.account.currentAgentID(),
182
179
  ) as SessionID)
183
180
  : this.node.currentSessionID;
184
181
 
@@ -192,19 +189,19 @@ export class CoValueCore {
192
189
  sessionID: SessionID,
193
190
  newTransactions: Transaction[],
194
191
  givenExpectedNewHash: Hash | undefined,
195
- newSignature: Signature
192
+ newSignature: Signature,
196
193
  ): boolean {
197
- const signerID = getAgentSignerID(
194
+ const signerID = this.crypto.getAgentSignerID(
198
195
  this.node.resolveAccountAgent(
199
196
  accountOrAgentIDfromSessionID(sessionID),
200
- "Expected to know signer of transaction"
201
- )
197
+ "Expected to know signer of transaction",
198
+ ),
202
199
  );
203
200
 
204
201
  if (!signerID) {
205
202
  console.warn(
206
203
  "Unknown agent",
207
- accountOrAgentIDfromSessionID(sessionID)
204
+ accountOrAgentIDfromSessionID(sessionID),
208
205
  );
209
206
  return false;
210
207
  }
@@ -212,7 +209,7 @@ export class CoValueCore {
212
209
  // const beforeHash = performance.now();
213
210
  const { expectedNewHash, newStreamingHash } = this.expectedNewHashAfter(
214
211
  sessionID,
215
- newTransactions
212
+ newTransactions,
216
213
  );
217
214
  // const afterHash = performance.now();
218
215
  // console.log(
@@ -229,13 +226,13 @@ export class CoValueCore {
229
226
  }
230
227
 
231
228
  // const beforeVerify = performance.now();
232
- if (!verify(newSignature, expectedNewHash, signerID)) {
229
+ if (!this.crypto.verify(newSignature, expectedNewHash, signerID)) {
233
230
  console.warn(
234
231
  "Invalid signature in",
235
232
  this.id,
236
233
  newSignature,
237
234
  expectedNewHash,
238
- signerID
235
+ signerID,
239
236
  );
240
237
  return false;
241
238
  }
@@ -251,7 +248,7 @@ export class CoValueCore {
251
248
  newSignature,
252
249
  expectedNewHash,
253
250
  newStreamingHash,
254
- "immediate"
251
+ "immediate",
255
252
  );
256
253
 
257
254
  return true;
@@ -261,7 +258,7 @@ export class CoValueCore {
261
258
  sessionID: SessionID,
262
259
  newTransactions: Transaction[],
263
260
  givenExpectedNewHash: Hash | undefined,
264
- newSignature: Signature
261
+ newSignature: Signature,
265
262
  ): Promise<boolean> {
266
263
  if (this.currentlyAsyncApplyingTxDone) {
267
264
  await this.currentlyAsyncApplyingTxDone;
@@ -272,17 +269,17 @@ export class CoValueCore {
272
269
  resolveDone = resolve;
273
270
  });
274
271
 
275
- const signerID = getAgentSignerID(
272
+ const signerID = this.crypto.getAgentSignerID(
276
273
  await this.node.resolveAccountAgentAsync(
277
274
  accountOrAgentIDfromSessionID(sessionID),
278
- "Expected to know signer of transaction"
279
- )
275
+ "Expected to know signer of transaction",
276
+ ),
280
277
  );
281
278
 
282
279
  if (!signerID) {
283
280
  console.warn(
284
281
  "Unknown agent",
285
- accountOrAgentIDfromSessionID(sessionID)
282
+ accountOrAgentIDfromSessionID(sessionID),
286
283
  );
287
284
  resolveDone();
288
285
  return false;
@@ -324,13 +321,13 @@ export class CoValueCore {
324
321
  }
325
322
 
326
323
  performance.mark("verifyStart" + this.id);
327
- if (!verify(newSignature, expectedNewHash, signerID)) {
324
+ if (!this.crypto.verify(newSignature, expectedNewHash, signerID)) {
328
325
  console.warn(
329
326
  "Invalid signature in",
330
327
  this.id,
331
328
  newSignature,
332
329
  expectedNewHash,
333
- signerID
330
+ signerID,
334
331
  );
335
332
  resolveDone();
336
333
  return false;
@@ -339,7 +336,7 @@ export class CoValueCore {
339
336
  performance.measure(
340
337
  "verify" + this.id,
341
338
  "verifyStart" + this.id,
342
- "verifyEnd" + this.id
339
+ "verifyEnd" + this.id,
343
340
  );
344
341
 
345
342
  this.doAddTransactions(
@@ -348,7 +345,7 @@ export class CoValueCore {
348
345
  newSignature,
349
346
  expectedNewHash,
350
347
  newStreamingHash,
351
- "deferred"
348
+ "deferred",
352
349
  );
353
350
 
354
351
  resolveDone();
@@ -361,7 +358,7 @@ export class CoValueCore {
361
358
  newSignature: Signature,
362
359
  expectedNewHash: Hash,
363
360
  newStreamingHash: StreamingHash,
364
- notifyMode: "immediate" | "deferred"
361
+ notifyMode: "immediate" | "deferred",
365
362
  ) {
366
363
  const transactions =
367
364
  this.sessionLogs.get(sessionID)?.transactions ?? [];
@@ -372,7 +369,7 @@ export class CoValueCore {
372
369
 
373
370
  const lastInbetweenSignatureIdx = Object.keys(signatureAfter).reduce(
374
371
  (max, idx) => (parseInt(idx) > max ? parseInt(idx) : max),
375
- -1
372
+ -1,
376
373
  );
377
374
 
378
375
  const sizeOfTxsSinceLastInbetweenSignature = transactions
@@ -383,7 +380,7 @@ export class CoValueCore {
383
380
  (tx.privacy === "private"
384
381
  ? tx.encryptedChanges.length
385
382
  : tx.changes.length),
386
- 0
383
+ 0,
387
384
  );
388
385
 
389
386
  if (sizeOfTxsSinceLastInbetweenSignature > 100 * 1024) {
@@ -448,11 +445,11 @@ export class CoValueCore {
448
445
 
449
446
  expectedNewHashAfter(
450
447
  sessionID: SessionID,
451
- newTransactions: Transaction[]
448
+ newTransactions: Transaction[],
452
449
  ): { expectedNewHash: Hash; newStreamingHash: StreamingHash } {
453
450
  const streamingHash =
454
451
  this.sessionLogs.get(sessionID)?.streamingHash.clone() ??
455
- new StreamingHash();
452
+ new StreamingHash(this.crypto);
456
453
  for (const transaction of newTransactions) {
457
454
  streamingHash.update(transaction);
458
455
  }
@@ -467,11 +464,11 @@ export class CoValueCore {
467
464
 
468
465
  async expectedNewHashAfterAsync(
469
466
  sessionID: SessionID,
470
- newTransactions: Transaction[]
467
+ newTransactions: Transaction[],
471
468
  ): Promise<{ expectedNewHash: Hash; newStreamingHash: StreamingHash }> {
472
469
  const streamingHash =
473
470
  this.sessionLogs.get(sessionID)?.streamingHash.clone() ??
474
- new StreamingHash();
471
+ new StreamingHash(this.crypto);
475
472
  let before = performance.now();
476
473
  for (const transaction of newTransactions) {
477
474
  streamingHash.update(transaction);
@@ -493,7 +490,7 @@ export class CoValueCore {
493
490
 
494
491
  makeTransaction(
495
492
  changes: JsonValue[],
496
- privacy: "private" | "trusting"
493
+ privacy: "private" | "trusting",
497
494
  ): boolean {
498
495
  const madeAt = Date.now();
499
496
 
@@ -504,14 +501,18 @@ export class CoValueCore {
504
501
 
505
502
  if (!keySecret) {
506
503
  throw new Error(
507
- "Can't make transaction without read key secret"
504
+ "Can't make transaction without read key secret",
508
505
  );
509
506
  }
510
507
 
511
- const encrypted = encryptForTransaction(changes, keySecret, {
512
- in: this.id,
513
- tx: this.nextTransactionID(),
514
- });
508
+ const encrypted = this.crypto.encryptForTransaction(
509
+ changes,
510
+ keySecret,
511
+ {
512
+ in: this.id,
513
+ tx: this.nextTransactionID(),
514
+ },
515
+ );
515
516
 
516
517
  this._decryptionCache[encrypted] = changes;
517
518
 
@@ -534,7 +535,7 @@ export class CoValueCore {
534
535
  this.header.meta?.type === "account"
535
536
  ? (this.node.currentSessionID.replace(
536
537
  this.node.account.id,
537
- this.node.account.currentAgentID()
538
+ this.node.account.currentAgentID(),
538
539
  ) as SessionID)
539
540
  : this.node.currentSessionID;
540
541
 
@@ -542,16 +543,16 @@ export class CoValueCore {
542
543
  transaction,
543
544
  ]);
544
545
 
545
- const signature = sign(
546
+ const signature = this.crypto.sign(
546
547
  this.node.account.currentSignerSecret(),
547
- expectedNewHash
548
+ expectedNewHash,
548
549
  );
549
550
 
550
551
  const success = this.tryAddTransactions(
551
552
  sessionID,
552
553
  [transaction],
553
554
  expectedNewHash,
554
- signature
555
+ signature,
555
556
  );
556
557
 
557
558
  if (success) {
@@ -603,14 +604,15 @@ export class CoValueCore {
603
604
  this._decryptionCache[tx.encryptedChanges];
604
605
 
605
606
  if (!decrytedChanges) {
606
- const decryptedString = decryptRawForTransaction(
607
- tx.encryptedChanges,
608
- readKey,
609
- {
610
- in: this.id,
611
- tx: txID,
612
- }
613
- );
607
+ const decryptedString =
608
+ this.crypto.decryptRawForTransaction(
609
+ tx.encryptedChanges,
610
+ readKey,
611
+ {
612
+ in: this.id,
613
+ tx: txID,
614
+ },
615
+ );
614
616
  decrytedChanges =
615
617
  decryptedString && parseJSON(decryptedString);
616
618
  this._decryptionCache[tx.encryptedChanges] =
@@ -619,7 +621,7 @@ export class CoValueCore {
619
621
 
620
622
  if (!decrytedChanges) {
621
623
  console.error(
622
- "Failed to decrypt transaction despite having key"
624
+ "Failed to decrypt transaction despite having key",
623
625
  );
624
626
  return undefined;
625
627
  }
@@ -636,7 +638,7 @@ export class CoValueCore {
636
638
  (a, b) =>
637
639
  a.madeAt - b.madeAt ||
638
640
  (a.txID.sessionID < b.txID.sessionID ? -1 : 1) ||
639
- a.txID.txIndex - b.txID.txIndex
641
+ a.txID.txIndex - b.txID.txIndex,
640
642
  );
641
643
 
642
644
  return allTransactions;
@@ -664,7 +666,7 @@ export class CoValueCore {
664
666
  .getCurrentReadKey();
665
667
  } else {
666
668
  throw new Error(
667
- "Only groups or values owned by groups have read secrets"
669
+ "Only groups or values owned by groups have read secrets",
668
670
  );
669
671
  }
670
672
  }
@@ -688,7 +690,7 @@ export class CoValueCore {
688
690
  getUncachedReadKey(keyID: KeyID): KeySecret | undefined {
689
691
  if (this.header.ruleset.type === "group") {
690
692
  const content = expectGroup(
691
- this.getCurrentContent({ ignorePrivateTransactions: true })
693
+ this.getCurrentContent({ ignorePrivateTransactions: true }),
692
694
  );
693
695
 
694
696
  const keyForEveryone = content.get(`${keyID}_for_everyone`);
@@ -701,24 +703,24 @@ export class CoValueCore {
701
703
  : this.node.account.id;
702
704
 
703
705
  const lastReadyKeyEdit = content.lastEditAt(
704
- `${keyID}_for_${lookupAccountOrAgentID}`
706
+ `${keyID}_for_${lookupAccountOrAgentID}`,
705
707
  );
706
708
 
707
709
  if (lastReadyKeyEdit?.value) {
708
710
  const revealer = lastReadyKeyEdit.by;
709
711
  const revealerAgent = this.node.resolveAccountAgent(
710
712
  revealer,
711
- "Expected to know revealer"
713
+ "Expected to know revealer",
712
714
  );
713
715
 
714
- const secret = unseal(
716
+ const secret = this.crypto.unseal(
715
717
  lastReadyKeyEdit.value,
716
718
  this.node.account.currentSealerSecret(),
717
- getAgentSealerID(revealerAgent),
719
+ this.crypto.getAgentSealerID(revealerAgent),
718
720
  {
719
721
  in: this.id,
720
722
  tx: lastReadyKeyEdit.tx,
721
- }
723
+ },
722
724
  );
723
725
 
724
726
  if (secret) {
@@ -740,20 +742,20 @@ export class CoValueCore {
740
742
 
741
743
  const encryptedPreviousKey = content.get(co)!;
742
744
 
743
- const secret = decryptKeySecret(
745
+ const secret = this.crypto.decryptKeySecret(
744
746
  {
745
747
  encryptedID: keyID,
746
748
  encryptingID: encryptingKeyID,
747
749
  encrypted: encryptedPreviousKey,
748
750
  },
749
- encryptingKeySecret
751
+ encryptingKeySecret,
750
752
  );
751
753
 
752
754
  if (secret) {
753
755
  return secret as KeySecret;
754
756
  } else {
755
757
  console.error(
756
- `Encrypting ${encryptingKeyID} key didn't decrypt ${keyID}`
758
+ `Encrypting ${encryptingKeyID} key didn't decrypt ${keyID}`,
757
759
  );
758
760
  }
759
761
  }
@@ -766,7 +768,7 @@ export class CoValueCore {
766
768
  .getReadKey(keyID);
767
769
  } else {
768
770
  throw new Error(
769
- "Only groups or values owned by groups have read secrets"
771
+ "Only groups or values owned by groups have read secrets",
770
772
  );
771
773
  }
772
774
  }
@@ -779,7 +781,7 @@ export class CoValueCore {
779
781
  return expectGroup(
780
782
  this.node
781
783
  .expectCoValueLoaded(this.header.ruleset.group)
782
- .getCurrentContent()
784
+ .getCurrentContent(),
783
785
  );
784
786
  }
785
787
 
@@ -788,7 +790,7 @@ export class CoValueCore {
788
790
  }
789
791
 
790
792
  newContentSince(
791
- knownState: CoValueKnownState | undefined
793
+ knownState: CoValueKnownState | undefined,
792
794
  ): NewContentMessage[] | undefined {
793
795
  const isKnownStateEmpty = !knownState?.header && !knownState?.sessions;
794
796
 
@@ -829,7 +831,7 @@ export class CoValueCore {
829
831
  const nextKnownSignatureIdx = getNextKnownSignatureIdx(
830
832
  log,
831
833
  knownStateForSessionID,
832
- sentStateForSessionID
834
+ sentStateForSessionID,
833
835
  );
834
836
 
835
837
  const firstNewTxIdx =
@@ -911,7 +913,7 @@ export class CoValueCore {
911
913
  }
912
914
 
913
915
  const piecesWithContent = pieces.filter(
914
- (piece) => Object.keys(piece.new).length > 0 || piece.header
916
+ (piece) => Object.keys(piece.new).length > 0 || piece.header,
915
917
  );
916
918
 
917
919
  if (piecesWithContent.length === 0) {
@@ -948,13 +950,13 @@ export class CoValueCore {
948
950
  [...this.sessionLogs.keys()]
949
951
  .map((sessionID) =>
950
952
  accountOrAgentIDfromSessionID(
951
- sessionID as SessionID
952
- )
953
+ sessionID as SessionID,
954
+ ),
953
955
  )
954
956
  .filter(
955
957
  (session): session is AccountID =>
956
- isAccountID(session) && session !== this.id
957
- )
958
+ isAccountID(session) && session !== this.id,
959
+ ),
958
960
  ),
959
961
  ]
960
962
  : [];
@@ -964,13 +966,13 @@ export class CoValueCore {
964
966
  function getNextKnownSignatureIdx(
965
967
  log: SessionLog,
966
968
  knownStateForSessionID?: number,
967
- sentStateForSessionID?: number
969
+ sentStateForSessionID?: number,
968
970
  ) {
969
971
  return Object.keys(log.signatureAfter)
970
972
  .map(Number)
971
973
  .sort((a, b) => a - b)
972
974
  .find(
973
975
  (idx) =>
974
- idx >= (sentStateForSessionID ?? knownStateForSessionID ?? -1)
976
+ idx >= (sentStateForSessionID ?? knownStateForSessionID ?? -1),
975
977
  );
976
978
  }
@@ -2,16 +2,12 @@ import { CoValueCore, CoValueHeader } from "../coValueCore.js";
2
2
  import { CoID, RawCoValue } from "../coValue.js";
3
3
  import {
4
4
  AgentSecret,
5
+ CryptoProvider,
5
6
  SealerID,
6
7
  SealerSecret,
7
8
  SignerID,
8
9
  SignerSecret,
9
- getAgentID,
10
- getAgentSealerID,
11
- getAgentSealerSecret,
12
- getAgentSignerID,
13
- getAgentSignerSecret,
14
- } from "../crypto.js";
10
+ } from "../crypto/crypto.js";
15
11
  import { AgentID } from "../ids.js";
16
12
  import { RawCoMap } from "./coMap.js";
17
13
  import { RawGroup, InviteSecret } from "./group.js";
@@ -19,9 +15,10 @@ import { LocalNode } from "../index.js";
19
15
  import { JsonObject } from "../jsonValue.js";
20
16
 
21
17
  export function accountHeaderForInitialAgentSecret(
22
- agentSecret: AgentSecret
18
+ agentSecret: AgentSecret,
19
+ crypto: CryptoProvider,
23
20
  ): CoValueHeader {
24
- const agent = getAgentID(agentSecret);
21
+ const agent = crypto.getAgentID(agentSecret);
25
22
  return {
26
23
  type: "comap",
27
24
  ruleset: { type: "group", initialAdmin: agent },
@@ -38,12 +35,12 @@ export class RawAccount<
38
35
  > extends RawGroup<Meta> {
39
36
  currentAgentID(): AgentID {
40
37
  const agents = this.keys().filter((k): k is AgentID =>
41
- k.startsWith("sealer_")
38
+ k.startsWith("sealer_"),
42
39
  );
43
40
 
44
41
  if (agents.length !== 1) {
45
42
  throw new Error(
46
- "Expected exactly one agent in account, got " + agents.length
43
+ "Expected exactly one agent in account, got " + agents.length,
47
44
  );
48
45
  }
49
46
 
@@ -68,11 +65,13 @@ export class RawControlledAccount<Meta extends AccountMeta = AccountMeta>
68
65
  implements ControlledAccountOrAgent
69
66
  {
70
67
  agentSecret: AgentSecret;
68
+ crypto: CryptoProvider;
71
69
 
72
70
  constructor(core: CoValueCore, agentSecret: AgentSecret) {
73
71
  super(core);
74
72
 
75
73
  this.agentSecret = agentSecret;
74
+ this.crypto = core.node.crypto;
76
75
  }
77
76
 
78
77
  /**
@@ -85,62 +84,61 @@ export class RawControlledAccount<Meta extends AccountMeta = AccountMeta>
85
84
 
86
85
  async acceptInvite<T extends RawCoValue>(
87
86
  groupOrOwnedValueID: CoID<T>,
88
- inviteSecret: InviteSecret
87
+ inviteSecret: InviteSecret,
89
88
  ): Promise<void> {
90
89
  return this.core.node.acceptInvite(groupOrOwnedValueID, inviteSecret);
91
90
  }
92
91
 
93
92
  currentAgentID(): AgentID {
94
- return getAgentID(this.agentSecret);
93
+ return this.crypto.getAgentID(this.agentSecret);
95
94
  }
96
95
 
97
96
  currentSignerID(): SignerID {
98
- return getAgentSignerID(this.currentAgentID());
97
+ return this.crypto.getAgentSignerID(this.currentAgentID());
99
98
  }
100
99
 
101
100
  currentSignerSecret(): SignerSecret {
102
- return getAgentSignerSecret(this.agentSecret);
101
+ return this.crypto.getAgentSignerSecret(this.agentSecret);
103
102
  }
104
103
 
105
104
  currentSealerID(): SealerID {
106
- return getAgentSealerID(this.currentAgentID());
105
+ return this.crypto.getAgentSealerID(this.currentAgentID());
107
106
  }
108
107
 
109
108
  currentSealerSecret(): SealerSecret {
110
- return getAgentSealerSecret(this.agentSecret);
109
+ return this.crypto.getAgentSealerSecret(this.agentSecret);
111
110
  }
112
111
  }
113
112
 
114
113
  /** @hidden */
115
114
  export class ControlledAgent implements ControlledAccountOrAgent {
116
- agentSecret: AgentSecret;
117
-
118
- constructor(agentSecret: AgentSecret) {
119
- this.agentSecret = agentSecret;
120
- }
115
+ constructor(
116
+ public agentSecret: AgentSecret,
117
+ public crypto: CryptoProvider,
118
+ ) {}
121
119
 
122
120
  get id(): AgentID {
123
- return getAgentID(this.agentSecret);
121
+ return this.crypto.getAgentID(this.agentSecret);
124
122
  }
125
123
 
126
124
  currentAgentID(): AgentID {
127
- return getAgentID(this.agentSecret);
125
+ return this.crypto.getAgentID(this.agentSecret);
128
126
  }
129
127
 
130
128
  currentSignerID(): SignerID {
131
- return getAgentSignerID(this.currentAgentID());
129
+ return this.crypto.getAgentSignerID(this.currentAgentID());
132
130
  }
133
131
 
134
132
  currentSignerSecret(): SignerSecret {
135
- return getAgentSignerSecret(this.agentSecret);
133
+ return this.crypto.getAgentSignerSecret(this.agentSecret);
136
134
  }
137
135
 
138
136
  currentSealerID(): SealerID {
139
- return getAgentSealerID(this.currentAgentID());
137
+ return this.crypto.getAgentSealerID(this.currentAgentID());
140
138
  }
141
139
 
142
140
  currentSealerSecret(): SealerSecret {
143
- return getAgentSealerSecret(this.agentSecret);
141
+ return this.crypto.getAgentSealerSecret(this.agentSecret);
144
142
  }
145
143
  }
146
144
 
@@ -159,5 +157,5 @@ export class RawProfile<
159
157
  export type RawAccountMigration<Meta extends AccountMeta = AccountMeta> = (
160
158
  account: RawControlledAccount<Meta>,
161
159
  localNode: LocalNode,
162
- creationProps?: { name: string }
160
+ creationProps?: { name: string },
163
161
  ) => void | Promise<void>;