mesh-peer-role.ts

1.9 KB · raw

1import type { PairedDevice } from "./sync-types";23export const MESH_WAITER_RETRY_DELAY_MS = 2_000;4export const MESH_WAITER_MAX_RETRY_DELAY_MS = 30_000;56export function isRouteCapableMeshPeer(device: PairedDevice): boolean {7  // PB-26-006: fail closed on an unknown/absent trustSource — sibling trust8  // gates reject it, so routing must not treat it as direct/vouched.9  //10  // PB-26-213: the rendezvous secret is intentionally NOT part of this check.11  // The public registry carries no reusable secrets; the secret resolves from12  // the encrypted pair-secret record (or trusted session) at dial time, and13  // peers without one are skipped there.14  const trusted =15    device.trustSource === "direct" || device.trustSource === "vouched";16  return (17    trusted &&18    (device.deviceType === "extension" ||19      device.deviceType === "android" ||20      device.deviceType === "ios") &&21    typeof device.longTermPublicKeyHex === "string" &&22    /^[0-9a-f]{64}$/.test(device.longTermPublicKeyHex)23  );24}2526/** Lower public key dials; higher public key waits. */27export function weDialMeshPeer(28  ourLongTermPublicKeyHex: string,29  peerLongTermPublicKeyHex: string,30): boolean {31  if (32    !/^[0-9a-f]{64}$/.test(ourLongTermPublicKeyHex) ||33    !/^[0-9a-f]{64}$/.test(peerLongTermPublicKeyHex) ||34    ourLongTermPublicKeyHex === peerLongTermPublicKeyHex35  ) {36    return false;37  }38  return ourLongTermPublicKeyHex < peerLongTermPublicKeyHex;39}4041export function selectDialTargets(42  devices: PairedDevice[],43  ourLongTermPublicKeyHex: string,44): PairedDevice[] {45  return devices.filter(46    (device) =>47      isRouteCapableMeshPeer(device) &&48      weDialMeshPeer(49        ourLongTermPublicKeyHex,50        device.longTermPublicKeyHex!,51      ),52  );53}5455export function nextMeshWaiterRetryDelay(56  current: number,57  succeeded: boolean,58): number {59  return succeeded60    ? MESH_WAITER_RETRY_DELAY_MS61    : Math.min(current * 2, MESH_WAITER_MAX_RETRY_DELAY_MS);62}