@zapo-js/fake-server 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +89 -30
  2. package/dist/api/FakeMobilePrimary.d.ts +55 -0
  3. package/dist/api/FakeMobilePrimary.js +106 -0
  4. package/dist/api/FakePairingDriver.js +1 -3
  5. package/dist/api/FakeServerSession.d.ts +35 -2
  6. package/dist/api/FakeServerSession.js +65 -3
  7. package/dist/api/FakeWaServer.d.ts +73 -1
  8. package/dist/api/FakeWaServer.js +230 -4
  9. package/dist/api/ServerRegistries.d.ts +7 -0
  10. package/dist/api/ServerRegistries.js +20 -5
  11. package/dist/api/iq-handlers.d.ts +19 -1
  12. package/dist/api/iq-handlers.js +96 -1
  13. package/dist/esm/api/FakeMobilePrimary.js +102 -0
  14. package/dist/esm/api/FakePairingDriver.js +2 -4
  15. package/dist/esm/api/FakeServerSession.js +66 -4
  16. package/dist/esm/api/FakeWaServer.js +230 -4
  17. package/dist/esm/api/ServerRegistries.js +20 -5
  18. package/dist/esm/api/iq-handlers.js +96 -1
  19. package/dist/esm/index.js +8 -0
  20. package/dist/esm/infra/WaFakeConnection.js +8 -22
  21. package/dist/esm/infra/WaFakeConnectionPipeline.js +17 -5
  22. package/dist/esm/infra/WaFakeTcpServer.js +69 -0
  23. package/dist/esm/infra/WaFakeWsServer.js +2 -1
  24. package/dist/esm/infra/socket-adapters.js +79 -0
  25. package/dist/esm/protocol/auth/client-payload-validate.js +77 -1
  26. package/dist/esm/protocol/auth/pair-device.js +16 -1
  27. package/dist/esm/protocol/iq/companion-host.js +104 -0
  28. package/dist/esm/protocol/iq/link-code.js +90 -0
  29. package/dist/esm/protocol/iq/router.js +3 -3
  30. package/dist/esm/protocol/push/mobile-notification.js +97 -0
  31. package/dist/esm/state/fake-companion-host.js +158 -0
  32. package/dist/esm/transport/auth.js +7 -0
  33. package/dist/esm/transport/crypto.js +1 -1
  34. package/dist/esm/transport/util.js +1 -0
  35. package/dist/index.d.ts +17 -2
  36. package/dist/index.js +28 -1
  37. package/dist/infra/WaFakeConnection.d.ts +21 -3
  38. package/dist/infra/WaFakeConnection.js +8 -22
  39. package/dist/infra/WaFakeConnectionPipeline.d.ts +1 -0
  40. package/dist/infra/WaFakeConnectionPipeline.js +17 -5
  41. package/dist/infra/WaFakeTcpServer.d.ts +29 -0
  42. package/dist/infra/WaFakeTcpServer.js +73 -0
  43. package/dist/infra/WaFakeWsServer.js +2 -1
  44. package/dist/infra/socket-adapters.d.ts +18 -0
  45. package/dist/infra/socket-adapters.js +83 -0
  46. package/dist/protocol/auth/client-payload-validate.d.ts +24 -0
  47. package/dist/protocol/auth/client-payload-validate.js +77 -1
  48. package/dist/protocol/auth/pair-device.d.ts +15 -1
  49. package/dist/protocol/auth/pair-device.js +16 -0
  50. package/dist/protocol/iq/companion-host.d.ts +52 -0
  51. package/dist/protocol/iq/companion-host.js +109 -0
  52. package/dist/protocol/iq/link-code.d.ts +46 -0
  53. package/dist/protocol/iq/link-code.js +95 -0
  54. package/dist/protocol/iq/router.d.ts +15 -2
  55. package/dist/protocol/iq/router.js +3 -3
  56. package/dist/protocol/push/mobile-notification.d.ts +58 -0
  57. package/dist/protocol/push/mobile-notification.js +102 -0
  58. package/dist/state/fake-companion-host.d.ts +73 -0
  59. package/dist/state/fake-companion-host.js +163 -0
  60. package/dist/transport/auth.d.ts +8 -0
  61. package/dist/transport/auth.js +8 -0
  62. package/dist/transport/crypto.d.ts +1 -1
  63. package/dist/transport/crypto.js +2 -1
  64. package/dist/transport/util.d.ts +1 -0
  65. package/dist/transport/util.js +2 -1
  66. package/package.json +1 -1
