cojson 0.7.0-alpha.37 → 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 +1 -34
- package/.turbo/turbo-lint.log +4 -0
- package/.turbo/turbo-test.log +1106 -0
- package/CHANGELOG.md +6 -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 +4 -8
- 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 +11 -19
- 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 +41 -37
- 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
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { KeySecret } from "../crypto/crypto.js";
|
|
2
|
+
import { WasmCrypto } from "../crypto/WasmCrypto.js";
|
|
3
|
+
import { PureJSCrypto } from "../crypto/PureJSCrypto.js";
|
|
4
|
+
import { describe, test, expect } from "vitest";
|
|
5
|
+
import { SessionID } from "../index.js";
|
|
6
|
+
|
|
7
|
+
describe.each([
|
|
8
|
+
{ impl: await WasmCrypto.create(), name: "Wasm" },
|
|
9
|
+
{ impl: await PureJSCrypto.create(), name: "PureJS" },
|
|
10
|
+
])("Crypto $name", ({ impl }) => {
|
|
11
|
+
test("randomBytes", () => {
|
|
12
|
+
expect(impl.randomBytes(32).length).toEqual(32);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("blake3HashOnce", () => {
|
|
16
|
+
const data = new Uint8Array([1, 2, 3, 4, 5]);
|
|
17
|
+
const hash = impl.blake3HashOnce(data);
|
|
18
|
+
// prettier-ignore
|
|
19
|
+
expect(hash).toEqual(new Uint8Array([2,79,103,192,66,90,61,192,47,186,245,140,185,61,229,19,46,61,117,197,25,250,160,186,218,33,73,29,136,201,112,87]));
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("blake3HashOnceWithContext", () => {
|
|
23
|
+
const data = new Uint8Array([1, 2, 3, 4, 5]);
|
|
24
|
+
const context = new Uint8Array([6, 7, 8, 9, 10]);
|
|
25
|
+
const hash = impl.blake3HashOnceWithContext(data, { context });
|
|
26
|
+
// prettier-ignore
|
|
27
|
+
expect(hash).toEqual(new Uint8Array([26,197,20,5,159,115,36,109,188,32,237,183,252,248,89,48,212,51,102,180,94,56,201,33,196,52,222,121,103,112,153,98]));
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("incrementalBlake3", () => {
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
+
const state = impl.emptyBlake3State() as any;
|
|
33
|
+
const data = new Uint8Array([1, 2, 3, 4, 5]);
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
|
+
const state2 = impl.blake3IncrementalUpdate(state, data) as any;
|
|
36
|
+
expect(impl.blake3DigestForState(state2)).toEqual(
|
|
37
|
+
// prettier-ignore
|
|
38
|
+
new Uint8Array([2,79,103,192,66,90,61,192,47,186,245,140,185,61,229,19,46,61,117,197,25,250,160,186,218,33,73,29,136,201,112,87]),
|
|
39
|
+
);
|
|
40
|
+
const data2 = new Uint8Array([6, 7, 8, 9, 10]);
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
42
|
+
const state3 = impl.blake3IncrementalUpdate(state2, data2) as any;
|
|
43
|
+
expect(impl.blake3DigestForState(state3)).toEqual(
|
|
44
|
+
// prettier-ignore
|
|
45
|
+
new Uint8Array([165,131,141,69,2,69,39,236,196,244,180,213,147,124,222,39,68,223,54,176,242,97,200,101,204,79,21,233,56,51,1,199]),
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("newEd25519SigningKey", () => {
|
|
50
|
+
expect(impl.newEd25519SigningKey().length).toEqual(32);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("getSignerID", () => {
|
|
54
|
+
const secret = impl.signerSecretFromBytes(
|
|
55
|
+
new Uint8Array(Array.from({ length: 32 }, (_, i) => i * 2)),
|
|
56
|
+
);
|
|
57
|
+
const id = impl.getSignerID(secret);
|
|
58
|
+
expect(id).toEqual(
|
|
59
|
+
"signer_z8wDhwtSA2twZvmz8J3Df63TdCBxC7ta9hFASV9cZC9xw",
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("sign and verify", () => {
|
|
64
|
+
const secret = impl.signerSecretFromBytes(
|
|
65
|
+
new Uint8Array(Array.from({ length: 32 }, (_, i) => i * 2)),
|
|
66
|
+
);
|
|
67
|
+
const wrongSecret = impl.signerSecretFromBytes(
|
|
68
|
+
new Uint8Array(
|
|
69
|
+
Array.from({ length: 32 }, (_, i) => (i === 0 ? 1 : i * 2)),
|
|
70
|
+
),
|
|
71
|
+
);
|
|
72
|
+
const message = { foo: "bar" };
|
|
73
|
+
const wrongMessage = { foo: "baz" };
|
|
74
|
+
const signature = impl.sign(secret, message);
|
|
75
|
+
const wrongSignature = impl.sign(wrongSecret, message);
|
|
76
|
+
expect(signature).toEqual(
|
|
77
|
+
"signature_zHH7trJJ4iJFgYv9B8QtF96qcG7PbPJcfojy9ACtJc6z9FqzbJNM6xeqVLxCEK1oSj1oSYAgy2V3CW5SzjYDh8ax",
|
|
78
|
+
);
|
|
79
|
+
expect(
|
|
80
|
+
impl.verify(signature, message, impl.getSignerID(secret)),
|
|
81
|
+
).toBeTruthy();
|
|
82
|
+
expect(
|
|
83
|
+
impl.verify(wrongSignature, message, impl.getSignerID(secret)),
|
|
84
|
+
).toBeFalsy();
|
|
85
|
+
expect(
|
|
86
|
+
impl.verify(signature, wrongMessage, impl.getSignerID(secret)),
|
|
87
|
+
).toBeFalsy();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("newX25519StaticSecret", () => {
|
|
91
|
+
expect(impl.newX25519StaticSecret().length).toEqual(32);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("getSealerID", () => {
|
|
95
|
+
const secret = impl.sealerSecretFromBytes(
|
|
96
|
+
new Uint8Array(Array.from({ length: 32 }, (_, i) => i * 2)),
|
|
97
|
+
);
|
|
98
|
+
const id = impl.getSealerID(secret);
|
|
99
|
+
expect(id).toEqual(
|
|
100
|
+
"sealer_zBvzQipsgJZwgy4rN5dfnWQWQNrV4PnJpWfDjZkJ5iYqy",
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("encrypt and decrypt", () => {
|
|
105
|
+
const secret: KeySecret =
|
|
106
|
+
"keySecret_zyRhQCZGM3LwEjB2nXTktyBQ56JiDBHbRtYjjddiugjT";
|
|
107
|
+
const data = { foo: "bar" };
|
|
108
|
+
const nOnceMaterial = { bar: "foo" };
|
|
109
|
+
const encrypted = impl.encrypt(data, secret, nOnceMaterial);
|
|
110
|
+
expect(encrypted).toEqual("encrypted_UDrVbWTATlj-ECtLqJQ==");
|
|
111
|
+
const decrypted = impl.decrypt(encrypted, secret, nOnceMaterial);
|
|
112
|
+
expect(decrypted).toEqual(data);
|
|
113
|
+
const wrongSecret =
|
|
114
|
+
"keySecret_zyRhQCZGM3LwEjB2nXTktyBQ56JiDBHbRtYjjddingjT";
|
|
115
|
+
expect(
|
|
116
|
+
impl.decrypt(encrypted, wrongSecret, nOnceMaterial),
|
|
117
|
+
).toBeUndefined();
|
|
118
|
+
const wrongNOnceMaterial = { bar: "baz" };
|
|
119
|
+
expect(
|
|
120
|
+
impl.decrypt(encrypted, secret, wrongNOnceMaterial),
|
|
121
|
+
).toBeUndefined();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("seal and unseal", () => {
|
|
125
|
+
const senderSecret =
|
|
126
|
+
"sealerSecret_zFffKAY7Ln5ouAbmC6K21N6uPs1RQXyhpcuQzK3kPWhhg";
|
|
127
|
+
const recipientSecret =
|
|
128
|
+
"sealerSecret_z3K6m9AHmEJeTrCQW4zY5vicneorj9sVQasA7E8FYvDQa";
|
|
129
|
+
const message = { foo: "bar" };
|
|
130
|
+
const nOnceMaterial = {
|
|
131
|
+
in: "co_zSomeCoValue" as const,
|
|
132
|
+
tx: {
|
|
133
|
+
sessionID: "co_zSomeAccount_session_zSomeSession" as SessionID,
|
|
134
|
+
txIndex: 42,
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const sealed = impl.seal({
|
|
139
|
+
message,
|
|
140
|
+
from: senderSecret,
|
|
141
|
+
to: impl.getSealerID(recipientSecret),
|
|
142
|
+
nOnceMaterial,
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
expect(sealed).toEqual(
|
|
146
|
+
"sealed_UtuddAQjop6zMR47aI7HOqj-f0kR2tdFvRxzDe4I=",
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
const unsealed = impl.unseal(
|
|
150
|
+
sealed,
|
|
151
|
+
recipientSecret,
|
|
152
|
+
impl.getSealerID(senderSecret),
|
|
153
|
+
nOnceMaterial,
|
|
154
|
+
);
|
|
155
|
+
expect(unsealed).toEqual(message);
|
|
156
|
+
|
|
157
|
+
const wrongRecipientSecret =
|
|
158
|
+
"sealerSecret_zHV1Y1VPbc31B8bi7yw4oL5CPPnPFspKwUjkFErgJFuoB";
|
|
159
|
+
expect(() =>
|
|
160
|
+
impl.unseal(
|
|
161
|
+
sealed,
|
|
162
|
+
wrongRecipientSecret,
|
|
163
|
+
impl.getSealerID(senderSecret),
|
|
164
|
+
nOnceMaterial,
|
|
165
|
+
),
|
|
166
|
+
).toThrow();
|
|
167
|
+
const wrongNOnceMaterial = {
|
|
168
|
+
in: "co_zSomeCoValue" as const,
|
|
169
|
+
tx: {
|
|
170
|
+
sessionID: "co_zSomeAccount_session_zSomeSession" as SessionID,
|
|
171
|
+
txIndex: 43,
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
expect(() =>
|
|
175
|
+
impl.unseal(
|
|
176
|
+
sealed,
|
|
177
|
+
recipientSecret,
|
|
178
|
+
impl.getSealerID(senderSecret),
|
|
179
|
+
wrongNOnceMaterial,
|
|
180
|
+
),
|
|
181
|
+
).toThrow();
|
|
182
|
+
});
|
|
183
|
+
});
|
package/src/tests/group.test.ts
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import { expect, test
|
|
2
|
-
import {
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
LocalNode,
|
|
4
|
+
RawCoMap,
|
|
5
|
+
RawCoList,
|
|
6
|
+
RawCoStream,
|
|
7
|
+
RawBinaryCoStream,
|
|
8
|
+
WasmCrypto,
|
|
9
|
+
} from "../index.js";
|
|
3
10
|
import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
|
|
4
11
|
|
|
5
|
-
|
|
6
|
-
if (!("crypto" in globalThis)) {
|
|
7
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
|
-
(globalThis as any).crypto = webcrypto;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
beforeEach(async () => {
|
|
12
|
-
await cojsonReady;
|
|
13
|
-
});
|
|
12
|
+
const Crypto = await WasmCrypto.create();
|
|
14
13
|
|
|
15
14
|
test("Can create a RawCoMap in a group", () => {
|
|
16
|
-
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
|
|
15
|
+
const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
|
|
17
16
|
|
|
18
17
|
const group = node.createGroup();
|
|
19
18
|
|
|
@@ -24,7 +23,7 @@ test("Can create a RawCoMap in a group", () => {
|
|
|
24
23
|
});
|
|
25
24
|
|
|
26
25
|
test("Can create a CoList in a group", () => {
|
|
27
|
-
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
|
|
26
|
+
const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
|
|
28
27
|
|
|
29
28
|
const group = node.createGroup();
|
|
30
29
|
|
|
@@ -32,10 +31,10 @@ test("Can create a CoList in a group", () => {
|
|
|
32
31
|
|
|
33
32
|
expect(list.core.getCurrentContent().type).toEqual("colist");
|
|
34
33
|
expect(list instanceof RawCoList).toEqual(true);
|
|
35
|
-
})
|
|
34
|
+
});
|
|
36
35
|
|
|
37
36
|
test("Can create a CoStream in a group", () => {
|
|
38
|
-
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
|
|
37
|
+
const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
|
|
39
38
|
|
|
40
39
|
const group = node.createGroup();
|
|
41
40
|
|
|
@@ -46,7 +45,7 @@ test("Can create a CoStream in a group", () => {
|
|
|
46
45
|
});
|
|
47
46
|
|
|
48
47
|
test("Can create a BinaryCoStream in a group", () => {
|
|
49
|
-
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
|
|
48
|
+
const node = new LocalNode(...randomAnonymousAccountAndSessionID(), Crypto);
|
|
50
49
|
|
|
51
50
|
const group = node.createGroup();
|
|
52
51
|
|
|
@@ -55,4 +54,4 @@ test("Can create a BinaryCoStream in a group", () => {
|
|
|
55
54
|
expect(stream.core.getCurrentContent().type).toEqual("costream");
|
|
56
55
|
expect(stream.headerMeta.type).toEqual("binary");
|
|
57
56
|
expect(stream instanceof RawBinaryCoStream).toEqual(true);
|
|
58
|
-
})
|
|
57
|
+
});
|