@vex-chat/libvex 5.1.0 → 5.3.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 (142) hide show
  1. package/CLA.md +38 -0
  2. package/LICENSE-COMMERCIAL +10 -0
  3. package/LICENSING.md +15 -0
  4. package/README.md +8 -2
  5. package/dist/Client.d.ts +47 -3
  6. package/dist/Client.d.ts.map +1 -1
  7. package/dist/Client.js +998 -496
  8. package/dist/Client.js.map +1 -1
  9. package/dist/Storage.d.ts +5 -0
  10. package/dist/Storage.d.ts.map +1 -1
  11. package/dist/Storage.js +5 -0
  12. package/dist/Storage.js.map +1 -1
  13. package/dist/__tests__/harness/memory-storage.d.ts +7 -2
  14. package/dist/__tests__/harness/memory-storage.d.ts.map +1 -1
  15. package/dist/__tests__/harness/memory-storage.js +44 -29
  16. package/dist/__tests__/harness/memory-storage.js.map +1 -1
  17. package/dist/codec.d.ts +9 -9
  18. package/dist/codec.d.ts.map +1 -1
  19. package/dist/codec.js +17 -19
  20. package/dist/codec.js.map +1 -1
  21. package/dist/codecs.d.ts +5 -0
  22. package/dist/codecs.d.ts.map +1 -1
  23. package/dist/codecs.js +5 -0
  24. package/dist/codecs.js.map +1 -1
  25. package/dist/index.d.ts +5 -0
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +5 -0
  28. package/dist/index.js.map +1 -1
  29. package/dist/keystore/memory.d.ts +5 -0
  30. package/dist/keystore/memory.d.ts.map +1 -1
  31. package/dist/keystore/memory.js +5 -0
  32. package/dist/keystore/memory.js.map +1 -1
  33. package/dist/keystore/node.d.ts +5 -0
  34. package/dist/keystore/node.d.ts.map +1 -1
  35. package/dist/keystore/node.js +16 -8
  36. package/dist/keystore/node.js.map +1 -1
  37. package/dist/preset/common.d.ts +5 -0
  38. package/dist/preset/common.d.ts.map +1 -1
  39. package/dist/preset/common.js +5 -0
  40. package/dist/preset/common.js.map +1 -1
  41. package/dist/preset/node.d.ts +5 -0
  42. package/dist/preset/node.d.ts.map +1 -1
  43. package/dist/preset/node.js +9 -1
  44. package/dist/preset/node.js.map +1 -1
  45. package/dist/preset/test.d.ts +5 -0
  46. package/dist/preset/test.d.ts.map +1 -1
  47. package/dist/preset/test.js +9 -1
  48. package/dist/preset/test.js.map +1 -1
  49. package/dist/storage/node/http-agents.d.ts +5 -0
  50. package/dist/storage/node/http-agents.d.ts.map +1 -1
  51. package/dist/storage/node/http-agents.js +5 -0
  52. package/dist/storage/node/http-agents.js.map +1 -1
  53. package/dist/storage/node.d.ts +6 -1
  54. package/dist/storage/node.d.ts.map +1 -1
  55. package/dist/storage/node.js +7 -4
  56. package/dist/storage/node.js.map +1 -1
  57. package/dist/storage/schema.d.ts +5 -0
  58. package/dist/storage/schema.d.ts.map +1 -1
  59. package/dist/storage/schema.js +5 -0
  60. package/dist/storage/schema.js.map +1 -1
  61. package/dist/storage/sqlite.d.ts +22 -4
  62. package/dist/storage/sqlite.d.ts.map +1 -1
  63. package/dist/storage/sqlite.js +172 -98
  64. package/dist/storage/sqlite.js.map +1 -1
  65. package/dist/transport/types.d.ts +5 -0
  66. package/dist/transport/types.d.ts.map +1 -1
  67. package/dist/transport/types.js +5 -0
  68. package/dist/transport/types.js.map +1 -1
  69. package/dist/transport/websocket.d.ts +5 -0
  70. package/dist/transport/websocket.d.ts.map +1 -1
  71. package/dist/transport/websocket.js +5 -0
  72. package/dist/transport/websocket.js.map +1 -1
  73. package/dist/types/crypto.d.ts +5 -0
  74. package/dist/types/crypto.d.ts.map +1 -1
  75. package/dist/types/crypto.js +3 -5
  76. package/dist/types/crypto.js.map +1 -1
  77. package/dist/types/identity.d.ts +5 -0
  78. package/dist/types/identity.d.ts.map +1 -1
  79. package/dist/types/identity.js +3 -2
  80. package/dist/types/identity.js.map +1 -1
  81. package/dist/types/index.d.ts +5 -0
  82. package/dist/types/index.d.ts.map +1 -1
  83. package/dist/types/index.js +5 -0
  84. package/dist/types/index.js.map +1 -1
  85. package/dist/utils/capitalize.d.ts +5 -0
  86. package/dist/utils/capitalize.d.ts.map +1 -1
  87. package/dist/utils/capitalize.js +5 -0
  88. package/dist/utils/capitalize.js.map +1 -1
  89. package/dist/utils/fipsMailExtra.d.ts +30 -0
  90. package/dist/utils/fipsMailExtra.d.ts.map +1 -0
  91. package/dist/utils/fipsMailExtra.js +114 -0
  92. package/dist/utils/fipsMailExtra.js.map +1 -0
  93. package/dist/utils/formatBytes.d.ts +5 -0
  94. package/dist/utils/formatBytes.d.ts.map +1 -1
  95. package/dist/utils/formatBytes.js +5 -0
  96. package/dist/utils/formatBytes.js.map +1 -1
  97. package/dist/utils/resolveAtRestAesKey.d.ts +13 -0
  98. package/dist/utils/resolveAtRestAesKey.d.ts.map +1 -0
  99. package/dist/utils/resolveAtRestAesKey.js +26 -0
  100. package/dist/utils/resolveAtRestAesKey.js.map +1 -0
  101. package/dist/utils/sqlSessionToCrypto.d.ts +5 -0
  102. package/dist/utils/sqlSessionToCrypto.d.ts.map +1 -1
  103. package/dist/utils/sqlSessionToCrypto.js +5 -0
  104. package/dist/utils/sqlSessionToCrypto.js.map +1 -1
  105. package/dist/utils/uint8uuid.d.ts +5 -0
  106. package/dist/utils/uint8uuid.d.ts.map +1 -1
  107. package/dist/utils/uint8uuid.js +5 -0
  108. package/dist/utils/uint8uuid.js.map +1 -1
  109. package/package.json +10 -3
  110. package/src/Client.ts +1281 -642
  111. package/src/Storage.ts +6 -0
  112. package/src/__tests__/codec.test.ts +6 -0
  113. package/src/__tests__/harness/fixtures.ts +6 -0
  114. package/src/__tests__/harness/memory-storage.ts +72 -52
  115. package/src/__tests__/harness/platform-transports.ts +6 -0
  116. package/src/__tests__/harness/poison-node-imports.ts +6 -0
  117. package/src/__tests__/harness/shared-suite.ts +288 -124
  118. package/src/__tests__/platform-browser.test.ts +15 -1
  119. package/src/__tests__/platform-node.test.ts +17 -3
  120. package/src/codec.ts +21 -8
  121. package/src/codecs.ts +6 -0
  122. package/src/index.ts +6 -0
  123. package/src/keystore/memory.ts +6 -0
  124. package/src/keystore/node.ts +27 -13
  125. package/src/preset/common.ts +6 -0
  126. package/src/preset/node.ts +14 -1
  127. package/src/preset/test.ts +14 -1
  128. package/src/storage/node/http-agents.ts +6 -0
  129. package/src/storage/node.ts +11 -4
  130. package/src/storage/schema.ts +6 -0
  131. package/src/storage/sqlite.ts +208 -135
  132. package/src/transport/types.ts +6 -0
  133. package/src/transport/websocket.ts +6 -0
  134. package/src/types/crypto.ts +6 -0
  135. package/src/types/identity.ts +6 -0
  136. package/src/types/index.ts +6 -0
  137. package/src/utils/capitalize.ts +6 -0
  138. package/src/utils/fipsMailExtra.ts +164 -0
  139. package/src/utils/formatBytes.ts +6 -0
  140. package/src/utils/resolveAtRestAesKey.ts +39 -0
  141. package/src/utils/sqlSessionToCrypto.ts +6 -0
  142. package/src/utils/uint8uuid.ts +6 -0
