pairing-handshake.ts

1.1 KB · raw

1// Shared pairing primitives used by the live relay-adoption path.23import type { DeviceType } from "./sync-types";45export interface PairingTransport {6  send(msg: Uint8Array): Promise<void>;7  receive(): Promise<Uint8Array>;8}910export interface LocalIdentity {11  deviceId: string;12  deviceName: string;13  deviceType: DeviceType;14  longTermPublicKeyHex: string;15  signingPublicKeyHex?: string;16  /** Server-only field — Android/iOS sync hosts announce their port. */17  wsPort?: number;18  /** Stable per-vault id, omitted while the vault is locked. */19  vaultId?: string;20  /** Signed Pro license blob advertised after SAS confirmation. */21  license?: string;22  relayUrl?: string;23}2425/** Derive the cross-platform 8-digit SAS from a Noise handshake hash. */26export function derivePairingSAS(handshakeHash: Uint8Array): string {27  if (handshakeHash.length < 8) {28    throw new Error("handshake hash too short for SAS");29  }30  // The big-endian rule is pinned by the Android cross-platform KATs.31  let value = 0n;32  for (const byte of handshakeHash.subarray(0, 8)) {33    value = (value << 8n) | BigInt(byte);34  }35  return String(value % 100_000_000n).padStart(8, "0");36}