cojson 0.18.12 → 0.18.14

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 (71) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +17 -0
  3. package/dist/coValueCore/branching.d.ts +5 -0
  4. package/dist/coValueCore/branching.d.ts.map +1 -1
  5. package/dist/coValueCore/branching.js +26 -8
  6. package/dist/coValueCore/branching.js.map +1 -1
  7. package/dist/coValueCore/coValueCore.d.ts +3 -6
  8. package/dist/coValueCore/coValueCore.d.ts.map +1 -1
  9. package/dist/coValueCore/coValueCore.js +22 -17
  10. package/dist/coValueCore/coValueCore.js.map +1 -1
  11. package/dist/coValues/account.d.ts +0 -5
  12. package/dist/coValues/account.d.ts.map +1 -1
  13. package/dist/coValues/account.js +0 -20
  14. package/dist/coValues/account.js.map +1 -1
  15. package/dist/crypto/PureJSCrypto.d.ts +0 -5
  16. package/dist/crypto/PureJSCrypto.d.ts.map +1 -1
  17. package/dist/crypto/PureJSCrypto.js +13 -30
  18. package/dist/crypto/PureJSCrypto.js.map +1 -1
  19. package/dist/crypto/WasmCrypto.d.ts +0 -4
  20. package/dist/crypto/WasmCrypto.d.ts.map +1 -1
  21. package/dist/crypto/WasmCrypto.js +1 -14
  22. package/dist/crypto/WasmCrypto.js.map +1 -1
  23. package/dist/crypto/crypto.d.ts +1 -39
  24. package/dist/crypto/crypto.d.ts.map +1 -1
  25. package/dist/crypto/crypto.js +11 -53
  26. package/dist/crypto/crypto.js.map +1 -1
  27. package/dist/exports.d.ts +3 -2
  28. package/dist/exports.d.ts.map +1 -1
  29. package/dist/exports.js +3 -2
  30. package/dist/exports.js.map +1 -1
  31. package/dist/knownState.d.ts +3 -0
  32. package/dist/knownState.d.ts.map +1 -0
  33. package/dist/knownState.js +8 -0
  34. package/dist/knownState.js.map +1 -0
  35. package/dist/localNode.d.ts.map +1 -1
  36. package/dist/localNode.js +4 -0
  37. package/dist/localNode.js.map +1 -1
  38. package/dist/sync.d.ts.map +1 -1
  39. package/dist/sync.js +8 -1
  40. package/dist/sync.js.map +1 -1
  41. package/dist/tests/branching.test.js +36 -29
  42. package/dist/tests/branching.test.js.map +1 -1
  43. package/dist/tests/coValueCore.test.js +31 -4
  44. package/dist/tests/coValueCore.test.js.map +1 -1
  45. package/dist/tests/coValueCoreLoadingState.test.js +15 -5
  46. package/dist/tests/coValueCoreLoadingState.test.js.map +1 -1
  47. package/dist/tests/crypto.test.js +0 -41
  48. package/dist/tests/crypto.test.js.map +1 -1
  49. package/dist/tests/sync.upload.test.js +20 -1
  50. package/dist/tests/sync.upload.test.js.map +1 -1
  51. package/package.json +2 -2
  52. package/src/coValueCore/branching.ts +37 -16
  53. package/src/coValueCore/coValueCore.ts +30 -21
  54. package/src/coValues/account.ts +0 -25
  55. package/src/crypto/PureJSCrypto.ts +14 -43
  56. package/src/crypto/WasmCrypto.ts +0 -19
  57. package/src/crypto/crypto.ts +12 -94
  58. package/src/exports.ts +2 -2
  59. package/src/knownState.ts +17 -0
  60. package/src/localNode.ts +5 -0
  61. package/src/sync.ts +11 -2
  62. package/src/tests/branching.test.ts +45 -34
  63. package/src/tests/coValueCore.test.ts +40 -4
  64. package/src/tests/coValueCoreLoadingState.test.ts +17 -5
  65. package/src/tests/crypto.test.ts +0 -53
  66. package/src/tests/sync.upload.test.ts +28 -1
  67. package/dist/tests/cryptoImpl.test.d.ts +0 -2
  68. package/dist/tests/cryptoImpl.test.d.ts.map +0 -1
  69. package/dist/tests/cryptoImpl.test.js +0 -144
  70. package/dist/tests/cryptoImpl.test.js.map +0 -1
  71. package/src/tests/cryptoImpl.test.ts +0 -213
