wacore 0.6.0

Core WhatsApp protocol implementation without runtime dependencies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use anyhow::{Result, anyhow};
use wacore_binary::Jid;
use wacore_binary::builder::NodeBuilder;
use wacore_binary::{Node, NodeRef};

/// A LID mapping learned from usync response
#[derive(Debug, Clone)]
pub struct UsyncLidMapping {
    /// The phone number user part (e.g., "559980000001")
    pub phone_number: wacore_binary::CompactString,
    /// The LID user part (e.g., "100000012345678")
    pub lid: wacore_binary::CompactString,
}

#[derive(Debug, Clone)]
pub struct UsyncDevice {
    pub device: u16,
    pub key_index: Option<u32>,
}

#[derive(Debug, Clone)]
pub struct UserDeviceList {
    pub user: Jid,
    pub devices: Vec<UsyncDevice>,
    pub phash: Option<String>,
    pub key_index_bytes: Option<Vec<u8>>,
}

pub fn build_get_user_devices_query(jids: &[Jid], sid: &str) -> Node {
    let user_nodes = jids
        .iter()
        .map(|jid| {
            NodeBuilder::new("user")
                .attr("jid", jid.to_non_ad())
                .build()
        })
        .collect::<Vec<_>>();

    let query_node = NodeBuilder::new("query")
        .children([NodeBuilder::new("devices").attr("version", "2").build()])
        .build();

    let list_node = NodeBuilder::new("list").children(user_nodes).build();

    NodeBuilder::new("usync")
        .attrs([
            ("context", "message"),
            ("index", "0"),
            ("last", "true"),
            ("mode", "query"),
            ("sid", sid),
        ])
        .children([query_node, list_node])
        .build()
}

/// Parse usync response returning devices grouped by user with phash.
/// This is the full-featured version that includes the participant hash.
pub fn parse_get_user_devices_response_with_phash(resp_node: &Node) -> Result<Vec<UserDeviceList>> {
    let list_node = resp_node
        .get_optional_child_by_tag(&["usync", "list"])
        .ok_or_else(|| anyhow!("<usync> or <list> not found in usync response"))?;

    let mut result = Vec::new();

    for user_node in list_node.get_children_by_tag("user") {
        let user_jid = user_node.attrs().jid("jid");
        let device_list_node = user_node
            .get_optional_child_by_tag(&["devices", "device-list"])
            .ok_or_else(|| anyhow!("<device-list> not found for user {user_jid}"))?;

        // Extract phash from device-list node attributes
        let phash = device_list_node
            .attrs()
            .optional_string("hash")
            .map(|s| s.to_string());

        // Parse key-index-list from <devices> node (sibling of <device-list>)
        // WA Web: WAWebUsyncDevice.deviceParser() extracts both deviceList and keyIndex
        let devices_parent = user_node.get_optional_child("devices");
        let key_index_bytes = devices_parent
            .and_then(|dp| dp.get_optional_child("key-index-list"))
            .and_then(|ki| match &ki.content {
                Some(wacore_binary::NodeContent::Bytes(b)) if !b.is_empty() => Some(b.clone()),
                _ => None,
            });

        let capacity = device_list_node.children().map_or(0, |c| c.len());
        let mut devices = Vec::with_capacity(capacity);
        for device_node in device_list_node.get_children_by_tag("device") {
            let device_id_str = match device_node.attrs().optional_string("id") {
                Some(id) => id,
                None => {
                    log::warn!(target: "usync", "device node missing 'id' attribute, skipping");
                    continue;
                }
            };
            let device_id: u16 = match device_id_str.parse() {
                Ok(id) => id,
                Err(_) => {
                    log::warn!(target: "usync", "invalid device id '{device_id_str}' for user {user_jid}, skipping");
                    continue;
                }
            };

            let key_index = device_node
                .attrs()
                .optional_string("key-index")
                .and_then(|s| s.parse::<u32>().ok());
            devices.push(UsyncDevice {
                device: device_id,
                key_index,
            });
        }

        // WA Web: WAWebHandleAdvForUsyncApi.handleADVSyncResult() rejects usync results
        // that have companion devices but no signedKeyIndexBytes.
        let has_companion = devices.iter().any(|d| d.device != 0);
        if has_companion && key_index_bytes.is_none() {
            log::warn!(
                target: "usync",
                "User {user_jid} has companion devices but no signedKeyIndexBytes, skipping"
            );
            continue;
        }

        result.push(UserDeviceList {
            user: user_jid.to_non_ad(),
            devices,
            phash,
            key_index_bytes,
        });
    }

    Ok(result)
}

