# Session Import (Path B) — WhatsApp Web Transport Handover **Status: LIVE-VALIDATED · impersonation-grade — treat the payload as a live account secret.** Path B takes an **existing `web.whatsapp.com` session**, decrypts its non-extractable WebCrypto Noise + Signal keys inside the live browser context (`companion --mode=dump`), quiesces and wipes the browser session **before** handover (preventing `440 conflict`), and converts it into a native `hypermeow` Go device via `POST /session/import` — **without any new database migration** (it writes through the existing sqlstore). --- ## 1. When to use it | | Path A (WebAuthn / Native QR) | Path B (WA-Web Transport Handover) | |---|---|---| | Mechanism | Direct `hypermeow` QR / pair-code / WebAuthn | Real browser pairs `web.whatsapp.com` → `POST /session/import` | | Device type | First-class linked device (supports `deviceProfile` presets) | Reconstructed companion device from real WA Web session | | Fragility | Low | Medium (depends on WA Web storage & WebCrypto layout) | | Coex / Cloud API | Supported (`IsHosted` auto-detected) | Supported (`ADVEncryptionType_HOSTED` + `WAIsHostedMeAccount` auto-detected) | | Recommendation | **Default** | Opt-in (useful when pairing must originate in a real browser) | --- ## 2. Endpoint ``` POST /session/import Header: token: ``` Accepts **either** shape: ### 2.1 Structured `WebDeviceSnapshot` (recommended, produced automatically by `companion --mode=dump`) ```jsonc { "meJid": "5521999999999:11@s.whatsapp.net", // required — "@c.us" is normalized to "@s.whatsapp.net" "lid": "111111111111111:11@lid", // optional — normalized to "@lid" "registrationId": 12345, // required, non-zero "noiseKey": { "priv": "YOUR_NOISE_KEY_PRIVATE_BASE64_32_BYTES" }, // required "identityKey": { "priv": "YOUR_IDENTITY_KEY_PRIVATE_BASE64_32_BYTES" }, // required "signedPreKey": { // required "keyId": 1, "priv": "YOUR_SIGNED_PRE_KEY_PRIVATE_BASE64_32_BYTES", "signature": "base64-64-bytes" // signature by the identity key }, "account": { // required — ADVSignedDeviceIdentity "details": "base64", "accountSignature": "base64", "accountSignatureKey": "YOUR_ACCOUNT_SIGNATURE_KEY_BASE64", "deviceSignature": "base64" }, "advSecretKey": "YOUR_ADV_SECRET_KEY_BASE64_32_BYTES", // optional "primaryIdentity": "BASE64_32_BYTES", // optional — seeded into identity store "isHosted": false, // optional — also auto-detected from account.details "deviceProfile": "web_chrome", // optional — per-device ClientProfile preset "waVersion": "2.3000.1056658690", // optional — WA Web version captured during handover "platform": "web", "pushName": "…", "businessName": "…" // optional } ``` All binary fields are base64 (std or url; padded or not). Public keys accept 32-byte raw Curve25519 or 33-byte `0x05`-prefixed Signal format. Legacy `@c.us` JIDs emitted by WA Web (`last-wid-md`) are automatically normalized to `@s.whatsapp.net`. ### 2.2 Generic wa-web dump (`{ "source": "wa-web", "localStorage": ..., "indexeddb": ... }`) If a caller submits a raw plaintext dump, the backend runs best-effort extraction. Because modern `web.whatsapp.com` stores `WANoiseInfo` (HKDF-SHA256 + AES-CBC-128) and `signal_static_privkey` (AES-CTR-128) encrypted under **non-extractable** WebCrypto `CryptoKey` handles, raw offline dumps cannot be decrypted server-side and honestly return `422` listing the encrypted/missing fields. Use `companion --mode=dump` so decryption runs inside the live browser origin before handover. --- ## 3. Running `companion --mode=dump` ```bash ./companion --mode=dump \ --api=https://your-api.example.com \ --token= \ --device-profile=web_chrome \ --timeout=10m ``` Flow: 1. Launches an isolated Chrome window at `https://web.whatsapp.com`. 2. Waits until login is stable and both `last-wid-md` and `WANoiseInfo` are persisted. 3. Decrypts `WANoiseInfo`, `signal_static_privkey`, `signedPreKey`, `adv_signed_identity`, `WAADVSecretKey`, `WAIsHostedMeAccount`, `primaryIdentity`, and `waVersion` in-memory via browser WebCrypto (with `window.require` fallback). 4. **Quiesces & wipes** IndexedDB, `localStorage`, `sessionStorage`, `CacheStorage`, unregisters service workers, and navigates to `about:blank` **before** calling `POST /session/import` so Chrome never competes with Go for the same device (`440 conflict`). 5. Polls `GET /session/status` until the native Go `hypermeow` session reports `loggedIn: true`. --- ## 4. Responses | Status | Meaning | |---|---| | `200` | Imported; device booting. Poll `GET /session/status` until `loggedIn`. | | `400` | Invalid/unrecognized payload. | | `422` | Snapshot conversion failed OR incomplete/browser-encrypted generic dump (see `missing`). | | `500` | Container unavailable or persist failed. | `200` body: ```json { "status": "imported", "jid": "5521999999999:11@s.whatsapp.net", "lid": "111111111111111:11@lid", "isHosted": false, "deviceProfile": "web_chrome", "note": "device booting; poll /session/status for loggedIn" } ``` --- ## 5. How the conversion works (no new migration) 1. Acquires the per-user session lock and cleanly retires any active client on that instance. 2. `container.NewDevice()` seeds a fresh device (keys + store backends). 3. `applySnapshotToDevice` validates every required field, normalizes `@c.us` → `@s.whatsapp.net` and `@lid`, validates `deviceProfile` and `waVersion`, and auto-detects `IsHosted` (Cloud API Coex) from `ADVDeviceIdentity.AccountType == HOSTED`. 4. `container.PutDevice(...)` persists through the **existing** sqlstore schema — **no new migration**. 5. Seeds `LID ↔ PN` mapping (`dev.LIDs.PutLIDMapping`) and the primary phone's identity key (`dev.Identities.PutIdentity`) so ADV validation, app-state sync key requests, and E2EE messaging work immediately on first connect. 6. Persists `jid`, `wa_platform_type`, and `wa_version` to `users` and boots via `startClient`. --- ## 6. Security (mandatory) - The snapshot is **impersonation-grade** — anyone holding it controls the account. - **Authentication:** `/session/import` requires the real per-instance user token in the `token` header. Connector `ct_…` tokens are restricted to `/connector/*` and are rejected. - **Always HTTPS.** - The companion **never writes key material to disk** and **wipes the browser session before handover**. - **Never log the snapshot.** The backend logs only the resulting JID, `isHosted`, `deviceProfile`, and field-level `missing` list — never key material.