cojson 0.8.12 → 0.8.17

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 (164) hide show
  1. package/CHANGELOG.md +95 -83
  2. package/dist/native/PeerKnownStates.js +6 -1
  3. package/dist/native/PeerKnownStates.js.map +1 -1
  4. package/dist/native/PeerState.js +4 -3
  5. package/dist/native/PeerState.js.map +1 -1
  6. package/dist/native/PriorityBasedMessageQueue.js +1 -10
  7. package/dist/native/PriorityBasedMessageQueue.js.map +1 -1
  8. package/dist/native/SyncStateSubscriptionManager.js +70 -0
  9. package/dist/native/SyncStateSubscriptionManager.js.map +1 -0
  10. package/dist/native/base64url.js.map +1 -1
  11. package/dist/native/base64url.test.js +1 -1
  12. package/dist/native/base64url.test.js.map +1 -1
  13. package/dist/native/coValue.js.map +1 -1
  14. package/dist/native/coValueCore.js +141 -149
  15. package/dist/native/coValueCore.js.map +1 -1
  16. package/dist/native/coValueState.js.map +1 -1
  17. package/dist/native/coValues/account.js +6 -6
  18. package/dist/native/coValues/account.js.map +1 -1
  19. package/dist/native/coValues/coList.js +2 -3
  20. package/dist/native/coValues/coList.js.map +1 -1
  21. package/dist/native/coValues/coMap.js +1 -1
  22. package/dist/native/coValues/coMap.js.map +1 -1
  23. package/dist/native/coValues/coStream.js +3 -5
  24. package/dist/native/coValues/coStream.js.map +1 -1
  25. package/dist/native/coValues/group.js +11 -11
  26. package/dist/native/coValues/group.js.map +1 -1
  27. package/dist/native/coreToCoValue.js +2 -2
  28. package/dist/native/coreToCoValue.js.map +1 -1
  29. package/dist/native/crypto/PureJSCrypto.js +4 -4
  30. package/dist/native/crypto/PureJSCrypto.js.map +1 -1
  31. package/dist/native/crypto/crypto.js.map +1 -1
  32. package/dist/native/exports.js +12 -12
  33. package/dist/native/exports.js.map +1 -1
  34. package/dist/native/ids.js.map +1 -1
  35. package/dist/native/jsonStringify.js.map +1 -1
  36. package/dist/native/localNode.js +5 -7
  37. package/dist/native/localNode.js.map +1 -1
  38. package/dist/native/permissions.js +4 -7
  39. package/dist/native/permissions.js.map +1 -1
  40. package/dist/native/priority.js.map +1 -1
  41. package/dist/native/storage/FileSystem.js.map +1 -1
  42. package/dist/native/storage/chunksAndKnownStates.js +2 -4
  43. package/dist/native/storage/chunksAndKnownStates.js.map +1 -1
  44. package/dist/native/storage/index.js +6 -15
  45. package/dist/native/storage/index.js.map +1 -1
  46. package/dist/native/streamUtils.js.map +1 -1
  47. package/dist/native/sync.js +57 -7
  48. package/dist/native/sync.js.map +1 -1
  49. package/dist/native/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
  50. package/dist/native/typeUtils/expectGroup.js.map +1 -1
  51. package/dist/native/typeUtils/isAccountID.js.map +1 -1
  52. package/dist/native/typeUtils/isCoValue.js +1 -1
  53. package/dist/native/typeUtils/isCoValue.js.map +1 -1
  54. package/dist/web/PeerKnownStates.js +6 -1
  55. package/dist/web/PeerKnownStates.js.map +1 -1
  56. package/dist/web/PeerState.js +4 -3
  57. package/dist/web/PeerState.js.map +1 -1
  58. package/dist/web/PriorityBasedMessageQueue.js +1 -10
  59. package/dist/web/PriorityBasedMessageQueue.js.map +1 -1
  60. package/dist/web/SyncStateSubscriptionManager.js +70 -0
  61. package/dist/web/SyncStateSubscriptionManager.js.map +1 -0
  62. package/dist/web/base64url.js.map +1 -1
  63. package/dist/web/base64url.test.js +1 -1
  64. package/dist/web/base64url.test.js.map +1 -1
  65. package/dist/web/coValue.js.map +1 -1
  66. package/dist/web/coValueCore.js +141 -149
  67. package/dist/web/coValueCore.js.map +1 -1
  68. package/dist/web/coValueState.js.map +1 -1
  69. package/dist/web/coValues/account.js +6 -6
  70. package/dist/web/coValues/account.js.map +1 -1
  71. package/dist/web/coValues/coList.js +2 -3
  72. package/dist/web/coValues/coList.js.map +1 -1
  73. package/dist/web/coValues/coMap.js +1 -1
  74. package/dist/web/coValues/coMap.js.map +1 -1
  75. package/dist/web/coValues/coStream.js +3 -5
  76. package/dist/web/coValues/coStream.js.map +1 -1
  77. package/dist/web/coValues/group.js +11 -11
  78. package/dist/web/coValues/group.js.map +1 -1
  79. package/dist/web/coreToCoValue.js +2 -2
  80. package/dist/web/coreToCoValue.js.map +1 -1
  81. package/dist/web/crypto/PureJSCrypto.js +4 -4
  82. package/dist/web/crypto/PureJSCrypto.js.map +1 -1
  83. package/dist/web/crypto/WasmCrypto.js +5 -5
  84. package/dist/web/crypto/WasmCrypto.js.map +1 -1
  85. package/dist/web/crypto/crypto.js.map +1 -1
  86. package/dist/web/exports.js +12 -12
  87. package/dist/web/exports.js.map +1 -1
  88. package/dist/web/ids.js.map +1 -1
  89. package/dist/web/jsonStringify.js.map +1 -1
  90. package/dist/web/localNode.js +5 -7
  91. package/dist/web/localNode.js.map +1 -1
  92. package/dist/web/permissions.js +4 -7
  93. package/dist/web/permissions.js.map +1 -1
  94. package/dist/web/priority.js.map +1 -1
  95. package/dist/web/storage/FileSystem.js.map +1 -1
  96. package/dist/web/storage/chunksAndKnownStates.js +2 -4
  97. package/dist/web/storage/chunksAndKnownStates.js.map +1 -1
  98. package/dist/web/storage/index.js +6 -15
  99. package/dist/web/storage/index.js.map +1 -1
  100. package/dist/web/streamUtils.js.map +1 -1
  101. package/dist/web/sync.js +57 -7
  102. package/dist/web/sync.js.map +1 -1
  103. package/dist/web/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
  104. package/dist/web/typeUtils/expectGroup.js.map +1 -1
  105. package/dist/web/typeUtils/isAccountID.js.map +1 -1
  106. package/dist/web/typeUtils/isCoValue.js +1 -1
  107. package/dist/web/typeUtils/isCoValue.js.map +1 -1
  108. package/package.json +4 -14
  109. package/src/PeerKnownStates.ts +98 -90
  110. package/src/PeerState.ts +92 -73
  111. package/src/PriorityBasedMessageQueue.ts +42 -49
  112. package/src/SyncStateSubscriptionManager.ts +124 -0
  113. package/src/base64url.test.ts +24 -24
  114. package/src/base64url.ts +44 -45
  115. package/src/coValue.ts +45 -45
  116. package/src/coValueCore.ts +746 -785
  117. package/src/coValueState.ts +82 -72
  118. package/src/coValues/account.ts +143 -150
  119. package/src/coValues/coList.ts +520 -522
  120. package/src/coValues/coMap.ts +283 -285
  121. package/src/coValues/coStream.ts +320 -324
  122. package/src/coValues/group.ts +306 -305
  123. package/src/coreToCoValue.ts +28 -31
  124. package/src/crypto/PureJSCrypto.ts +188 -194
  125. package/src/crypto/WasmCrypto.ts +236 -254
  126. package/src/crypto/crypto.ts +302 -309
  127. package/src/exports.ts +116 -116
  128. package/src/ids.ts +9 -9
  129. package/src/jsonStringify.ts +46 -46
  130. package/src/jsonValue.ts +24 -10
  131. package/src/localNode.ts +635 -660
  132. package/src/media.ts +3 -3
  133. package/src/permissions.ts +272 -278
  134. package/src/priority.ts +21 -19
  135. package/src/storage/FileSystem.ts +91 -99
  136. package/src/storage/chunksAndKnownStates.ts +110 -115
  137. package/src/storage/index.ts +466 -497
  138. package/src/streamUtils.ts +60 -60
  139. package/src/sync.ts +656 -608
  140. package/src/tests/PeerKnownStates.test.ts +38 -34
  141. package/src/tests/PeerState.test.ts +101 -64
  142. package/src/tests/PriorityBasedMessageQueue.test.ts +91 -91
  143. package/src/tests/SyncStateSubscriptionManager.test.ts +232 -0
  144. package/src/tests/account.test.ts +59 -59
  145. package/src/tests/coList.test.ts +65 -65
  146. package/src/tests/coMap.test.ts +137 -137
  147. package/src/tests/coStream.test.ts +254 -257
  148. package/src/tests/coValueCore.test.ts +153 -156
  149. package/src/tests/crypto.test.ts +136 -144
  150. package/src/tests/cryptoImpl.test.ts +205 -197
  151. package/src/tests/group.test.ts +24 -24
  152. package/src/tests/permissions.test.ts +1306 -1371
  153. package/src/tests/priority.test.ts +65 -82
  154. package/src/tests/sync.test.ts +1573 -1263
  155. package/src/tests/testUtils.ts +85 -53
  156. package/src/typeUtils/accountOrAgentIDfromSessionID.ts +4 -4
  157. package/src/typeUtils/expectGroup.ts +9 -9
  158. package/src/typeUtils/isAccountID.ts +1 -1
  159. package/src/typeUtils/isCoValue.ts +9 -9
  160. package/tsconfig.json +4 -6
  161. package/tsconfig.native.json +9 -11
  162. package/tsconfig.web.json +4 -10
  163. package/.eslintrc.cjs +0 -25
  164. package/.prettierrc.js +0 -9