/// Parse usync response returning a flat list of device JIDs.
/// This is a convenience wrapper around `parse_get_user_devices_response_with_phash`.
pub fn parse_get_user_devices_response(resp_node: &Node) -> Result<Vec<Jid>> {
    Ok(parse_get_user_devices_response_with_phash(resp_node)?
        .into_iter()
        .flat_map(|u| {
            let user_jid = u.user;
            u.devices.into_iter().map(move |d| {
                let mut jid = user_jid.clone();
                jid.device = d.device;
                jid
            })
        })
        .collect())
}

/// Parse LID mappings from a usync `NodeRef` response (zero-copy path).
pub fn parse_lid_mappings_from_response(resp_node: &NodeRef<'_>) -> Vec<UsyncLidMapping> {
    let mut mappings = Vec::new();

    let list_node = match resp_node.get_optional_child_by_tag(&["usync", "list"]) {
        Some(node) => node,
        None => return mappings,
    };

    for user_node in list_node.get_children_by_tag("user") {
        let user_jid_str = match user_node.attrs().optional_string("jid") {
            Some(jid) => jid,
            None => continue,
        };
        let user_jid: Jid = match user_jid_str.parse() {
            Ok(j) => j,
            Err(_) => continue,
        };

        if user_jid.server != wacore_binary::Server::Pn {
            continue;
        }

        if let Some(lid_node) = user_node.get_optional_child("lid") {
            let lid_val = match lid_node.attrs().optional_string("val") {
                Some(v) => v,
                None => continue,
            };
            if !lid_val.is_empty()
                && let Ok(lid_jid) = lid_val.parse::<Jid>()
                && lid_jid.server == wacore_binary::Server::Lid
            {
                mappings.push(UsyncLidMapping {
                    phone_number: user_jid.user.clone(),
                    lid: lid_jid.user.clone(),
                });
            }
        }
    }

    mappings
}

#[cfg(test)]
mod tests {
    use super::*;
    use wacore_binary::builder::NodeBuilder;

    /// Helper to build a usync response node for testing.
    /// The structure matches actual server responses:
    /// <iq> (wrapper - this is resp_node)
    ///   <usync>
    ///     <list>
    ///       <user jid="...">
    ///         <devices>
    ///           <device-list hash="...">
    ///             <device id="0" />
    ///           </device-list>
    ///         </devices>
    ///       </user>
    ///     </list>
    ///   </usync>
    /// </iq>
    /// Build dummy ADV signed key index bytes for tests.
    fn build_test_key_index_bytes(device_ids: &[u16]) -> Vec<u8> {
        use prost::Message;
        let valid_indexes: Vec<u32> = device_ids.iter().map(|&id| id as u32).collect();
        let key_index = waproto::whatsapp::AdvKeyIndexList {
            raw_id: Some(1),
            timestamp: Some(1000),
            current_index: Some(valid_indexes.iter().copied().max().unwrap_or(0)),
            valid_indexes,
            account_type: None,
        };
        let signed = waproto::whatsapp::AdvSignedKeyIndexList {
            details: Some(key_index.encode_to_vec()),
            account_signature: None,
            account_signature_key: None,
        };
        signed.encode_to_vec()
    }

