pyleys 0.1.0a1__py3-none-any.whl
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.
- pyleys/__init__.py +120 -0
- pyleys/appstate/__init__.py +52 -0
- pyleys/appstate/keys.py +52 -0
- pyleys/appstate/lthash.py +55 -0
- pyleys/appstate/processor.py +490 -0
- pyleys/appstate/sync.py +86 -0
- pyleys/auth/__init__.py +21 -0
- pyleys/auth/backends.py +227 -0
- pyleys/auth/creds.py +73 -0
- pyleys/auth/serde.py +65 -0
- pyleys/auth/state.py +28 -0
- pyleys/auth/store.py +128 -0
- pyleys/auth/utils.py +45 -0
- pyleys/client.py +6301 -0
- pyleys/connection/__init__.py +12 -0
- pyleys/connection/handshake.py +191 -0
- pyleys/connection/websocket.py +102 -0
- pyleys/crypto/__init__.py +19 -0
- pyleys/crypto/aes.py +74 -0
- pyleys/crypto/curve.py +53 -0
- pyleys/crypto/curve25519_signature.py +283 -0
- pyleys/crypto/hkdf.py +37 -0
- pyleys/crypto/noise.py +257 -0
- pyleys/defaults/__init__.py +40 -0
- pyleys/defaults/browsers.py +41 -0
- pyleys/defaults/constants.py +25 -0
- pyleys/exceptions.py +55 -0
- pyleys/media/__init__.py +37 -0
- pyleys/media/core.py +270 -0
- pyleys/media/meta.py +190 -0
- pyleys/messages/__init__.py +85 -0
- pyleys/messages/wa_message.py +374 -0
- pyleys/messages/wa_message_builder.py +1019 -0
- pyleys/proto/WAProto_pb2.py +1398 -0
- pyleys/proto/__init__.py +5 -0
- pyleys/py.typed +1 -0
- pyleys/signal/__init__.py +10 -0
- pyleys/signal/address.py +35 -0
- pyleys/signal/group.py +462 -0
- pyleys/signal/kdf.py +53 -0
- pyleys/signal/repository.py +923 -0
- pyleys/signal/util.py +38 -0
- pyleys/socket/__init__.py +37 -0
- pyleys/socket/communities.py +115 -0
- pyleys/socket/config.py +30 -0
- pyleys/socket/socket.py +1814 -0
- pyleys/store.py +315 -0
- pyleys/types/__init__.py +123 -0
- pyleys/types/api.py +429 -0
- pyleys/usync/__init__.py +21 -0
- pyleys/usync/query.py +237 -0
- pyleys/utils/__init__.py +25 -0
- pyleys/utils/asyncio.py +44 -0
- pyleys/utils/bytes.py +43 -0
- pyleys/utils/events.py +296 -0
- pyleys/utils/json.py +33 -0
- pyleys/wabinary/__init__.py +29 -0
- pyleys/wabinary/constants.py +16 -0
- pyleys/wabinary/decode.py +217 -0
- pyleys/wabinary/encode.py +193 -0
- pyleys/wabinary/jid.py +88 -0
- pyleys/wabinary/types.py +22 -0
- pyleys-0.1.0a1.dist-info/METADATA +430 -0
- pyleys-0.1.0a1.dist-info/RECORD +66 -0
- pyleys-0.1.0a1.dist-info/WHEEL +4 -0
- pyleys-0.1.0a1.dist-info/licenses/LICENSE +21 -0
pyleys/__init__.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"""Async Python client for the WhatsApp Web protocol."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .auth.backends import SingleFileAuthState, SQLiteAuthState
|
|
6
|
+
from .auth.state import AuthenticationState, AuthStatePersistence
|
|
7
|
+
from .auth.store import MultiFileAuthState
|
|
8
|
+
from .client import (
|
|
9
|
+
ChatState,
|
|
10
|
+
ClientConfig,
|
|
11
|
+
MediaUpload,
|
|
12
|
+
NewsletterMetadata,
|
|
13
|
+
NewsletterPicture,
|
|
14
|
+
WhatsAppClient,
|
|
15
|
+
make_wa_client,
|
|
16
|
+
)
|
|
17
|
+
from .defaults import BrowserIdentity, Browsers
|
|
18
|
+
from .exceptions import MediaRetryError, PyLeysError
|
|
19
|
+
from .messages import MessageBuilder, MessageUpsert, QuotedMessage, WAMessage, WAMessageKey
|
|
20
|
+
from .socket import (
|
|
21
|
+
CommunityGroup,
|
|
22
|
+
CommunityGroups,
|
|
23
|
+
CommunityMetadata,
|
|
24
|
+
CommunityParticipant,
|
|
25
|
+
ConnectionUpdate,
|
|
26
|
+
GroupMetadata,
|
|
27
|
+
GroupParticipant,
|
|
28
|
+
MediaConnInfo,
|
|
29
|
+
MediaHost,
|
|
30
|
+
SocketConfig,
|
|
31
|
+
)
|
|
32
|
+
from .store import ChatInfo, ContactInfo, MessageInfo, SQLiteStore
|
|
33
|
+
from .types import (
|
|
34
|
+
ButtonsSpec,
|
|
35
|
+
CarouselSpec,
|
|
36
|
+
DecryptedMessage,
|
|
37
|
+
EventHandler,
|
|
38
|
+
EventInviteContent,
|
|
39
|
+
EventInviteSpec,
|
|
40
|
+
EventName,
|
|
41
|
+
EventSpec,
|
|
42
|
+
ListSpec,
|
|
43
|
+
MediaUpdate,
|
|
44
|
+
MessageContent,
|
|
45
|
+
NativeFlowSpec,
|
|
46
|
+
ProductSnapshotSpec,
|
|
47
|
+
ProductSpec,
|
|
48
|
+
RichHtmlSpec,
|
|
49
|
+
RichResponseSpec,
|
|
50
|
+
SendOptions,
|
|
51
|
+
StickerAudioContent,
|
|
52
|
+
StickerAudioSpec,
|
|
53
|
+
TemplateButtonsSpec,
|
|
54
|
+
)
|
|
55
|
+
from .wabinary import BinaryNode
|
|
56
|
+
|
|
57
|
+
# aliasing
|
|
58
|
+
WAClient = WhatsAppClient
|
|
59
|
+
|
|
60
|
+
__all__ = [
|
|
61
|
+
"AuthStatePersistence",
|
|
62
|
+
"AuthenticationState",
|
|
63
|
+
"BinaryNode",
|
|
64
|
+
"BrowserIdentity",
|
|
65
|
+
"Browsers",
|
|
66
|
+
"ButtonsSpec",
|
|
67
|
+
"CarouselSpec",
|
|
68
|
+
"ChatInfo",
|
|
69
|
+
"ChatState",
|
|
70
|
+
"ClientConfig",
|
|
71
|
+
"CommunityGroup",
|
|
72
|
+
"CommunityGroups",
|
|
73
|
+
"CommunityMetadata",
|
|
74
|
+
"CommunityParticipant",
|
|
75
|
+
"ConnectionUpdate",
|
|
76
|
+
"ContactInfo",
|
|
77
|
+
"DecryptedMessage",
|
|
78
|
+
"EventHandler",
|
|
79
|
+
"EventInviteContent",
|
|
80
|
+
"EventInviteSpec",
|
|
81
|
+
"EventName",
|
|
82
|
+
"EventSpec",
|
|
83
|
+
"GroupMetadata",
|
|
84
|
+
"GroupParticipant",
|
|
85
|
+
"ListSpec",
|
|
86
|
+
"MediaConnInfo",
|
|
87
|
+
"MediaHost",
|
|
88
|
+
"MediaRetryError",
|
|
89
|
+
"MediaUpdate",
|
|
90
|
+
"MediaUpload",
|
|
91
|
+
"MessageBuilder",
|
|
92
|
+
"MessageContent",
|
|
93
|
+
"MessageInfo",
|
|
94
|
+
"MessageUpsert",
|
|
95
|
+
"MultiFileAuthState",
|
|
96
|
+
"NativeFlowSpec",
|
|
97
|
+
"NewsletterMetadata",
|
|
98
|
+
"NewsletterPicture",
|
|
99
|
+
"ProductSnapshotSpec",
|
|
100
|
+
"ProductSpec",
|
|
101
|
+
"PyLeysError",
|
|
102
|
+
"QuotedMessage",
|
|
103
|
+
"RichHtmlSpec",
|
|
104
|
+
"RichResponseSpec",
|
|
105
|
+
"SQLiteAuthState",
|
|
106
|
+
"SQLiteStore",
|
|
107
|
+
"SendOptions",
|
|
108
|
+
"SingleFileAuthState",
|
|
109
|
+
"SocketConfig",
|
|
110
|
+
"StickerAudioContent",
|
|
111
|
+
"StickerAudioSpec",
|
|
112
|
+
"TemplateButtonsSpec",
|
|
113
|
+
"WAClient",
|
|
114
|
+
"WAMessage",
|
|
115
|
+
"WAMessageKey",
|
|
116
|
+
"WhatsAppClient",
|
|
117
|
+
"make_wa_client",
|
|
118
|
+
]
|
|
119
|
+
|
|
120
|
+
__version__ = "0.1.0a1"
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""
|
|
2
|
+
App state sync (w:sync:app:state).
|
|
3
|
+
|
|
4
|
+
This package implements WhatsApp's "app state" snapshot/patch mechanism used to
|
|
5
|
+
sync chat/contact state between devices.
|
|
6
|
+
|
|
7
|
+
Portions of the algorithms (key expansion, LT-hash update and MAC validation)
|
|
8
|
+
are based on the MIT-licensed `wacore-appstate` crate from:
|
|
9
|
+
https://github.com/jlucaso1/whatsapp-rust
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from .keys import ExpandedAppStateKeys, expand_app_state_keys
|
|
15
|
+
from .lthash import WAPATCH_INTEGRITY, WAPATCH_INTEGRITY_INFO, LTHash
|
|
16
|
+
from .processor import (
|
|
17
|
+
AppStateMutationMAC,
|
|
18
|
+
HashState,
|
|
19
|
+
Mutation,
|
|
20
|
+
PatchProcessingResult,
|
|
21
|
+
ProcessedSnapshot,
|
|
22
|
+
generate_content_mac,
|
|
23
|
+
generate_patch_mac,
|
|
24
|
+
process_patch,
|
|
25
|
+
process_snapshot,
|
|
26
|
+
validate_index_mac,
|
|
27
|
+
validate_patch_macs,
|
|
28
|
+
validate_snapshot_mac,
|
|
29
|
+
)
|
|
30
|
+
from .sync import ALL_WA_PATCH_NAMES, extract_syncd_patches
|
|
31
|
+
|
|
32
|
+
__all__ = [
|
|
33
|
+
"ALL_WA_PATCH_NAMES",
|
|
34
|
+
"WAPATCH_INTEGRITY",
|
|
35
|
+
"WAPATCH_INTEGRITY_INFO",
|
|
36
|
+
"AppStateMutationMAC",
|
|
37
|
+
"ExpandedAppStateKeys",
|
|
38
|
+
"HashState",
|
|
39
|
+
"LTHash",
|
|
40
|
+
"Mutation",
|
|
41
|
+
"PatchProcessingResult",
|
|
42
|
+
"ProcessedSnapshot",
|
|
43
|
+
"expand_app_state_keys",
|
|
44
|
+
"extract_syncd_patches",
|
|
45
|
+
"generate_content_mac",
|
|
46
|
+
"generate_patch_mac",
|
|
47
|
+
"process_patch",
|
|
48
|
+
"process_snapshot",
|
|
49
|
+
"validate_index_mac",
|
|
50
|
+
"validate_patch_macs",
|
|
51
|
+
"validate_snapshot_mac",
|
|
52
|
+
]
|
pyleys/appstate/keys.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""
|
|
2
|
+
App-state key expansion.
|
|
3
|
+
|
|
4
|
+
Algorithm is compatible with `wacore-appstate` (MIT):
|
|
5
|
+
Copyright (c) 2025 Joao Lucas de Oliveira Lopes
|
|
6
|
+
https://github.com/jlucaso1/whatsapp-rust
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
|
|
13
|
+
from ..crypto.hkdf import hkdf_sha256
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True, slots=True)
|
|
17
|
+
class ExpandedAppStateKeys:
|
|
18
|
+
"""
|
|
19
|
+
Expanded app-state keys derived from a 32-byte master key.
|
|
20
|
+
|
|
21
|
+
Mirrors `ExpandedAppStateKeys` in whatsmeow / wacore-appstate.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
index: bytes
|
|
25
|
+
value_encryption: bytes
|
|
26
|
+
value_mac: bytes
|
|
27
|
+
snapshot_mac: bytes
|
|
28
|
+
patch_mac: bytes
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def expand_app_state_keys(key_data: bytes) -> ExpandedAppStateKeys:
|
|
32
|
+
"""
|
|
33
|
+
Expand the 32-byte master app state sync key into 5 sub-keys (160 bytes total).
|
|
34
|
+
|
|
35
|
+
Algorithm:
|
|
36
|
+
- HKDF-SHA256(key_data, salt=nil/empty, info="WhatsApp Mutation Keys", length=160)
|
|
37
|
+
- slice into five 32-byte keys.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
if not isinstance(key_data, (bytes, bytearray, memoryview)):
|
|
41
|
+
raise TypeError("key_data must be bytes-like")
|
|
42
|
+
key = bytes(key_data)
|
|
43
|
+
if not key:
|
|
44
|
+
raise ValueError("key_data is empty")
|
|
45
|
+
okm = hkdf_sha256(ikm=key, length=160, salt=b"", info=b"WhatsApp Mutation Keys")
|
|
46
|
+
return ExpandedAppStateKeys(
|
|
47
|
+
index=okm[0:32],
|
|
48
|
+
value_encryption=okm[32:64],
|
|
49
|
+
value_mac=okm[64:96],
|
|
50
|
+
snapshot_mac=okm[96:128],
|
|
51
|
+
patch_mac=okm[128:160],
|
|
52
|
+
)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""
|
|
2
|
+
LT-hash update logic for app-state patches.
|
|
3
|
+
|
|
4
|
+
Algorithm is compatible with `wacore-appstate` (MIT):
|
|
5
|
+
Copyright (c) 2025 Joao Lucas de Oliveira Lopes
|
|
6
|
+
https://github.com/jlucaso1/whatsapp-rust
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
|
|
13
|
+
from ..crypto.hkdf import hkdf_sha256
|
|
14
|
+
|
|
15
|
+
WAPATCH_INTEGRITY_INFO = b"WhatsApp Patch Integrity"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _perform_pointwise_with_overflow(base: bytearray, inp: bytes, *, subtract: bool) -> None:
|
|
19
|
+
if len(base) != len(inp):
|
|
20
|
+
raise ValueError("length mismatch")
|
|
21
|
+
if (len(base) % 2) != 0:
|
|
22
|
+
raise ValueError("slice lengths must be even")
|
|
23
|
+
|
|
24
|
+
# Operate on little-endian u16 words with wraparound (matches rust bridge).
|
|
25
|
+
for i in range(0, len(base), 2):
|
|
26
|
+
x = int.from_bytes(base[i : i + 2], "little", signed=False)
|
|
27
|
+
y = int.from_bytes(inp[i : i + 2], "little", signed=False)
|
|
28
|
+
r = (x - y) & 0xFFFF if subtract else (x + y) & 0xFFFF
|
|
29
|
+
base[i : i + 2] = r.to_bytes(2, "little", signed=False)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True, slots=True)
|
|
33
|
+
class LTHash:
|
|
34
|
+
"""
|
|
35
|
+
Linear-Transform hash updater used by WhatsApp app state (LT-Hash).
|
|
36
|
+
|
|
37
|
+
Ported from `wacore-appstate` (MIT). See:
|
|
38
|
+
https://github.com/jlucaso1/whatsapp-rust
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
hkdf_info: bytes
|
|
42
|
+
hkdf_size: int
|
|
43
|
+
|
|
44
|
+
def subtract_then_add_in_place(
|
|
45
|
+
self, base: bytearray, subtract: list[bytes], add: list[bytes]
|
|
46
|
+
) -> None:
|
|
47
|
+
for item in subtract:
|
|
48
|
+
derived = hkdf_sha256(ikm=item, length=self.hkdf_size, salt=b"", info=self.hkdf_info)
|
|
49
|
+
_perform_pointwise_with_overflow(base, derived, subtract=True)
|
|
50
|
+
for item in add:
|
|
51
|
+
derived = hkdf_sha256(ikm=item, length=self.hkdf_size, salt=b"", info=self.hkdf_info)
|
|
52
|
+
_perform_pointwise_with_overflow(base, derived, subtract=False)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
WAPATCH_INTEGRITY = LTHash(hkdf_info=WAPATCH_INTEGRITY_INFO, hkdf_size=128)
|