@@ -1,188 +1,180 @@
1
- import { expect, test } from "vitest";
2
- import { WasmCrypto } from "../crypto/WasmCrypto.js";
3
- import { base58, base64url } from "@scure/base";
4
- import { x25519 } from "@noble/curves/ed25519";
5
1
  import { xsalsa20_poly1305 } from "@noble/ciphers/salsa";
2
+ import { x25519 } from "@noble/curves/ed25519";
6
3
  import { blake3 } from "@noble/hashes/blake3";
4
+ import { base58, base64url } from "@scure/base";
5
+ import { expect, test } from "vitest";
6
+ import { WasmCrypto } from "../crypto/WasmCrypto.js";
7
7
  import { SessionID } from "../ids.js";
8
8
  import { stableStringify } from "../jsonStringify.js";
9
9
 
10
10
  const Crypto = await WasmCrypto.create();
11
11
 
12
12
  test("Signatures round-trip and use stable stringify", () => {
13
- const data = { b: "world", a: "hello" };
14
- const signer = Crypto.newRandomSigner();
15
- const signature = Crypto.sign(signer, data);
16
-
17
- expect(signature).toMatch(/^signature_z/);
18
- expect(
19
- Crypto.verify(
20
- signature,
21
- { a: "hello", b: "world" },
22
- Crypto.getSignerID(signer),
23
- ),
24
- ).toBe(true);
13
+ const data = { b: "world", a: "hello" };
14
+ const signer = Crypto.newRandomSigner();
15
+ const signature = Crypto.sign(signer, data);
16
+
17
+ expect(signature).toMatch(/^signature_z/);
18
+ expect(
19
+ Crypto.verify(
20
+ signature,
21
+ { a: "hello", b: "world" },
22
+ Crypto.getSignerID(signer),
23
+ ),
24
+ ).toBe(true);
25
25
  });
