@velmlabs/unstuck-protocol 0.1.5 → 0.1.7

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/dist/index.d.ts CHANGED
@@ -76,6 +76,8 @@ interface TakeoverParams {
76
76
  requireCode?: boolean;
77
77
  /** Sites besides the page's own the human may navigate to, e.g. ["accounts.google.com"] (page lock). */
78
78
  allowedSites?: string[];
79
+ /** Send this takeover to the user's paired phones (Unstuck Cloud); undefined means yes, false skips it. */
80
+ alerts?: boolean;
79
81
  }
80
82
  interface CdpSendParams {
81
83
  pageId: string;
@@ -346,5 +348,26 @@ declare function openMsg(key: CryptoKey, sessionId: string, dir: Direction, conn
346
348
  plain: Uint8Array;
347
349
  } | null>;
348
350
 
349
- export { EngineToViewerSchema, FRAME_TYPE_JPEG, FieldKinds, FieldSchema, KEY_SUBPROTOCOL_PREFIX, Methods, PROTOCOL_VERSION, RELAY_CLOSE, RELAY_LIMITS, RELAY_SUBPROTOCOL, RESUME_WINDOW_MS, RpcCallError, SESSION_ID_RE, SessionStatusSchema, ViewerErrorCodes, ViewerToEngineSchema, b64urlDecode, b64urlEncode, createRpcPeer, decodeEnvelope, decodeFrame, encodeEnvelope, encodeFrame, importE2eKey, isJsonPlaintext, newConnNonce, newE2eKey, open, openMsg, padPlaintext, parseAgentToRelay, parseRelayToAgent, parseViewerMessage, seal, sealMsg };
350
- export type { AgentToRelay, CdpClosedParams, CdpEventParams, CdpSendParams, CdpSubscribeParams, Direction, EngineToViewer, Field, FieldKind, Frame, HelloParams, HelloResult, PagePopupParams, RelayToAgent, RpcError, RpcNotification, RpcPeer, RpcRequest, RpcResponse, SessionStatus, TakeoverLinkParams, TakeoverParams, TakeoverResult, TakeoverStatus, ViewerErrorCode, ViewerInput, ViewerToEngine };
351
+ interface AlertPayload {
352
+ t: string;
353
+ k: string;
354
+ reason: string;
355
+ site: string;
356
+ expiresAt: number;
357
+ }
358
+ /** ECDH P-256 key pair for a paired phone; the private key is non-extractable. */
359
+ declare function newDeviceKeyPair(): Promise<CryptoKeyPair>;
360
+ /** The raw 65-byte uncompressed public key, base64url. */
361
+ declare function exportDevicePublicKey(k: CryptoKey): Promise<string>;
362
+ /** Seals the payload to the device's public key; bound to the device and session. */
363
+ declare function sealAlert(devicePublicKey: string, deviceId: string, sessionId: string, p: AlertPayload): Promise<string>;
364
+ /** The payload, or null when the blob wasn't sealed to this key, device and session (or is malformed). */
365
+ declare function openAlert(devicePrivateKey: CryptoKey, deviceId: string, sessionId: string, blob: string): Promise<AlertPayload | null>;
366
+ /** Reads `#x=<device id>.<blob>`; null for any other fragment. */
367
+ declare function parseAlertFragment(hash: string): {
368
+ deviceId: string;
369
+ blob: string;
370
+ } | null;
371
+
372
+ export { EngineToViewerSchema, FRAME_TYPE_JPEG, FieldKinds, FieldSchema, KEY_SUBPROTOCOL_PREFIX, Methods, PROTOCOL_VERSION, RELAY_CLOSE, RELAY_LIMITS, RELAY_SUBPROTOCOL, RESUME_WINDOW_MS, RpcCallError, SESSION_ID_RE, SessionStatusSchema, ViewerErrorCodes, ViewerToEngineSchema, b64urlDecode, b64urlEncode, createRpcPeer, decodeEnvelope, decodeFrame, encodeEnvelope, encodeFrame, exportDevicePublicKey, importE2eKey, isJsonPlaintext, newConnNonce, newDeviceKeyPair, newE2eKey, open, openAlert, openMsg, padPlaintext, parseAgentToRelay, parseAlertFragment, parseRelayToAgent, parseViewerMessage, seal, sealAlert, sealMsg };
373
+ export type { AgentToRelay, AlertPayload, CdpClosedParams, CdpEventParams, CdpSendParams, CdpSubscribeParams, Direction, EngineToViewer, Field, FieldKind, Frame, HelloParams, HelloResult, PagePopupParams, RelayToAgent, RpcError, RpcNotification, RpcPeer, RpcRequest, RpcResponse, SessionStatus, TakeoverLinkParams, TakeoverParams, TakeoverResult, TakeoverStatus, ViewerErrorCode, ViewerInput, ViewerToEngine };
package/dist/index.js CHANGED
@@ -351,6 +351,70 @@ async function openMsg(key, sessionId, dir, conn, sealed) {
351
351
  return null;
352
352
  }
353
353
  }