@@ -10,17 +10,20 @@ import { WaFakeNoiseHandshake } from './WaFakeNoiseHandshake.js';
10
10
  import { WaFakeTransport } from './WaFakeTransport.js';
11
11
  const NOISE_XX_NAME = new TextEncoder().encode('Noise_XX_25519_AESGCM_SHA256\0\0\0\0');
12
12
  const NOISE_IK_NAME = new TextEncoder().encode('Noise_IK_25519_AESGCM_SHA256\0\0\0\0');
13
- const PROLOGUE = new Uint8Array([0x57, 0x41, 0x06, 0x03]);
13
+ const PROLOGUE_MAGIC_W = 0x57;
14
+ const PROLOGUE_MAGIC_A = 0x41;
15
+ const DEFAULT_PROLOGUE = new Uint8Array([PROLOGUE_MAGIC_W, PROLOGUE_MAGIC_A, 0x06, 0x03]);
14
16
  export class WaFakeConnectionPipeline {
15
17
  constructor(config) {
16
18
  this.events = {};
17
19
  this.state = { kind: 'awaiting_prologue' };
18
20
  this.chain = Promise.resolve();
19
21
  this.authenticatedPayload = null;
22
+ this.clientPrologue = DEFAULT_PROLOGUE;
20
23
  this.config = config;
21
24
  this.frameSocket = new WaFakeFrameSocket(config.connection);
22
25
  this.frameSocket.setHandlers({
23
- onPrologue: () => this.onPrologue(),
26
+ onPrologue: (prologue) => this.onPrologue(prologue),
24
27
  onFrame: (frame) => this.scheduleFrame(frame),
25
28
  onClose: (info) => {
26
29
  this.state = { kind: 'closed' };
@@ -48,11 +51,20 @@ export class WaFakeConnectionPipeline {
48
51
  const ciphertext = await this.state.transport.encryptFrame(stanza);
49
52
  this.frameSocket.sendFrame(ciphertext);
50
53
  }
51
- onPrologue() {
54
+ onPrologue(prologue) {
52
55
  if (this.state.kind !== 'awaiting_prologue') {
53
56
  this.events.onError?.(new Error('received second prologue from client'));
54
57
  return;
55
58
  }
59
+ // Mix back the header the client actually sent instead of a fixed one:
60
+ // it is hashed into the handshake, and a client is free to advertise a
61
+ // protocol/dict version other than the web client's 6/3.
62
+ this.clientPrologue = new Uint8Array([
63
+ PROLOGUE_MAGIC_W,
64
+ PROLOGUE_MAGIC_A,
65
+ prologue.protocolVersion,
66
+ prologue.dictVersion
67
+ ]);
56
68
  this.state = { kind: 'awaiting_client_hello' };
57
69
  }
58
70
  scheduleFrame(frame) {
@@ -94,7 +106,7 @@ export class WaFakeConnectionPipeline {
94
106
  }
95
107
  async handleXxClientHello(clientEphemeralPub) {
96
108
  const handshake = new WaFakeNoiseHandshake();
97
- handshake.start(NOISE_XX_NAME, PROLOGUE);
109
+ handshake.start(NOISE_XX_NAME, this.clientPrologue);
98
110
  handshake.authenticate(clientEphemeralPub);
99
111
  const serverEphemeral = await X25519.generateKeyPair();
100
112
  handshake.authenticate(serverEphemeral.pubKey);
@@ -130,7 +142,7 @@ export class WaFakeConnectionPipeline {
130
142
  const encryptedClientStatic = clientHello.static;
131
143
  const encryptedClientPayload = clientHello.payload;
132
144
  const handshake = new WaFakeNoiseHandshake();
133
- handshake.start(NOISE_IK_NAME, PROLOGUE);
145
+ handshake.start(NOISE_IK_NAME, this.clientPrologue);
134
146
  handshake.authenticate(this.config.serverStaticKeyPair.pubKey);
135
147
  handshake.authenticate(clientEphemeralPub);
136
148
  handshake.mixIntoKey(await X25519.scalarMult(this.config.serverStaticKeyPair.privKey, clientEphemeralPub));
@@ -0,0 +1,69 @@
1
+ import { createServer } from 'node:net';
2
+ import { createTcpSocketAdapter } from './socket-adapters.js';
3
+ import { WaFakeConnection } from './WaFakeConnection.js';
4
+ /**
5
+ * Raw-TCP listener for the WhatsApp Mobile transport. The mobile client dials
6
+ * `tcp://host:port` instead of upgrading to a WebSocket, but speaks the exact
7
+ * same prologue + length-prefixed frames on top, so every layer above the
8
+ * carrier is shared with {@link WaFakeWsServer}.
9
+ */
10
+ export class WaFakeTcpServer {
11
+ constructor(options = {}) {
12
+ this.server = null;
13
+ this.connectionListener = null;
14
+ this.sockets = new Set();
15
+ this.nextConnectionId = 0;
16
+ this.options = {
17
+ host: options.host ?? '127.0.0.1',
18
+ port: options.port ?? 0
19
+ };
20
+ }
21
+ onConnection(listener) {
22
+ this.connectionListener = listener;
23
+ }
24
+ async listen() {
25
+ if (this.server) {
26
+ throw new Error('fake tcp server is already listening');
27
+ }
28
+ const server = createServer((socket) => {
29
+ this.sockets.add(socket);
30
+ socket.on('close', () => this.sockets.delete(socket));
31
+ const id = `t${this.nextConnectionId++}`;
32
+ this.connectionListener?.(new WaFakeConnection(id, createTcpSocketAdapter(socket)));
33
+ });
34
+ await new Promise((resolve, reject) => {
35
+ const onError = (error) => {
36
+ server.off('listening', onListening);
37
+ reject(error);
38
+ };
39
+ const onListening = () => {
40
+ server.off('error', onError);
41
+ resolve();
42
+ };
43
+ server.once('error', onError);
44
+ server.once('listening', onListening);
45
+ server.listen(this.options.port, this.options.host);
46
+ });
47
+ this.server = server;
48
+ const address = server.address();
49
+ return {
50
+ host: address.address,
51
+ port: address.port,
52
+ url: `tcp://${address.address}:${address.port}`
53
+ };
54
+ }
55
+ async close() {
56
+ const server = this.server;
57
+ if (!server) {
58
+ return;
59
+ }
60
+ for (const socket of this.sockets) {
61
+ socket.destroy();
62
+ }
63
+ this.sockets.clear();
64
+ await new Promise((resolve, reject) => {
65
+ server.close((error) => (error ? reject(error) : resolve()));
66
+ });
67
+ this.server = null;
68
+ }
69
+ }
@@ -1,5 +1,6 @@
1
1
  import { createServer } from 'node:http';
2
2
  import { WebSocketServer } from 'ws';
3
+ import { createWebSocketAdapter } from './socket-adapters.js';
3
4
  import { WaFakeConnection } from './WaFakeConnection.js';
4
5
  export class WaFakeWsServer {
5
6
  constructor(options = {}) {
@@ -49,7 +50,7 @@ export class WaFakeWsServer {
49
50
  const wsServer = new WebSocketServer({ server: httpServer, path: this.options.path });
50
51
  wsServer.on('connection', (socket) => {
51
52
  const id = `c${this.nextConnectionId++}`;
52
- const connection = new WaFakeConnection(id, socket);
53
+ const connection = new WaFakeConnection(id, createWebSocketAdapter(socket));
53
54
  this.connectionListener?.(connection);
54
55
  });
55
56
  await new Promise((resolve, reject) => {
@@ -0,0 +1,79 @@
1
+ /** Carrier adapters: WebSocket (companion transport) and raw TCP (mobile). */
2
+ import { TEXT_DECODER } from '../transport/util.js';
3
+ /**
4
+ * Adapts a `ws` socket to {@link WaFakeSocketLike}. Owns the two WebSocket
5
+ * quirks the rest of the server should not know about: a message can arrive as
6
+ * a fragment array, and a text frame is a protocol violation here because every
7
+ * WhatsApp frame is binary.
8
+ */
9
+ export function createWebSocketAdapter(socket) {
10
+ return {
11
+ send: (frame) => socket.send(frame),
12
+ close: (code, reason) => socket.close(code, reason),
13
+ listen: (events) => {
14
+ socket.on('message', (data, isBinary) => {
15
+ if (!isBinary) {
16
+ events.onError(new Error('received unexpected text frame'));
17
+ return;
18
+ }
19
+ events.onFrame(toFrameBytes(data));
20
+ });
21
+ socket.on('close', (code, reason) => {
22
+ events.onClose({ code, reason: TEXT_DECODER.decode(reason) });
23
+ });
24
+ socket.on('error', (error) => {
25
+ events.onError(error);
26
+ });
27
+ }
28
+ };
29
+ }
30
+ /**
31
+ * Adapts a Node TCP socket to {@link WaFakeSocketLike} for the mobile
32
+ * transport. A TCP stream has no message boundaries and no close codes: every
33
+ * chunk is forwarded as-is (the frame codec above reassembles them) and the
34
+ * code/reason the server asked for is replayed when the socket finally closes.
35
+ */
36
+ export function createTcpSocketAdapter(socket) {
37
+ let closeCode = 1000;
38
+ let closeReason = '';
39
+ return {
40
+ send: (frame) => {
41
+ socket.write(frame);
42
+ },
43
+ close: (code, reason) => {
44
+ closeCode = code;
45
+ closeReason = reason;
46
+ socket.end();
47
+ },
48
+ listen: (events) => {
49
+ socket.on('data', (chunk) => {
50
+ events.onFrame(chunk);
51
+ });
52
+ socket.on('close', () => {
53
+ events.onClose({ code: closeCode, reason: closeReason });
54
+ });
55
+ socket.on('error', (error) => {
56
+ events.onError(error);
57
+ });
58
+ }
59
+ };
60
+ }
61
+ function toFrameBytes(data) {
62
+ if (data instanceof Uint8Array) {
63
+ return data;
64
+ }
65
+ if (data instanceof ArrayBuffer) {
66
+ return new Uint8Array(data);
67
+ }
68
+ let total = 0;
69
+ for (const part of data) {
70
+ total += part.byteLength;
71
+ }
72
+ const out = new Uint8Array(total);
73
+ let offset = 0;
74
+ for (const part of data) {
75
+ out.set(part, offset);
76
+ offset += part.byteLength;
77
+ }
78
+ return out;
79
+ }
@@ -27,14 +27,90 @@ export function parseClientPayload(bytes) {
27
27
  if (raw.username === undefined || raw.username === null) {
28
28
  throw new ClientPayloadValidationError('missing_username', 'login ClientPayload is missing the username field');
29
29
  }
30
+ const flavor = resolveLoginFlavor(raw);
30
31
  return {
31
32
  kind: 'login',
32
33
  raw,
33
34
  username: String(raw.username),
34
35
  device: typeof raw.device === 'number' ? raw.device : 0,
35
- loginCounter: typeof raw.lc === 'number' ? raw.lc : 0
36
+ loginCounter: typeof raw.lc === 'number' ? raw.lc : 0,
37
+ flavor,
38
+ mobile: flavor === 'mobile' ? parseMobileDetails(raw) : null
36
39
  };
37
40
  }
41
+ /**
42
+ * Resolves the transport flavor of a login payload. `webInfo` and the `WEB`
43
+ * platform mark a companion; a phone is identified by the device fields only
44
+ * the mobile payload carries. Sniffing those fields (rather than the platform
45
+ * enum alone) is deliberate: `ANDROID` is the zero value of the enum, so a
46
+ * payload that omits `platform` entirely would otherwise read as mobile.
47
+ */
48
+ function resolveLoginFlavor(raw) {
49
+ if (raw.webInfo) {
50
+ return 'web';
51
+ }
52
+ const userAgent = raw.userAgent;
53
+ if (!userAgent || userAgent.platform === proto.ClientPayload.UserAgent.Platform.WEB) {
54
+ return 'web';
55
+ }
56
+ return userAgent.manufacturer && userAgent.device ? 'mobile' : 'web';
57
+ }
58
+ function parseMobileDetails(raw) {
59
+ const userAgent = raw.userAgent;
60
+ if (!userAgent) {
61
+ throw new ClientPayloadValidationError('missing_field', 'mobile login ClientPayload is missing the userAgent');
62
+ }
63
+ const required = [
64
+ ['manufacturer', 'device manufacturer'],
65
+ ['device', 'device model'],
66
+ ['osVersion', 'os version'],
67
+ ['osBuildNumber', 'os build number'],
68
+ ['phoneId', 'phone id']
69
+ ];
70
+ for (const [field, label] of required) {
71
+ const value = userAgent[field];
72
+ if (value === undefined || value === null || value === '') {
73
+ throw new ClientPayloadValidationError('missing_field', `mobile login ClientPayload missing ${label} (userAgent.${String(field)})`);
74
+ }
75
+ }
76
+ const appVersion = userAgent.appVersion;
77
+ if (!appVersion || appVersion.primary === undefined || appVersion.primary === null) {
78
+ throw new ClientPayloadValidationError('missing_field', 'mobile login ClientPayload missing the app version (userAgent.appVersion)');
79
+ }
80
+ return {
81
+ manufacturer: String(userAgent.manufacturer),
82
+ device: String(userAgent.device),
83
+ osVersion: String(userAgent.osVersion),
84
+ osBuildNumber: String(userAgent.osBuildNumber),
85
+ appVersion: formatAppVersion(appVersion),
86
+ phoneId: String(userAgent.phoneId),
87
+ mcc: userAgent.mcc ? String(userAgent.mcc) : '000',
88
+ mnc: userAgent.mnc ? String(userAgent.mnc) : '000',
89
+ pushName: typeof raw.pushName === 'string' ? raw.pushName : null,
90
+ yearClass: toOptionalNumber(raw.yearClass),
91
+ memClass: toOptionalNumber(raw.memClass)
92
+ };
93
+ }
94
+ function formatAppVersion(version) {
95
+ const parts = [
96
+ version.primary,
97
+ version.secondary,
98
+ version.tertiary,
99
+ version.quaternary,
100
+ version.quinary
101
+ ];
102
+ const present = [];
103
+ for (const part of parts) {
104
+ if (part === undefined || part === null) {
105
+ break;
106
+ }
107
+ present.push(Number(part));
108
+ }
109
+ return present.join('.');
110
+ }
111
+ function toOptionalNumber(value) {
112
+ return typeof value === 'number' && Number.isFinite(value) ? value : null;
113
+ }
38
114
  function validateRegistrationFields(devicePairingData) {
39
115
  const required = [
40
116
  ['eIdent', 'identity public key'],
@@ -1,5 +1,20 @@
1
1
  /** Pairing IQ builders/parsers (source: `WAWebHandlePairDevice*`, `/wa-web`). */
2
- import { decodeBase64Url } from '../../transport/util.js';
2
+ import { randomBytesAsync } from '../../transport/crypto.js';
3
+ import { bytesToBase64UrlSafe, decodeBase64Url } from '../../transport/util.js';
4
+ const PAIR_DEVICE_REF_COUNT = 6;
5
+ const PAIR_DEVICE_REF_BYTES = 16;
6
+ /**
7
+ * Mints the rotating refs a `pair-device` push carries.
8
+ *
9
+ * They are printable on purpose. A client reads `<ref>` as text and pastes it
10
+ * into a comma-separated QR payload, so raw random bytes can decode into a
11
+ * comma or a newline and quietly corrupt whatever reads that payload back -
12
+ * including the primary that has to echo the ref in its upload.
13
+ */
14
+ export async function mintPairingRefs(count = PAIR_DEVICE_REF_COUNT) {
15
+ const raw = await Promise.all(Array.from({ length: count }, () => randomBytesAsync(PAIR_DEVICE_REF_BYTES)));
16
+ return raw.map((bytes) => bytesToBase64UrlSafe(bytes));
17
+ }
3
18
  export function buildPairDeviceIq(input) {
4
19
  if (input.refs.length !== 6) {
5
20
  throw new Error(`pair-device requires exactly 6 refs, got ${input.refs.length}`);
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Primary-side `md` IQ parsers: a phone hosting companion devices.
3
+ *
4
+ * These are the inverse of `protocol/auth/pair-device.ts`, which drives the
5
+ * companion side. Here the client is the primary: it signs a companion's device
6
+ * identity and uploads it, republishes its key-index list, and revokes devices.
7
+ */
8
+ import { TEXT_DECODER } from '../../transport/util.js';
9
+ /**
10
+ * Parses the primary's `<iq type="set" xmlns="md"><pair-device>` upload.
11
+ * Returns `null` when the stanza is not a well-formed pair-device upload, which
12
+ * lets the router fall through to another handler.
13
+ */
14
+ export function parsePairDeviceUpload(iq) {
15
+ const pairDevice = findChild(iq, 'pair-device');
16
+ if (!pairDevice) {
17
+ return null;
18
+ }
19
+ const ref = readText(findChild(pairDevice, 'ref'));
20
+ const companionNoisePublicKey = readBytes(findChild(pairDevice, 'pub-key'));
21
+ const deviceIdentityBytes = readBytes(findChild(pairDevice, 'device-identity'));
22
+ const keyIndexList = findChild(pairDevice, 'key-index-list');
23
+ const keyIndexListBytes = readBytes(keyIndexList);
24
+ if (!ref || !companionNoisePublicKey || !deviceIdentityBytes || !keyIndexListBytes) {
25
+ return null;
26
+ }
27
+ return {
28
+ ref,
29
+ companionNoisePublicKey,
30
+ deviceIdentityBytes,
31
+ keyIndexListBytes,
32
+ keyIndexListTimestampSeconds: readTimestampSeconds(keyIndexList),
33
+ clientPropsBytes: readBytes(findChild(pairDevice, 'client-props')),
34
+ pemKeyBytes: readPemKey(pairDevice)
35
+ };
36
+ }
37
+ /**
38
+ * Parses the standalone key-index-list republish the primary sends whenever its
39
+ * companion set changes. Returns `null` when the stanza carries no list.
40
+ */
41
+ export function parseKeyIndexListPublish(iq) {
42
+ const keyIndexList = findChild(iq, 'key-index-list');
43
+ const keyIndexListBytes = readBytes(keyIndexList);
44
+ if (!keyIndexListBytes) {
45
+ return null;
46
+ }
47
+ return {
48
+ keyIndexListBytes,
49
+ timestampSeconds: readTimestampSeconds(keyIndexList)
50
+ };
51
+ }
52
+ /**
53
+ * Parses `<remove-companion-device>`. The same stanza serves a companion
54
+ * logging itself out (`jid` = its own device) and a primary unlinking one of
55
+ * its companions, so the caller decides by comparing against the connection's
56
+ * own jid.
57
+ */
58
+ export function parseRemoveCompanionDevice(iq) {
59
+ const remove = findChild(iq, 'remove-companion-device');
60
+ if (!remove) {
61
+ return null;
62
+ }
63
+ const jid = remove.attrs.jid;
64
+ return {
65
+ deviceJid: typeof jid === 'string' && jid.length > 0 ? jid : null,
66
+ all: remove.attrs.all === 'true',
67
+ reason: typeof remove.attrs.reason === 'string' ? remove.attrs.reason : '',
68
+ excludeHostedCompanion: remove.attrs.exclude_hosted_companion === 'true'
69
+ };
70
+ }
71
+ function findChild(parent, tag) {
72
+ if (!Array.isArray(parent.content)) {
73
+ return null;
74
+ }
75
+ return parent.content.find((child) => child.tag === tag) ?? null;
76
+ }
77
+ function readBytes(node) {
78
+ if (!node) {
79
+ return null;
80
+ }
81
+ return node.content instanceof Uint8Array ? node.content : null;
82
+ }
83
+ function readText(node) {
84
+ if (!node) {
85
+ return null;
86
+ }
87
+ if (typeof node.content === 'string') {
88
+ return node.content;
89
+ }
90
+ return node.content instanceof Uint8Array ? TEXT_DECODER.decode(node.content) : null;
91
+ }
92
+ function readTimestampSeconds(node) {
93
+ const raw = node?.attrs.ts;
94
+ const parsed = typeof raw === 'string' ? Number.parseInt(raw, 10) : Number.NaN;
95
+ return Number.isFinite(parsed) ? parsed : 0;
96
+ }
97
+ /** The `<pem>` wrapper nests the key bytes in a second `<pem>` element. */
98
+ function readPemKey(pairDevice) {
99
+ const pem = findChild(pairDevice, 'pem');
100
+ if (!pem) {
101
+ return null;
102
+ }
103
+ return readBytes(findChild(pem, 'pem'));
104
+ }
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Link-code (pairing-code) relay stanzas.
3
+ *
4
+ * The handshake runs between two clients with the server in the middle: the
5
+ * companion asks for a code (`companion_hello`), the primary answers with its
6
+ * wrapped ephemeral (`primary_hello`), and the companion closes with the key
7
+ * bundle (`companion_finish`). Each stage arrives as an IQ from one client and
8
+ * leaves as a notification to the other; the server only routes and mints the
9
+ * ref that ties the three together.
10
+ */
11
+ import { TEXT_DECODER } from '../../transport/util.js';
12
+ const RELAYED_STAGES = new Set([
13
+ 'companion_hello',
14
+ 'primary_hello',
15
+ 'companion_finish'
16
+ ]);
17
+ /**
18
+ * Parses a `<link_code_companion_reg>` IQ. Returns `null` for a stage this
19
+ * server does not relay (for example `get_country_code`), letting the router
20
+ * fall through to another handler.
21
+ */
22
+ export function parseLinkCodeStanza(iq) {
23
+ const node = findChild(iq, 'link_code_companion_reg');
24
+ const stage = node?.attrs.stage;
25
+ if (!node || typeof stage !== 'string' || !RELAYED_STAGES.has(stage)) {
26
+ return null;
27
+ }
28
+ const jid = node.attrs.jid;
29
+ return {
30
+ stage: stage,
31
+ phoneJid: typeof jid === 'string' && jid.length > 0 ? jid : null,
32
+ ref: readText(findChild(node, 'link_code_pairing_ref')),
33
+ children: Array.isArray(node.content) ? node.content : []
34
+ };
35
+ }
36
+ /**
37
+ * Builds the `companion_hello` IQ result carrying the ref the server minted.
38
+ * The companion keys its whole pairing session off it.
39
+ */
40
+ export function buildCompanionHelloResultContent(ref) {
41
+ return [
42
+ {
43
+ tag: 'link_code_companion_reg',
44
+ attrs: { stage: 'companion_hello' },
45
+ content: [{ tag: 'link_code_pairing_ref', attrs: {}, content: ref }]
46
+ }
47
+ ];
48
+ }
49
+ /**
50
+ * Wraps a relayed stage as the notification the receiving client handles. The
51
+ * ref is (re)stamped so the receiver can match it against its pairing session,
52
+ * replacing any copy carried by the source stanza.
53
+ */
54
+ export function buildLinkCodeNotification(input) {
55
+ const children = input.children.filter((child) => child.tag !== 'link_code_pairing_ref');
56
+ return {
57
+ tag: 'notification',
58
+ attrs: {
59
+ id: input.id ?? `link-code-${Math.random().toString(36).slice(2, 10)}`,
60
+ from: 's.whatsapp.net',
61
+ type: 'link_code_companion_reg',
62
+ ...(input.to !== undefined ? { to: input.to } : {})
63
+ },
64
+ content: [
65
+ {
66
+ tag: 'link_code_companion_reg',
67
+ attrs: { stage: input.stage },
68
+ content: [
69
+ { tag: 'link_code_pairing_ref', attrs: {}, content: input.ref },
70
+ ...children
71
+ ]
72
+ }
73
+ ]
74
+ };
75
+ }
76
+ function findChild(parent, tag) {
77
+ if (!Array.isArray(parent.content)) {
78
+ return null;
79
+ }
80
+ return parent.content.find((child) => child.tag === tag) ?? null;
81
+ }
82
+ function readText(node) {
83
+ if (!node) {
84
+ return null;
85
+ }
86
+ if (typeof node.content === 'string') {
87
+ return node.content;
88
+ }
89
+ return node.content instanceof Uint8Array ? TEXT_DECODER.decode(node.content) : null;
90
+ }
@@ -19,13 +19,13 @@ export class WaFakeIqRouter {
19
19
  setEvents(events) {
20
20
  this.events = events;
21
21
  }
22
- async route(iq) {
22
+ async route(iq, context) {
23
23
  if (iq.tag !== 'iq') {
24
24
  return null;
25
25
  }
26
26
  for (const handler of this.highPriorityHandlers) {
27
27
  if (matches(iq, handler.matcher)) {
28
- const response = await handler.respond(iq);
28
+ const response = await handler.respond(iq, context);
29
29
  if (response !== null) {
30
30
  return response;
31
31
  }
@@ -33,7 +33,7 @@ export class WaFakeIqRouter {
33
33
  }
34
34
  for (const handler of this.handlers) {
35
35
  if (matches(iq, handler.matcher)) {
36
- const response = await handler.respond(iq);
36
+ const response = await handler.respond(iq, context);
37
37
  if (response !== null) {
38
38
  return response;
39
39
  }
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Push notifications only a phone receives.
3
+ *
4
+ * `account_sync` carries the account's live device set: the primary keys its
5
+ * companion bookkeeping off it, allocating a fresh key index for the next link
6
+ * and dropping companions the server no longer lists. The registration
7
+ * notifications carry the login code and the takeover warning that fire while
8
+ * a number is being moved to another phone.
9
+ */
10
+ /**
11
+ * Builds `<notification type="account_sync">` with the account's `<devices>`
12
+ * list. Every device the account still owns must be present: the primary reads
13
+ * this as authoritative and prunes anything missing from it.
14
+ */
15
+ export function buildAccountSyncDevicesNotification(input) {
16
+ return {
17
+ tag: 'notification',
18
+ attrs: {
19
+ id: input.id ?? `account-sync-${Math.random().toString(36).slice(2, 10)}`,
20
+ from: input.from ?? 's.whatsapp.net',
21
+ type: 'account_sync',
22
+ t: String(input.timestampSeconds ?? Math.floor(Date.now() / 1000))
23
+ },
24
+ content: [
25
+ {
26
+ tag: 'devices',
27
+ attrs: {},
28
+ content: input.devices.map((device) => ({
29
+ tag: 'device',
30
+ attrs: {
31
+ jid: device.deviceJid,
32
+ ...(device.keyIndex !== undefined
33
+ ? { 'key-index': String(device.keyIndex) }
34
+ : {}),
35
+ ...(device.deviceLid !== undefined ? { lid: device.deviceLid } : {})
36
+ }
37
+ }))
38
+ }
39
+ ]
40
+ };
41
+ }
42
+ /**
43
+ * Builds the `<notification type="registration">` carrying a login code, which
44
+ * the client surfaces as `mobile_registration_code`.
45
+ */
46
+ export function buildRegistrationCodeNotification(input) {
47
+ const nowSeconds = Math.floor(Date.now() / 1000);
48
+ return {
49
+ tag: 'notification',
50
+ attrs: {
51
+ id: input.id ?? `registration-${Math.random().toString(36).slice(2, 10)}`,
52
+ from: input.from ?? 's.whatsapp.net',
53
+ type: 'registration'
54
+ },
55
+ content: [
56
+ {
57
+ tag: 'wa_old_registration',
58
+ attrs: {
59
+ code: input.code,
60
+ expiry_t: String(input.expirySeconds ?? nowSeconds + 300),
61
+ device_id: input.fromDeviceId
62
+ }
63
+ }
64
+ ]
65
+ };
66
+ }
67
+ /**
68
+ * Builds the `<notification type="registration">` warning that the number is
69
+ * being registered on another phone, surfaced as
70
+ * `mobile_account_takeover_notice`.
71
+ */
72
+ export function buildAccountTakeoverNotice(input) {
73
+ return {
74
+ tag: 'notification',
75
+ attrs: {
76
+ id: input.id ?? `takeover-${Math.random().toString(36).slice(2, 10)}`,
77
+ from: input.from ?? 's.whatsapp.net',
78
+ type: 'registration'
79
+ },
80
+ content: [
81
+ {
82
+ tag: 'device_logout',
83
+ attrs: {
84
+ id: input.serverToken,
85
+ t: String(input.attemptTimestampSeconds ?? Math.floor(Date.now() / 1000)),
86
+ ...(input.newDeviceName !== undefined ? { device: input.newDeviceName } : {}),
87
+ ...(input.newDevicePlatform !== undefined
88
+ ? { new_device_platform: input.newDevicePlatform }
89
+ : {}),
90
+ ...(input.newDeviceAppVersion !== undefined
91
+ ? { new_device_app_version: input.newDeviceAppVersion }
92
+ : {})
93
+ }
94
+ }
95
+ ]
96
+ };
97
+ }