Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
whatsmeow
What it is
A faithful 1:1 Rust port of whatsmeow, the
Go client for WhatsApp's web multidevice (linked device) API. Ported from
revision b2b1e71d45e2ff171153b025b5a42f475bb30ef8.
Start here
src/lib.rs: module map; one module per Go package, one file per Go file.src/client.rs: theClient(Go'sclient.go); every other top-level file adds animpl Clientblock for its Go file. Its header lists the deliberate deviations (config struct,Dialer, async event handlers).- Phone-number pairing:
pair_phoneafter the first QR event; passkey pairing:Event::PairPasskeyRequest→send_passkey_response,Event::PairPasskeyConfirmation→send_passkey_confirmation(the QR channel surfaces both and confirms by itself when the handoff UX can be skipped). src/testing.rs: the fake WhatsApp server and fake Signal used by the client tests (also exported behind thetestingfeature).src/signal.rs: the libsignal boundary (see below) — read before porting anything that encrypts, decrypts or signs.src/appstate.rs: theappstatepackage.Processor::new(&Device)clones the device'sapp_state_keysandapp_statehandles; the client port (appstate.go) callsdecode_patches,encode_patch,get_missing_key_ids,process_recovery,parse_patch_listandparse_recovery, and checksDecodeError::sentinel()where Go useserrors.Is(err, ErrKeyNotFound).src/appstate/client.rs: the top-levelappstate.go(the exception tosrc/<file>.rs, sincesrc/appstate.rsis the package): fetching, applying and sending patches,server_syncnotifications andhandle_app_state_sync_key_share(frommessage.go). Key requests go throughClient::send_peer_message, which encrypts and sends to the account’s other devices.
Public API (what services use)
let client = Client::new(device, signal /* Arc<dyn Signal> */, ClientConfig::default());
let mut events = client.subscribe(); // or add_event_handler(Arc<dyn EventHandler>)
let qr = client.get_qr_channel(cancel).await?; // before connect, unpaired devices only
client.connect().await?;
client.send_message(&group_jid, message, SendRequestExtra::default()).await?;
client.get_joined_groups().await?; client.get_group_info(&group_jid).await?;
client.download(&*image_message).await?; client.upload(&bytes, MediaType::Image).await?;
client.mark_read(&ids, now, &chat, &sender, None).await?;
client.disconnect().await; client.shutdown().await;
device: astore::Devicewhose store handles are your persistence (MemoryStorein tests). The client owns it asclient.store(tokio::sync::RwLock<Device>); pairing and logins write it back withDevice::save.SignalisSignalProtocol + CurveSignatures(blanket impl): pass oneArcof the libsignal implementation.- Events (
types::events::Event):Qr,PairSuccess,PairError,Connected,Disconnected,LoggedOut,StreamReplaced,Message,UndecryptableMessage,Receipt,GroupInfo,JoinedGroup,HistorySync,PushName, … WithClientConfig::synchronous_ack, anEventHandlerreturningfalseleaves the node unacked so the server redelivers it. SendRequestExtra::no_disconnect_retry(not in whatsmeow; #8304 patched it in) makes a send fail withClientError::Disconnectedinstead of resending after a reconnect, so an unacked send can be recorded as unknown.- Deviation from Go: the handler queue loop handles a
stream:errorit dequeued after the connection ended (Go drops it), so the post-pairing 515 that arrives with the socket close still triggers the login. ClientConfig::media_base_url(not in whatsmeow) sends media uploads, downloads and deletes to onescheme://host[:port]instead of the media connection's hosts, for a local fake media server in tests.ClientConfig::dialeropens the websocket;testing::FakeServer(featuretesting) provides an in-memory one andtesting::FakeSignala fake Signal implementation for service tests.
What is ported
Client core (client, request, connectionevents, keepalive, qrchan,
pair, prekeys, notification, tctoken, cstoken), receiving
(message, receipt, retry, history sync download), sending (send:
1:1, groups with sender keys, peer messages, revoke/reaction/edit
builders), user, group, presence, app state (appstate package and
its client in appstate/client.rs), media (mediaconn, download,
download_to_file, upload, mediaretry), pairing by QR, phone number
code (pair_code) and passkey (pair_passkey), privacysettings,
msgsecret (encrypted reactions, comments, poll votes, secret edits).
Not ported (skipped features): newsletters, calls, status/broadcast lists,
business profiles and links, communities admin, bots and Meta AI (msmsg,
WASA), Messenger/Instagram (sendfb.go, Armadillo v=3, FB device lists),
WASA/bot parts of msgsecret.go, reporting tokens, armadillomessage.go, push.go,
update.go, store/sqlstore (the Postgres store lives in
crates/whatsapp_store_pg).
Porting rules
- Mirror the Go layout: Go package
x/y→ modulex::y(src/x/y.rsre-exporting its files),x/y/file.go→src/x/y/file.rs, GoFuncName→func_name. A package whose only file shares its name collapses intosrc/x/y.rs(binary/token). Top-level Go files (handshake.go) map tosrc/<file>.rs. - Every ported file starts with the MPL-2.0 header and a line naming the Go
file it ports:
// Ported from whatsmeow <path> (MPL-2.0, Copyright (c) Tulir Asokan). Files that aren't ports (signal.rs,store/memorystore.rs) say so. - Keep Go's logic, including its string splitting; repo rules such as CS-58
(nom) don't override a line-by-line port. Where Go panics, silently
truncates, or uses a sentinel/
Foundflag, return an error orOptionand say so in the doc comment. - Go callbacks and channels become event receivers (
socket::FrameEvent,socket::NoiseEvent);context.Contextparameters are dropped. - Protobuf types come from
crates/whatsmeow_proto, re-exported asproto. - whatsmeow has almost no Go tests. Tests here use expected values produced by
running whatsmeow's Go code at the pinned revision, so the port is checked
byte for byte. The generators live in
testdata/generator/(see its README):inpackage/generate.shcopies its*_test.gofiles into a whatsmeow checkout (they arepackage whatsmeow, so they reach unexported code) and writestestdata/*.json, read bysrc/vectors.rs; themediaandappstateprograms reach unexported code withgo:linknameand writetestdata/<area>/vectors.json. - The media functions are free functions taking what the client supplies
(
download::download(&MediaHttpClient, &MediaConn, msg));Clientwraps them. - Client behaviour (pairing, login, IQs, reconnects) is tested against the
fake server in
src/testing.rsover in-memory websockets, with#[tokio::test(start_paused = true)]so timeouts run instantly. - Nodes the client sends are checked against Go's: the generator's
captureNoderecords what a Go client method sends (its send log fires right before a zeroNoiseSocketpanics), andclient::test::capture_requestgets the Rust side; both replace the iqidand usyncsid. - Go formats JIDs (and sometimes node XML) into error messages; ported errors
leave them out, since
#[tracing::instrument(err)]logs them.IqErrornames only the node's tag, andSignalError::Internal/StoreError::Internalleave their report out of the message (it stays in the field andDebug);errors/test.rsformats each and asserts no JID. - Methods that spawn work which can reconnect return
BoxFuture(seeClient::auto_reconnect): without it,Sendinference cycles throughconnect. - Log opaque values only (tags, IDs, counts); unlike Go, never log node XML, JIDs or phone numbers.
Telemetry (a Macro addition)
whatsmeow has no tracing spans; the spans here are Macro's, added around the
ported logic without changing it. Helpers live in src/telemetry.rs; its test
captures spans from a client on the fake server.
- Spans nest under the caller:
Client::connectstores the current span (connection_parent), the connection's loops, reconnects and history sync run under it, andClient::spawnkeeps the spawning span (in_current_span). whatsmeow.connect(reason:initial,restart_515,network_drop,forced_reset,keepalive_timeout,connect_failed;attempt) with children.dial,.handshake,.login(ends atsuccess/failure, or after the post-login prekey and passive requests). Disconnects that don't reconnect logdisconnect_reason(conflict_replaced,logged_out).whatsmeow.iqper info query:xmlns,iq_type,kind(first child tag),outcome(ok,error+error_code,timeout,disconnected). Noterr: an IQ error's message can hold the raw response node.- Receive:
whatsmeow.receive_messagecountsdecrypted, flagsdecrypt_failed/retry_receipt, recordsack_ms; each<enc>gets a DEBUGwhatsmeow.decryptspan (enc_type,outcome). - Send (
devicesfan-out, encrypt andwait_ackphases), media (whatsmeow.media.encrypt,.upload_http,.fetchwithsize,retries,hash_check), app state (patches,events),whatsmeow.history_sync.process,whatsmeow.prekeys.upload,whatsmeow.retry_receipt.send. - Failed spans set
otel.status_code = "ERROR"(declare itEmpty). Fields are kinds, counts, outcomes and random message ids; never JIDs, phone numbers, content or keys.
Signal protocol boundary
Every Signal call goes through the traits in src/signal.rs:
CurveSignatures: XEdDSA signing and verification for prekeys, pairing, and the server's Noise certificate.SignalProtocol: pairwise sessions and group sender keys.SignalProtocolStore: opaque protocol records, implemented bystore::DeviceSignalStore.
crates/whatsmeow_signal implements these traits with Signal's official
AGPL-3.0-only libsignal core and vendored X3DH protocol modules. The repository
is AGPL-3.0; retain MPL-2.0 headers on the whatsmeow port and Signal's SPDX
headers and NOTICE on its protocol implementation. Client unit tests use fakes;
Signal interop and linked-device end-to-end tests exercise the implementation.
Checks
cargo test -p whatsmeow -p whatsmeow_proto- After changing a generator:
go run -ldflags=-checklinkname=0 ./appstateintestdata/generator, then the tests.
Gotchas
License is MPL-2.0 (
LICENSE), file-level copyleft: keep ported code in these crates, don't copy it into Macro crates.Never connect to real WhatsApp servers or link a real account from tests; socket tests run over in-memory websockets (
tokio::io::duplex).FrameSocket::connectdialswss://with rustls; the binary must install a rustls crypto provider first.Media functions take
download::MediaHttpClient; its crate-privateschemeandhost_override(set bywith_base_url) let tests talk plain HTTP to local fake hosts (Go hardcodeshttps). Tests never contact WhatsApp media hosts.Regenerating
testdata/media/vectors.jsonchanges it every run: Go'sUploadpicks a random media key, which the vectors record.Node hex in the
inpackagevectors changes every run (Go map order of attributes), so tests compare unmarshaled nodes; revert files whose only change is that churn.tungstenite has no permessage-deflate, so connections are uncompressed (Go negotiates compression).
store::clientpayloadkeeps the WA version and OS info in process-wide locks, as Go keeps package variables; tests use the private builders instead of the setters so they don't race.Async closures that borrow their captures don't produce
Sendfutures, so a Go callback awaited inside an async fn becomes an enum or trait (appstate::PreviousValueMacs);appstate/keys/test.rsasserts theProcessorfutures areSend.Go's appstate encodes with random IVs, so encoded patches are tested by decoding them with MAC checks, not byte comparison.
Go marshals node attributes in random map order: compare Go-generated nodes decoded (
Vectors::node), not as bytes. In-package generators capture a request by logging it throughsendLogand sending with a cancelled context (appstate_client_vectors_test.go).Go's
EncodePatchdoesn't set the patch version (the server does) and doesn't look inside its own patch for an earlier SET of the same index, so a test patch must not set one index twice (BuildArchivealso unpins).
Related
crates/whatsmeow_proto/AGENTS.md