354
+
355
+ // src/alerts.ts
356
+ var PUB_BYTES = 65;
357
+ var IV_BYTES2 = 12;
358
+ var TAG_BYTES2 = 16;
359
+ var enc2 = new TextEncoder();
360
+ var dec = new TextDecoder();
361
+ var CURVE = { name: "ECDH", namedCurve: "P-256" };
362
+ function newDeviceKeyPair() {
363
+ return crypto.subtle.generateKey(CURVE, false, ["deriveBits"]);
364
+ }
365
+ async function exportDevicePublicKey(k) {
366
+ return b64urlEncode(new Uint8Array(await crypto.subtle.exportKey("raw", k)));
367
+ }
368
+ async function deriveAesKey(priv, pub, deviceId, sessionId, usage) {
369
+ const secret = await crypto.subtle.deriveBits({ name: "ECDH", public: pub }, priv, 256);
370
+ const hkdf = await crypto.subtle.importKey("raw", secret, "HKDF", false, ["deriveKey"]);
371
+ return crypto.subtle.deriveKey(
372
+ { name: "HKDF", hash: "SHA-256", salt: enc2.encode(sessionId), info: enc2.encode(`unstuck/notify/1|${deviceId}`) },
373
+ hkdf,
374
+ { name: "AES-GCM", length: 256 },
375
+ false,
376
+ [usage]
377
+ );
378
+ }
379
+ var aad2 = (sessionId, deviceId) => enc2.encode(`unstuck/notify/1|${sessionId}|${deviceId}`);
380
+ async function sealAlert(devicePublicKey, deviceId, sessionId, p) {
381
+ const raw = b64urlDecode(devicePublicKey);
382
+ if (!raw || raw.length !== PUB_BYTES) throw new Error("invalid device public key");
383
+ const devicePub = await crypto.subtle.importKey("raw", new Uint8Array(raw), CURVE, false, []);
384
+ const eph = await crypto.subtle.generateKey(CURVE, false, ["deriveBits"]);
385
+ const key = await deriveAesKey(eph.privateKey, devicePub, deviceId, sessionId, "encrypt");
386
+ const iv = crypto.getRandomValues(new Uint8Array(IV_BYTES2));
387
+ const ct = new Uint8Array(
388
+ await crypto.subtle.encrypt({ name: "AES-GCM", iv, additionalData: aad2(sessionId, deviceId) }, key, enc2.encode(JSON.stringify(p)))
389
+ );
390
+ const ephPub = new Uint8Array(await crypto.subtle.exportKey("raw", eph.publicKey));
391
+ const out = new Uint8Array(PUB_BYTES + IV_BYTES2 + ct.length);
392
+ out.set(ephPub);
393
+ out.set(iv, PUB_BYTES);
394
+ out.set(ct, PUB_BYTES + IV_BYTES2);
395
+ return b64urlEncode(out);
396
+ }
397
+ async function openAlert(devicePrivateKey, deviceId, sessionId, blob) {
398
+ const raw = b64urlDecode(blob);
399
+ if (!raw || raw.length < PUB_BYTES + IV_BYTES2 + TAG_BYTES2) return null;
400
+ try {
401
+ const ephPub = await crypto.subtle.importKey("raw", raw.slice(0, PUB_BYTES), CURVE, false, []);
402
+ const key = await deriveAesKey(devicePrivateKey, ephPub, deviceId, sessionId, "decrypt");
403
+ const pt = await crypto.subtle.decrypt(
404
+ { name: "AES-GCM", iv: raw.slice(PUB_BYTES, PUB_BYTES + IV_BYTES2), additionalData: aad2(sessionId, deviceId) },
405
+ key,
406
+ raw.slice(PUB_BYTES + IV_BYTES2)
407
+ );
408
+ const p = JSON.parse(dec.decode(pt));
409
+ return p && typeof p === "object" ? p : null;
410
+ } catch {
411
+ return null;
412
+ }
413
+ }
414
+ function parseAlertFragment(hash) {
415
+ const m = /^#?x=([^.&=]+)\.([^.&=]+)$/.exec(hash);
416
+ return m ? { deviceId: m[1], blob: m[2] } : null;
417
+ }
354
418
  export {
355
419
  EngineToViewerSchema,
356
420
  FRAME_TYPE_JPEG,
@@ -375,16 +439,21 @@ export {
375
439
  decodeFrame,
376
440
  encodeEnvelope,
377
441
  encodeFrame,
442
+ exportDevicePublicKey,
378
443
  importE2eKey,
379
444
  isJsonPlaintext,
380
445
  newConnNonce,
446
+ newDeviceKeyPair,
381
447
  newE2eKey,
382
448
  open,
449
+ openAlert,
383
450
  openMsg,
384
451
  padPlaintext,
385
452
  parseAgentToRelay,
453
+ parseAlertFragment,
386
454
  parseRelayToAgent,
387
455
  parseViewerMessage,
388
456
  seal,
457
+ sealAlert,
389
458
  sealMsg
390
459
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@velmlabs/unstuck-protocol",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Wire protocol shared by the Unstuck engine, viewer and relays (frames, RPC, end-to-end encryption).",
5
5
  "keywords": [
6
6
  "ai",