/** * Local device id helpers shared between the lifecycle tick and the * 6-digit pairing flow. Both need the same base64url-encoded 16-byte id * the native host expects in `Advertise` / `StartPairingServer`. */ const DEVICE_ID_KEY = "peach_device_id"; export async function getOrCreateLocalDeviceId(): Promise { try { const stored = await chrome.storage.local.get(DEVICE_ID_KEY); const value = (stored as Record)[DEVICE_ID_KEY]; if (typeof value === "string" && value) return value; } catch { // fall through to generation } const random = crypto.getRandomValues(new Uint8Array(16)); let binary = ""; for (const byte of random) binary += String.fromCharCode(byte); const id = btoa(binary) .replace(/\+/g, "-") .replace(/\//g, "_") .replace(/=+$/g, ""); try { await chrome.storage.local.set({ [DEVICE_ID_KEY]: id }); } catch { // best-effort } return id; }