shellzapy 0.1.0__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.
- shellzapy-0.1.0.dist-info/METADATA +112 -0
- shellzapy-0.1.0.dist-info/RECORD +345 -0
- shellzapy-0.1.0.dist-info/WHEEL +4 -0
- shellzapy-0.1.0.dist-info/licenses/LICENSE +21 -0
- shellzapy-0.1.0.dist-info/licenses/NOTICE +9 -0
- zapy/__init__.py +58 -0
- zapy/_appstate/__init__.py +59 -0
- zapy/_appstate/constants.py +82 -0
- zapy/_appstate/crypto/__init__.py +17 -0
- zapy/_appstate/crypto/app_state_crypto.py +401 -0
- zapy/_appstate/parsers/__init__.py +23 -0
- zapy/_appstate/parsers/encoding.py +157 -0
- zapy/_appstate/parsers/response_parser.py +135 -0
- zapy/_appstate/sync/__init__.py +11 -0
- zapy/_appstate/sync/sync_client.py +1559 -0
- zapy/_appstate/types.py +137 -0
- zapy/_appstate/utils.py +100 -0
- zapy/_auth/__init__.py +3 -0
- zapy/_auth/auth_client.py +618 -0
- zapy/_auth/credentials_flow.py +290 -0
- zapy/_auth/pairing/__init__.py +3 -0
- zapy/_auth/pairing/companion_host.py +239 -0
- zapy/_auth/pairing/flow.py +808 -0
- zapy/_auth/pairing/pairing_code_crypto.py +143 -0
- zapy/_auth/pairing/qr_flow.py +123 -0
- zapy/_auth/pairing/shortcake_crypto.py +122 -0
- zapy/_auth/pairing/shortcake_flow.py +393 -0
- zapy/_auth/types.py +110 -0
- zapy/_client/__init__.py +26 -0
- zapy/_client/client.py +972 -0
- zapy/_client/connection/__init__.py +6 -0
- zapy/_client/connection/manager.py +525 -0
- zapy/_client/connection/receipt_queue.py +74 -0
- zapy/_client/coordinators/__init__.py +81 -0
- zapy/_client/coordinators/ab_props.py +260 -0
- zapy/_client/coordinators/app_state_mutation.py +2538 -0
- zapy/_client/coordinators/bot.py +830 -0
- zapy/_client/coordinators/broadcast_list.py +101 -0
- zapy/_client/coordinators/business.py +247 -0
- zapy/_client/coordinators/email.py +158 -0
- zapy/_client/coordinators/group.py +1478 -0
- zapy/_client/coordinators/incoming_node.py +758 -0
- zapy/_client/coordinators/low_level.py +119 -0
- zapy/_client/coordinators/message.py +1153 -0
- zapy/_client/coordinators/message_dispatch.py +1637 -0
- zapy/_client/coordinators/newsletter.py +110 -0
- zapy/_client/coordinators/offline_resume.py +271 -0
- zapy/_client/coordinators/passive_tasks.py +447 -0
- zapy/_client/coordinators/presence.py +103 -0
- zapy/_client/coordinators/privacy.py +623 -0
- zapy/_client/coordinators/profile.py +1024 -0
- zapy/_client/coordinators/retry.py +1878 -0
- zapy/_client/coordinators/status.py +132 -0
- zapy/_client/coordinators/stream_control.py +260 -0
- zapy/_client/coordinators/trusted_contact_token.py +417 -0
- zapy/_client/events/__init__.py +266 -0
- zapy/_client/events/abprops.py +58 -0
- zapy/_client/events/appstate_mutation.py +248 -0
- zapy/_client/events/business.py +565 -0
- zapy/_client/events/call.py +144 -0
- zapy/_client/events/chatstate.py +41 -0
- zapy/_client/events/devices.py +122 -0
- zapy/_client/events/dirty.py +384 -0
- zapy/_client/events/group.py +655 -0
- zapy/_client/events/identity.py +40 -0
- zapy/_client/events/incoming.py +1079 -0
- zapy/_client/events/mex_notification.py +317 -0
- zapy/_client/events/offline.py +123 -0
- zapy/_client/events/picture.py +67 -0
- zapy/_client/events/presence.py +74 -0
- zapy/_client/events/privacy.py +187 -0
- zapy/_client/events/privacy_token.py +72 -0
- zapy/_client/events/receipt.py +109 -0
- zapy/_client/events/registration.py +70 -0
- zapy/_client/events/username.py +76 -0
- zapy/_client/factory.py +2020 -0
- zapy/_client/media.py +829 -0
- zapy/_client/newsletter/__init__.py +90 -0
- zapy/_client/newsletter/admin.py +323 -0
- zapy/_client/newsletter/content.py +499 -0
- zapy/_client/newsletter/discovery.py +233 -0
- zapy/_client/newsletter/messaging.py +396 -0
- zapy/_client/newsletter/mex.py +99 -0
- zapy/_client/newsletter/parse.py +406 -0
- zapy/_client/newsletter/types.py +393 -0
- zapy/_client/persistence/__init__.py +89 -0
- zapy/_client/persistence/companion_host.py +133 -0
- zapy/_client/persistence/ephemeral_setting.py +245 -0
- zapy/_client/persistence/group_history.py +361 -0
- zapy/_client/persistence/history_blob.py +169 -0
- zapy/_client/persistence/history_sync.py +646 -0
- zapy/_client/persistence/mailbox.py +232 -0
- zapy/_client/persistence/write_behind_persistence.py +368 -0
- zapy/_client/plugins/__init__.py +23 -0
- zapy/_client/plugins/define.py +44 -0
- zapy/_client/plugins/install.py +254 -0
- zapy/_client/plugins/types.py +64 -0
- zapy/_client/tokens/__init__.py +21 -0
- zapy/_client/tokens/cs_token.py +47 -0
- zapy/_client/tokens/tc_token.py +43 -0
- zapy/_client/types.py +988 -0
- zapy/_crypto/__init__.py +65 -0
- zapy/_crypto/core/__init__.py +62 -0
- zapy/_crypto/core/hkdf.py +41 -0
- zapy/_crypto/core/keys.py +51 -0
- zapy/_crypto/core/nonce.py +25 -0
- zapy/_crypto/core/primitives.py +179 -0
- zapy/_crypto/core/random.py +32 -0
- zapy/_crypto/core/xeddsa.py +72 -0
- zapy/_crypto/curves/__init__.py +3 -0
- zapy/_crypto/curves/constants.py +10 -0
- zapy/_crypto/curves/ed25519.py +61 -0
- zapy/_crypto/curves/types.py +18 -0
- zapy/_crypto/curves/x25519.py +117 -0
- zapy/_crypto/math/__init__.py +3 -0
- zapy/_crypto/math/constants.py +35 -0
- zapy/_crypto/math/edwards.py +174 -0
- zapy/_crypto/math/fe.py +120 -0
- zapy/_crypto/math/le.py +13 -0
- zapy/_crypto/math/mod.py +15 -0
- zapy/_crypto/math/prime.py +11 -0
- zapy/_crypto/math/types.py +25 -0
- zapy/_crypto/native_backend.py +40 -0
- zapy/_infra/__init__.py +3 -0
- zapy/_infra/log.py +142 -0
- zapy/_infra/perf.py +77 -0
- zapy/_media/__init__.py +64 -0
- zapy/_media/constants.py +133 -0
- zapy/_media/container.py +110 -0
- zapy/_media/crypto/__init__.py +19 -0
- zapy/_media/crypto/crypto.py +633 -0
- zapy/_media/crypto/media_retry.py +107 -0
- zapy/_media/processor.py +100 -0
- zapy/_media/sticker/__init__.py +8 -0
- zapy/_media/sticker/pack.py +188 -0
- zapy/_media/transfer/__init__.py +15 -0
- zapy/_media/transfer/client.py +635 -0
- zapy/_media/transfer/conn.py +48 -0
- zapy/_media/types.py +169 -0
- zapy/_message/__init__.py +121 -0
- zapy/_message/addons/__init__.py +5 -0
- zapy/_message/addons/link_preview/__init__.py +54 -0
- zapy/_message/addons/link_preview/builder.py +56 -0
- zapy/_message/addons/link_preview/detect.py +72 -0
- zapy/_message/addons/link_preview/fetcher.py +553 -0
- zapy/_message/addons/link_preview/types.py +100 -0
- zapy/_message/client.py +486 -0
- zapy/_message/context_info.py +424 -0
- zapy/_message/crypto/__init__.py +71 -0
- zapy/_message/crypto/addon_crypto.py +656 -0
- zapy/_message/crypto/icdc.py +138 -0
- zapy/_message/crypto/phash.py +87 -0
- zapy/_message/crypto/reporting_token.py +498 -0
- zapy/_message/crypto/use_case_secret.py +113 -0
- zapy/_message/encode/__init__.py +84 -0
- zapy/_message/encode/content.py +636 -0
- zapy/_message/encode/device_sent.py +88 -0
- zapy/_message/encode/media_payload.py +122 -0
- zapy/_message/encode/padding.py +27 -0
- zapy/_message/kinds/__init__.py +1 -0
- zapy/_message/kinds/bot.py +179 -0
- zapy/_message/kinds/group_history.py +206 -0
- zapy/_message/kinds/newsletter.py +388 -0
- zapy/_message/kinds/sticker_pack.py +74 -0
- zapy/_message/messaging/__init__.py +336 -0
- zapy/_message/messaging/companion_host.py +117 -0
- zapy/_message/messaging/fanout.py +311 -0
- zapy/_message/messaging/group_metadata.py +444 -0
- zapy/_message/messaging/ignore_key.py +264 -0
- zapy/_message/messaging/key_protocol.py +217 -0
- zapy/_message/messaging/link_preview.py +448 -0
- zapy/_message/messaging/messages.py +1429 -0
- zapy/_message/primitives/__init__.py +1 -0
- zapy/_message/primitives/ack.py +232 -0
- zapy/_message/primitives/incoming.py +1141 -0
- zapy/_message/primitives/media_retry.py +451 -0
- zapy/_message/primitives/peer_data_operation.py +271 -0
- zapy/_message/types.py +434 -0
- zapy/_proto/WAProto_pb2.py +2584 -0
- zapy/_proto/__init__.py +1 -0
- zapy/_protocol/__init__.py +3 -0
- zapy/_protocol/abprops_spec.py +6216 -0
- zapy/_protocol/auth.py +17 -0
- zapy/_protocol/browser.py +57 -0
- zapy/_protocol/defaults.py +38 -0
- zapy/_protocol/device_identity.py +54 -0
- zapy/_protocol/jid.py +241 -0
- zapy/_protocol/message.py +127 -0
- zapy/_protocol/nodes.py +144 -0
- zapy/_protocol/notification.py +18 -0
- zapy/_retry/__init__.py +77 -0
- zapy/_retry/codec.py +209 -0
- zapy/_retry/constants.py +28 -0
- zapy/_retry/parse.py +132 -0
- zapy/_retry/reason.py +65 -0
- zapy/_retry/replay.py +483 -0
- zapy/_retry/tracker.py +161 -0
- zapy/_retry/types.py +122 -0
- zapy/_signal/__init__.py +171 -0
- zapy/_signal/api/__init__.py +133 -0
- zapy/_signal/api/codec.py +164 -0
- zapy/_signal/api/constants.py +14 -0
- zapy/_signal/api/device_sync.py +915 -0
- zapy/_signal/api/digest_sync.py +363 -0
- zapy/_signal/api/identity_sync.py +250 -0
- zapy/_signal/api/missing_pre_keys_sync.py +267 -0
- zapy/_signal/api/prekeys.py +191 -0
- zapy/_signal/api/result_map.py +40 -0
- zapy/_signal/api/rotate_key.py +111 -0
- zapy/_signal/api/session_sync.py +297 -0
- zapy/_signal/attestation.py +145 -0
- zapy/_signal/constants.py +22 -0
- zapy/_signal/encoding.py +78 -0
- zapy/_signal/group/__init__.py +35 -0
- zapy/_signal/group/chain.py +139 -0
- zapy/_signal/group/codec.py +78 -0
- zapy/_signal/group/encoding.py +166 -0
- zapy/_signal/group/manager.py +361 -0
- zapy/_signal/registration/__init__.py +44 -0
- zapy/_signal/registration/keygen.py +46 -0
- zapy/_signal/registration/reg_encoding.py +66 -0
- zapy/_signal/registration/reg_utils.py +52 -0
- zapy/_signal/session/__init__.py +114 -0
- zapy/_signal/session/encoding.py +385 -0
- zapy/_signal/session/protocol.py +590 -0
- zapy/_signal/session/ratchet.py +381 -0
- zapy/_signal/session/resolver.py +630 -0
- zapy/_signal/session/serializer.py +96 -0
- zapy/_signal/session/session.py +272 -0
- zapy/_signal/types.py +214 -0
- zapy/_store/__init__.py +118 -0
- zapy/_store/cache/__init__.py +166 -0
- zapy/_store/cache/identity.py +130 -0
- zapy/_store/cache/privacy_token.py +116 -0
- zapy/_store/cache/sender_key.py +168 -0
- zapy/_store/cache/session.py +142 -0
- zapy/_store/contracts/__init__.py +84 -0
- zapy/_store/contracts/appstate.py +82 -0
- zapy/_store/contracts/auth.py +16 -0
- zapy/_store/contracts/chat_metadata.py +29 -0
- zapy/_store/contracts/contact.py +31 -0
- zapy/_store/contracts/device_list.py +35 -0
- zapy/_store/contracts/group_metadata.py +33 -0
- zapy/_store/contracts/identity.py +18 -0
- zapy/_store/contracts/message.py +36 -0
- zapy/_store/contracts/message_secret.py +41 -0
- zapy/_store/contracts/pre_key.py +32 -0
- zapy/_store/contracts/privacy_token.py +32 -0
- zapy/_store/contracts/retry.py +47 -0
- zapy/_store/contracts/sender_key.py +37 -0
- zapy/_store/contracts/session.py +21 -0
- zapy/_store/contracts/signal.py +23 -0
- zapy/_store/contracts/thread.py +35 -0
- zapy/_store/create_store.py +890 -0
- zapy/_store/locks.py +1154 -0
- zapy/_store/memory/__init__.py +40 -0
- zapy/_store/memory/appstate.py +200 -0
- zapy/_store/memory/auth.py +24 -0
- zapy/_store/memory/chat_metadata.py +124 -0
- zapy/_store/memory/contact.py +125 -0
- zapy/_store/memory/device_list.py +161 -0
- zapy/_store/memory/group_metadata.py +125 -0
- zapy/_store/memory/identity.py +86 -0
- zapy/_store/memory/message.py +85 -0
- zapy/_store/memory/message_secret.py +145 -0
- zapy/_store/memory/pre_key.py +112 -0
- zapy/_store/memory/privacy_token.py +103 -0
- zapy/_store/memory/retry.py +250 -0
- zapy/_store/memory/sender_key.py +162 -0
- zapy/_store/memory/session.py +111 -0
- zapy/_store/memory/signal.py +45 -0
- zapy/_store/memory/thread.py +66 -0
- zapy/_store/noop.py +279 -0
- zapy/_transport/__init__.py +3 -0
- zapy/_transport/binary/__init__.py +77 -0
- zapy/_transport/binary/constants.py +80 -0
- zapy/_transport/binary/decoder.py +260 -0
- zapy/_transport/binary/encoder.py +279 -0
- zapy/_transport/binary/tokens.py +1289 -0
- zapy/_transport/comms.py +759 -0
- zapy/_transport/keepalive/__init__.py +3 -0
- zapy/_transport/keepalive/keep_alive.py +252 -0
- zapy/_transport/node/__init__.py +3 -0
- zapy/_transport/node/builders/__init__.py +9 -0
- zapy/_transport/node/builders/abprops.py +33 -0
- zapy/_transport/node/builders/account_sync.py +120 -0
- zapy/_transport/node/builders/bot.py +56 -0
- zapy/_transport/node/builders/business.py +261 -0
- zapy/_transport/node/builders/chatstate.py +74 -0
- zapy/_transport/node/builders/community.py +116 -0
- zapy/_transport/node/builders/device.py +30 -0
- zapy/_transport/node/builders/email.py +181 -0
- zapy/_transport/node/builders/global_.py +74 -0
- zapy/_transport/node/builders/group.py +279 -0
- zapy/_transport/node/builders/media.py +21 -0
- zapy/_transport/node/builders/message.py +436 -0
- zapy/_transport/node/builders/mobile.py +227 -0
- zapy/_transport/node/builders/newsletter.py +293 -0
- zapy/_transport/node/builders/offline.py +20 -0
- zapy/_transport/node/builders/pairing.py +130 -0
- zapy/_transport/node/builders/passive.py +28 -0
- zapy/_transport/node/builders/prekeys.py +215 -0
- zapy/_transport/node/builders/presence.py +100 -0
- zapy/_transport/node/builders/privacy.py +243 -0
- zapy/_transport/node/builders/privacy_token.py +94 -0
- zapy/_transport/node/builders/profile.py +176 -0
- zapy/_transport/node/builders/retry.py +156 -0
- zapy/_transport/node/builders/shortcake.py +87 -0
- zapy/_transport/node/builders/tos.py +92 -0
- zapy/_transport/node/builders/usync.py +185 -0
- zapy/_transport/node/helpers.py +201 -0
- zapy/_transport/node/orchestrator.py +210 -0
- zapy/_transport/node/query.py +82 -0
- zapy/_transport/node/transport.py +105 -0
- zapy/_transport/node/usync.py +33 -0
- zapy/_transport/node/xml.py +38 -0
- zapy/_transport/noise/__init__.py +7 -0
- zapy/_transport/noise/cert.py +85 -0
- zapy/_transport/noise/client_payload.py +344 -0
- zapy/_transport/noise/constants.py +12 -0
- zapy/_transport/noise/frame_codec.py +120 -0
- zapy/_transport/noise/handshake.py +92 -0
- zapy/_transport/noise/session.py +493 -0
- zapy/_transport/noise/socket.py +45 -0
- zapy/_transport/noise/types.py +66 -0
- zapy/_transport/noise/version.json +3 -0
- zapy/_transport/proxy.py +35 -0
- zapy/_transport/types.py +19 -0
- zapy/_transport/wa_version_fetcher.py +159 -0
- zapy/_transport/web_socket.py +602 -0
- zapy/_util/__init__.py +3 -0
- zapy/_util/bytes.py +131 -0
- zapy/_util/coercion.py +29 -0
- zapy/_util/collections.py +45 -0
- zapy/_util/primitives.py +49 -0
- zapy/client.py +840 -0
- zapy/errors.py +148 -0
- zapy/events.py +564 -0
- zapy/jid.py +140 -0
- zapy/py.typed +0 -0
- zapy/stores/__init__.py +50 -0
- zapy/stores/base.py +1627 -0
- zapy/stores/json_file.py +359 -0
- zapy/stores/memory.py +322 -0
- zapy/stores/sqlite.py +343 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"""Missing-prekey bundle fetch (mirrors ``SignalMissingPreKeysSyncApi``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Awaitable, Callable, Sequence
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
|
|
8
|
+
from zapy._infra.log import Logger
|
|
9
|
+
from zapy._protocol.jid import split_jid as _split_jid
|
|
10
|
+
from zapy._protocol.jid import to_user_jid as _to_user_jid
|
|
11
|
+
from zapy._protocol.nodes import WA_NODE_TAGS
|
|
12
|
+
from zapy._signal.api.codec import (
|
|
13
|
+
decode_exact_length,
|
|
14
|
+
parse_signal_key_bundle_from_node,
|
|
15
|
+
parse_uint,
|
|
16
|
+
)
|
|
17
|
+
from zapy._signal.types import SignalPreKeyBundle
|
|
18
|
+
from zapy._signal.api.constants import SIGNAL_REGISTRATION_ID_LENGTH
|
|
19
|
+
from zapy._signal.api.prekeys import (
|
|
20
|
+
MissingPreKeysDeviceTarget as SignalMissingPreKeysDeviceTarget,
|
|
21
|
+
)
|
|
22
|
+
from zapy._signal.api.prekeys import (
|
|
23
|
+
MissingPreKeysFetchTarget as SignalMissingPreKeysTarget,
|
|
24
|
+
)
|
|
25
|
+
from zapy._signal.api.prekeys import build_missing_pre_keys_fetch_iq
|
|
26
|
+
from zapy._signal.api.result_map import (
|
|
27
|
+
register_parsed_result_by_raw_and_canonical_key,
|
|
28
|
+
)
|
|
29
|
+
from zapy._transport.node.helpers import find_node_child, get_node_children_by_tag
|
|
30
|
+
from zapy._transport.node.query import assert_iq_result
|
|
31
|
+
from zapy._transport.types import BinaryNode
|
|
32
|
+
|
|
33
|
+
QueryFn = Callable[[BinaryNode, float | None], Awaitable[BinaryNode]]
|
|
34
|
+
|
|
35
|
+
_SIGNAL_FETCH_KEY_BUNDLES_TIMEOUT_MS: float = 20_000
|
|
36
|
+
|
|
37
|
+
__all__ = [
|
|
38
|
+
"SignalMissingPreKeysDeviceTarget",
|
|
39
|
+
"SignalMissingPreKeysTarget",
|
|
40
|
+
"SignalMissingPreKeysDeviceBundle",
|
|
41
|
+
"SignalMissingPreKeysUserDevices",
|
|
42
|
+
"SignalMissingPreKeysUserError",
|
|
43
|
+
"SignalMissingPreKeysUserResult",
|
|
44
|
+
"SignalMissingPreKeysSyncApi",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _parse_js_int(text: str) -> int | None:
|
|
49
|
+
"""Leading-digit integer like ``Number.parseInt(text, 10)``, else ``None``."""
|
|
50
|
+
stripped = text.lstrip()
|
|
51
|
+
digits = ""
|
|
52
|
+
for char in stripped:
|
|
53
|
+
if "0" <= char <= "9":
|
|
54
|
+
digits += char
|
|
55
|
+
else:
|
|
56
|
+
break
|
|
57
|
+
if not digits:
|
|
58
|
+
return None
|
|
59
|
+
value = int(digits, 10)
|
|
60
|
+
return value if value <= 2**53 - 1 else None
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass(frozen=True, slots=True)
|
|
64
|
+
class SignalMissingPreKeysDeviceBundle:
|
|
65
|
+
"""Fetched bundle for one ``(user, device)`` pair."""
|
|
66
|
+
|
|
67
|
+
device_jid: str
|
|
68
|
+
bundle: SignalPreKeyBundle
|
|
69
|
+
device_identity: bytes | None = None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass(frozen=True, slots=True)
|
|
73
|
+
class SignalMissingPreKeysUserDevices:
|
|
74
|
+
"""Per-device bundle list for one user."""
|
|
75
|
+
|
|
76
|
+
user_jid: str
|
|
77
|
+
devices: tuple[SignalMissingPreKeysDeviceBundle, ...]
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@dataclass(frozen=True, slots=True)
|
|
81
|
+
class SignalMissingPreKeysUserError:
|
|
82
|
+
"""Per-user error envelope when no bundle could be fetched."""
|
|
83
|
+
|
|
84
|
+
user_jid: str
|
|
85
|
+
error_text: str
|
|
86
|
+
error_code: int | None = None
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
SignalMissingPreKeysUserResult = (
|
|
90
|
+
SignalMissingPreKeysUserDevices | SignalMissingPreKeysUserError
|
|
91
|
+
)
|
|
92
|
+
"""One fetch entry per user: a per-device bundle list or an error."""
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _is_missing_pre_keys_user_result_preferred(
|
|
96
|
+
result: SignalMissingPreKeysUserResult,
|
|
97
|
+
) -> bool:
|
|
98
|
+
return isinstance(result, SignalMissingPreKeysUserDevices)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class SignalMissingPreKeysSyncApi:
|
|
102
|
+
"""Fetches missing pre-key bundles for ``(user, device, reg id)`` triples.
|
|
103
|
+
|
|
104
|
+
Used during message encryption when a Signal session is missing.
|
|
105
|
+
|
|
106
|
+
Example:
|
|
107
|
+
>>> api = SignalMissingPreKeysSyncApi(logger=logger, query=send_iq)
|
|
108
|
+
>>> out = await api.fetch_missing_pre_keys([SignalMissingPreKeysTarget(
|
|
109
|
+
... user_jid=user, devices=(SignalMissingPreKeysDeviceTarget(
|
|
110
|
+
... device_id=device, registration_id=reg),))])
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
def __init__(
|
|
114
|
+
self,
|
|
115
|
+
logger: Logger,
|
|
116
|
+
query: QueryFn,
|
|
117
|
+
default_timeout_ms: float | None = None,
|
|
118
|
+
) -> None:
|
|
119
|
+
self._logger = logger
|
|
120
|
+
self._query = query
|
|
121
|
+
self._default_timeout_ms = (
|
|
122
|
+
_SIGNAL_FETCH_KEY_BUNDLES_TIMEOUT_MS
|
|
123
|
+
if default_timeout_ms is None
|
|
124
|
+
else default_timeout_ms
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
async def fetch_missing_pre_keys(
|
|
128
|
+
self,
|
|
129
|
+
targets: Sequence[SignalMissingPreKeysTarget],
|
|
130
|
+
timeout_ms: float | None = None,
|
|
131
|
+
) -> list[SignalMissingPreKeysUserResult]:
|
|
132
|
+
"""Fetch bundles for ``targets`` in one IQ; errors stay per-user."""
|
|
133
|
+
effective_timeout = (
|
|
134
|
+
self._default_timeout_ms if timeout_ms is None else timeout_ms
|
|
135
|
+
)
|
|
136
|
+
if not targets:
|
|
137
|
+
return []
|
|
138
|
+
self._logger.debug(
|
|
139
|
+
"signal fetch missing prekeys request",
|
|
140
|
+
{"users": len(targets), "timeoutMs": effective_timeout},
|
|
141
|
+
)
|
|
142
|
+
response = await self._query(
|
|
143
|
+
build_missing_pre_keys_fetch_iq(list(targets)), effective_timeout
|
|
144
|
+
)
|
|
145
|
+
parsed = self._parse_fetch_missing_pre_keys_response(response, targets)
|
|
146
|
+
self._logger.debug(
|
|
147
|
+
"signal fetch missing prekeys success", {"users": len(parsed)}
|
|
148
|
+
)
|
|
149
|
+
return parsed
|
|
150
|
+
|
|
151
|
+
def _parse_fetch_missing_pre_keys_response(
|
|
152
|
+
self,
|
|
153
|
+
node: BinaryNode,
|
|
154
|
+
requested_targets: Sequence[SignalMissingPreKeysTarget],
|
|
155
|
+
) -> list[SignalMissingPreKeysUserResult]:
|
|
156
|
+
assert_iq_result(node, "missing prekeys")
|
|
157
|
+
|
|
158
|
+
list_node = find_node_child(node, WA_NODE_TAGS["LIST"])
|
|
159
|
+
if list_node is None:
|
|
160
|
+
raise ValueError("missing prekeys response missing list node")
|
|
161
|
+
users = get_node_children_by_tag(list_node, WA_NODE_TAGS["USER"])
|
|
162
|
+
parsed_by_jid: dict[str, SignalMissingPreKeysUserResult] = {}
|
|
163
|
+
parsed_by_canonical_jid: dict[str, SignalMissingPreKeysUserResult] = {}
|
|
164
|
+
|
|
165
|
+
for user_node in users:
|
|
166
|
+
user_jid = user_node.attrs.get("jid")
|
|
167
|
+
if not user_jid:
|
|
168
|
+
continue
|
|
169
|
+
canonical_user_jid = _to_user_jid(user_jid, canonicalize=True)
|
|
170
|
+
|
|
171
|
+
user_error_node = find_node_child(user_node, WA_NODE_TAGS["ERROR"])
|
|
172
|
+
if user_error_node is not None:
|
|
173
|
+
parsed_code = _parse_js_int(user_error_node.attrs.get("code", ""))
|
|
174
|
+
register_parsed_result_by_raw_and_canonical_key(
|
|
175
|
+
parsed_by_jid,
|
|
176
|
+
parsed_by_canonical_jid,
|
|
177
|
+
user_jid,
|
|
178
|
+
canonical_user_jid,
|
|
179
|
+
SignalMissingPreKeysUserError(
|
|
180
|
+
user_jid=user_jid,
|
|
181
|
+
error_text=user_error_node.attrs.get("text")
|
|
182
|
+
or user_error_node.attrs.get("type")
|
|
183
|
+
or "unknown",
|
|
184
|
+
error_code=parsed_code,
|
|
185
|
+
),
|
|
186
|
+
_is_missing_pre_keys_user_result_preferred,
|
|
187
|
+
)
|
|
188
|
+
continue
|
|
189
|
+
|
|
190
|
+
register_parsed_result_by_raw_and_canonical_key(
|
|
191
|
+
parsed_by_jid,
|
|
192
|
+
parsed_by_canonical_jid,
|
|
193
|
+
user_jid,
|
|
194
|
+
canonical_user_jid,
|
|
195
|
+
SignalMissingPreKeysUserDevices(
|
|
196
|
+
user_jid=user_jid,
|
|
197
|
+
devices=tuple(
|
|
198
|
+
self._parse_user_devices(user_node, canonical_user_jid)
|
|
199
|
+
),
|
|
200
|
+
),
|
|
201
|
+
_is_missing_pre_keys_user_result_preferred,
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
results: list[SignalMissingPreKeysUserResult] = []
|
|
205
|
+
for target in requested_targets:
|
|
206
|
+
hit = parsed_by_jid.get(target.user_jid)
|
|
207
|
+
if hit is None:
|
|
208
|
+
hit = parsed_by_canonical_jid.get(
|
|
209
|
+
_to_user_jid(target.user_jid, canonicalize=True)
|
|
210
|
+
)
|
|
211
|
+
if hit is None:
|
|
212
|
+
hit = SignalMissingPreKeysUserError(
|
|
213
|
+
user_jid=target.user_jid,
|
|
214
|
+
error_text="missing user in key_fetch response",
|
|
215
|
+
)
|
|
216
|
+
results.append(hit)
|
|
217
|
+
return results
|
|
218
|
+
|
|
219
|
+
def _parse_user_devices(
|
|
220
|
+
self, node: BinaryNode, user_jid: str
|
|
221
|
+
) -> list[SignalMissingPreKeysDeviceBundle]:
|
|
222
|
+
user, server = _split_jid(user_jid)
|
|
223
|
+
devices = get_node_children_by_tag(node, WA_NODE_TAGS["DEVICE"])
|
|
224
|
+
bundles: list[SignalMissingPreKeysDeviceBundle] = []
|
|
225
|
+
for index, device_node in enumerate(devices):
|
|
226
|
+
device_id_value = device_node.attrs.get("id")
|
|
227
|
+
if not device_id_value:
|
|
228
|
+
raise ValueError(f"missing prekeys device[{index}].id is missing")
|
|
229
|
+
device_id = _parse_js_int(device_id_value)
|
|
230
|
+
if device_id is None or device_id < 0 or device_id > 2**53 - 1:
|
|
231
|
+
raise ValueError(f"missing prekeys device[{index}].id is invalid")
|
|
232
|
+
|
|
233
|
+
registration_node = find_node_child(
|
|
234
|
+
device_node, WA_NODE_TAGS["REGISTRATION"]
|
|
235
|
+
)
|
|
236
|
+
if registration_node is None:
|
|
237
|
+
raise ValueError(
|
|
238
|
+
f"missing prekeys device payload is incomplete for {user_jid}"
|
|
239
|
+
)
|
|
240
|
+
reg_id = parse_uint(
|
|
241
|
+
decode_exact_length(
|
|
242
|
+
registration_node.content,
|
|
243
|
+
"missing prekeys device registration",
|
|
244
|
+
SIGNAL_REGISTRATION_ID_LENGTH,
|
|
245
|
+
),
|
|
246
|
+
"missing prekeys device registration",
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
parsed = parse_signal_key_bundle_from_node(
|
|
250
|
+
device_node, "missing prekeys device"
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
bundle = SignalPreKeyBundle(
|
|
254
|
+
reg_id=reg_id,
|
|
255
|
+
identity=parsed.identity,
|
|
256
|
+
signed_key=parsed.signed_key,
|
|
257
|
+
one_time_key=parsed.one_time_key,
|
|
258
|
+
)
|
|
259
|
+
device_jid = user_jid if device_id == 0 else f"{user}:{device_id}@{server}"
|
|
260
|
+
bundles.append(
|
|
261
|
+
SignalMissingPreKeysDeviceBundle(
|
|
262
|
+
device_jid=device_jid,
|
|
263
|
+
bundle=bundle,
|
|
264
|
+
device_identity=parsed.device_identity,
|
|
265
|
+
)
|
|
266
|
+
)
|
|
267
|
+
return bundles
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"""Pre-key upload/fetch IQ builders (mirrors ``src/signal/api/prekeys.ts``).
|
|
2
|
+
|
|
3
|
+
The oracle re-exports these from ``src/transport/node/builders/prekeys.ts``;
|
|
4
|
+
the builder helpers live here, next to their caller
|
|
5
|
+
verbatim until the sibling lands them.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
|
|
12
|
+
from zapy._crypto.core.keys import to_raw_pub_key
|
|
13
|
+
from zapy._protocol import defaults as _defaults
|
|
14
|
+
from zapy._protocol.nodes import WA_IQ_TYPES, WA_NODE_TAGS, WA_XMLNS
|
|
15
|
+
from zapy._signal.api.constants import SIGNAL_KEY_BUNDLE_TYPE_BYTES
|
|
16
|
+
from zapy._signal.types import PreKeyRecord, RegistrationInfo, SignedPreKeyRecord
|
|
17
|
+
from zapy._transport.node.query import build_iq_node, parse_iq_error
|
|
18
|
+
from zapy._transport.types import BinaryNode
|
|
19
|
+
from zapy._util.bytes import int_to_bytes
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True, slots=True)
|
|
23
|
+
class PreKeyUploadFailure:
|
|
24
|
+
"""Numeric error code and text from a failed ``prekey`` upload IQ."""
|
|
25
|
+
|
|
26
|
+
error_text: str
|
|
27
|
+
error_code: int | None = None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True, slots=True)
|
|
31
|
+
class MissingPreKeysDeviceTarget:
|
|
32
|
+
"""One ``(device, registration id)`` pair inside a fetch target."""
|
|
33
|
+
|
|
34
|
+
device_id: int
|
|
35
|
+
registration_id: int
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass(frozen=True, slots=True)
|
|
39
|
+
class MissingPreKeysFetchTarget:
|
|
40
|
+
"""One ``(user, devices)`` entry of a ``key_fetch`` request."""
|
|
41
|
+
|
|
42
|
+
user_jid: str
|
|
43
|
+
devices: tuple[MissingPreKeysDeviceTarget, ...]
|
|
44
|
+
reason_identity: bool = False
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _build_signed_pre_key_node(signed_pre_key: SignedPreKeyRecord) -> BinaryNode:
|
|
48
|
+
"""``<skey><id/><value/><signature/></skey>`` for uploads and rotation."""
|
|
49
|
+
return BinaryNode(
|
|
50
|
+
tag=WA_NODE_TAGS["SKEY"],
|
|
51
|
+
attrs={},
|
|
52
|
+
content=[
|
|
53
|
+
BinaryNode(
|
|
54
|
+
tag=WA_NODE_TAGS["ID"],
|
|
55
|
+
attrs={},
|
|
56
|
+
content=int_to_bytes(3, signed_pre_key.key_id),
|
|
57
|
+
),
|
|
58
|
+
BinaryNode(
|
|
59
|
+
tag=WA_NODE_TAGS["VALUE"],
|
|
60
|
+
attrs={},
|
|
61
|
+
content=to_raw_pub_key(signed_pre_key.key_pair.pub_key),
|
|
62
|
+
),
|
|
63
|
+
BinaryNode(
|
|
64
|
+
tag=WA_NODE_TAGS["SIGNATURE"],
|
|
65
|
+
attrs={},
|
|
66
|
+
content=signed_pre_key.signature,
|
|
67
|
+
),
|
|
68
|
+
],
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def build_pre_key_upload_iq(
|
|
73
|
+
registration_info: RegistrationInfo,
|
|
74
|
+
signed_pre_key: SignedPreKeyRecord,
|
|
75
|
+
pre_keys: list[PreKeyRecord] | tuple[PreKeyRecord, ...],
|
|
76
|
+
) -> BinaryNode:
|
|
77
|
+
"""Upload IQ: identity, registration, type, one-time pre-keys, signed key."""
|
|
78
|
+
return build_iq_node(
|
|
79
|
+
WA_IQ_TYPES["SET"],
|
|
80
|
+
_defaults.HOST_DOMAIN,
|
|
81
|
+
WA_XMLNS["SIGNAL"],
|
|
82
|
+
[
|
|
83
|
+
BinaryNode(
|
|
84
|
+
tag=WA_NODE_TAGS["IDENTITY"],
|
|
85
|
+
attrs={},
|
|
86
|
+
content=to_raw_pub_key(registration_info.identity_key_pair.pub_key),
|
|
87
|
+
),
|
|
88
|
+
BinaryNode(
|
|
89
|
+
tag=WA_NODE_TAGS["REGISTRATION"],
|
|
90
|
+
attrs={},
|
|
91
|
+
content=int_to_bytes(4, registration_info.registration_id),
|
|
92
|
+
),
|
|
93
|
+
BinaryNode(
|
|
94
|
+
tag=WA_NODE_TAGS["TYPE"],
|
|
95
|
+
attrs={},
|
|
96
|
+
content=SIGNAL_KEY_BUNDLE_TYPE_BYTES,
|
|
97
|
+
),
|
|
98
|
+
BinaryNode(
|
|
99
|
+
tag=WA_NODE_TAGS["LIST"],
|
|
100
|
+
attrs={},
|
|
101
|
+
content=[
|
|
102
|
+
BinaryNode(
|
|
103
|
+
tag=WA_NODE_TAGS["KEY"],
|
|
104
|
+
attrs={},
|
|
105
|
+
content=[
|
|
106
|
+
BinaryNode(
|
|
107
|
+
tag=WA_NODE_TAGS["ID"],
|
|
108
|
+
attrs={},
|
|
109
|
+
content=int_to_bytes(3, record.key_id),
|
|
110
|
+
),
|
|
111
|
+
BinaryNode(
|
|
112
|
+
tag=WA_NODE_TAGS["VALUE"],
|
|
113
|
+
attrs={},
|
|
114
|
+
content=to_raw_pub_key(record.key_pair.pub_key),
|
|
115
|
+
),
|
|
116
|
+
],
|
|
117
|
+
)
|
|
118
|
+
for record in pre_keys
|
|
119
|
+
],
|
|
120
|
+
),
|
|
121
|
+
_build_signed_pre_key_node(signed_pre_key),
|
|
122
|
+
],
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def build_signed_pre_key_rotate_iq(signed_pre_key: SignedPreKeyRecord) -> BinaryNode:
|
|
127
|
+
"""``<iq type=set>`` carrying a fresh signed pre-key inside ``<rotate>``."""
|
|
128
|
+
return build_iq_node(
|
|
129
|
+
WA_IQ_TYPES["SET"],
|
|
130
|
+
_defaults.HOST_DOMAIN,
|
|
131
|
+
WA_XMLNS["SIGNAL"],
|
|
132
|
+
[
|
|
133
|
+
BinaryNode(
|
|
134
|
+
tag=WA_NODE_TAGS["ROTATE"],
|
|
135
|
+
attrs={},
|
|
136
|
+
content=[_build_signed_pre_key_node(signed_pre_key)],
|
|
137
|
+
)
|
|
138
|
+
],
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def build_missing_pre_keys_fetch_iq(
|
|
143
|
+
users: list[MissingPreKeysFetchTarget] | tuple[MissingPreKeysFetchTarget, ...],
|
|
144
|
+
) -> BinaryNode:
|
|
145
|
+
"""``<iq type=get>`` fetching bundles for ``(user, device, reg id)`` triples."""
|
|
146
|
+
return build_iq_node(
|
|
147
|
+
WA_IQ_TYPES["GET"],
|
|
148
|
+
_defaults.HOST_DOMAIN,
|
|
149
|
+
WA_XMLNS["SIGNAL"],
|
|
150
|
+
[
|
|
151
|
+
BinaryNode(
|
|
152
|
+
tag=WA_NODE_TAGS["KEY_FETCH"],
|
|
153
|
+
attrs={},
|
|
154
|
+
content=[
|
|
155
|
+
BinaryNode(
|
|
156
|
+
tag=WA_NODE_TAGS["USER"],
|
|
157
|
+
attrs=(
|
|
158
|
+
{"jid": user.user_jid, "reason": "identity"}
|
|
159
|
+
if user.reason_identity
|
|
160
|
+
else {"jid": user.user_jid}
|
|
161
|
+
),
|
|
162
|
+
content=[
|
|
163
|
+
BinaryNode(
|
|
164
|
+
tag=WA_NODE_TAGS["DEVICE"],
|
|
165
|
+
attrs={"id": str(device.device_id)},
|
|
166
|
+
content=[
|
|
167
|
+
BinaryNode(
|
|
168
|
+
tag=WA_NODE_TAGS["REGISTRATION"],
|
|
169
|
+
attrs={},
|
|
170
|
+
content=int_to_bytes(4, device.registration_id),
|
|
171
|
+
)
|
|
172
|
+
],
|
|
173
|
+
)
|
|
174
|
+
for device in user.devices
|
|
175
|
+
],
|
|
176
|
+
)
|
|
177
|
+
for user in users
|
|
178
|
+
],
|
|
179
|
+
)
|
|
180
|
+
],
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def parse_pre_key_upload_failure(node: BinaryNode) -> PreKeyUploadFailure:
|
|
185
|
+
"""Extract the numeric error code and text from a failed upload response."""
|
|
186
|
+
error = parse_iq_error(node)
|
|
187
|
+
numeric_code = error.get("numericCode")
|
|
188
|
+
return PreKeyUploadFailure(
|
|
189
|
+
error_text=str(error.get("text", "unknown")),
|
|
190
|
+
error_code=numeric_code if isinstance(numeric_code, int) else None,
|
|
191
|
+
)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Keyed result merging for key-bundle fetches."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from typing import TypeVar
|
|
7
|
+
|
|
8
|
+
T = TypeVar("T")
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _merge_preferred_parsed_result(
|
|
12
|
+
target: dict[str, T],
|
|
13
|
+
key: str,
|
|
14
|
+
next_value: T,
|
|
15
|
+
is_preferred: Callable[[T], bool],
|
|
16
|
+
) -> None:
|
|
17
|
+
"""Store ``next_value`` unless the current entry is already preferred."""
|
|
18
|
+
current = target.get(key)
|
|
19
|
+
if current is None or not is_preferred(current):
|
|
20
|
+
target[key] = next_value
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def register_parsed_result_by_raw_and_canonical_key(
|
|
24
|
+
parsed_by_raw_key: dict[str, T],
|
|
25
|
+
parsed_by_canonical_key: dict[str, T],
|
|
26
|
+
raw_key: str,
|
|
27
|
+
canonical_key: str,
|
|
28
|
+
result: T,
|
|
29
|
+
is_preferred: Callable[[T], bool],
|
|
30
|
+
) -> None:
|
|
31
|
+
"""Index ``result`` under both the raw and the canonical JID.
|
|
32
|
+
|
|
33
|
+
A bundle (preferred) overwrites an error envelope recorded earlier for
|
|
34
|
+
the same user; an error never overwrites a bundle. This maps
|
|
35
|
+
``hosted.lid`` responses back to the requested ``lid`` JID.
|
|
36
|
+
"""
|
|
37
|
+
_merge_preferred_parsed_result(parsed_by_raw_key, raw_key, result, is_preferred)
|
|
38
|
+
_merge_preferred_parsed_result(
|
|
39
|
+
parsed_by_canonical_key, canonical_key, result, is_preferred
|
|
40
|
+
)
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""Signed pre-key rotation (mirrors ``src/signal/api/SignalRotateKeyApi.ts``)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Awaitable, Callable
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
|
|
8
|
+
from zapy._infra.log import Logger
|
|
9
|
+
from zapy._protocol import defaults as _defaults
|
|
10
|
+
from zapy._protocol.nodes import WA_IQ_TYPES, WA_NODE_TAGS
|
|
11
|
+
from zapy._signal.api.prekeys import build_signed_pre_key_rotate_iq
|
|
12
|
+
from zapy._signal.registration import generate_signed_pre_key
|
|
13
|
+
from zapy._store.contracts.signal import WaSignalStore
|
|
14
|
+
from zapy._transport.node.query import parse_iq_error
|
|
15
|
+
from zapy._transport.types import BinaryNode
|
|
16
|
+
|
|
17
|
+
QueryFn = Callable[[BinaryNode, float | None], Awaitable[BinaryNode]]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True, slots=True)
|
|
21
|
+
class SignalRotateKeyResult:
|
|
22
|
+
"""Rotation outcome; ``should_digest_key`` requests a digest re-check."""
|
|
23
|
+
|
|
24
|
+
should_digest_key: bool
|
|
25
|
+
error_code: int | None = None
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class SignalRotateKeyApi:
|
|
29
|
+
"""Generates a fresh signed pre-key and uploads it to the server.
|
|
30
|
+
|
|
31
|
+
Handles the ``406``/``409``/``5xx`` error codes by signaling whether a
|
|
32
|
+
digest re-validation is required.
|
|
33
|
+
|
|
34
|
+
Example:
|
|
35
|
+
>>> api = SignalRotateKeyApi(logger=logger, query=send_iq,
|
|
36
|
+
... signal_store=signal_store)
|
|
37
|
+
>>> result = await api.rotate_signed_pre_key()
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
def __init__(
|
|
41
|
+
self,
|
|
42
|
+
logger: Logger,
|
|
43
|
+
query: QueryFn,
|
|
44
|
+
signal_store: WaSignalStore,
|
|
45
|
+
default_timeout_ms: float | None = None,
|
|
46
|
+
) -> None:
|
|
47
|
+
self._logger = logger
|
|
48
|
+
self._query = query
|
|
49
|
+
self._signal_store = signal_store
|
|
50
|
+
self._default_timeout_ms = (
|
|
51
|
+
_defaults.IQ_TIMEOUT_MS
|
|
52
|
+
if default_timeout_ms is None
|
|
53
|
+
else default_timeout_ms
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
async def rotate_signed_pre_key(
|
|
57
|
+
self, timeout_ms: float | None = None
|
|
58
|
+
) -> SignalRotateKeyResult:
|
|
59
|
+
"""Rotate the signed pre-key (next key id) and upload it."""
|
|
60
|
+
effective_timeout = (
|
|
61
|
+
self._default_timeout_ms if timeout_ms is None else timeout_ms
|
|
62
|
+
)
|
|
63
|
+
registration_info = await self._signal_store.get_registration_info()
|
|
64
|
+
current_signed_pre_key = await self._signal_store.get_signed_pre_key()
|
|
65
|
+
if registration_info is None:
|
|
66
|
+
raise ValueError("signal rotate key requires registration info")
|
|
67
|
+
|
|
68
|
+
next_signed_pre_key = generate_signed_pre_key(
|
|
69
|
+
current_signed_pre_key.key_id + 1 if current_signed_pre_key else 1,
|
|
70
|
+
registration_info.identity_key_pair.priv_key,
|
|
71
|
+
)
|
|
72
|
+
await self._signal_store.set_signed_pre_key(next_signed_pre_key)
|
|
73
|
+
|
|
74
|
+
self._logger.info(
|
|
75
|
+
"signal signed prekey uploading",
|
|
76
|
+
{"keyId": next_signed_pre_key.key_id, "timeoutMs": effective_timeout},
|
|
77
|
+
)
|
|
78
|
+
response = await self._query(
|
|
79
|
+
build_signed_pre_key_rotate_iq(next_signed_pre_key), effective_timeout
|
|
80
|
+
)
|
|
81
|
+
if response.tag != WA_NODE_TAGS["IQ"]:
|
|
82
|
+
raise ValueError(f"invalid signal rotate response tag: {response.tag}")
|
|
83
|
+
if response.attrs.get("type") == WA_IQ_TYPES["RESULT"]:
|
|
84
|
+
self._logger.info(
|
|
85
|
+
"signal signed prekey upload completed",
|
|
86
|
+
{"keyId": next_signed_pre_key.key_id},
|
|
87
|
+
)
|
|
88
|
+
return SignalRotateKeyResult(should_digest_key=False)
|
|
89
|
+
if response.attrs.get("type") != WA_IQ_TYPES["ERROR"]:
|
|
90
|
+
response_type = response.attrs.get("type", "unknown")
|
|
91
|
+
raise ValueError(f"invalid signal rotate response type: {response_type}")
|
|
92
|
+
|
|
93
|
+
failure = parse_iq_error(response)
|
|
94
|
+
raw_code = failure.get("numericCode")
|
|
95
|
+
error_code = raw_code if isinstance(raw_code, int) else None
|
|
96
|
+
if error_code == 406:
|
|
97
|
+
self._logger.warn("signal rotate key generated bad key")
|
|
98
|
+
return SignalRotateKeyResult(should_digest_key=False, error_code=error_code)
|
|
99
|
+
if error_code == 409:
|
|
100
|
+
self._logger.warn("signal rotate key validation mismatch")
|
|
101
|
+
return SignalRotateKeyResult(should_digest_key=True, error_code=error_code)
|
|
102
|
+
if error_code is not None and error_code >= 500:
|
|
103
|
+
self._logger.warn(
|
|
104
|
+
"signal rotate key server error", {"errorCode": error_code}
|
|
105
|
+
)
|
|
106
|
+
return SignalRotateKeyResult(should_digest_key=False, error_code=error_code)
|
|
107
|
+
self._logger.warn(
|
|
108
|
+
"signal rotate key unrecognized error",
|
|
109
|
+
{"errorCode": error_code, "errorText": failure.get("text")},
|
|
110
|
+
)
|
|
111
|
+
return SignalRotateKeyResult(should_digest_key=True, error_code=error_code)
|