@@ -98,59 +98,6 @@ const pureJSCrypto = await PureJSCrypto.create();
98
98
  );
99
99
  });
100
100
 
101
- test(`Encryption for transactions round-trips [${name}]`, () => {
102
- const { secret } = crypto.newRandomKeySecret();
103
-
104
- const encrypted1 = crypto.encryptForTransaction({ a: "hello" }, secret, {
105
- in: "co_zTEST",
106
- tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
107
- });
108
-
109
- const encrypted2 = crypto.encryptForTransaction({ b: "world" }, secret, {
110
- in: "co_zTEST",
111
- tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 1 },
112
- });
113
-
114
- const decrypted1 = crypto.decryptForTransaction(encrypted1, secret, {
115
- in: "co_zTEST",
116
- tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
117
- });
118
-
119
- const decrypted2 = crypto.decryptForTransaction(encrypted2, secret, {
120
- in: "co_zTEST",
121
- tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 1 },
122
- });
123
-
124
- expect([decrypted1, decrypted2]).toEqual([{ a: "hello" }, { b: "world" }]);
125
- });
126
-
127
- test(`Encryption for transactions doesn't decrypt with a wrong key [${name}]`, () => {
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]);
152
- });
153
-
154
101
  test(`Encryption of keySecrets round-trips [${name}]`, () => {
155
102
  const toEncrypt = crypto.newRandomKeySecret();
156
103
  const encrypting = crypto.newRandomKeySecret();
@@ -1,4 +1,4 @@
1
- import { beforeEach, describe, expect, test } from "vitest";
1
+ import { assert, beforeEach, describe, expect, test } from "vitest";
2
2
 
3
3
  import { expectList, expectMap } from "../coValue";
4
4
  import { WasmCrypto } from "../crypto/WasmCrypto";
@@ -10,6 +10,7 @@ import {
10
10
  waitFor,
11
11
  } from "./testUtils";
12
12
  import { determineValidTransactions } from "../permissions";
13
+ import { RawCoMap } from "../coValues/coMap";
13
14
 
14
15
  // We want to simulate a real world communication that happens asynchronously
15
16
  TEST_NODE_CONFIG.withAsyncPeers = true;
@@ -445,4 +446,30 @@ describe("client to server upload", () => {
445
446
  ]
446
447
  `);
447
448
  });
449
+
450
+ test("uploading a coValue with a non-matching header", async () => {
451
+ const client = setupTestNode({
452
+ connected: false,
453
+ });
454
+
455
+ const coValue = client.node.createCoValue({
456
+ type: "comap",
457
+ ruleset: { type: "unsafeAllowAll" },
458
+ meta: null,
459
+ ...Crypto.createdNowUnique(),
460
+ });
461
+
462
+ const map = coValue.getCurrentContent() as RawCoMap;
463
+ map.set("hello", "world", "trusting");
464
+
465
+ const content = map.core.verified.newContentSince(undefined)?.[0];
466
+ assert(content);
467
+
468
+ // Simulate a mismatch between the header and the coValue id
469
+ content.id = "co_ztest123";
470
+
471
+ jazzCloud.node.syncManager.handleNewContent(content, "import");
472
+
473
+ expect(jazzCloud.node.getCoValue(content.id).isAvailable()).toBe(false);
474
+ });
448
475
  });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=cryptoImpl.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"cryptoImpl.test.d.ts","sourceRoot":"","sources":["../../src/tests/cryptoImpl.test.ts"],"names":[],"mappings":""}
@@ -1,144 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
- import { PureJSCrypto } from "../crypto/PureJSCrypto.js";
3
- import { WasmCrypto } from "../crypto/WasmCrypto.js";
4
- import { StreamingHash } from "../crypto/crypto.js";
5
- describe.each([
6
- { impl: await WasmCrypto.create(), name: "Wasm" },
7
- { impl: await PureJSCrypto.create(), name: "PureJS" },
8
- ])("$name implementation", ({ impl, name }) => {
9
- test("randomBytes", () => {
10
- expect(impl.randomBytes(32).length).toEqual(32);
11
- });
12
- test("blake3HashOnce", () => {
13
- const data = new Uint8Array([1, 2, 3, 4, 5]);
14
- const hash = impl.blake3HashOnce(data);
15
- // prettier-ignore
16
- expect(hash).toEqual(new Uint8Array([
17
- 2, 79, 103, 192, 66, 90, 61, 192, 47, 186, 245, 140, 185, 61, 229, 19,
18
- 46, 61, 117, 197, 25, 250, 160, 186, 218, 33, 73, 29, 136, 201, 112, 87,
19
- ]));
20
- });
21
- test("blake3HashOnceWithContext", () => {
22
- const data = new Uint8Array([1, 2, 3, 4, 5]);
23
- const context = new Uint8Array([6, 7, 8, 9, 10]);
24
- const hash = impl.blake3HashOnceWithContext(data, { context });
25
- // prettier-ignore
26
- expect(hash).toEqual(new Uint8Array([
27
- 26, 197, 20, 5, 159, 115, 36, 109, 188, 32, 237, 183, 252, 248, 89, 48,
28
- 212, 51, 102, 180, 94, 56, 201, 33, 196, 52, 222, 121, 103, 112, 153,
29
- 98,
30
- ]));
31
- });
32
- test("incrementalBlake3", () => {
33
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
- const state = impl.emptyBlake3State();
35
- const data = new Uint8Array([1, 2, 3, 4, 5]);
36
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
- const state2 = impl.blake3IncrementalUpdate(state, data);
38
- expect(impl.blake3DigestForState(state2)).toEqual(
39
- // prettier-ignore
40
- new Uint8Array([
41
- 2, 79, 103, 192, 66, 90, 61, 192, 47, 186, 245, 140, 185, 61, 229, 19,
42
- 46, 61, 117, 197, 25, 250, 160, 186, 218, 33, 73, 29, 136, 201, 112, 87,
43
- ]));
44
- const data2 = new Uint8Array([6, 7, 8, 9, 10]);
45
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
- const state3 = impl.blake3IncrementalUpdate(state2, data2);
47
- expect(impl.blake3DigestForState(state3)).toEqual(
48
- // prettier-ignore
49
- new Uint8Array([
50
- 165, 131, 141, 69, 2, 69, 39, 236, 196, 244, 180, 213, 147, 124, 222,
51
- 39, 68, 223, 54, 176, 242, 97, 200, 101, 204, 79, 21, 233, 56, 51, 1,
52
- 199,
53
- ]));
54
- });
55
- test("newEd25519SigningKey", () => {
56
- expect(impl.newEd25519SigningKey().length).toEqual(32);
57
- });
58
- test("getSignerID", () => {
59
- const secret = impl.signerSecretFromBytes(new Uint8Array(Array.from({ length: 32 }, (_, i) => i * 2)));
60
- const id = impl.getSignerID(secret);
61
- expect(id).toEqual("signer_z8wDhwtSA2twZvmz8J3Df63TdCBxC7ta9hFASV9cZC9xw");
62
- });
63
- test("sign and verify", () => {
64
- const secret = impl.signerSecretFromBytes(new Uint8Array(Array.from({ length: 32 }, (_, i) => i * 2)));
65
- const wrongSecret = impl.signerSecretFromBytes(new Uint8Array(Array.from({ length: 32 }, (_, i) => (i === 0 ? 1 : i * 2))));
66
- const message = { foo: "bar" };
67
- const wrongMessage = { foo: "baz" };
68
- const signature = impl.sign(secret, message);
69
- const wrongSignature = impl.sign(wrongSecret, message);
70
- expect(signature).toEqual("signature_zHH7trJJ4iJFgYv9B8QtF96qcG7PbPJcfojy9ACtJc6z9FqzbJNM6xeqVLxCEK1oSj1oSYAgy2V3CW5SzjYDh8ax");
71
- expect(impl.verify(signature, message, impl.getSignerID(secret))).toBeTruthy();
72
- expect(impl.verify(wrongSignature, message, impl.getSignerID(secret))).toBeFalsy();
73
- expect(impl.verify(signature, wrongMessage, impl.getSignerID(secret))).toBeFalsy();
74
- });
75
- test("newX25519StaticSecret", () => {
76
- expect(impl.newX25519StaticSecret().length).toEqual(32);
77
- });
78
- test("getSealerID", () => {
79
- const secret = impl.sealerSecretFromBytes(new Uint8Array(Array.from({ length: 32 }, (_, i) => i * 2)));
80
- const id = impl.getSealerID(secret);
81
- expect(id).toEqual("sealer_zBvzQipsgJZwgy4rN5dfnWQWQNrV4PnJpWfDjZkJ5iYqy");
82
- });
83
- test("encrypt and decrypt", () => {
84
- const secret = "keySecret_zyRhQCZGM3LwEjB2nXTktyBQ56JiDBHbRtYjjddiugjT";
85
- const data = { foo: "bar" };
86
- const nOnceMaterial = { bar: "foo" };
87
- const encrypted = impl.encrypt(data, secret, nOnceMaterial);
88
- expect(encrypted).toEqual("encrypted_UDrVbWTATlj-ECtLqJQ==");
89
- const decrypted = impl.decrypt(encrypted, secret, nOnceMaterial);
90
- expect(decrypted).toEqual(data);
91
- const wrongSecret = "keySecret_zyRhQCZGM3LwEjB2nXTktyBQ56JiDBHbRtYjjddingjT";
92
- expect(impl.decrypt(encrypted, wrongSecret, nOnceMaterial)).toBeUndefined();
93
- const wrongNOnceMaterial = { bar: "baz" };
94
- expect(impl.decrypt(encrypted, secret, wrongNOnceMaterial)).toBeUndefined();
95
- });
96
- test("seal and unseal", () => {
97
- const senderSecret = "sealerSecret_zFffKAY7Ln5ouAbmC6K21N6uPs1RQXyhpcuQzK3kPWhhg";
98
- const recipientSecret = "sealerSecret_z3K6m9AHmEJeTrCQW4zY5vicneorj9sVQasA7E8FYvDQa";
99
- const message = { foo: "bar" };
100
- const nOnceMaterial = {
101
- in: "co_zSomeCoValue",
102
- tx: {
103
- sessionID: "co_zSomeAccount_session_zSomeSession",
104
- txIndex: 42,
105
- },
106
- };
107
- const sealed = impl.seal({
108
- message,
109
- from: senderSecret,
110
- to: impl.getSealerID(recipientSecret),
111
- nOnceMaterial,
112
- });
113
- expect(sealed).toEqual("sealed_UtuddAQjop6zMR47aI7HOqj-f0kR2tdFvRxzDe4I=");
114
- const unsealed = impl.unseal(sealed, recipientSecret, impl.getSealerID(senderSecret), nOnceMaterial);
115
- expect(unsealed).toEqual(message);
116
- const wrongRecipientSecret = "sealerSecret_zHV1Y1VPbc31B8bi7yw4oL5CPPnPFspKwUjkFErgJFuoB";
117
- expect(() => impl.unseal(sealed, wrongRecipientSecret, impl.getSealerID(senderSecret), nOnceMaterial)).toThrow();
118
- const wrongNOnceMaterial = {
119
- in: "co_zSomeCoValue",
120
- tx: {
121
- sessionID: "co_zSomeAccount_session_zSomeSession",
122
- txIndex: 43,
123
- },
124
- };
125
- expect(() => impl.unseal(sealed, recipientSecret, impl.getSealerID(senderSecret), wrongNOnceMaterial)).toThrow();
126
- });
127
- test("StreamingHash clone", () => {
128
- const originalHash = new StreamingHash(impl);
129
- originalHash.update({ foo: "bar" });
130
- const clonedHash = originalHash.clone();
131
- // Update the original hash
132
- originalHash.update({ baz: "qux" });
133
- // Update the cloned hash differently
134
- clonedHash.update({ quux: "corge" });
135
- // The digests should be different
136
- expect(originalHash.digest()).not.toEqual(clonedHash.digest());
137
- // The cloned hash should match a new hash with the same updates
138
- const newHash = new StreamingHash(impl);
139
- newHash.update({ foo: "bar" });
140
- newHash.update({ quux: "corge" });
141
- expect(clonedHash.digest()).toEqual(newHash.digest());
142
- });
143
- });
144
- //# sourceMappingURL=cryptoImpl.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"cryptoImpl.test.js","sourceRoot":"","sources":["../../src/tests/cryptoImpl.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAa,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAG/D,QAAQ,CAAC,IAAI,CAAC;IACZ,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;IACjD,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;CACtD,CAAC,CAAC,sBAAsB,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,EAA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AC,MAAM,EAAE,CAAC,CAAC;QAE/D,gEAAgE;QAChE,MAAM,OAAO,GAAG,IAAI,aAAa,CAAC,IAAI,CAAC,CAAC;QACxC,OAAO,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC;QAC/B,OAAO,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;QAClC,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IACxD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -1,213 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
- import { PureJSCrypto } from "../crypto/PureJSCrypto.js";
3
- import { WasmCrypto } from "../crypto/WasmCrypto.js";
4
- import { KeySecret, StreamingHash } from "../crypto/crypto.js";
5
- import { SessionID } from "../ids.js";
6
-
7
- describe.each([
8
- { impl: await WasmCrypto.create(), name: "Wasm" },
9
- { impl: await PureJSCrypto.create(), name: "PureJS" },
10
- ])("$name implementation", ({ impl, name }) => {
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(
20
- new Uint8Array([
21
- 2, 79, 103, 192, 66, 90, 61, 192, 47, 186, 245, 140, 185, 61, 229, 19,
22
- 46, 61, 117, 197, 25, 250, 160, 186, 218, 33, 73, 29, 136, 201, 112, 87,
23
- ]),
24
- );
25
- });
26
-
27
- test("blake3HashOnceWithContext", () => {
28
- const data = new Uint8Array([1, 2, 3, 4, 5]);
29
- const context = new Uint8Array([6, 7, 8, 9, 10]);
30
- const hash = impl.blake3HashOnceWithContext(data, { context });
31
- // prettier-ignore
32
- expect(hash).toEqual(
33
- new Uint8Array([
34
- 26, 197, 20, 5, 159, 115, 36, 109, 188, 32, 237, 183, 252, 248, 89, 48,
35
- 212, 51, 102, 180, 94, 56, 201, 33, 196, 52, 222, 121, 103, 112, 153,
36
- 98,
37
- ]),
38
- );
39
- });
40
-
41
- test("incrementalBlake3", () => {
42
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
- const state = impl.emptyBlake3State() as any;
44
- const data = new Uint8Array([1, 2, 3, 4, 5]);
45
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
- const state2 = impl.blake3IncrementalUpdate(state, data) as any;
47
- expect(impl.blake3DigestForState(state2)).toEqual(
48
- // prettier-ignore
49
- new Uint8Array([
50
- 2, 79, 103, 192, 66, 90, 61, 192, 47, 186, 245, 140, 185, 61, 229, 19,
51
- 46, 61, 117, 197, 25, 250, 160, 186, 218, 33, 73, 29, 136, 201, 112, 87,
52
- ]),
53
- );
54
- const data2 = new Uint8Array([6, 7, 8, 9, 10]);
55
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
- const state3 = impl.blake3IncrementalUpdate(state2, data2) as any;
57
- expect(impl.blake3DigestForState(state3)).toEqual(
58
- // prettier-ignore
59
- new Uint8Array([
60
- 165, 131, 141, 69, 2, 69, 39, 236, 196, 244, 180, 213, 147, 124, 222,
61
- 39, 68, 223, 54, 176, 242, 97, 200, 101, 204, 79, 21, 233, 56, 51, 1,
62
- 199,
63
- ]),
64
- );
65
- });
66
-
67
- test("newEd25519SigningKey", () => {
68
- expect(impl.newEd25519SigningKey().length).toEqual(32);
69
- });
70
-
71
- test("getSignerID", () => {
72
- const secret = impl.signerSecretFromBytes(
73
- new Uint8Array(Array.from({ length: 32 }, (_, i) => i * 2)),
74
- );
75
- const id = impl.getSignerID(secret);
76
- expect(id).toEqual("signer_z8wDhwtSA2twZvmz8J3Df63TdCBxC7ta9hFASV9cZC9xw");
77
- });
78
-
79
- test("sign and verify", () => {
80
- const secret = impl.signerSecretFromBytes(
81
- new Uint8Array(Array.from({ length: 32 }, (_, i) => i * 2)),
82
- );
83
- const wrongSecret = impl.signerSecretFromBytes(
84
- new Uint8Array(
85
- Array.from({ length: 32 }, (_, i) => (i === 0 ? 1 : i * 2)),
86
- ),
87
- );
88
- const message = { foo: "bar" };
89
- const wrongMessage = { foo: "baz" };
90
- const signature = impl.sign(secret, message);
91
- const wrongSignature = impl.sign(wrongSecret, message);
92
- expect(signature).toEqual(
93
- "signature_zHH7trJJ4iJFgYv9B8QtF96qcG7PbPJcfojy9ACtJc6z9FqzbJNM6xeqVLxCEK1oSj1oSYAgy2V3CW5SzjYDh8ax",
94
- );
95
- expect(
96
- impl.verify(signature, message, impl.getSignerID(secret)),
97
- ).toBeTruthy();
98
- expect(
99
- impl.verify(wrongSignature, message, impl.getSignerID(secret)),
100
- ).toBeFalsy();
101
- expect(
102
- impl.verify(signature, wrongMessage, impl.getSignerID(secret)),
103
- ).toBeFalsy();
104
- });
105
-
106
- test("newX25519StaticSecret", () => {
107
- expect(impl.newX25519StaticSecret().length).toEqual(32);
108
- });
109
-
110
- test("getSealerID", () => {
111
- const secret = impl.sealerSecretFromBytes(
112
- new Uint8Array(Array.from({ length: 32 }, (_, i) => i * 2)),
113
- );
114
- const id = impl.getSealerID(secret);
115
- expect(id).toEqual("sealer_zBvzQipsgJZwgy4rN5dfnWQWQNrV4PnJpWfDjZkJ5iYqy");
116
- });
117
-
118
- test("encrypt and decrypt", () => {
119
- const secret: KeySecret =
120
- "keySecret_zyRhQCZGM3LwEjB2nXTktyBQ56JiDBHbRtYjjddiugjT";
121
- const data = { foo: "bar" };
122
- const nOnceMaterial = { bar: "foo" };
123
- const encrypted = impl.encrypt(data, secret, nOnceMaterial);
124
- expect(encrypted).toEqual("encrypted_UDrVbWTATlj-ECtLqJQ==");
125
- const decrypted = impl.decrypt(encrypted, secret, nOnceMaterial);
126
- expect(decrypted).toEqual(data);
127
- const wrongSecret =
128
- "keySecret_zyRhQCZGM3LwEjB2nXTktyBQ56JiDBHbRtYjjddingjT";
129
- expect(impl.decrypt(encrypted, wrongSecret, nOnceMaterial)).toBeUndefined();
130
- const wrongNOnceMaterial = { bar: "baz" };
131
- expect(impl.decrypt(encrypted, secret, wrongNOnceMaterial)).toBeUndefined();
132
- });
133
-
134
- test("seal and unseal", () => {
135
- const senderSecret =
136
- "sealerSecret_zFffKAY7Ln5ouAbmC6K21N6uPs1RQXyhpcuQzK3kPWhhg";
137
- const recipientSecret =
138
- "sealerSecret_z3K6m9AHmEJeTrCQW4zY5vicneorj9sVQasA7E8FYvDQa";
139
- const message = { foo: "bar" };
140
- const nOnceMaterial = {
141
- in: "co_zSomeCoValue" as const,
142
- tx: {
143
- sessionID: "co_zSomeAccount_session_zSomeSession" as SessionID,
144
- txIndex: 42,
145
- },
146
- };
147
-
148
- const sealed = impl.seal({
149
- message,
150
- from: senderSecret,
151
- to: impl.getSealerID(recipientSecret),
152
- nOnceMaterial,
153
- });
154
-
155
- expect(sealed).toEqual("sealed_UtuddAQjop6zMR47aI7HOqj-f0kR2tdFvRxzDe4I=");
156
-
157
- const unsealed = impl.unseal(
158
- sealed,
159
- recipientSecret,
160
- impl.getSealerID(senderSecret),
161
- nOnceMaterial,
162
- );
163
- expect(unsealed).toEqual(message);
164
-
165
- const wrongRecipientSecret =
166
- "sealerSecret_zHV1Y1VPbc31B8bi7yw4oL5CPPnPFspKwUjkFErgJFuoB";
167
- expect(() =>
168
- impl.unseal(
169
- sealed,
170
- wrongRecipientSecret,
171
- impl.getSealerID(senderSecret),
172
- nOnceMaterial,
173
- ),
174
- ).toThrow();
175
- const wrongNOnceMaterial = {
176
- in: "co_zSomeCoValue" as const,
177
- tx: {
178
- sessionID: "co_zSomeAccount_session_zSomeSession" as SessionID,
179
- txIndex: 43,
180
- },
181
- };
182
- expect(() =>
183
- impl.unseal(
184
- sealed,
185
- recipientSecret,
186
- impl.getSealerID(senderSecret),
187
- wrongNOnceMaterial,
188
- ),
189
- ).toThrow();
190
- });
191
-
192
- test("StreamingHash clone", () => {
193
- const originalHash = new StreamingHash(impl);
194
- originalHash.update({ foo: "bar" });
195
-
196
- const clonedHash = originalHash.clone();
197
-
198
- // Update the original hash
199
- originalHash.update({ baz: "qux" });
200
-
201
- // Update the cloned hash differently
202
- clonedHash.update({ quux: "corge" });
203
-
204
- // The digests should be different
205
- expect(originalHash.digest()).not.toEqual(clonedHash.digest());
206
-
207
- // The cloned hash should match a new hash with the same updates
208
- const newHash = new StreamingHash(impl);
209
- newHash.update({ foo: "bar" });
210
- newHash.update({ quux: "corge" });
211
- expect(clonedHash.digest()).toEqual(newHash.digest());
212
- });
213
- });