26
26
 
27
27
  test("Invalid signatures don't verify", () => {
28
- const data = { b: "world", a: "hello" };
29
- const signer = Crypto.newRandomSigner();
30
- const signer2 = Crypto.newRandomSigner();
31
- const wrongSignature = Crypto.sign(signer2, data);
32
-
33
- expect(
34
- Crypto.verify(wrongSignature, data, Crypto.getSignerID(signer)),
35
- ).toBe(false);
28
+ const data = { b: "world", a: "hello" };
29
+ const signer = Crypto.newRandomSigner();
30
+ const signer2 = Crypto.newRandomSigner();
31
+ const wrongSignature = Crypto.sign(signer2, data);
32
+
33
+ expect(Crypto.verify(wrongSignature, data, Crypto.getSignerID(signer))).toBe(
34
+ false,
35
+ );
36
36
  });
37
37
 
38
38
  test("encrypting round-trips, but invalid receiver can't unseal", () => {
39
- const data = { b: "world", a: "hello" };
40
- const sender = Crypto.newRandomSealer();
41
- const sealer = Crypto.newRandomSealer();
42
- const wrongSealer = Crypto.newRandomSealer();
43
-
44
- const nOnceMaterial = {
45
- in: "co_zTEST",
46
- tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
47
- } as const;
48
-
49
- const sealed = Crypto.seal({
50
- message: data,
51
- from: sender,
52
- to: Crypto.getSealerID(sealer),
53
- nOnceMaterial,
54
- });
55
-
56
- expect(
57
- Crypto.unseal(
58
- sealed,
59
- sealer,
60
- Crypto.getSealerID(sender),
61
- nOnceMaterial,
62
- ),
63
- ).toEqual(data);
64
- expect(() =>
65
- Crypto.unseal(
66
- sealed,
67
- wrongSealer,
68
- Crypto.getSealerID(sender),
69
- nOnceMaterial,
70
- ),
71
- ).toThrow(/Wrong tag/);
72
-
73
- // trying with wrong sealer secret, by hand
74
- const nOnce = blake3(
75
- new TextEncoder().encode(stableStringify(nOnceMaterial)),
76
- ).slice(0, 24);
77
- const sealer3priv = base58.decode(
78
- wrongSealer.substring("sealerSecret_z".length),
79
- );
80
- const senderPub = base58.decode(
81
- Crypto.getSealerID(sender).substring("sealer_z".length),
82
- );
83
- const sealedBytes = base64url.decode(sealed.substring("sealed_U".length));
84
- const sharedSecret = x25519.getSharedSecret(sealer3priv, senderPub);
85
-
86
- expect(() => {
87
- const _ = xsalsa20_poly1305(sharedSecret, nOnce).decrypt(sealedBytes);
88
- }).toThrow("Wrong tag");
39
+ const data = { b: "world", a: "hello" };
40
+ const sender = Crypto.newRandomSealer();
41
+ const sealer = Crypto.newRandomSealer();
42
+ const wrongSealer = Crypto.newRandomSealer();
43
+
44
+ const nOnceMaterial = {
45
+ in: "co_zTEST",
46
+ tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
47
+ } as const;
48
+
49
+ const sealed = Crypto.seal({
50
+ message: data,
51
+ from: sender,
52
+ to: Crypto.getSealerID(sealer),
53
+ nOnceMaterial,
54
+ });
55
+
56
+ expect(
57
+ Crypto.unseal(sealed, sealer, Crypto.getSealerID(sender), nOnceMaterial),
58
+ ).toEqual(data);
59
+ expect(() =>
60
+ Crypto.unseal(
61
+ sealed,
62
+ wrongSealer,
63
+ Crypto.getSealerID(sender),
64
+ nOnceMaterial,
65
+ ),
66
+ ).toThrow(/Wrong tag/);
67
+
68
+ // trying with wrong sealer secret, by hand
69
+ const nOnce = blake3(
70
+ new TextEncoder().encode(stableStringify(nOnceMaterial)),
71
+ ).slice(0, 24);
72
+ const sealer3priv = base58.decode(
73
+ wrongSealer.substring("sealerSecret_z".length),
74
+ );
75
+ const senderPub = base58.decode(
76
+ Crypto.getSealerID(sender).substring("sealer_z".length),
77
+ );
78
+ const sealedBytes = base64url.decode(sealed.substring("sealed_U".length));
79
+ const sharedSecret = x25519.getSharedSecret(sealer3priv, senderPub);
80
+
81
+ expect(() => {
82
+ const _ = xsalsa20_poly1305(sharedSecret, nOnce).decrypt(sealedBytes);
83
+ }).toThrow("Wrong tag");
89
84
  });
