cojson 0.8.11 → 0.8.16
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/CHANGELOG.md +94 -82
- package/dist/native/PeerKnownStates.js +1 -1
- package/dist/native/PeerKnownStates.js.map +1 -1
- package/dist/native/PeerState.js +4 -1
- package/dist/native/PeerState.js.map +1 -1
- package/dist/native/PriorityBasedMessageQueue.js +1 -10
- package/dist/native/PriorityBasedMessageQueue.js.map +1 -1
- package/dist/native/base64url.js.map +1 -1
- package/dist/native/base64url.test.js +1 -1
- package/dist/native/base64url.test.js.map +1 -1
- package/dist/native/coValue.js.map +1 -1
- package/dist/native/coValueCore.js +141 -149
- package/dist/native/coValueCore.js.map +1 -1
- package/dist/native/coValueState.js.map +1 -1
- package/dist/native/coValues/account.js +6 -6
- package/dist/native/coValues/account.js.map +1 -1
- package/dist/native/coValues/coList.js +2 -3
- package/dist/native/coValues/coList.js.map +1 -1
- package/dist/native/coValues/coMap.js +1 -1
- package/dist/native/coValues/coMap.js.map +1 -1
- package/dist/native/coValues/coStream.js +3 -5
- package/dist/native/coValues/coStream.js.map +1 -1
- package/dist/native/coValues/group.js +11 -11
- package/dist/native/coValues/group.js.map +1 -1
- package/dist/native/coreToCoValue.js +2 -2
- package/dist/native/coreToCoValue.js.map +1 -1
- package/dist/native/crypto/PureJSCrypto.js +4 -4
- package/dist/native/crypto/PureJSCrypto.js.map +1 -1
- package/dist/native/crypto/crypto.js.map +1 -1
- package/dist/native/exports.js +12 -12
- package/dist/native/exports.js.map +1 -1
- package/dist/native/ids.js.map +1 -1
- package/dist/native/jsonStringify.js.map +1 -1
- package/dist/native/localNode.js +6 -8
- package/dist/native/localNode.js.map +1 -1
- package/dist/native/permissions.js +4 -7
- package/dist/native/permissions.js.map +1 -1
- package/dist/native/priority.js.map +1 -1
- package/dist/native/storage/FileSystem.js.map +1 -1
- package/dist/native/storage/chunksAndKnownStates.js +2 -4
- package/dist/native/storage/chunksAndKnownStates.js.map +1 -1
- package/dist/native/storage/index.js +7 -16
- package/dist/native/storage/index.js.map +1 -1
- package/dist/native/streamUtils.js.map +1 -1
- package/dist/native/sync.js +6 -8
- package/dist/native/sync.js.map +1 -1
- package/dist/native/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
- package/dist/native/typeUtils/expectGroup.js.map +1 -1
- package/dist/native/typeUtils/isAccountID.js.map +1 -1
- package/dist/native/typeUtils/isCoValue.js +1 -1
- package/dist/native/typeUtils/isCoValue.js.map +1 -1
- package/dist/web/PeerKnownStates.js +1 -1
- package/dist/web/PeerKnownStates.js.map +1 -1
- package/dist/web/PeerState.js +4 -1
- package/dist/web/PeerState.js.map +1 -1
- package/dist/web/PriorityBasedMessageQueue.js +1 -10
- package/dist/web/PriorityBasedMessageQueue.js.map +1 -1
- package/dist/web/base64url.js.map +1 -1
- package/dist/web/base64url.test.js +1 -1
- package/dist/web/base64url.test.js.map +1 -1
- package/dist/web/coValue.js.map +1 -1
- package/dist/web/coValueCore.js +141 -149
- package/dist/web/coValueCore.js.map +1 -1
- package/dist/web/coValueState.js.map +1 -1
- package/dist/web/coValues/account.js +6 -6
- package/dist/web/coValues/account.js.map +1 -1
- package/dist/web/coValues/coList.js +2 -3
- package/dist/web/coValues/coList.js.map +1 -1
- package/dist/web/coValues/coMap.js +1 -1
- package/dist/web/coValues/coMap.js.map +1 -1
- package/dist/web/coValues/coStream.js +3 -5
- package/dist/web/coValues/coStream.js.map +1 -1
- package/dist/web/coValues/group.js +11 -11
- package/dist/web/coValues/group.js.map +1 -1
- package/dist/web/coreToCoValue.js +2 -2
- package/dist/web/coreToCoValue.js.map +1 -1
- package/dist/web/crypto/PureJSCrypto.js +4 -4
- package/dist/web/crypto/PureJSCrypto.js.map +1 -1
- package/dist/web/crypto/WasmCrypto.js +5 -5
- package/dist/web/crypto/WasmCrypto.js.map +1 -1
- package/dist/web/crypto/crypto.js.map +1 -1
- package/dist/web/exports.js +12 -12
- package/dist/web/exports.js.map +1 -1
- package/dist/web/ids.js.map +1 -1
- package/dist/web/jsonStringify.js.map +1 -1
- package/dist/web/localNode.js +6 -8
- package/dist/web/localNode.js.map +1 -1
- package/dist/web/permissions.js +4 -7
- package/dist/web/permissions.js.map +1 -1
- package/dist/web/priority.js.map +1 -1
- package/dist/web/storage/FileSystem.js.map +1 -1
- package/dist/web/storage/chunksAndKnownStates.js +2 -4
- package/dist/web/storage/chunksAndKnownStates.js.map +1 -1
- package/dist/web/storage/index.js +7 -16
- package/dist/web/storage/index.js.map +1 -1
- package/dist/web/streamUtils.js.map +1 -1
- package/dist/web/sync.js +6 -8
- package/dist/web/sync.js.map +1 -1
- package/dist/web/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
- package/dist/web/typeUtils/expectGroup.js.map +1 -1
- package/dist/web/typeUtils/isAccountID.js.map +1 -1
- package/dist/web/typeUtils/isCoValue.js +1 -1
- package/dist/web/typeUtils/isCoValue.js.map +1 -1
- package/package.json +4 -14
- package/src/PeerKnownStates.ts +91 -89
- package/src/PeerState.ts +72 -69
- package/src/PriorityBasedMessageQueue.ts +42 -49
- package/src/base64url.test.ts +24 -24
- package/src/base64url.ts +44 -45
- package/src/coValue.ts +45 -45
- package/src/coValueCore.ts +746 -785
- package/src/coValueState.ts +82 -72
- package/src/coValues/account.ts +143 -150
- package/src/coValues/coList.ts +520 -522
- package/src/coValues/coMap.ts +283 -285
- package/src/coValues/coStream.ts +320 -324
- package/src/coValues/group.ts +306 -305
- package/src/coreToCoValue.ts +28 -31
- package/src/crypto/PureJSCrypto.ts +188 -194
- package/src/crypto/WasmCrypto.ts +236 -254
- package/src/crypto/crypto.ts +302 -309
- package/src/exports.ts +116 -116
- package/src/ids.ts +9 -9
- package/src/jsonStringify.ts +46 -46
- package/src/jsonValue.ts +24 -10
- package/src/localNode.ts +635 -660
- package/src/media.ts +3 -3
- package/src/permissions.ts +272 -278
- package/src/priority.ts +21 -19
- package/src/storage/FileSystem.ts +91 -99
- package/src/storage/chunksAndKnownStates.ts +110 -115
- package/src/storage/index.ts +466 -497
- package/src/streamUtils.ts +60 -60
- package/src/sync.ts +593 -615
- package/src/tests/PeerKnownStates.test.ts +38 -34
- package/src/tests/PeerState.test.ts +101 -64
- package/src/tests/PriorityBasedMessageQueue.test.ts +91 -91
- package/src/tests/account.test.ts +59 -59
- package/src/tests/coList.test.ts +65 -65
- package/src/tests/coMap.test.ts +137 -137
- package/src/tests/coStream.test.ts +254 -257
- package/src/tests/coValueCore.test.ts +153 -156
- package/src/tests/crypto.test.ts +136 -144
- package/src/tests/cryptoImpl.test.ts +205 -197
- package/src/tests/group.test.ts +24 -24
- package/src/tests/permissions.test.ts +1306 -1371
- package/src/tests/priority.test.ts +65 -82
- package/src/tests/sync.test.ts +1300 -1291
- package/src/tests/testUtils.ts +52 -53
- package/src/typeUtils/accountOrAgentIDfromSessionID.ts +4 -4
- package/src/typeUtils/expectGroup.ts +9 -9
- package/src/typeUtils/isAccountID.ts +1 -1
- package/src/typeUtils/isCoValue.ts +9 -9
- package/tsconfig.json +4 -6
- package/tsconfig.native.json +9 -11
- package/tsconfig.web.json +4 -10
- package/.eslintrc.cjs +0 -25
- package/.prettierrc.js +0 -9
package/src/tests/crypto.test.ts
CHANGED
|
@@ -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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
).
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
);
|
|
83
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
87
|
+
expect(Crypto.secureHash({ b: "world", a: "hello" })).toEqual(
|
|
88
|
+
Crypto.secureHash({ a: "hello", b: "world" }),
|
|
89
|
+
);
|
|
95
90
|
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
97
|
+
const { secret } = Crypto.newRandomKeySecret();
|
|
103
98
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
-
|
|
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
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
-
|
|
156
|
-
|
|
150
|
+
const toEncrypt = Crypto.newRandomKeySecret();
|
|
151
|
+
const encrypting = Crypto.newRandomKeySecret();
|
|
157
152
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
153
|
+
const keys = {
|
|
154
|
+
toEncrypt,
|
|
155
|
+
encrypting,
|
|
156
|
+
};
|
|
162
157
|
|
|
163
|
-
|
|
158
|
+
const encrypted = Crypto.encryptKeySecret(keys);
|
|
164
159
|
|
|
165
|
-
|
|
160
|
+
const decrypted = Crypto.decryptKeySecret(encrypted, encrypting.secret);
|
|
166
161
|
|
|
167
|
-
|
|
162
|
+
expect(decrypted).toEqual(toEncrypt.secret);
|
|
168
163
|
});
|
|
169
164
|
|
|
170
165
|
test("Encryption of keySecrets doesn't decrypt with a wrong key", () => {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
166
|
+
const toEncrypt = Crypto.newRandomKeySecret();
|
|
167
|
+
const encrypting = Crypto.newRandomKeySecret();
|
|
168
|
+
const encryptingWrong = Crypto.newRandomKeySecret();
|
|
174
169
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
170
|
+
const keys = {
|
|
171
|
+
toEncrypt,
|
|
172
|
+
encrypting,
|
|
173
|
+
};
|
|
179
174
|
|
|
180
|
-
|
|
175
|
+
const encrypted = Crypto.encryptKeySecret(keys);
|
|
181
176
|
|
|
182
|
-
|
|
183
|
-
encrypted,
|
|
184
|
-
encryptingWrong.secret,
|
|
185
|
-
);
|
|
177
|
+
const decrypted = Crypto.decryptKeySecret(encrypted, encryptingWrong.secret);
|
|
186
178
|
|
|
187
|
-
|
|
179
|
+
expect(decrypted).toBeUndefined();
|
|
188
180
|
});
|