@@ -0,0 +1,164 @@
1
+ /**
2
+ * Copyright (c) 2020-2026 Vex Heavy Industries LLC
3
+ * Licensed under AGPL-3.0. See LICENSE for details.
4
+ * Commercial licenses available at vex.wtf
5
+ */
6
+
7
+ import { xConcat } from "@vex-chat/crypto";
8
+
9
+ export const FIPS_INITIAL_EXTRA_V1: readonly [number, number, number] = [
10
+ 0xf1, 0x02, 0x01,
11
+ ] as const;
12
+
13
+ const FIPS_SUBSEQUENT_V1: readonly [number, number, number] = [
14
+ 0xf1, 0x03, 0x01,
15
+ ] as const;
16
+
17
+ function u16be(n: number): [number, number] {
18
+ if (!Number.isInteger(n) || n < 0 || n > 65535) {
19
+ throw new Error(`FIPS: invalid u16 length: ${String(n)}`);
20
+ }
21
+ return [n >> 8, n & 0xff];
22
+ }
23
+
24
+ function u16read(buf: Uint8Array, offset: number): number {
25
+ if (offset + 2 > buf.length) {
26
+ return -1;
27
+ }
28
+ return ((buf[offset] ?? 0) * 256 + (buf[offset + 1] ?? 0)) >>> 0;
29
+ }
30
+
31
+ export function isFipsInitialExtraV1(buf: Uint8Array): boolean {
32
+ return (
33
+ buf.length >= 3 &&
34
+ buf[0] === FIPS_INITIAL_EXTRA_V1[0] &&
35
+ buf[1] === FIPS_INITIAL_EXTRA_V1[1] &&
36
+ buf[2] === FIPS_INITIAL_EXTRA_V1[2]
37
+ );
38
+ }
39
+
40
+ /**
41
+ * P-256 fips (Web Crypto) initial mail `extra` bytes (version 1). Length-prefixed segments
42
+ * (variable lengths) plus a 6-byte OTK index, AD length includes both identity raw publics.
43
+ */
44
+ export function encodeFipsInitialExtraV1(
45
+ signPub: Uint8Array,
46
+ ephPub: Uint8Array,
47
+ pk: Uint8Array,
48
+ ad: Uint8Array,
49
+ index6: Uint8Array,
50
+ ): Uint8Array {
51
+ if (index6.length !== 6) {
52
+ throw new Error("FIPS: OTK index must be 6 bytes.");
53
+ }
54
+ const a = (len: number) => new Uint8Array([...u16be(len)]);
55
+ return xConcat(
56
+ Uint8Array.from([...FIPS_INITIAL_EXTRA_V1]),
57
+ a(signPub.length),
58
+ signPub,
59
+ a(ephPub.length),
60
+ ephPub,
61
+ a(pk.length),
62
+ pk,
63
+ a(ad.length),
64
+ ad,
65
+ index6,
66
+ );
67
+ }
68
+
69
+ /**
70
+ * @returns [signKey, ephKey, ad, index6] (Uint8Array slices, index is always length 6)
71
+ */
72
+ export function decodeFipsInitialExtraV1(
73
+ extra: Uint8Array,
74
+ ): [Uint8Array, Uint8Array, Uint8Array, Uint8Array] {
75
+ if (!isFipsInitialExtraV1(extra)) {
76
+ throw new Error("FIPS: not a v1 initial extra payload.");
77
+ }
78
+ let p = 3;
79
+ const readSeg = (label: string) => {
80
+ const len = u16read(extra, p);
81
+ if (len < 0) {
82
+ throw new Error(
83
+ `FIPS: extra too short (need len for ${label} at ${String(p)}).`,
84
+ );
85
+ }
86
+ p += 2;
87
+ if (p + len > extra.length) {
88
+ throw new Error(
89
+ `FIPS: truncated ${label} segment (need ${String(len + p)} but got ${String(extra.length)}).`,
90
+ );
91
+ }
92
+ const seg = extra.subarray(p, p + len);
93
+ p += len;
94
+ return seg;
95
+ };
96
+ const signKey = readSeg("sign");
97
+ const ephKey = readSeg("eph");
98
+ const _pk = readSeg("PK");
99
+ const ad = readSeg("ad");
100
+ void _pk; // not required for DH / open (matches tweetnacl extra layout)
101
+ if (p + 6 > extra.length) {
102
+ throw new Error("FIPS: missing 6-byte OTK index at end of extra.");
103
+ }
104
+ const index6 = extra.subarray(p, p + 6);
105
+ return [signKey, ephKey, ad, index6];
106
+ }
107
+
108
+ export function isFipsSubsequentExtraV1(buf: Uint8Array): boolean {
109
+ return (
110
+ buf.length >= 3 &&
111
+ buf[0] === FIPS_SUBSEQUENT_V1[0] &&
112
+ buf[1] === FIPS_SUBSEQUENT_V1[1] &&
113
+ buf[2] === FIPS_SUBSEQUENT_V1[2]
114
+ );
115
+ }
116
+
117
+ /**
118
+ * P-256 fips subsequent mail: session lookup key (variable, typically 65B uncompressed ECDH public).
119
+ */
120
+ export function encodeFipsSubsequentExtraV1(
121
+ sessionPub: Uint8Array,
122
+ ): Uint8Array {
123
+ if (sessionPub.length > 65535) {
124
+ throw new Error("FIPS: session public key is too long.");
125
+ }
126
+ return xConcat(
127
+ Uint8Array.from([...FIPS_SUBSEQUENT_V1]),
128
+ new Uint8Array([...u16be(sessionPub.length)]),
129
+ sessionPub,
130
+ );
131
+ }
132
+
133
+ export function decodeFipsSubsequentExtraV1(extra: Uint8Array): Uint8Array {
134
+ if (!isFipsSubsequentExtraV1(extra)) {
135
+ throw new Error("FIPS: not a v1 subsequent extra.");
136
+ }
137
+ const len = u16read(extra, 3);
138
+ if (len < 0) {
139
+ throw new Error("FIPS: bad subsequent extra length field.");
140
+ }
141
+ const p = 5;
142
+ if (p + len > extra.length) {
143
+ throw new Error("FIPS: subsequent extra truncated.");
144
+ }
145
+ return extra.subarray(p, p + len);
146
+ }
147
+
148
+ /** P-256: message bytes signed to bind a prekey (FIPS) — 1-byte tag + uncompressed public. */
149
+ export function fipsP256PreKeySignPayload(
150
+ preKeyRawPublic: Uint8Array,
151
+ ): Uint8Array {
152
+ return xConcat(new Uint8Array([0xa1]), preKeyRawPublic);
153
+ }
154
+
155
+ /**
156
+ * P-256 identity AD: concatenation of our and their P-256 ECDH identity publics (uncompressed, 65B each if standard).
157
+ * Used instead of `xEncode` + X25519 for FIPS.
158
+ */
159
+ export function fipsP256AdFromIdentityPubs(
160
+ ourIdentityRaw: Uint8Array,
161
+ theirIdentityFromBundle: Uint8Array,
162
+ ): Uint8Array {
163
+ return xConcat(ourIdentityRaw, theirIdentityFromBundle);
164
+ }
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Copyright (c) 2020-2026 Vex Heavy Industries LLC
3
+ * Licensed under AGPL-3.0. See LICENSE for details.
4
+ * Commercial licenses available at vex.wtf
5
+ */
6
+
1
7
  export const formatBytes = (bytes: number, decimals = 2) => {
2
8
  if (bytes === 0) {
3
9
  return "0 Bytes";
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Copyright (c) 2020-2026 Vex Heavy Industries LLC
3
+ * Licensed under AGPL-3.0. See LICENSE for details.
4
+ * Commercial licenses available at vex.wtf
5
+ */
6
+
7
+ import {
8
+ type CryptoProfile,
9
+ xEcdhKeyPairFromEcdsaKeyPairAsync,
10
+ xSignKeyPairFromSecret,
11
+ xSignKeyPairFromSecretAsync,
12
+ XKeyConvert,
13
+ XUtils,
14
+ } from "@vex-chat/crypto";
15
+
16
+ /**
17
+ * Produces the 32-byte at-rest key used by {@link Storage} (sqlite, memory) from
18
+ * a hex signing secret, matching the derivation used by {@link Client.create}.
19
+ * `setCryptoProfile(profile)` should already be in effect (or `tweetnacl` implied).
20
+ */
21
+ export async function resolveAtRestAesKeyFromSignKeyHex(
22
+ privateKeyHex: string,
23
+ profile: CryptoProfile,
24
+ ): Promise<Uint8Array> {
25
+ const dec = XUtils.decodeHex(privateKeyHex);
26
+ if (profile === "tweetnacl") {
27
+ const sign = xSignKeyPairFromSecret(dec);
28
+ const id = XKeyConvert.convertKeyPair(sign);
29
+ if (!id) {
30
+ throw new Error(
31
+ "Could not convert signing key to X25519 identity.",
32
+ );
33
+ }
34
+ return XUtils.deriveLocalAtRestAesKey(id.secretKey, "tweetnacl");
35
+ }
36
+ const sign = await xSignKeyPairFromSecretAsync(dec);
37
+ const id = await xEcdhKeyPairFromEcdsaKeyPairAsync(sign);
38
+ return XUtils.deriveLocalAtRestAesKey(id.secretKey, "fips");
39
+ }
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Copyright (c) 2020-2026 Vex Heavy Industries LLC
3
+ * Licensed under AGPL-3.0. See LICENSE for details.
4
+ * Commercial licenses available at vex.wtf
5
+ */
6
+
1
7
  import type { SessionCrypto } from "../types/index.js";
2
8
  import type { SessionSQL } from "@vex-chat/types";
3
9
 
@@ -1,3 +1,9 @@
1
+ /**
2
+ * Copyright (c) 2020-2026 Vex Heavy Industries LLC
3
+ * Licensed under AGPL-3.0. See LICENSE for details.
4
+ * Commercial licenses available at vex.wtf
5
+ */
6
+
1
7
  import { parse as uuidParse } from "uuid";
2
8
  /**
3
9
  * @ignore