toolroll 0.9.128 → 0.9.130

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 (129) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/dist/accent-colors.js +3 -1
  3. package/dist/agent-fence.js +0 -2
  4. package/dist/agents-board.js +0 -3
  5. package/dist/approval-authority.js +2 -1
  6. package/dist/assignment.js +56 -2
  7. package/dist/backup-ui.js +6 -3
  8. package/dist/backup.js +6 -0
  9. package/dist/bounded-spawn.js +0 -1
  10. package/dist/browser/manifest.json +57 -0
  11. package/dist/browser/terminal.2a682d0b91a35288271c73b7ee253e524cca0917d17fbaf6733e80b7a767a37c.js.br +0 -0
  12. package/dist/browser/terminal.2a682d0b91a35288271c73b7ee253e524cca0917d17fbaf6733e80b7a767a37c.js.gz +0 -0
  13. package/dist/browser/{terminal.css → terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css} +1 -1
  14. package/dist/browser/terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css.br +0 -0
  15. package/dist/browser/terminal.4d1cf20c32ad7c2099df657358d540d5fd92bb55195a88121e74b28467238b42.css.gz +0 -0
  16. package/dist/browser/{workspace.js → workspace.9bd1db81d3c897045a5d82d4804e374e41e78b1b2cfd05d37b982a4ea0d02626.js} +25 -25
  17. package/dist/browser/workspace.9bd1db81d3c897045a5d82d4804e374e41e78b1b2cfd05d37b982a4ea0d02626.js.br +0 -0
  18. package/dist/browser/workspace.9bd1db81d3c897045a5d82d4804e374e41e78b1b2cfd05d37b982a4ea0d02626.js.gz +0 -0
  19. package/dist/browser/{workspace.css → workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css} +1 -1
  20. package/dist/browser/workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css.br +0 -0
  21. package/dist/browser/workspace.a0752f935cfeaf16758d1085fe8f3e1719a7cd525b91a9c9943c789d65879a54.css.gz +0 -0
  22. package/dist/browser-shell.js +271 -25
  23. package/dist/canvas-freshness.js +122 -0
  24. package/dist/canvas-fs.js +273 -0
  25. package/dist/canvas-reads.js +747 -0
  26. package/dist/canvas-state-key.js +45 -0
  27. package/dist/canvas-state-worker.js +12 -0
  28. package/dist/central-recovery-stage.js +3 -3
  29. package/dist/chat-actions.js +26 -0
  30. package/dist/chat-channel.js +4 -3
  31. package/dist/chat-task-actions.js +1 -1
  32. package/dist/chat-warmth.js +0 -1
  33. package/dist/check-report.js +0 -6
  34. package/dist/commit-extract.js +79 -40
  35. package/dist/companion/access.js +162 -0
  36. package/dist/companion/actions.js +622 -0
  37. package/dist/companion/bonjour.js +45 -0
  38. package/dist/companion/canvas.js +141 -0
  39. package/dist/companion/chat-origin.js +92 -0
  40. package/dist/companion/chat.js +571 -0
  41. package/dist/companion/daemon.js +1387 -0
  42. package/dist/companion/direct-server.js +177 -0
  43. package/dist/companion/finish.js +51 -0
  44. package/dist/companion/framing.js +498 -0
  45. package/dist/companion/noise.js +461 -0
  46. package/dist/companion/passkey-gate.js +287 -0
  47. package/dist/companion/paths.js +90 -0
  48. package/dist/companion/reads.js +283 -0
  49. package/dist/companion/relay-auth.js +265 -0
  50. package/dist/companion/relay-server.js +870 -0
  51. package/dist/companion/schema.js +289 -0
  52. package/dist/companion/service.js +581 -0
  53. package/dist/companion/store.js +325 -0
  54. package/dist/companion/webauthn.js +407 -0
  55. package/dist/console-instance.js +0 -4
  56. package/dist/contracts/console-api.js +18 -0
  57. package/dist/converse.js +0 -1
  58. package/dist/coordinator.js +1 -1
  59. package/dist/design/kit.css +30 -0
  60. package/dist/desktop-config.js +156 -0
  61. package/dist/desktop-host.js +6 -152
  62. package/dist/desktop-update.js +1 -1
  63. package/dist/finished-task-sql.js +14 -0
  64. package/dist/flexible-answers.js +0 -1
  65. package/dist/github-read.js +41 -0
  66. package/dist/guides.js +4 -3
  67. package/dist/lead-doors.js +50 -3
  68. package/dist/lead-queue-runtime.js +11 -2
  69. package/dist/lead-queue.js +25 -0
  70. package/dist/live-backup-change.js +93 -0
  71. package/dist/live-backup.js +75 -31
  72. package/dist/live-bus.js +3 -2
  73. package/dist/live-records.js +0 -1
  74. package/dist/local-landing.js +47 -0
  75. package/dist/local-owner.js +0 -1
  76. package/dist/model-choices.js +0 -11
  77. package/dist/nightly-check.js +0 -3
  78. package/dist/nightly-github.js +1 -2
  79. package/dist/notification-retention.js +119 -0
  80. package/dist/observations.js +0 -1
  81. package/dist/operate.js +17 -4
  82. package/dist/person-proof.js +5 -1
  83. package/dist/pull-request-flow.js +116 -2
  84. package/dist/qr.js +360 -0
  85. package/dist/release-adapters.js +14 -43
  86. package/dist/release-auto.js +17 -13
  87. package/dist/release-card.js +0 -4
  88. package/dist/release-cli.js +37 -2
  89. package/dist/release.js +13 -2
  90. package/dist/restore-retirement.js +6 -1
  91. package/dist/result-review.js +0 -3
  92. package/dist/retention-ui.js +1 -1
  93. package/dist/retention.js +14 -21
  94. package/dist/review-context.js +0 -5
  95. package/dist/route-rules.js +1 -5
  96. package/dist/sandbox-runtime.js +0 -1
  97. package/dist/scope.js +68 -17
  98. package/dist/scout.js +0 -6
  99. package/dist/serve.js +60 -9
  100. package/dist/server/kit.js +1 -0
  101. package/dist/server/live.js +9 -2
  102. package/dist/server/pages.js +12 -4
  103. package/dist/server/phone-engine-api.js +79 -0
  104. package/dist/server/render-pages.js +1 -11
  105. package/dist/server/render-results.js +0 -1
  106. package/dist/server/render-tasks.js +0 -68
  107. package/dist/server/route-table.js +12 -1
  108. package/dist/server/settings-phone.js +515 -0
  109. package/dist/server/settings-scripts.js +2 -0
  110. package/dist/server/settings-section-table.js +0 -3
  111. package/dist/server/settings-sections.js +10 -4
  112. package/dist/server/settings.js +20 -5
  113. package/dist/server/terminals.js +2 -1
  114. package/dist/settings-groups-ui.js +2 -1
  115. package/dist/settings-proofs.js +6 -0
  116. package/dist/store.js +52 -4
  117. package/dist/structured-output.js +0 -1
  118. package/dist/style-asset.js +20 -7
  119. package/dist/tailnet.js +39 -0
  120. package/dist/task-control.js +5 -0
  121. package/dist/task-sizing.js +0 -1
  122. package/dist/toolroll-update-cli.js +19 -3
  123. package/dist/toolroll-update-ui.js +2 -2
  124. package/dist/toolroll-update.js +98 -16
  125. package/dist/youiyoux-intake.js +8 -0
  126. package/package.json +7 -2
  127. package/dist/contracts/contract-test.js +0 -55
  128. package/dist/unfenced-test-support.js +0 -6
  129. /package/dist/browser/{terminal.js → terminal.2a682d0b91a35288271c73b7ee253e524cca0917d17fbaf6733e80b7a767a37c.js} +0 -0