    fn build_usync_response(users: Vec<(&str, Vec<u16>, Option<&str>)>) -> Node {
        let user_nodes: Vec<Node> = users
            .into_iter()
            .map(|(jid, device_ids, phash)| {
                let device_nodes: Vec<Node> = device_ids
                    .iter()
                    .map(|id| NodeBuilder::new("device").attr("id", *id).build())
                    .collect();

                let mut device_list_builder = NodeBuilder::new("device-list");
                if let Some(hash) = phash {
                    device_list_builder = device_list_builder.attr("hash", hash);
                }
                let device_list = device_list_builder.children(device_nodes).build();

                // Add key-index-list if there are companion devices
                let has_companion = device_ids.iter().any(|&id| id != 0);
                let mut devices_children = vec![device_list];
                if has_companion {
                    let ki_bytes = build_test_key_index_bytes(&device_ids);
                    devices_children.push(
                        NodeBuilder::new("key-index-list")
                            .attr("ts", "1000")
                            .bytes(ki_bytes)
                            .build(),
                    );
                }

                let devices_node = NodeBuilder::new("devices")
                    .children(devices_children)
                    .build();

                NodeBuilder::new("user")
                    .attr("jid", jid)
                    .children([devices_node])
                    .build()
            })
            .collect();

        let list_node = NodeBuilder::new("list").children(user_nodes).build();
        let usync_node = NodeBuilder::new("usync").children([list_node]).build();
        // Wrap in an outer node (like IQ response) since the parser looks for usync as a child
        NodeBuilder::new("iq").children([usync_node]).build()
    }

