Overview
WhatsApp-Rust supports two authentication methods for linking companion devices:- QR Code Pairing - Scan a QR code with your phone
- Pair Code (Phone Number Linking) - Enter an 8-character code on your phone
Authentication Flow
QR Code Pairing
How It Works
Location:src/pair.rs, wacore/src/pair.rs
- Server sends pairing refs: After connection, server sends
pair-devicewith multiple refs - Generate QR codes: Each ref becomes a QR code containing device keys
- QR rotation: First code valid for 60s, subsequent codes for 20s each
- Phone scans: User scans QR with WhatsApp > Linked Devices
- Crypto handshake: Noise-based key exchange establishes trust
- Completion: Server sends
pair-success, device signs identity
QR Code Contents
ref,noise_pub,identity_pub,adv_secret
ref: Pairing reference from servernoise_pub: Static Noise public key (32 bytes, base64)identity_pub: Signal identity public key (32 bytes, base64)adv_secret: Advertisement secret key (32 bytes, base64)
Implementation
QR Code Events
Event:Event::PairingQrCode
src/pair.rs:63-97
Pair Code (Phone Number Linking)
How It Works
Location:src/pair_code.rs, wacore/src/pair_code.rs
- Generate code: 8-character Crockford Base32 code
- Stage 1 - Hello: Send phone number + encrypted ephemeral key
- Server response: Returns pairing reference
- User enters code: On phone: WhatsApp > Linked Devices > Link with phone number
- Stage 2 - Finish: Phone confirms, companion sends key bundle
- Completion: Server sends
pair-success
Pair Code Format
Alphabet: Crockford Base32 (excludes 0, I, O, U)ABCD1234, MYCODE12
Implementation
Random Code
Custom Code
Pair Code Options
Pair Code Events
Event:Event::PairingCode
src/pair_code.rs:215-219
Two-Stage Flow
Stage 1: Hello
Purpose: Register phone number and encrypted ephemeral keyStage 2: Finish
Trigger:link_code_companion_reg notification from server
Handling: src/pair_code.rs:229-376
Cryptography
Noise Protocol Handshake
Pattern: Noise XX (mutual authentication)- Initiator → Responder: ephemeral pub
- Responder → Initiator: ephemeral pub, static pub, encrypted payload
- Initiator → Responder: static pub, encrypted payload
Key Derivation
For QR Code:- Algorithm: AES-256-CBC
- KDF: PBKDF2-HMAC-SHA256
- Iterations: 2^16 (65,536)
- Salt: 16 random bytes
- IV: 16 random bytes
Signal Protocol Setup
After pairing:- Server sends signed device identity
- Companion verifies signature
- Identity keys exchanged
- Pre-keys registered
Concurrent Pairing
Both methods can run simultaneously:Success Events
PairSuccess
PairError
Error Handling
QR Code Errors
Pair Code Errors
Session Persistence
After Successful Pairing
State saved to storage:- Device JID (Phone Number)
- LID (Long-term Identifier)
- Identity keys
- Noise keys
- Registration ID
- Push name
Logout
Best Practices
Phone Number Format
Event Handling
Concurrent Usage
Related Sections
Architecture
Understand the project structure
Events
Learn about all event types
Storage
Explore session persistence
Quick Start
Build your first bot