90
85
 
91
86
  test("Hashing is deterministic", () => {
92
- expect(Crypto.secureHash({ b: "world", a: "hello" })).toEqual(
93
- Crypto.secureHash({ a: "hello", b: "world" }),
94
- );
87
+ expect(Crypto.secureHash({ b: "world", a: "hello" })).toEqual(
88
+ Crypto.secureHash({ a: "hello", b: "world" }),
89
+ );
95
90
 
96
- expect(Crypto.shortHash({ b: "world", a: "hello" })).toEqual(
97
- Crypto.shortHash({ a: "hello", b: "world" }),
98
- );
91
+ expect(Crypto.shortHash({ b: "world", a: "hello" })).toEqual(
92
+ Crypto.shortHash({ a: "hello", b: "world" }),
93
+ );
99
94
  });
100
95
 
101
96
  test("Encryption for transactions round-trips", () => {
102
- const { secret } = Crypto.newRandomKeySecret();
97
+ const { secret } = Crypto.newRandomKeySecret();
103
98
 
104
- const encrypted1 = Crypto.encryptForTransaction({ a: "hello" }, secret, {
105
- in: "co_zTEST",
106
- tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
107
- });
99
+ const encrypted1 = Crypto.encryptForTransaction({ a: "hello" }, secret, {
100
+ in: "co_zTEST",
101
+ tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
102
+ });
108
103
 
109
- const encrypted2 = Crypto.encryptForTransaction({ b: "world" }, secret, {
110
- in: "co_zTEST",
111
- tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 1 },
112
- });
104
+ const encrypted2 = Crypto.encryptForTransaction({ b: "world" }, secret, {
105
+ in: "co_zTEST",
106
+ tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 1 },
107
+ });
113
108
 