@@ -0,0 +1,461 @@
1
+ import { createCipheriv, createDecipheriv, createHash, createHmac, createPrivateKey, createPublicKey, diffieHellman, generateKeyPairSync, hkdfSync, randomBytes, timingSafeEqual, } from "node:crypto";
2
+ export const NOISE_PROTOCOL_NAME = "Noise_IKpsk2_25519_ChaChaPoly_SHA256";
3
+ export const MAX_NOISE_MESSAGE_BYTES = 65_535;
4
+ export const AEAD_TAG_BYTES = 16;
5
+ export const MAX_NOISE_PLAINTEXT_BYTES = MAX_NOISE_MESSAGE_BYTES - AEAD_TAG_BYTES;
6
+ export const RESERVED_MAX_NONCE = 0xffffffffffffffffn;
7
+ export const MAX_SESSION_MESSAGES = 1n << 20n;
8
+ export const MAX_SESSION_AGE_MS = 24 * 60 * 60 * 1000;
9
+ const X25519_PKCS8_PREFIX = Buffer.from("302e020100300506032b656e04220420", "hex");
10
+ const X25519_SPKI_PREFIX = Buffer.from("302a300506032b656e032100", "hex");
11
+ const ZERO_32 = Buffer.alloc(32);
12
+ export function generateX25519KeyPair() {
13
+ const { privateKey, publicKey } = generateKeyPairSync("x25519");
14
+ const privDer = privateKey.export({ format: "der", type: "pkcs8" });
15
+ const pubDer = publicKey.export({ format: "der", type: "spki" });
16
+ return {
17
+ privateKey: Buffer.from(privDer.subarray(privDer.length - 32)),
18
+ publicKey: Buffer.from(pubDer.subarray(pubDer.length - 32)),
19
+ };
20
+ }
21
+ export function x25519PublicFromPrivate(privateKey) {
22
+ assertLen(privateKey, 32, "x25519 privateKey");
23
+ const privObj = x25519PrivateKeyObject(privateKey);
24
+ const pubObj = createPublicKey(privObj);
25
+ const pubDer = pubObj.export({ format: "der", type: "spki" });
26
+ return Buffer.from(pubDer.subarray(pubDer.length - 32));
27
+ }
28
+ function x25519PrivateKeyObject(raw) {
29
+ assertLen(raw, 32, "x25519 privateKey");
30
+ return createPrivateKey({
31
+ key: Buffer.concat([X25519_PKCS8_PREFIX, raw]),
32
+ format: "der",
33
+ type: "pkcs8",
34
+ });
35
+ }
36
+ function x25519PublicKeyObject(raw) {
37
+ assertLen(raw, 32, "x25519 publicKey");
38
+ return createPublicKey({
39
+ key: Buffer.concat([X25519_SPKI_PREFIX, raw]),
40
+ format: "der",
41
+ type: "spki",
42
+ });
43
+ }
44
+ function x25519Dh(privateKey, publicKey) {
45
+ const secret = diffieHellman({
46
+ privateKey: x25519PrivateKeyObject(privateKey),
47
+ publicKey: x25519PublicKeyObject(publicKey),
48
+ });
49
+ if (timingSafeEqual(secret, ZERO_32)) {
50
+ throw new Error("X25519 shared secret is all zeros (low-order public key)");
51
+ }
52
+ return secret;
53
+ }
54
+ function assertLen(buf, expected, label) {
55
+ if (buf.length !== expected) {
56
+ throw new Error(`${label} must be ${expected} bytes (got ${buf.length})`);
57
+ }
58
+ }
59
+ function sha256(data) {
60
+ return createHash("sha256").update(data).digest();
61
+ }
62
+ function hmacSha256(key, data) {
63
+ return createHmac("sha256", key).update(data).digest();
64
+ }
65
+ function noiseHkdf(ck, ikm, outputs) {
66
+ const tempKey = hmacSha256(ck, ikm);
67
+ const out1 = hmacSha256(tempKey, Buffer.from([0x01]));
68
+ const out2 = hmacSha256(tempKey, Buffer.concat([out1, Buffer.from([0x02])]));
69
+ if (outputs === 2)
70
+ return [out1, out2];
71
+ const out3 = hmacSha256(tempKey, Buffer.concat([out2, Buffer.from([0x03])]));
72
+ return [out1, out2, out3];
73
+ }
74
+ export function encodeNoiseNonce(n) {
75
+ if (n < 0n || n >= RESERVED_MAX_NONCE) {
76
+ throw new Error(`Noise nonce out of range or reserved (2^64 - 1): ${n.toString()}`);
77
+ }
78
+ const nonce = Buffer.alloc(12);
79
+ nonce.writeBigUInt64LE(n, 4);
80
+ return nonce;
81
+ }
82
+ function chachaEncrypt(key, nonce, ad, plaintext) {
83
+ if (plaintext.length > MAX_NOISE_PLAINTEXT_BYTES) {
84
+ throw new Error(`Noise plaintext exceeds ${MAX_NOISE_PLAINTEXT_BYTES} bytes`);
85
+ }
86
+ const iv = encodeNoiseNonce(nonce);
87
+ const cipher = createCipheriv("chacha20-poly1305", key, iv, { authTagLength: AEAD_TAG_BYTES });
88
+ cipher.setAAD(ad);
89
+ const ct = cipher.update(plaintext);
90
+ const fin = cipher.final();
91
+ const tag = cipher.getAuthTag();
92
+ return Buffer.concat([ct, fin, tag]);
93
+ }
94
+ function chachaDecrypt(key, nonce, ad, ciphertext) {
95
+ if (ciphertext.length < AEAD_TAG_BYTES || ciphertext.length > MAX_NOISE_MESSAGE_BYTES) {
96
+ throw new Error(`Noise ciphertext length out of bounds: ${ciphertext.length}`);
97
+ }
98
+ const iv = encodeNoiseNonce(nonce);
99
+ const body = ciphertext.subarray(0, ciphertext.length - AEAD_TAG_BYTES);
100
+ const tag = ciphertext.subarray(ciphertext.length - AEAD_TAG_BYTES);
101
+ const decipher = createDecipheriv("chacha20-poly1305", key, iv, { authTagLength: AEAD_TAG_BYTES });
102
+ decipher.setAAD(ad);
103
+ decipher.setAuthTag(tag);
104
+ return Buffer.concat([decipher.update(body), decipher.final()]);
105
+ }
106
+ export class NoiseCipherState {
107
+ key;
108
+ n;
109
+ maxMessages;
110
+ constructor(key = null, maxMessages = MAX_SESSION_MESSAGES) {
111
+ if (key !== null)
112
+ assertLen(key, 32, "cipher key");
113
+ this.key = key === null ? null : Buffer.from(key);
114
+ this.n = 0n;
115
+ this.maxMessages = maxMessages;
116
+ }
117
+ hasKey() {
118
+ return this.key !== null;
119
+ }
120
+ get nonce() {
121
+ return this.n;
122
+ }
123
+ initializeKey(key) {
124
+ assertLen(key, 32, "cipher key");
125
+ this.key = Buffer.from(key);
126
+ this.n = 0n;
127
+ }
128
+ encryptWithAd(ad, plaintext, enforceSessionCap = false) {
129
+ if (this.key === null)
130
+ return Buffer.from(plaintext);
131
+ if (this.n >= RESERVED_MAX_NONCE) {
132
+ throw new Error("Noise cipher reached reserved nonce 2^64 - 1");
133
+ }
134
+ if (enforceSessionCap && this.n >= this.maxMessages) {
135
+ throw new Error("Noise session message limit reached — re-handshake required");
136
+ }
137
+ const out = chachaEncrypt(this.key, this.n, ad, plaintext);
138
+ this.n += 1n;
139
+ return out;
140
+ }
141
+ decryptWithAd(ad, ciphertext, enforceSessionCap = false) {
142
+ if (this.key === null)
143
+ return Buffer.from(ciphertext);
144
+ if (this.n >= RESERVED_MAX_NONCE) {
145
+ throw new Error("Noise cipher reached reserved nonce 2^64 - 1");
146
+ }
147
+ if (enforceSessionCap && this.n >= this.maxMessages) {
148
+ throw new Error("Noise session message limit reached — re-handshake required");
149
+ }
150
+ const out = chachaDecrypt(this.key, this.n, ad, ciphertext);
151
+ this.n += 1n;
152
+ return out;
153
+ }
154
+ encryptExplicitNonce(nonce, ad, plaintext) {
155
+ if (this.key === null)
156
+ throw new Error("CipherState has no key");
157
+ return chachaEncrypt(this.key, nonce, ad, plaintext);
158
+ }
159
+ decryptExplicitNonce(nonce, ad, ciphertext) {
160
+ if (this.key === null)
161
+ throw new Error("CipherState has no key");
162
+ return chachaDecrypt(this.key, nonce, ad, ciphertext);
163
+ }
164
+ }
165
+ class SymmetricState {
166
+ cipher = new NoiseCipherState();
167
+ ck;
168
+ h;
169
+ constructor(protocolName) {
170
+ const nameBytes = Buffer.from(protocolName, "ascii");
171
+ this.h = nameBytes.length <= 32
172
+ ? Buffer.concat([nameBytes, Buffer.alloc(32 - nameBytes.length)])
173
+ : sha256(nameBytes);
174
+ this.ck = Buffer.from(this.h);
175
+ }
176
+ mixHash(data) {
177
+ this.h = sha256(Buffer.concat([this.h, data]));
178
+ }
179
+ mixKey(ikm) {
180
+ const [ck, tempK] = noiseHkdf(this.ck, ikm, 2);
181
+ this.ck = ck;
182
+ this.cipher.initializeKey(tempK);
183
+ }
184
+ mixKeyAndHash(ikm) {
185
+ const [ck, tempH, tempK] = noiseHkdf(this.ck, ikm, 3);
186
+ this.ck = ck;
187
+ this.mixHash(tempH);
188
+ this.cipher.initializeKey(tempK);
189
+ }
190
+ encryptAndHash(plaintext) {
191
+ const ct = this.cipher.encryptWithAd(this.h, plaintext);
192
+ this.mixHash(ct);
193
+ return ct;
194
+ }
195
+ decryptAndHash(ciphertext) {
196
+ const pt = this.cipher.decryptWithAd(this.h, ciphertext);
197
+ this.mixHash(ciphertext);
198
+ return pt;
199
+ }
200
+ split() {
201
+ const [k1, k2] = noiseHkdf(this.ck, Buffer.alloc(0), 2);
202
+ return [new NoiseCipherState(k1), new NoiseCipherState(k2)];
203
+ }
204
+ }
205
+ export class NoiseTransportSession {
206
+ handshakeHash;
207
+ remoteStaticPublicKey;
208
+ sendCipher;
209
+ recvCipher;
210
+ createdAtMs;
211
+ closed = false;
212
+ constructor(params) {
213
+ this.handshakeHash = Buffer.from(params.handshakeHash);
214
+ this.remoteStaticPublicKey = Buffer.from(params.remoteStaticPublicKey);
215
+ this.sendCipher = params.sendCipher;
216
+ this.recvCipher = params.recvCipher;
217
+ this.createdAtMs = params.createdAtMs ?? Date.now();
218
+ }
219
+ isExpired(nowMs = Date.now()) {
220
+ return this.closed
221
+ || nowMs - this.createdAtMs >= MAX_SESSION_AGE_MS
222
+ || this.sendCipher.nonce >= MAX_SESSION_MESSAGES
223
+ || this.recvCipher.nonce >= MAX_SESSION_MESSAGES;
224
+ }
225
+ close() {
226
+ this.closed = true;
227
+ }
228
+ encrypt(plaintext, nowMs = Date.now()) {
229
+ if (this.closed)
230
+ throw new Error("Noise transport session is closed");
231
+ if (nowMs - this.createdAtMs >= MAX_SESSION_AGE_MS) {
232
+ this.closed = true;
233
+ throw new Error("Noise session exceeded 24h lifetime — re-handshake required");
234
+ }
235
+ if (plaintext.length > MAX_NOISE_PLAINTEXT_BYTES) {
236
+ throw new Error(`Noise transport plaintext exceeds ${MAX_NOISE_PLAINTEXT_BYTES} bytes`);
237
+ }
238
+ try {
239
+ return this.sendCipher.encryptWithAd(Buffer.alloc(0), plaintext, true);
240
+ }
241
+ catch (err) {
242
+ this.closed = true;
243
+ throw err;
244
+ }
245
+ }
246
+ decrypt(ciphertext, nowMs = Date.now()) {
247
+ if (this.closed)
248
+ throw new Error("Noise transport session is closed");
249
+ if (nowMs - this.createdAtMs >= MAX_SESSION_AGE_MS) {
250
+ this.closed = true;
251
+ throw new Error("Noise session exceeded 24h lifetime — re-handshake required");
252
+ }
253
+ if (ciphertext.length > MAX_NOISE_MESSAGE_BYTES) {
254
+ this.closed = true;
255
+ throw new Error(`Noise transport ciphertext exceeds ${MAX_NOISE_MESSAGE_BYTES} bytes`);
256
+ }
257
+ try {
258
+ return this.recvCipher.decryptWithAd(Buffer.alloc(0), ciphertext, true);
259
+ }
260
+ catch (err) {
261
+ this.closed = true;
262
+ throw err;
263
+ }
264
+ }
265
+ }
266
+ export class NoiseIKpsk2Initiator {
267
+ sym;
268
+ s;
269
+ rs;
270
+ psk;
271
+ fixedE;
272
+ e = null;
273
+ step = 0;
274
+ constructor(opts) {
275
+ assertLen(opts.localStatic.privateKey, 32, "localStatic.privateKey");
276
+ assertLen(opts.localStatic.publicKey, 32, "localStatic.publicKey");
277
+ assertLen(opts.remoteStaticPublicKey, 32, "remoteStaticPublicKey");
278
+ assertLen(opts.psk, 32, "psk");
279
+ this.s = opts.localStatic;
280
+ this.rs = Buffer.from(opts.remoteStaticPublicKey);
281
+ this.psk = Buffer.from(opts.psk);
282
+ this.fixedE = opts.ephemeralForTest;
283
+ this.sym = new SymmetricState(NOISE_PROTOCOL_NAME);
284
+ this.sym.mixHash(opts.prologue);
285
+ this.sym.mixHash(this.rs);
286
+ }
287
+ writeMessage1(payload) {
288
+ if (this.step !== 0)
289
+ throw new Error("writeMessage1 already called");
290
+ const e = this.fixedE ?? generateX25519KeyPair();
291
+ this.e = e;
292
+ this.sym.mixHash(e.publicKey);
293
+ this.sym.mixKey(e.publicKey);
294
+ this.sym.mixKey(x25519Dh(e.privateKey, this.rs));
295
+ const encS = this.sym.encryptAndHash(this.s.publicKey);
296
+ this.sym.mixKey(x25519Dh(this.s.privateKey, this.rs));
297
+ const encPayload = this.sym.encryptAndHash(payload);
298
+ const msg = Buffer.concat([e.publicKey, encS, encPayload]);
299
+ if (msg.length > MAX_NOISE_MESSAGE_BYTES) {
300
+ throw new Error(`Noise message 1 exceeds ${MAX_NOISE_MESSAGE_BYTES} bytes`);
301
+ }
302
+ this.step = 1;
303
+ return msg;
304
+ }
305
+ readMessage2(message, nowMs) {
306
+ if (this.step !== 1 || this.e === null)
307
+ throw new Error("readMessage2 called out of order");
308
+ if (message.length < 32 + AEAD_TAG_BYTES || message.length > MAX_NOISE_MESSAGE_BYTES) {
309
+ throw new Error(`Invalid Noise message 2 length: ${message.length}`);
310
+ }
311
+ const re = message.subarray(0, 32);
312
+ const encPayload = message.subarray(32);
313
+ this.sym.mixHash(re);
314
+ this.sym.mixKey(re);
315
+ this.sym.mixKey(x25519Dh(this.e.privateKey, re));
316
+ this.sym.mixKey(x25519Dh(this.s.privateKey, re));
317
+ this.sym.mixKeyAndHash(this.psk);
318
+ const payload = this.sym.decryptAndHash(encPayload);
319
+ const [sendCipher, recvCipher] = this.sym.split();
320
+ this.step = 2;
321
+ const session = new NoiseTransportSession({
322
+ handshakeHash: this.sym.h,
323
+ remoteStaticPublicKey: this.rs,
324
+ sendCipher,
325
+ recvCipher,
326
+ ...(nowMs !== undefined ? { createdAtMs: nowMs } : {}),
327
+ });
328
+ return { payload, session };
329
+ }
330
+ }
331
+ export class NoiseIKpsk2Responder {
332
+ sym;
333
+ s;
334
+ psk;
335
+ fixedE;
336
+ re = null;
337
+ rsRemote = null;
338
+ step = 0;
339
+ constructor(opts) {
340
+ assertLen(opts.localStatic.privateKey, 32, "localStatic.privateKey");
341
+ assertLen(opts.localStatic.publicKey, 32, "localStatic.publicKey");
342
+ assertLen(opts.psk, 32, "psk");
343
+ this.s = opts.localStatic;
344
+ this.psk = Buffer.from(opts.psk);
345
+ this.fixedE = opts.ephemeralForTest;
346
+ this.sym = new SymmetricState(NOISE_PROTOCOL_NAME);
347
+ this.sym.mixHash(opts.prologue);
348
+ this.sym.mixHash(this.s.publicKey);
349
+ }
350
+ readMessage1(message) {
351
+ if (this.step !== 0)
352
+ throw new Error("readMessage1 already called");
353
+ if (message.length < 32 + 48 + AEAD_TAG_BYTES || message.length > MAX_NOISE_MESSAGE_BYTES) {
354
+ throw new Error(`Invalid Noise message 1 length: ${message.length}`);
355
+ }
356
+ const re = message.subarray(0, 32);
357
+ const encS = message.subarray(32, 80);
358
+ const encPayload = message.subarray(80);
359
+ this.sym.mixHash(re);
360
+ this.sym.mixKey(re);
361
+ this.sym.mixKey(x25519Dh(this.s.privateKey, re));
362
+ const rsRemote = this.sym.decryptAndHash(encS);
363
+ assertLen(rsRemote, 32, "remote static public key");
364
+ this.sym.mixKey(x25519Dh(this.s.privateKey, rsRemote));
365
+ const payload = this.sym.decryptAndHash(encPayload);
366
+ this.re = Buffer.from(re);
367
+ this.rsRemote = rsRemote;
368
+ this.step = 1;
369
+ return { payload, remoteStaticPublicKey: Buffer.from(rsRemote) };
370
+ }
371
+ writeMessage2(payload, nowMs) {
372
+ if (this.step !== 1 || this.re === null || this.rsRemote === null) {
373
+ throw new Error("writeMessage2 called out of order");
374
+ }
375
+ const e = this.fixedE ?? generateX25519KeyPair();
376
+ this.sym.mixHash(e.publicKey);
377
+ this.sym.mixKey(e.publicKey);
378
+ this.sym.mixKey(x25519Dh(e.privateKey, this.re));
379
+ this.sym.mixKey(x25519Dh(e.privateKey, this.rsRemote));
380
+ this.sym.mixKeyAndHash(this.psk);
381
+ const encPayload = this.sym.encryptAndHash(payload);
382
+ const message = Buffer.concat([e.publicKey, encPayload]);
383
+ if (message.length > MAX_NOISE_MESSAGE_BYTES) {
384
+ throw new Error(`Noise message 2 exceeds ${MAX_NOISE_MESSAGE_BYTES} bytes`);
385
+ }
386
+ const [recvCipher, sendCipher] = this.sym.split();
387
+ this.step = 2;
388
+ const session = new NoiseTransportSession({
389
+ handshakeHash: this.sym.h,
390
+ remoteStaticPublicKey: this.rsRemote,
391
+ sendCipher,
392
+ recvCipher,
393
+ ...(nowMs !== undefined ? { createdAtMs: nowMs } : {}),
394
+ });
395
+ return { message, session };
396
+ }
397
+ }
398
+ function assertNoNul(value, label) {
399
+ if (value.length === 0 || value.includes("\0")) {
400
+ throw new Error(`${label} must be non-empty and contain no NUL bytes`);
401
+ }
402
+ }
403
+ export function buildPairingPrologue(workspaceId, pairSessionId) {
404
+ assertNoNul(workspaceId, "workspaceId");
405
+ assertNoNul(pairSessionId, "pairSessionId");
406
+ return Buffer.from(`toolroll-noise-v1\0pair\0${workspaceId}\0${pairSessionId}`, "utf8");
407
+ }
408
+ export function buildSessionPrologue(workspaceId, deviceId, recoveryGeneration) {
409
+ assertNoNul(workspaceId, "workspaceId");
410
+ assertNoNul(deviceId, "deviceId");
411
+ if (!/^[0-9a-f]{32}$/.test(recoveryGeneration)) {
412
+ throw new Error(`Invalid recoveryGeneration (expected 32 lowercase hex chars): ${recoveryGeneration}`);
413
+ }
414
+ return Buffer.from(`toolroll-noise-v1\0session\0${workspaceId}\0${deviceId}\0${recoveryGeneration}`, "utf8");
415
+ }
416
+ const CROCKFORD_ALPHABET = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
417
+ const RFC4648_LOWER_ALPHABET = "abcdefghijklmnopqrstuvwxyz234567";
418
+ export function base32Encode(data, alphabet) {
419
+ let out = "";
420
+ let acc = 0;
421
+ let bits = 0;
422
+ for (const byte of data) {
423
+ acc = (acc << 8) | byte;
424
+ bits += 8;
425
+ while (bits >= 5) {
426
+ bits -= 5;
427
+ out += alphabet[(acc >>> bits) & 0x1f];
428
+ }
429
+ }
430
+ if (bits > 0) {
431
+ out += alphabet[(acc << (5 - bits)) & 0x1f];
432
+ }
433
+ return out;
434
+ }
435
+ export function deriveConfirmationCode(handshakeHash) {
436
+ assertLen(handshakeHash, 32, "handshakeHash");
437
+ const okm = Buffer.from(hkdfSync("sha256", handshakeHash, Buffer.alloc(0), Buffer.from("toolroll-pair-confirm-v1", "ascii"), 8));
438
+ const raw = base32Encode(okm, CROCKFORD_ALPHABET);
439
+ return `${raw.slice(0, 3)}-${raw.slice(3, 6)}`;
440
+ }
441
+ export function deriveDevicePsk(pskEntropy, handshakeHash, workspaceId, deviceId) {
442
+ assertLen(pskEntropy, 32, "pskEntropy");
443
+ assertLen(handshakeHash, 32, "handshakeHash");
444
+ assertNoNul(workspaceId, "workspaceId");
445
+ assertNoNul(deviceId, "deviceId");
446
+ const info = Buffer.from(`toolroll-device-psk-v1:${workspaceId}:${deviceId}`, "utf8");
447
+ return Buffer.from(hkdfSync("sha256", pskEntropy, handshakeHash, info, 32));
448
+ }
449
+ export function deriveMachineId(relayMachinePublicKey) {
450
+ assertLen(relayMachinePublicKey, 32, "relayMachinePublicKey");
451
+ const digest = sha256(Buffer.concat([Buffer.from("toolroll-machine-id-v1\0", "ascii"), relayMachinePublicKey]));
452
+ return base32Encode(digest, RFC4648_LOWER_ALPHABET).slice(0, 26);
453
+ }
454
+ export function derivePasskeyUserHandle(workspaceId, accountName) {
455
+ assertNoNul(workspaceId, "workspaceId");
456
+ assertNoNul(accountName, "accountName");
457
+ return sha256(Buffer.from(`toolroll-passkey-user-v1\0${workspaceId}\0${accountName}`, "utf8"));
458
+ }
459
+ export function randomPsk32() {
460
+ return randomBytes(32);
461
+ }