cojson 0.19.21 → 0.20.0
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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +67 -0
- package/dist/CojsonMessageChannel/CojsonMessageChannel.d.ts +42 -0
- package/dist/CojsonMessageChannel/CojsonMessageChannel.d.ts.map +1 -0
- package/dist/CojsonMessageChannel/CojsonMessageChannel.js +261 -0
- package/dist/CojsonMessageChannel/CojsonMessageChannel.js.map +1 -0
- package/dist/CojsonMessageChannel/MessagePortOutgoingChannel.d.ts +18 -0
- package/dist/CojsonMessageChannel/MessagePortOutgoingChannel.d.ts.map +1 -0
- package/dist/CojsonMessageChannel/MessagePortOutgoingChannel.js +37 -0
- package/dist/CojsonMessageChannel/MessagePortOutgoingChannel.js.map +1 -0
- package/dist/CojsonMessageChannel/index.d.ts +3 -0
- package/dist/CojsonMessageChannel/index.d.ts.map +1 -0
- package/dist/CojsonMessageChannel/index.js +2 -0
- package/dist/CojsonMessageChannel/index.js.map +1 -0
- package/dist/CojsonMessageChannel/types.d.ts +149 -0
- package/dist/CojsonMessageChannel/types.d.ts.map +1 -0
- package/dist/CojsonMessageChannel/types.js +36 -0
- package/dist/CojsonMessageChannel/types.js.map +1 -0
- package/dist/GarbageCollector.d.ts +4 -2
- package/dist/GarbageCollector.d.ts.map +1 -1
- package/dist/GarbageCollector.js +5 -3
- package/dist/GarbageCollector.js.map +1 -1
- package/dist/SyncStateManager.d.ts +3 -3
- package/dist/SyncStateManager.d.ts.map +1 -1
- package/dist/SyncStateManager.js +4 -4
- package/dist/SyncStateManager.js.map +1 -1
- package/dist/coValueContentMessage.d.ts +0 -2
- package/dist/coValueContentMessage.d.ts.map +1 -1
- package/dist/coValueContentMessage.js +0 -8
- package/dist/coValueContentMessage.js.map +1 -1
- package/dist/coValueCore/SessionMap.d.ts +4 -2
- package/dist/coValueCore/SessionMap.d.ts.map +1 -1
- package/dist/coValueCore/SessionMap.js +30 -0
- package/dist/coValueCore/SessionMap.js.map +1 -1
- package/dist/coValueCore/coValueCore.d.ts +86 -4
- package/dist/coValueCore/coValueCore.d.ts.map +1 -1
- package/dist/coValueCore/coValueCore.js +318 -17
- package/dist/coValueCore/coValueCore.js.map +1 -1
- package/dist/coValueCore/verifiedState.d.ts +6 -1
- package/dist/coValueCore/verifiedState.d.ts.map +1 -1
- package/dist/coValueCore/verifiedState.js +9 -0
- package/dist/coValueCore/verifiedState.js.map +1 -1
- package/dist/coValues/coList.d.ts +3 -2
- package/dist/coValues/coList.d.ts.map +1 -1
- package/dist/coValues/coList.js.map +1 -1
- package/dist/coValues/group.d.ts.map +1 -1
- package/dist/coValues/group.js +3 -6
- package/dist/coValues/group.js.map +1 -1
- package/dist/config.d.ts +0 -6
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +0 -8
- package/dist/config.js.map +1 -1
- package/dist/crypto/NapiCrypto.d.ts +1 -2
- package/dist/crypto/NapiCrypto.d.ts.map +1 -1
- package/dist/crypto/NapiCrypto.js +19 -4
- package/dist/crypto/NapiCrypto.js.map +1 -1
- package/dist/crypto/RNCrypto.d.ts.map +1 -1
- package/dist/crypto/RNCrypto.js +19 -4
- package/dist/crypto/RNCrypto.js.map +1 -1
- package/dist/crypto/WasmCrypto.d.ts +11 -4
- package/dist/crypto/WasmCrypto.d.ts.map +1 -1
- package/dist/crypto/WasmCrypto.js +52 -10
- package/dist/crypto/WasmCrypto.js.map +1 -1
- package/dist/crypto/WasmCryptoEdge.d.ts +1 -0
- package/dist/crypto/WasmCryptoEdge.d.ts.map +1 -1
- package/dist/crypto/WasmCryptoEdge.js +4 -1
- package/dist/crypto/WasmCryptoEdge.js.map +1 -1
- package/dist/crypto/crypto.d.ts +3 -3
- package/dist/crypto/crypto.d.ts.map +1 -1
- package/dist/crypto/crypto.js +6 -1
- package/dist/crypto/crypto.js.map +1 -1
- package/dist/exports.d.ts +3 -2
- package/dist/exports.d.ts.map +1 -1
- package/dist/exports.js +3 -1
- package/dist/exports.js.map +1 -1
- package/dist/ids.d.ts +4 -1
- package/dist/ids.d.ts.map +1 -1
- package/dist/ids.js +4 -0
- package/dist/ids.js.map +1 -1
- package/dist/knownState.d.ts +2 -0
- package/dist/knownState.d.ts.map +1 -1
- package/dist/localNode.d.ts +13 -3
- package/dist/localNode.d.ts.map +1 -1
- package/dist/localNode.js +17 -2
- package/dist/localNode.js.map +1 -1
- package/dist/platformUtils.d.ts +3 -0
- package/dist/platformUtils.d.ts.map +1 -0
- package/dist/platformUtils.js +24 -0
- package/dist/platformUtils.js.map +1 -0
- package/dist/storage/DeletedCoValuesEraserScheduler.d.ts +30 -0
- package/dist/storage/DeletedCoValuesEraserScheduler.d.ts.map +1 -0
- package/dist/storage/DeletedCoValuesEraserScheduler.js +84 -0
- package/dist/storage/DeletedCoValuesEraserScheduler.js.map +1 -0
- package/dist/storage/sqlite/client.d.ts +3 -0
- package/dist/storage/sqlite/client.d.ts.map +1 -1
- package/dist/storage/sqlite/client.js +44 -0
- package/dist/storage/sqlite/client.js.map +1 -1
- package/dist/storage/sqlite/sqliteMigrations.d.ts.map +1 -1
- package/dist/storage/sqlite/sqliteMigrations.js +7 -0
- package/dist/storage/sqlite/sqliteMigrations.js.map +1 -1
- package/dist/storage/sqliteAsync/client.d.ts +3 -0
- package/dist/storage/sqliteAsync/client.d.ts.map +1 -1
- package/dist/storage/sqliteAsync/client.js +42 -0
- package/dist/storage/sqliteAsync/client.js.map +1 -1
- package/dist/storage/storageAsync.d.ts +15 -3
- package/dist/storage/storageAsync.d.ts.map +1 -1
- package/dist/storage/storageAsync.js +60 -3
- package/dist/storage/storageAsync.js.map +1 -1
- package/dist/storage/storageSync.d.ts +14 -3
- package/dist/storage/storageSync.d.ts.map +1 -1
- package/dist/storage/storageSync.js +54 -3
- package/dist/storage/storageSync.js.map +1 -1
- package/dist/storage/types.d.ts +64 -0
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/storage/types.js +12 -1
- package/dist/storage/types.js.map +1 -1
- package/dist/sync.d.ts +6 -0
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +69 -15
- package/dist/sync.js.map +1 -1
- package/dist/tests/CojsonMessageChannel.test.d.ts +2 -0
- package/dist/tests/CojsonMessageChannel.test.d.ts.map +1 -0
- package/dist/tests/CojsonMessageChannel.test.js +236 -0
- package/dist/tests/CojsonMessageChannel.test.js.map +1 -0
- package/dist/tests/DeletedCoValuesEraserScheduler.test.d.ts +2 -0
- package/dist/tests/DeletedCoValuesEraserScheduler.test.d.ts.map +1 -0
- package/dist/tests/DeletedCoValuesEraserScheduler.test.js +149 -0
- package/dist/tests/DeletedCoValuesEraserScheduler.test.js.map +1 -0
- package/dist/tests/GarbageCollector.test.js +91 -18
- package/dist/tests/GarbageCollector.test.js.map +1 -1
- package/dist/tests/StorageApiAsync.test.js +510 -146
- package/dist/tests/StorageApiAsync.test.js.map +1 -1
- package/dist/tests/StorageApiSync.test.js +531 -130
- package/dist/tests/StorageApiSync.test.js.map +1 -1
- package/dist/tests/SyncManager.processQueues.test.js +1 -1
- package/dist/tests/SyncManager.processQueues.test.js.map +1 -1
- package/dist/tests/SyncStateManager.test.js +1 -1
- package/dist/tests/SyncStateManager.test.js.map +1 -1
- package/dist/tests/WasmCrypto.test.js +6 -3
- package/dist/tests/WasmCrypto.test.js.map +1 -1
- package/dist/tests/coPlainText.test.js +1 -1
- package/dist/tests/coPlainText.test.js.map +1 -1
- package/dist/tests/coValueCore.loadFromStorage.test.js +4 -0
- package/dist/tests/coValueCore.loadFromStorage.test.js.map +1 -1
- package/dist/tests/coValueCore.test.js +34 -13
- package/dist/tests/coValueCore.test.js.map +1 -1
- package/dist/tests/coreWasm.test.js +127 -4
- package/dist/tests/coreWasm.test.js.map +1 -1
- package/dist/tests/crypto.test.js +89 -93
- package/dist/tests/crypto.test.js.map +1 -1
- package/dist/tests/deleteCoValue.test.d.ts +2 -0
- package/dist/tests/deleteCoValue.test.d.ts.map +1 -0
- package/dist/tests/deleteCoValue.test.js +313 -0
- package/dist/tests/deleteCoValue.test.js.map +1 -0
- package/dist/tests/group.removeMember.test.js +18 -30
- package/dist/tests/group.removeMember.test.js.map +1 -1
- package/dist/tests/knownState.lazyLoading.test.js +4 -0
- package/dist/tests/knownState.lazyLoading.test.js.map +1 -1
- package/dist/tests/sync.deleted.test.d.ts +2 -0
- package/dist/tests/sync.deleted.test.d.ts.map +1 -0
- package/dist/tests/sync.deleted.test.js +214 -0
- package/dist/tests/sync.deleted.test.js.map +1 -0
- package/dist/tests/sync.garbageCollection.test.js +56 -32
- package/dist/tests/sync.garbageCollection.test.js.map +1 -1
- package/dist/tests/sync.load.test.js +3 -5
- package/dist/tests/sync.load.test.js.map +1 -1
- package/dist/tests/sync.mesh.test.js +4 -3
- package/dist/tests/sync.mesh.test.js.map +1 -1
- package/dist/tests/sync.peerReconciliation.test.js +3 -3
- package/dist/tests/sync.peerReconciliation.test.js.map +1 -1
- package/dist/tests/sync.storage.test.js +12 -11
- package/dist/tests/sync.storage.test.js.map +1 -1
- package/dist/tests/sync.storageAsync.test.js +7 -7
- package/dist/tests/sync.storageAsync.test.js.map +1 -1
- package/dist/tests/sync.test.js +3 -2
- package/dist/tests/sync.test.js.map +1 -1
- package/dist/tests/sync.tracking.test.js +35 -4
- package/dist/tests/sync.tracking.test.js.map +1 -1
- package/dist/tests/testStorage.d.ts +3 -0
- package/dist/tests/testStorage.d.ts.map +1 -1
- package/dist/tests/testStorage.js +16 -2
- package/dist/tests/testStorage.js.map +1 -1
- package/dist/tests/testUtils.d.ts +29 -4
- package/dist/tests/testUtils.d.ts.map +1 -1
- package/dist/tests/testUtils.js +84 -9
- package/dist/tests/testUtils.js.map +1 -1
- package/package.json +6 -16
- package/src/CojsonMessageChannel/CojsonMessageChannel.ts +332 -0
- package/src/CojsonMessageChannel/MessagePortOutgoingChannel.ts +52 -0
- package/src/CojsonMessageChannel/index.ts +9 -0
- package/src/CojsonMessageChannel/types.ts +200 -0
- package/src/GarbageCollector.ts +5 -5
- package/src/SyncStateManager.ts +6 -6
- package/src/coValueContentMessage.ts +0 -14
- package/src/coValueCore/SessionMap.ts +43 -1
- package/src/coValueCore/coValueCore.ts +430 -15
- package/src/coValueCore/verifiedState.ts +26 -3
- package/src/coValues/coList.ts +5 -3
- package/src/coValues/group.ts +5 -6
- package/src/config.ts +0 -9
- package/src/crypto/NapiCrypto.ts +29 -13
- package/src/crypto/RNCrypto.ts +29 -11
- package/src/crypto/WasmCrypto.ts +67 -20
- package/src/crypto/WasmCryptoEdge.ts +5 -1
- package/src/crypto/crypto.ts +16 -4
- package/src/exports.ts +3 -0
- package/src/ids.ts +11 -1
- package/src/localNode.ts +18 -5
- package/src/platformUtils.ts +26 -0
- package/src/storage/DeletedCoValuesEraserScheduler.ts +124 -0
- package/src/storage/sqlite/client.ts +77 -0
- package/src/storage/sqlite/sqliteMigrations.ts +7 -0
- package/src/storage/sqliteAsync/client.ts +75 -0
- package/src/storage/storageAsync.ts +77 -4
- package/src/storage/storageSync.ts +73 -4
- package/src/storage/types.ts +75 -0
- package/src/sync.ts +84 -15
- package/src/tests/CojsonMessageChannel.test.ts +306 -0
- package/src/tests/DeletedCoValuesEraserScheduler.test.ts +185 -0
- package/src/tests/GarbageCollector.test.ts +119 -22
- package/src/tests/StorageApiAsync.test.ts +615 -156
- package/src/tests/StorageApiSync.test.ts +623 -137
- package/src/tests/SyncManager.processQueues.test.ts +1 -1
- package/src/tests/SyncStateManager.test.ts +1 -1
- package/src/tests/WasmCrypto.test.ts +8 -3
- package/src/tests/coPlainText.test.ts +1 -1
- package/src/tests/coValueCore.loadFromStorage.test.ts +8 -0
- package/src/tests/coValueCore.test.ts +49 -14
- package/src/tests/coreWasm.test.ts +319 -10
- package/src/tests/crypto.test.ts +141 -150
- package/src/tests/deleteCoValue.test.ts +528 -0
- package/src/tests/group.removeMember.test.ts +35 -35
- package/src/tests/knownState.lazyLoading.test.ts +8 -0
- package/src/tests/sync.deleted.test.ts +294 -0
- package/src/tests/sync.garbageCollection.test.ts +69 -36
- package/src/tests/sync.load.test.ts +3 -5
- package/src/tests/sync.mesh.test.ts +6 -3
- package/src/tests/sync.peerReconciliation.test.ts +3 -3
- package/src/tests/sync.storage.test.ts +14 -11
- package/src/tests/sync.storageAsync.test.ts +7 -7
- package/src/tests/sync.test.ts +5 -2
- package/src/tests/sync.tracking.test.ts +54 -4
- package/src/tests/testStorage.ts +30 -3
- package/src/tests/testUtils.ts +113 -15
- package/dist/crypto/PureJSCrypto.d.ts +0 -77
- package/dist/crypto/PureJSCrypto.d.ts.map +0 -1
- package/dist/crypto/PureJSCrypto.js +0 -236
- package/dist/crypto/PureJSCrypto.js.map +0 -1
- package/dist/tests/PureJSCrypto.test.d.ts +0 -2
- package/dist/tests/PureJSCrypto.test.d.ts.map +0 -1
- package/dist/tests/PureJSCrypto.test.js +0 -145
- package/dist/tests/PureJSCrypto.test.js.map +0 -1
- package/src/crypto/PureJSCrypto.ts +0 -429
- package/src/tests/PureJSCrypto.test.ts +0 -217
package/src/tests/crypto.test.ts
CHANGED
|
@@ -3,176 +3,167 @@ import { x25519 } from "@noble/curves/ed25519";
|
|
|
3
3
|
import { blake3 } from "@noble/hashes/blake3";
|
|
4
4
|
import { base58, base64url } from "@scure/base";
|
|
5
5
|
import { expect, test, vi } from "vitest";
|
|
6
|
-
import { PureJSCrypto } from "../crypto/PureJSCrypto.js";
|
|
7
6
|
import { WasmCrypto } from "../crypto/WasmCrypto.js";
|
|
8
7
|
import { SessionID } from "../ids.js";
|
|
9
8
|
import { stableStringify } from "../jsonStringify.js";
|
|
10
9
|
|
|
11
|
-
const
|
|
12
|
-
const pureJSCrypto = await PureJSCrypto.create();
|
|
13
|
-
|
|
14
|
-
[wasmCrypto, pureJSCrypto].forEach((crypto) => {
|
|
15
|
-
const name = crypto.constructor.name;
|
|
16
|
-
|
|
17
|
-
test(`Signatures round-trip and use stable stringify [${name}]`, () => {
|
|
18
|
-
const data = { b: "world", a: "hello" };
|
|
19
|
-
const signer = crypto.newRandomSigner();
|
|
20
|
-
const signature = crypto.sign(signer, data);
|
|
21
|
-
|
|
22
|
-
expect(signature).toMatch(/^signature_z/);
|
|
23
|
-
expect(
|
|
24
|
-
crypto.verify(
|
|
25
|
-
signature,
|
|
26
|
-
{ a: "hello", b: "world" },
|
|
27
|
-
crypto.getSignerID(signer),
|
|
28
|
-
),
|
|
29
|
-
).toBe(true);
|
|
30
|
-
});
|
|
10
|
+
const crypto = await WasmCrypto.create();
|
|
31
11
|
|
|
32
|
-
|
|
33
|
-
const data = { b: "world", a: "hello" };
|
|
34
|
-
const signer = crypto.newRandomSigner();
|
|
35
|
-
const signer2 = crypto.newRandomSigner();
|
|
36
|
-
const wrongSignature = crypto.sign(signer2, data);
|
|
12
|
+
const name = crypto.constructor.name;
|
|
37
13
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
14
|
+
test(`Signatures round-trip and use stable stringify [${name}]`, () => {
|
|
15
|
+
const data = { b: "world", a: "hello" };
|
|
16
|
+
const signer = crypto.newRandomSigner();
|
|
17
|
+
const signature = crypto.sign(signer, data);
|
|
42
18
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
} as const;
|
|
53
|
-
|
|
54
|
-
const sealed = crypto.seal({
|
|
55
|
-
message: data,
|
|
56
|
-
from: sender,
|
|
57
|
-
to: crypto.getSealerID(sealer),
|
|
58
|
-
nOnceMaterial,
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
expect(
|
|
62
|
-
crypto.unseal(sealed, sealer, crypto.getSealerID(sender), nOnceMaterial),
|
|
63
|
-
).toEqual(data);
|
|
64
|
-
expect(() =>
|
|
65
|
-
crypto.unseal(
|
|
66
|
-
sealed,
|
|
67
|
-
wrongSealer,
|
|
68
|
-
crypto.getSealerID(sender),
|
|
69
|
-
nOnceMaterial,
|
|
70
|
-
),
|
|
71
|
-
).toThrow(name === "PureJSCrypto" ? "invalid tag" : "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 _ = xsalsa20poly1305(sharedSecret, nOnce).decrypt(sealedBytes);
|
|
88
|
-
}).toThrow("invalid tag");
|
|
89
|
-
});
|
|
19
|
+
expect(signature).toMatch(/^signature_z/);
|
|
20
|
+
expect(
|
|
21
|
+
crypto.verify(
|
|
22
|
+
signature,
|
|
23
|
+
{ a: "hello", b: "world" },
|
|
24
|
+
crypto.getSignerID(signer),
|
|
25
|
+
),
|
|
26
|
+
).toBe(true);
|
|
27
|
+
});
|
|
90
28
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
29
|
+
test(`Invalid signatures don't verify [${name}]`, () => {
|
|
30
|
+
const data = { b: "world", a: "hello" };
|
|
31
|
+
const signer = crypto.newRandomSigner();
|
|
32
|
+
const signer2 = crypto.newRandomSigner();
|
|
33
|
+
const wrongSignature = crypto.sign(signer2, data);
|
|
95
34
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
35
|
+
expect(crypto.verify(wrongSignature, data, crypto.getSignerID(signer))).toBe(
|
|
36
|
+
false,
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test(`encrypting round-trips, but invalid receiver can't unseal [${name}]`, () => {
|
|
41
|
+
const data = { b: "world", a: "hello" };
|
|
42
|
+
const sender = crypto.newRandomSealer();
|
|
43
|
+
const sealer = crypto.newRandomSealer();
|
|
44
|
+
const wrongSealer = crypto.newRandomSealer();
|
|
45
|
+
|
|
46
|
+
const nOnceMaterial = {
|
|
47
|
+
in: "co_zTEST",
|
|
48
|
+
tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
|
|
49
|
+
} as const;
|
|
50
|
+
|
|
51
|
+
const sealed = crypto.seal({
|
|
52
|
+
message: data,
|
|
53
|
+
from: sender,
|
|
54
|
+
to: crypto.getSealerID(sealer),
|
|
55
|
+
nOnceMaterial,
|
|
99
56
|
});
|
|
100
57
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
58
|
+
expect(
|
|
59
|
+
crypto.unseal(sealed, sealer, crypto.getSealerID(sender), nOnceMaterial),
|
|
60
|
+
).toEqual(data);
|
|
61
|
+
expect(() =>
|
|
62
|
+
crypto.unseal(
|
|
63
|
+
sealed,
|
|
64
|
+
wrongSealer,
|
|
65
|
+
crypto.getSealerID(sender),
|
|
66
|
+
nOnceMaterial,
|
|
67
|
+
),
|
|
68
|
+
).toThrow("Wrong tag");
|
|
69
|
+
|
|
70
|
+
// trying with wrong sealer secret, by hand
|
|
71
|
+
const nOnce = blake3(
|
|
72
|
+
new TextEncoder().encode(stableStringify(nOnceMaterial)),
|
|
73
|
+
).slice(0, 24);
|
|
74
|
+
const sealer3priv = base58.decode(
|
|
75
|
+
wrongSealer.substring("sealerSecret_z".length),
|
|
76
|
+
);
|
|
77
|
+
const senderPub = base58.decode(
|
|
78
|
+
crypto.getSealerID(sender).substring("sealer_z".length),
|
|
79
|
+
);
|
|
80
|
+
const sealedBytes = base64url.decode(sealed.substring("sealed_U".length));
|
|
81
|
+
const sharedSecret = x25519.getSharedSecret(sealer3priv, senderPub);
|
|
82
|
+
|
|
83
|
+
expect(() => {
|
|
84
|
+
const _ = xsalsa20poly1305(sharedSecret, nOnce).decrypt(sealedBytes);
|
|
85
|
+
}).toThrow("invalid tag");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test(`Hashing is deterministic [${name}]`, () => {
|
|
89
|
+
expect(crypto.secureHash({ b: "world", a: "hello" })).toEqual(
|
|
90
|
+
crypto.secureHash({ a: "hello", b: "world" }),
|
|
91
|
+
);
|
|
104
92
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
93
|
+
expect(crypto.shortHash({ b: "world", a: "hello" })).toEqual(
|
|
94
|
+
crypto.shortHash({ a: "hello", b: "world" }),
|
|
95
|
+
);
|
|
96
|
+
});
|
|
109
97
|
|
|
110
|
-
|
|
98
|
+
test(`Encryption of keySecrets round-trips [${name}]`, () => {
|
|
99
|
+
const toEncrypt = crypto.newRandomKeySecret();
|
|
100
|
+
const encrypting = crypto.newRandomKeySecret();
|
|
111
101
|
|
|
112
|
-
|
|
102
|
+
const keys = {
|
|
103
|
+
toEncrypt,
|
|
104
|
+
encrypting,
|
|
105
|
+
};
|
|
113
106
|
|
|
114
|
-
|
|
115
|
-
});
|
|
107
|
+
const encrypted = crypto.encryptKeySecret(keys);
|
|
116
108
|
|
|
117
|
-
|
|
118
|
-
const toEncrypt = crypto.newRandomKeySecret();
|
|
119
|
-
const encrypting = crypto.newRandomKeySecret();
|
|
120
|
-
const encryptingWrong = crypto.newRandomKeySecret();
|
|
109
|
+
const decrypted = crypto.decryptKeySecret(encrypted, encrypting.secret);
|
|
121
110
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
encrypting,
|
|
125
|
-
};
|
|
111
|
+
expect(decrypted).toEqual(toEncrypt.secret);
|
|
112
|
+
});
|
|
126
113
|
|
|
127
|
-
|
|
114
|
+
test(`Encryption of keySecrets doesn't decrypt with a wrong key [${name}]`, () => {
|
|
115
|
+
const toEncrypt = crypto.newRandomKeySecret();
|
|
116
|
+
const encrypting = crypto.newRandomKeySecret();
|
|
117
|
+
const encryptingWrong = crypto.newRandomKeySecret();
|
|
128
118
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
119
|
+
const keys = {
|
|
120
|
+
toEncrypt,
|
|
121
|
+
encrypting,
|
|
122
|
+
};
|
|
133
123
|
|
|
134
|
-
|
|
135
|
-
});
|
|
124
|
+
const encrypted = crypto.encryptKeySecret(keys);
|
|
136
125
|
|
|
137
|
-
|
|
138
|
-
const data = "not valid json";
|
|
139
|
-
const sender = crypto.newRandomSealer();
|
|
140
|
-
const sealer = crypto.newRandomSealer();
|
|
141
|
-
|
|
142
|
-
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
143
|
-
|
|
144
|
-
const nOnceMaterial = {
|
|
145
|
-
in: "co_zTEST",
|
|
146
|
-
tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
|
|
147
|
-
} as const;
|
|
148
|
-
|
|
149
|
-
// Create a sealed message with invalid JSON
|
|
150
|
-
const nOnce = blake3(
|
|
151
|
-
new TextEncoder().encode(stableStringify(nOnceMaterial)),
|
|
152
|
-
).slice(0, 24);
|
|
153
|
-
|
|
154
|
-
const senderPriv = base58.decode(sender.substring("sealerSecret_z".length));
|
|
155
|
-
const sealerPub = base58.decode(
|
|
156
|
-
crypto.getSealerID(sealer).substring("sealer_z".length),
|
|
157
|
-
);
|
|
158
|
-
|
|
159
|
-
const plaintext = new TextEncoder().encode(data);
|
|
160
|
-
const sharedSecret = x25519.getSharedSecret(senderPriv, sealerPub);
|
|
161
|
-
const sealedBytes = xsalsa20poly1305(sharedSecret, nOnce).encrypt(
|
|
162
|
-
plaintext,
|
|
163
|
-
);
|
|
164
|
-
const sealed = `sealed_U${base64url.encode(sealedBytes)}`;
|
|
165
|
-
|
|
166
|
-
const result = crypto.unseal(
|
|
167
|
-
sealed as any,
|
|
168
|
-
sealer,
|
|
169
|
-
crypto.getSealerID(sender),
|
|
170
|
-
nOnceMaterial,
|
|
171
|
-
);
|
|
126
|
+
const decrypted = crypto.decryptKeySecret(encrypted, encryptingWrong.secret);
|
|
172
127
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
128
|
+
expect(decrypted).toBeUndefined();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test(`Unsealing malformed JSON logs error [${name}]`, () => {
|
|
132
|
+
const data = "not valid json";
|
|
133
|
+
const sender = crypto.newRandomSealer();
|
|
134
|
+
const sealer = crypto.newRandomSealer();
|
|
135
|
+
|
|
136
|
+
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
137
|
+
|
|
138
|
+
const nOnceMaterial = {
|
|
139
|
+
in: "co_zTEST",
|
|
140
|
+
tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
|
|
141
|
+
} as const;
|
|
142
|
+
|
|
143
|
+
// Create a sealed message with invalid JSON
|
|
144
|
+
const nOnce = blake3(
|
|
145
|
+
new TextEncoder().encode(stableStringify(nOnceMaterial)),
|
|
146
|
+
).slice(0, 24);
|
|
147
|
+
|
|
148
|
+
const senderPriv = base58.decode(sender.substring("sealerSecret_z".length));
|
|
149
|
+
const sealerPub = base58.decode(
|
|
150
|
+
crypto.getSealerID(sealer).substring("sealer_z".length),
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
const plaintext = new TextEncoder().encode(data);
|
|
154
|
+
const sharedSecret = x25519.getSharedSecret(senderPriv, sealerPub);
|
|
155
|
+
const sealedBytes = xsalsa20poly1305(sharedSecret, nOnce).encrypt(plaintext);
|
|
156
|
+
const sealed = `sealed_U${base64url.encode(sealedBytes)}`;
|
|
157
|
+
|
|
158
|
+
const result = crypto.unseal(
|
|
159
|
+
sealed as any,
|
|
160
|
+
sealer,
|
|
161
|
+
crypto.getSealerID(sender),
|
|
162
|
+
nOnceMaterial,
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
expect(result).toBeUndefined();
|
|
166
|
+
expect(consoleSpy.mock.lastCall?.[0]).toContain(
|
|
167
|
+
"Failed to decrypt/parse sealed message",
|
|
168
|
+
);
|
|
178
169
|
});
|