114
- const decrypted1 = Crypto.decryptForTransaction(encrypted1, secret, {
115
- in: "co_zTEST",
116
- tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
117
- });
109
+ const decrypted1 = Crypto.decryptForTransaction(encrypted1, secret, {
110
+ in: "co_zTEST",
111
+ tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
112
+ });
118
113
 
119
- const decrypted2 = Crypto.decryptForTransaction(encrypted2, secret, {
120
- in: "co_zTEST",
121
- tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 1 },
122
- });
114
+ const decrypted2 = Crypto.decryptForTransaction(encrypted2, secret, {
115
+ in: "co_zTEST",
116
+ tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 1 },
117
+ });
123
118
 
124
- expect([decrypted1, decrypted2]).toEqual([{ a: "hello" }, { b: "world" }]);
119
+ expect([decrypted1, decrypted2]).toEqual([{ a: "hello" }, { b: "world" }]);
125
120
  });
126
121
 
127
122
  test("Encryption for transactions doesn't decrypt with a wrong key", () => {
128
- const { secret } = Crypto.newRandomKeySecret();
129
- const { secret: secret2 } = Crypto.newRandomKeySecret();
130
-
131
- const encrypted1 = Crypto.encryptForTransaction({ a: "hello" }, secret, {
132
- in: "co_zTEST",
133
- tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
134
- });
135
-
136
- const encrypted2 = Crypto.encryptForTransaction({ b: "world" }, secret, {
137
- in: "co_zTEST",
138
- tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 1 },
139
- });
140
-
141
- const decrypted1 = Crypto.decryptForTransaction(encrypted1, secret2, {
142
- in: "co_zTEST",
143
- tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
144
- });
145
-
146
- const decrypted2 = Crypto.decryptForTransaction(encrypted2, secret2, {
147
- in: "co_zTEST",
148
- tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 1 },
149
- });
150
-
151
- expect([decrypted1, decrypted2]).toEqual([undefined, undefined]);
123
+ const { secret } = Crypto.newRandomKeySecret();
124
+ const { secret: secret2 } = Crypto.newRandomKeySecret();
125
+
126
+ const encrypted1 = Crypto.encryptForTransaction({ a: "hello" }, secret, {
127
+ in: "co_zTEST",
128
+ tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
129
+ });
130
+
131
+ const encrypted2 = Crypto.encryptForTransaction({ b: "world" }, secret, {
132
+ in: "co_zTEST",
133
+ tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 1 },
134
+ });
135
+
136
+ const decrypted1 = Crypto.decryptForTransaction(encrypted1, secret2, {
137
+ in: "co_zTEST",
138
+ tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
139
+ });
140
+
141
+ const decrypted2 = Crypto.decryptForTransaction(encrypted2, secret2, {
142
+ in: "co_zTEST",
143
+ tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 1 },
144
+ });
145
+
146
+ expect([decrypted1, decrypted2]).toEqual([undefined, undefined]);
152
147
  });
