cojson 0.7.0-alpha.5 → 0.7.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.
Files changed (113) hide show
  1. package/.eslintrc.cjs +3 -2
  2. package/.prettierrc.js +9 -0
  3. package/.turbo/turbo-build.log +3 -30
  4. package/.turbo/turbo-lint.log +4 -0
  5. package/.turbo/turbo-test.log +1106 -0
  6. package/CHANGELOG.md +104 -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 +60 -37
  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/coList.js +1 -1
  15. package/dist/coValues/coList.js.map +1 -1
  16. package/dist/coValues/coMap.js +17 -8
  17. package/dist/coValues/coMap.js.map +1 -1
  18. package/dist/coValues/group.js +13 -14
  19. package/dist/coValues/group.js.map +1 -1
  20. package/dist/coreToCoValue.js.map +1 -1
  21. package/dist/crypto/PureJSCrypto.js +89 -0
  22. package/dist/crypto/PureJSCrypto.js.map +1 -0
  23. package/dist/crypto/WasmCrypto.js +127 -0
  24. package/dist/crypto/WasmCrypto.js.map +1 -0
  25. package/dist/crypto/crypto.js +151 -0
  26. package/dist/crypto/crypto.js.map +1 -0
  27. package/dist/ids.js +4 -2
  28. package/dist/ids.js.map +1 -1
  29. package/dist/index.js +6 -8
  30. package/dist/index.js.map +1 -1
  31. package/dist/jsonStringify.js.map +1 -1
  32. package/dist/localNode.js +41 -38
  33. package/dist/localNode.js.map +1 -1
  34. package/dist/permissions.js +6 -6
  35. package/dist/permissions.js.map +1 -1
  36. package/dist/storage/FileSystem.js +61 -0
  37. package/dist/storage/FileSystem.js.map +1 -0
  38. package/dist/storage/chunksAndKnownStates.js +97 -0
  39. package/dist/storage/chunksAndKnownStates.js.map +1 -0
  40. package/dist/storage/index.js +265 -0
  41. package/dist/storage/index.js.map +1 -0
  42. package/dist/sync.js +29 -25
  43. package/dist/sync.js.map +1 -1
  44. package/dist/tests/account.test.js +58 -0
  45. package/dist/tests/account.test.js.map +1 -0
  46. package/dist/tests/coList.test.js +76 -0
  47. package/dist/tests/coList.test.js.map +1 -0
  48. package/dist/tests/coMap.test.js +136 -0
  49. package/dist/tests/coMap.test.js.map +1 -0
  50. package/dist/tests/coStream.test.js +172 -0
  51. package/dist/tests/coStream.test.js.map +1 -0
  52. package/dist/tests/coValueCore.test.js +114 -0
  53. package/dist/tests/coValueCore.test.js.map +1 -0
  54. package/dist/tests/crypto.test.js +118 -0
  55. package/dist/tests/crypto.test.js.map +1 -0
  56. package/dist/tests/cryptoImpl.test.js +113 -0
  57. package/dist/tests/cryptoImpl.test.js.map +1 -0
  58. package/dist/tests/group.test.js +34 -0
  59. package/dist/tests/group.test.js.map +1 -0
  60. package/dist/tests/permissions.test.js +1060 -0
  61. package/dist/tests/permissions.test.js.map +1 -0
  62. package/dist/tests/sync.test.js +816 -0
  63. package/dist/tests/sync.test.js.map +1 -0
  64. package/dist/tests/testUtils.js +12 -11
  65. package/dist/tests/testUtils.js.map +1 -1
  66. package/dist/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
  67. package/dist/typeUtils/isAccountID.js.map +1 -1
  68. package/dist/typeUtils/isCoValue.js.map +1 -1
  69. package/package.json +14 -27
  70. package/src/base64url.test.ts +6 -5
  71. package/src/coValue.ts +1 -1
  72. package/src/coValueCore.ts +179 -126
  73. package/src/coValues/account.ts +30 -32
  74. package/src/coValues/coList.ts +11 -11
  75. package/src/coValues/coMap.ts +27 -17
  76. package/src/coValues/coStream.ts +17 -17
  77. package/src/coValues/group.ts +93 -109
  78. package/src/coreToCoValue.ts +5 -2
  79. package/src/crypto/PureJSCrypto.ts +200 -0
  80. package/src/crypto/WasmCrypto.ts +259 -0
  81. package/src/crypto/crypto.ts +336 -0
  82. package/src/ids.ts +8 -7
  83. package/src/index.ts +24 -24
  84. package/src/jsonStringify.ts +6 -4
  85. package/src/jsonValue.ts +2 -2
  86. package/src/localNode.ts +103 -109
  87. package/src/media.ts +3 -3
  88. package/src/permissions.ts +19 -21
  89. package/src/storage/FileSystem.ts +152 -0
  90. package/src/storage/chunksAndKnownStates.ts +139 -0
  91. package/src/storage/index.ts +479 -0
  92. package/src/streamUtils.ts +12 -12
  93. package/src/sync.ts +79 -63
  94. package/src/tests/account.test.ts +15 -15
  95. package/src/tests/coList.test.ts +94 -0
  96. package/src/tests/coMap.test.ts +162 -0
  97. package/src/tests/coStream.test.ts +246 -0
  98. package/src/tests/coValueCore.test.ts +36 -37
  99. package/src/tests/crypto.test.ts +66 -72
  100. package/src/tests/cryptoImpl.test.ts +183 -0
  101. package/src/tests/group.test.ts +16 -17
  102. package/src/tests/permissions.test.ts +269 -283
  103. package/src/tests/sync.test.ts +122 -123
  104. package/src/tests/testUtils.ts +24 -21
  105. package/src/typeUtils/accountOrAgentIDfromSessionID.ts +1 -2
  106. package/src/typeUtils/expectGroup.ts +1 -1
  107. package/src/typeUtils/isAccountID.ts +0 -1
  108. package/src/typeUtils/isCoValue.ts +1 -2
  109. package/tsconfig.json +0 -1
  110. package/dist/crypto.js +0 -254
  111. package/dist/crypto.js.map +0 -1
  112. package/src/crypto.ts +0 -484
  113. package/src/tests/coValue.test.ts +0 -497
@@ -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
+ });
@@ -1,19 +1,18 @@
1
- import { expect, test, beforeEach } from "vitest";
2
- import { LocalNode, RawCoMap, RawCoList, RawCoStream, RawBinaryCoStream, cojsonReady } from "../index";
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
- import { webcrypto } from "node:crypto";
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
+ });