    #[test]
    fn test_parse_with_phash_single_user() {
        let response = build_usync_response(vec![(
            "1234567890@s.whatsapp.net",
            vec![0, 1, 2],
            Some("2:abcdef123456"),
        )]);

        let result = parse_get_user_devices_response_with_phash(&response).unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].user.user, "1234567890");
        assert_eq!(result[0].devices.len(), 3);
        assert_eq!(result[0].phash, Some("2:abcdef123456".to_string()));
    }

    #[test]
    fn test_parse_with_phash_multiple_users() {
        let response = build_usync_response(vec![
            ("1111111111@s.whatsapp.net", vec![0, 1], Some("2:hash1")),
            ("2222222222@s.whatsapp.net", vec![0], Some("2:hash2")),
            (
                "3333333333@s.whatsapp.net",
                vec![0, 1, 2, 3],
                Some("2:hash3"),
            ),
        ]);

        let result = parse_get_user_devices_response_with_phash(&response).unwrap();

        assert_eq!(result.len(), 3);

        assert_eq!(result[0].user.user, "1111111111");
        assert_eq!(result[0].devices.len(), 2);
        assert_eq!(result[0].phash, Some("2:hash1".to_string()));

        assert_eq!(result[1].user.user, "2222222222");
        assert_eq!(result[1].devices.len(), 1);
        assert_eq!(result[1].phash, Some("2:hash2".to_string()));

        assert_eq!(result[2].user.user, "3333333333");
        assert_eq!(result[2].devices.len(), 4);
        assert_eq!(result[2].phash, Some("2:hash3".to_string()));
    }

    #[test]
    fn test_parse_without_phash() {
        let response = build_usync_response(vec![(
            "1234567890@s.whatsapp.net",
            vec![0, 1],
            None, // No phash
        )]);

        let result = parse_get_user_devices_response_with_phash(&response).unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].user.user, "1234567890");
        assert_eq!(result[0].devices.len(), 2);
        assert_eq!(result[0].phash, None);
    }

    #[test]
    fn test_parse_mixed_phash_presence() {
        let response = build_usync_response(vec![
            ("1111111111@s.whatsapp.net", vec![0], Some("2:hasphash")),
            ("2222222222@s.whatsapp.net", vec![0, 1], None), // No phash
        ]);

        let result = parse_get_user_devices_response_with_phash(&response).unwrap();

        assert_eq!(result.len(), 2);
        assert_eq!(result[0].phash, Some("2:hasphash".to_string()));
        assert_eq!(result[1].phash, None);
    }

    #[test]
    fn test_parse_device_ids_correct() {
        let response = build_usync_response(vec![(
            "1234567890@s.whatsapp.net",
            vec![0, 5, 10],
            Some("2:test"),
        )]);

        let result = parse_get_user_devices_response_with_phash(&response).unwrap();

        assert_eq!(result[0].devices.len(), 3);
        assert_eq!(result[0].devices[0].device, 0);
        assert_eq!(result[0].devices[1].device, 5);
        assert_eq!(result[0].devices[2].device, 10);
    }

    #[test]
    fn test_backward_compat_flat_list() {
        let response = build_usync_response(vec![
            ("1111111111@s.whatsapp.net", vec![0, 1], Some("2:hash1")),
            ("2222222222@s.whatsapp.net", vec![0], None),
        ]);

        // The backward-compatible function should return a flat list
        let result = parse_get_user_devices_response(&response).unwrap();

        assert_eq!(result.len(), 3); // 2 + 1 devices total
        assert_eq!(result[0].user, "1111111111");
        assert_eq!(result[0].device, 0);
        assert_eq!(result[1].user, "1111111111");
        assert_eq!(result[1].device, 1);
        assert_eq!(result[2].user, "2222222222");
        assert_eq!(result[2].device, 0);
    }

    #[test]
    fn test_user_jid_normalized_to_non_ad() {
        // Test with a JID that has a device suffix - should be normalized
        let response = build_usync_response(vec![(
            "1234567890:5@s.whatsapp.net", // With device suffix
            vec![0, 1],
            Some("2:test"),
        )]);

        let result = parse_get_user_devices_response_with_phash(&response).unwrap();

        // The user JID should be normalized (no device suffix)
        assert_eq!(result[0].user.device, 0);
        assert_eq!(result[0].user.user, "1234567890");
    }

    #[test]
    fn test_empty_device_list() {
        let response = build_usync_response(vec![(
            "1234567890@s.whatsapp.net",
            vec![], // No devices
            Some("2:empty"),
        )]);

        let result = parse_get_user_devices_response_with_phash(&response).unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].devices.len(), 0);
        assert_eq!(result[0].phash, Some("2:empty".to_string()));
    }

    #[test]
    fn test_server_returned_key_index_is_parsed() {
        // Build a response where devices have key-index attributes
        // (matches real server: <device id="4" key-index="93"/>)
        let device_nodes: Vec<Node> = vec![
            NodeBuilder::new("device").attr("id", "0").build(),
            NodeBuilder::new("device")
                .attr("id", "4")
                .attr("key-index", "93")
                .build(),
            NodeBuilder::new("device")
                .attr("id", "24")
                .attr("key-index", "113")
                .build(),
        ];

        let ki_bytes = build_test_key_index_bytes(&[0, 4, 24]);
        let device_list = NodeBuilder::new("device-list")
            .children(device_nodes)
            .build();
        let devices_node = NodeBuilder::new("devices")
            .children(vec![
                device_list,
                NodeBuilder::new("key-index-list")
                    .attr("ts", "1000")
                    .bytes(ki_bytes)
                    .build(),
            ])
            .build();

        let user_node = NodeBuilder::new("user")
            .attr("jid", "559900001111@s.whatsapp.net")
            .children(vec![devices_node])
            .build();

        let response = NodeBuilder::new("iq")
            .children(vec![
                NodeBuilder::new("usync")
                    .children(vec![
                        NodeBuilder::new("list").children(vec![user_node]).build(),
                    ])
                    .build(),
            ])
            .build();

        let result = parse_get_user_devices_response_with_phash(&response).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].devices.len(), 3);

        // Device 0: no key-index attribute → None
        assert_eq!(result[0].devices[0].device, 0);
        assert_eq!(result[0].devices[0].key_index, None);

        // Device 4: key-index="93"
        assert_eq!(result[0].devices[1].device, 4);
        assert_eq!(result[0].devices[1].key_index, Some(93));

        // Device 24: key-index="113"
        assert_eq!(result[0].devices[2].device, 24);
        assert_eq!(result[0].devices[2].key_index, Some(113));
    }
}