153
148
 
154
149
  test("Encryption of keySecrets round-trips", () => {
155
- const toEncrypt = Crypto.newRandomKeySecret();
156
- const encrypting = Crypto.newRandomKeySecret();
150
+ const toEncrypt = Crypto.newRandomKeySecret();
151
+ const encrypting = Crypto.newRandomKeySecret();
157
152
 
158
- const keys = {
159
- toEncrypt,
160
- encrypting,
161
- };
153
+ const keys = {
154
+ toEncrypt,
155
+ encrypting,
156
+ };
162
157
 
163
- const encrypted = Crypto.encryptKeySecret(keys);
158
+ const encrypted = Crypto.encryptKeySecret(keys);
164
159
 
165
- const decrypted = Crypto.decryptKeySecret(encrypted, encrypting.secret);
160
+ const decrypted = Crypto.decryptKeySecret(encrypted, encrypting.secret);
166
161
 
167
- expect(decrypted).toEqual(toEncrypt.secret);
162
+ expect(decrypted).toEqual(toEncrypt.secret);
168
163
  });
169
164
 
170
165
  test("Encryption of keySecrets doesn't decrypt with a wrong key", () => {
171
- const toEncrypt = Crypto.newRandomKeySecret();
172
- const encrypting = Crypto.newRandomKeySecret();
173
- const encryptingWrong = Crypto.newRandomKeySecret();
166
+ const toEncrypt = Crypto.newRandomKeySecret();
167
+ const encrypting = Crypto.newRandomKeySecret();
168
+ const encryptingWrong = Crypto.newRandomKeySecret();
174
169
 
175
- const keys = {
176
- toEncrypt,
177
- encrypting,
178
- };
170
+ const keys = {
171
+ toEncrypt,
172
+ encrypting,
173
+ };
179
174
 
180
- const encrypted = Crypto.encryptKeySecret(keys);
175
+ const encrypted = Crypto.encryptKeySecret(keys);
181
176
 
182
- const decrypted = Crypto.decryptKeySecret(
183
- encrypted,
184
- encryptingWrong.secret,
185
- );
177
+ const decrypted = Crypto.decryptKeySecret(encrypted, encryptingWrong.secret);
186
178
 
187
- expect(decrypted).toBeUndefined();
179
+ expect(decrypted).toBeUndefined();
188
180
  });