7h3-protocol 0.4.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.
@@ -0,0 +1,47 @@
1
+ Metadata-Version: 2.4
2
+ Name: 7h3-protocol
3
+ Version: 0.4.0
4
+ Summary: 7h3 Protocol — Python SDK. Deterministic, signed, replay-safe AI-to-AI messaging. Wire version 7h3/0.1.
5
+ Project-URL: Homepage, https://github.com/IceMasterT/7h3-protocol
6
+ Project-URL: Repository, https://github.com/IceMasterT/7h3-protocol
7
+ Project-URL: Documentation, https://github.com/IceMasterT/7h3-protocol/tree/main/sdk/python
8
+ Project-URL: Changelog, https://github.com/IceMasterT/7h3-protocol/blob/main/CHANGELOG.md
9
+ License: MIT
10
+ Keywords: 7h3,agent,ed25519,mcp,protocol,replay-protection,signing
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Topic :: Internet
21
+ Classifier: Topic :: Security :: Cryptography
22
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
23
+ Requires-Python: >=3.9
24
+ Provides-Extra: crypto
25
+ Requires-Dist: cryptography>=41.0; extra == 'crypto'
26
+ Provides-Extra: nacl
27
+ Requires-Dist: pynacl>=1.5; extra == 'nacl'
28
+ Description-Content-Type: text/markdown
29
+
30
+ # 7h3 Protocol AIP Python SDK (Skeleton)
31
+
32
+ Minimal reference SDK for `aip/0.1` parity with the TypeScript implementation.
33
+
34
+ ## Included
35
+
36
+ - deterministic canonicalization
37
+ - HS256 HMAC signing and verification
38
+ - ED25519 signing and verification (requires `cryptography` package)
39
+ - compact wire encode/decode
40
+ - envelope validation helpers (version, required fields, TTL)
41
+ - conformance tests using shared vectors from `conformance/aip_v0_1.json`
42
+
43
+ ## Run conformance tests
44
+
45
+ ```bash
46
+ PYTHONPATH=sdk/python python3 -m unittest discover -s sdk/python/tests -v
47
+ ```
@@ -0,0 +1,9 @@
1
+ protocol_7h3/__init__.py,sha256=VEk1DMHva_UcGLKA-iHPrSYJqFD5tpUm6C5fmYSR7QA,1191
2
+ protocol_7h3/http.py,sha256=i7tMsJpBqej1g9tpC6P52YF5PduFMO3sOsCbbx2CRP4,6726
3
+ protocol_7h3/keys.py,sha256=HRE2jGBRRF9s7gt3biYAteeB3OVPYwUZPXhUH2uCsrM,5476
4
+ protocol_7h3/protocol.py,sha256=6ZJwJ2RcNhMqpkIGcON_OwaATn2kccXPfUH_xgc_riY,16833
5
+ protocol_7h3/queue.py,sha256=r_Ge8YEBIsRWwHpp-nlkAvAmuUkJDb1C3FeRJ6nJCiM,3504
6
+ protocol_7h3/webhook.py,sha256=hVeFSgAbFZ9kjxFwM7bVlvQAxTpwnd-dUzgKSVmbVwE,3780
7
+ 7h3_protocol-0.4.0.dist-info/METADATA,sha256=0bkaScqbrNB20vHuXn0smuw3aBUzj20c6rUzXCMQ21Y,1914
8
+ 7h3_protocol-0.4.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
9
+ 7h3_protocol-0.4.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.30.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,46 @@
1
+ from .protocol import ( # noqa: F401
2
+ canonicalize_envelope,
3
+ decode_envelope,
4
+ encode_envelope_compact,
5
+ sign_canonical_payload_ed25519,
6
+ sign_canonical_payload_hmac,
7
+ sign_envelope_ed25519,
8
+ sign_envelope_hmac,
9
+ validate_envelope,
10
+ verify_canonical_payload_ed25519,
11
+ verify_canonical_payload_hmac,
12
+ verify_envelope_ed25519,
13
+ verify_envelope_hmac,
14
+ )
15
+
16
+ try:
17
+ from .http import ( # noqa: F401
18
+ DEFAULT_HEADER, KeyRegistry, StaticKeyRegistry,
19
+ verify_http_envelope, sign_http_request, build_signed_request_headers,
20
+ )
21
+ except ImportError:
22
+ pass
23
+
24
+ try:
25
+ from .webhook import ( # noqa: F401
26
+ WEBHOOK_SIG_HEADER, WEBHOOK_TS_HEADER,
27
+ sign_webhook, sign_webhook_hmac, verify_webhook, verify_webhook_hmac, consume_webhook,
28
+ )
29
+ except ImportError:
30
+ pass
31
+
32
+ try:
33
+ from .queue import ( # noqa: F401
34
+ sign_queue_message, verify_queue_message, verify_queue_batch,
35
+ )
36
+ except ImportError:
37
+ pass
38
+
39
+ try:
40
+ from .keys import ( # noqa: F401
41
+ KeyEntry, WellKnownKeysDocument, ManagedKeyPair,
42
+ KeyRotationManager, RevocationRegistry,
43
+ fetch_well_known_keys,
44
+ )
45
+ except ImportError:
46
+ pass
protocol_7h3/http.py ADDED
@@ -0,0 +1,212 @@
1
+ """HTTP binding for 7h3 Protocol — signs/verifies per-request envelopes."""
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ import time
6
+ import uuid
7
+ import os
8
+ from typing import Any, Callable, Dict, Optional, Tuple
9
+
10
+ from .protocol import (
11
+ verify_envelope_ed25519,
12
+ verify_envelope_hmac,
13
+ sign_envelope_ed25519,
14
+ validate_envelope,
15
+ ProtocolDiagnostic,
16
+ )
17
+
18
+ DEFAULT_HEADER = "x-7h3-envelope"
19
+
20
+ VerifyFailReason = str # 'missing-header' | 'malformed-envelope' | 'unknown-sender' | 'invalid-signature' | 'ttl-expired'
21
+
22
+
23
+ class KeyRegistry:
24
+ """Abstract key registry — subclass to implement custom lookup."""
25
+
26
+ def get_public_key(self, sender_id: str) -> Optional[str]:
27
+ raise NotImplementedError
28
+
29
+ def get_shared_secret(self, key_id: str) -> Optional[str]:
30
+ return None
31
+
32
+
33
+ class StaticKeyRegistry(KeyRegistry):
34
+ """In-memory registry backed by a dict {sender_id: public_key_b64url}."""
35
+
36
+ def __init__(self, keys: Dict[str, str]):
37
+ self._keys = keys
38
+
39
+ def get_public_key(self, sender_id: str) -> Optional[str]:
40
+ return self._keys.get(sender_id)
41
+
42
+
43
+ def _make_envelope(
44
+ sender: str,
45
+ ttl_ms: int,
46
+ body: Dict[str, Any],
47
+ recipient: Optional[str] = None,
48
+ ) -> Dict[str, Any]:
49
+ """Build an unsigned envelope dict."""
50
+ header: Dict[str, Any] = {
51
+ "version": "7h3/0.1",
52
+ "messageId": str(uuid.uuid4()),
53
+ "timestampMs": int(time.time() * 1000),
54
+ "ttlMs": ttl_ms,
55
+ "sender": sender,
56
+ "nonce": os.urandom(8).hex(),
57
+ }
58
+ if recipient is not None:
59
+ header["recipient"] = recipient
60
+ return {"header": header, "body": body}
61
+
62
+
63
+ def verify_http_envelope(
64
+ headers: Dict[str, str],
65
+ registry: KeyRegistry,
66
+ *,
67
+ header_name: str = DEFAULT_HEADER,
68
+ strict_ttl: bool = True,
69
+ ) -> Tuple[bool, Optional[dict], Optional[VerifyFailReason]]:
70
+ """
71
+ Verify a 7h3 envelope from HTTP headers.
72
+ Returns (ok, envelope_dict, reason).
73
+ reason is None on success, a VerifyFailReason string on failure.
74
+ """
75
+ # Headers may arrive with mixed case — normalise
76
+ normalised = {k.lower(): v for k, v in headers.items()}
77
+ raw = normalised.get(header_name.lower())
78
+ if not raw:
79
+ return False, None, "missing-header"
80
+
81
+ try:
82
+ envelope = json.loads(raw)
83
+ except Exception:
84
+ return False, None, "malformed-envelope"
85
+
86
+ if not isinstance(envelope, dict) or "header" not in envelope or "signature" not in envelope:
87
+ return False, None, "malformed-envelope"
88
+
89
+ if strict_ttl:
90
+ now_ms = int(time.time() * 1000)
91
+ diags: list[ProtocolDiagnostic] = validate_envelope(envelope, now_ms=now_ms)
92
+ errors = [d for d in diags if d.level == "error"]
93
+ if errors:
94
+ return False, None, "ttl-expired"
95
+
96
+ sender = envelope.get("header", {}).get("sender", "")
97
+ alg = envelope.get("signature", {}).get("alg", "")
98
+
99
+ if alg == "ED25519":
100
+ pub_key = registry.get_public_key(sender)
101
+ if not pub_key:
102
+ return False, None, "unknown-sender"
103
+ valid = verify_envelope_ed25519(envelope, pub_key)
104
+ if not valid:
105
+ return False, None, "invalid-signature"
106
+ elif alg == "HS256":
107
+ key_id = envelope.get("signature", {}).get("keyId", "")
108
+ secret = registry.get_shared_secret(key_id)
109
+ if not secret:
110
+ return False, None, "unknown-sender"
111
+ valid = verify_envelope_hmac(envelope, secret)
112
+ if not valid:
113
+ return False, None, "invalid-signature"
114
+ else:
115
+ return False, None, "malformed-envelope"
116
+
117
+ return True, envelope, None
118
+
119
+
120
+ def sign_http_request(
121
+ envelope_without_sig: dict,
122
+ private_key: str,
123
+ *,
124
+ header_name: str = DEFAULT_HEADER,
125
+ ) -> Dict[str, str]:
126
+ """Sign an envelope and return HTTP headers dict to add to your request."""
127
+ signed = sign_envelope_ed25519(envelope_without_sig, private_key)
128
+ return {header_name: json.dumps(signed, separators=(",", ":"))}
129
+
130
+
131
+ def build_signed_request_headers(
132
+ sender: str,
133
+ private_key: str,
134
+ *,
135
+ recipient: Optional[str] = None,
136
+ ttl_ms: int = 60_000,
137
+ content: str = "",
138
+ header_name: str = DEFAULT_HEADER,
139
+ ) -> Dict[str, str]:
140
+ """Convenience: build envelope + sign in one call. Returns headers dict."""
141
+ envelope = _make_envelope(
142
+ sender=sender,
143
+ ttl_ms=ttl_ms,
144
+ body={"intent": "TASK", "content": content},
145
+ recipient=recipient,
146
+ )
147
+ return sign_http_request(envelope, private_key, header_name=header_name)
148
+
149
+
150
+ # --- Framework integrations ---
151
+
152
+
153
+ def starlette_middleware_factory(registry: KeyRegistry, *, header_name: str = DEFAULT_HEADER):
154
+ """
155
+ Returns an ASGI middleware class for Starlette/FastAPI.
156
+
157
+ Usage:
158
+ app.add_middleware(starlette_middleware_factory(registry))
159
+ """
160
+ try:
161
+ from starlette.middleware.base import BaseHTTPMiddleware
162
+ from starlette.requests import Request
163
+ from starlette.responses import JSONResponse
164
+ except ImportError as e:
165
+ raise ImportError("starlette is required: pip install starlette") from e
166
+
167
+ class Protocol7h3Middleware(BaseHTTPMiddleware):
168
+ async def dispatch(self, request: Request, call_next: Callable) -> Any:
169
+ headers = dict(request.headers)
170
+ ok, envelope, reason = verify_http_envelope(
171
+ headers, registry, header_name=header_name
172
+ )
173
+ if not ok:
174
+ return JSONResponse(
175
+ {"error": "7h3: request verification failed", "reason": reason},
176
+ status_code=401,
177
+ )
178
+ request.state.envelope_7h3 = envelope
179
+ return await call_next(request)
180
+
181
+ return Protocol7h3Middleware
182
+
183
+
184
+ def flask_before_request_factory(registry: KeyRegistry, *, header_name: str = DEFAULT_HEADER):
185
+ """
186
+ Returns a Flask before_request handler.
187
+
188
+ Usage:
189
+ app.before_request(flask_before_request_factory(registry))
190
+ """
191
+
192
+ def before_request():
193
+ try:
194
+ from flask import request as flask_req, g
195
+ except ImportError as e:
196
+ raise ImportError("flask is required: pip install flask") from e
197
+
198
+ headers = dict(flask_req.headers)
199
+ ok, envelope, reason = verify_http_envelope(headers, registry, header_name=header_name)
200
+ if not ok:
201
+ from flask import make_response
202
+
203
+ resp = make_response(
204
+ json.dumps({"error": "7h3: request verification failed", "reason": reason}),
205
+ 401,
206
+ )
207
+ resp.headers["Content-Type"] = "application/json"
208
+ return resp
209
+ g.envelope_7h3 = envelope
210
+ return None # continue
211
+
212
+ return before_request
protocol_7h3/keys.py ADDED
@@ -0,0 +1,149 @@
1
+ """Key infrastructure for 7h3 Protocol — discovery, rotation, revocation."""
2
+ from __future__ import annotations
3
+ import json
4
+ import time
5
+ import threading
6
+ from typing import Dict, List, Optional, Any
7
+ from dataclasses import dataclass, field
8
+
9
+ WELL_KNOWN_PATH = "/.well-known/7h3-keys"
10
+ REVOCATION_PATH = "/.well-known/7h3-revoked"
11
+
12
+ @dataclass
13
+ class KeyEntry:
14
+ id: str
15
+ algorithm: str # 'Ed25519'
16
+ public_key: str # SPKI base64url
17
+ created: int # Unix ms
18
+ expires: Optional[int] = None
19
+ revoked: bool = False
20
+ revoked_at: Optional[int] = None
21
+
22
+ @dataclass
23
+ class WellKnownKeysDocument:
24
+ version: str # '7h3/0.1'
25
+ updated: int
26
+ keys: List[KeyEntry]
27
+
28
+ def to_json(self) -> str:
29
+ keys_list = []
30
+ for k in self.keys:
31
+ entry = {
32
+ "id": k.id, "algorithm": k.algorithm, "publicKey": k.public_key,
33
+ "created": k.created,
34
+ }
35
+ if k.expires is not None: entry["expires"] = k.expires
36
+ if k.revoked: entry["revoked"] = True
37
+ if k.revoked_at is not None: entry["revokedAt"] = k.revoked_at
38
+ keys_list.append(entry)
39
+ return json.dumps({"version": self.version, "updated": self.updated, "keys": keys_list}, separators=(",", ":"))
40
+
41
+ @classmethod
42
+ def from_json(cls, data: str) -> "WellKnownKeysDocument":
43
+ d = json.loads(data)
44
+ keys = []
45
+ for k in d.get("keys", []):
46
+ keys.append(KeyEntry(
47
+ id=k["id"], algorithm=k["algorithm"], public_key=k["publicKey"],
48
+ created=k["created"], expires=k.get("expires"), revoked=k.get("revoked", False),
49
+ revoked_at=k.get("revokedAt"),
50
+ ))
51
+ return cls(version=d["version"], updated=d["updated"], keys=keys)
52
+
53
+
54
+ def fetch_well_known_keys(base_url: str, *, timeout: float = 5.0) -> WellKnownKeysDocument:
55
+ """Fetch the /.well-known/7h3-keys document from a base URL."""
56
+ import urllib.request
57
+ url = base_url.rstrip("/") + WELL_KNOWN_PATH
58
+ with urllib.request.urlopen(url, timeout=timeout) as resp:
59
+ data = resp.read().decode("utf-8")
60
+ return WellKnownKeysDocument.from_json(data)
61
+
62
+
63
+ @dataclass
64
+ class ManagedKeyPair:
65
+ id: str
66
+ public_key: str # SPKI base64url
67
+ private_key: str # PKCS8 base64url
68
+ created: int
69
+ expires_at: Optional[int] = None
70
+
71
+
72
+ class KeyRotationManager:
73
+ """Manages Ed25519 key pairs with automatic rotation."""
74
+
75
+ def __init__(self, max_age_ms: int = 86_400_000, overlap_ms: int = 3_600_000):
76
+ self.max_age_ms = max_age_ms
77
+ self.overlap_ms = overlap_ms
78
+ self._keys: List[ManagedKeyPair] = []
79
+ self._lock = threading.Lock()
80
+
81
+ def add_key(self, pair: ManagedKeyPair) -> None:
82
+ with self._lock:
83
+ self._keys.append(pair)
84
+
85
+ def get_current_key(self) -> Optional[ManagedKeyPair]:
86
+ """Return the most recently created non-expired key."""
87
+ now = int(time.time() * 1000)
88
+ with self._lock:
89
+ active = [k for k in self._keys if not k.expires_at or k.expires_at > now]
90
+ if not active:
91
+ return None
92
+ return sorted(active, key=lambda k: k.created, reverse=True)[0]
93
+
94
+ def rotate_if_needed(self) -> Optional[ManagedKeyPair]:
95
+ """Generate a new key if the current one is too old.
96
+
97
+ NOTE: generate_ed25519_keypair does not exist in protocol.py.
98
+ Callers should supply keys externally via add_key() rather than
99
+ relying on this method.
100
+ """
101
+ raise NotImplementedError(
102
+ "rotate_if_needed requires generate_ed25519_keypair which is not "
103
+ "implemented in protocol.py. Supply new keys externally via add_key()."
104
+ )
105
+
106
+ def get_well_known_document(self) -> WellKnownKeysDocument:
107
+ now = int(time.time() * 1000)
108
+ with self._lock:
109
+ entries = []
110
+ for k in self._keys:
111
+ entry = KeyEntry(
112
+ id=k.id, algorithm="Ed25519", public_key=k.public_key,
113
+ created=k.created, expires=k.expires_at,
114
+ revoked=bool(k.expires_at and k.expires_at < now),
115
+ )
116
+ entries.append(entry)
117
+ return WellKnownKeysDocument(version="7h3/0.1", updated=now, keys=entries)
118
+
119
+
120
+ class RevocationRegistry:
121
+ """Tracks revoked key IDs."""
122
+
123
+ def __init__(self):
124
+ self._revoked: Dict[str, Dict[str, Any]] = {}
125
+ self._lock = threading.Lock()
126
+
127
+ def revoke(self, key_id: str, reason: Optional[str] = None) -> None:
128
+ with self._lock:
129
+ self._revoked[key_id] = {"revokedAt": int(time.time() * 1000), "reason": reason}
130
+
131
+ def is_revoked(self, key_id: str) -> bool:
132
+ with self._lock:
133
+ return key_id in self._revoked
134
+
135
+ def get_list(self) -> dict:
136
+ now = int(time.time() * 1000)
137
+ with self._lock:
138
+ revoked = [
139
+ {"id": kid, "revokedAt": v["revokedAt"], **({"reason": v["reason"]} if v["reason"] else {})}
140
+ for kid, v in self._revoked.items()
141
+ ]
142
+ return {"version": "7h3/0.1", "updated": now, "revokedKeys": revoked}
143
+
144
+ def import_list(self, revocation_list: dict) -> None:
145
+ with self._lock:
146
+ for entry in revocation_list.get("revokedKeys", []):
147
+ kid = entry["id"]
148
+ if kid not in self._revoked:
149
+ self._revoked[kid] = {"revokedAt": entry["revokedAt"], "reason": entry.get("reason")}
@@ -0,0 +1,525 @@
1
+ from __future__ import annotations
2
+
3
+ import base64
4
+ import hashlib
5
+ import hmac
6
+ import json
7
+ from dataclasses import dataclass
8
+ from typing import Any, Dict, Optional, TypedDict
9
+
10
+ # ---------------------------------------------------------------------------
11
+ # Optional native Ed25519 backend — try cryptography, then PyNaCl
12
+ # ---------------------------------------------------------------------------
13
+
14
+ try:
15
+ from cryptography.hazmat.primitives import serialization
16
+ from cryptography.hazmat.primitives.asymmetric.ed25519 import (
17
+ Ed25519PrivateKey,
18
+ Ed25519PublicKey,
19
+ )
20
+
21
+ _HAS_CRYPTOGRAPHY = True
22
+ except Exception:
23
+ _HAS_CRYPTOGRAPHY = False
24
+
25
+ try:
26
+ import nacl.signing as _nacl_signing # type: ignore[import]
27
+
28
+ _HAS_NACL = True
29
+ except Exception:
30
+ _HAS_NACL = False
31
+
32
+ # ---------------------------------------------------------------------------
33
+ # Pure-Python Ed25519 fallback (no external dependencies)
34
+ #
35
+ # Uses extended twisted Edwards coordinates (X:Y:Z:T) to eliminate the
36
+ # expensive field inversion from the inner loop — inversion runs once at
37
+ # point compression only. ~10–50 ms/op in CPython; correct for conformance
38
+ # testing. Install 'cryptography' for production-grade performance.
39
+ #
40
+ # Based on the reference implementation: https://ed25519.cr.yp.to/software.html
41
+ # ---------------------------------------------------------------------------
42
+
43
+ _P = 2**255 - 19
44
+ _L = 2**252 + 27742317777372353535851937790883648493
45
+ _D = -121665 * pow(121666, _P - 2, _P) % _P
46
+ _SQRT_M1 = pow(2, (_P - 1) // 4, _P)
47
+
48
+ # Base point G in extended coordinates (X:Y:Z:T)
49
+ _GY = 4 * pow(5, _P - 2, _P) % _P
50
+ _GX_SQ = (_GY * _GY - 1) * pow(_D * _GY * _GY + 1, _P - 2, _P) % _P
51
+ _GX = pow(_GX_SQ, (_P + 3) // 8, _P)
52
+ if (_GX * _GX - _GX_SQ) % _P != 0:
53
+ _GX = _GX * _SQRT_M1 % _P
54
+ if _GX & 1:
55
+ _GX = _P - _GX
56
+ _G_BASE = (_GX, _GY, 1, _GX * _GY % _P)
57
+
58
+
59
+ def _point_add(P: tuple, Q: tuple) -> tuple:
60
+ A = (P[1] - P[0]) * (Q[1] - Q[0]) % _P
61
+ B = (P[1] + P[0]) * (Q[1] + Q[0]) % _P
62
+ C = 2 * P[3] * Q[3] * _D % _P
63
+ D_ = 2 * P[2] * Q[2] % _P
64
+ E = B - A
65
+ F = D_ - C
66
+ G_ = D_ + C
67
+ H = B + A
68
+ return (E * F % _P, G_ * H % _P, F * G_ % _P, E * H % _P)
69
+
70
+
71
+ def _point_mul(s: int, P: tuple) -> tuple:
72
+ Q: tuple = (0, 1, 1, 0) # neutral element
73
+ while s > 0:
74
+ if s & 1:
75
+ Q = _point_add(Q, P)
76
+ P = _point_add(P, P)
77
+ s >>= 1
78
+ return Q
79
+
80
+
81
+ def _compress(P: tuple) -> bytes:
82
+ zi = pow(P[2], _P - 2, _P)
83
+ x = P[0] * zi % _P
84
+ y = P[1] * zi % _P
85
+ return int.to_bytes(y | ((x & 1) << 255), 32, "little")
86
+
87
+
88
+ def _decompress(b: bytes) -> Optional[tuple]:
89
+ y = int.from_bytes(b, "little")
90
+ sign = y >> 255
91
+ y &= (1 << 255) - 1
92
+ x2 = (y * y - 1) * pow(_D * y * y + 1, _P - 2, _P) % _P
93
+ if x2 == 0:
94
+ return (0, y, 1, 0) if sign == 0 else None
95
+ x = pow(x2, (_P + 3) // 8, _P)
96
+ if (x * x - x2) % _P != 0:
97
+ x = x * _SQRT_M1 % _P
98
+ if (x * x - x2) % _P != 0:
99
+ return None
100
+ if x & 1 != sign:
101
+ x = _P - x
102
+ return (x, y, 1, x * y % _P)
103
+
104
+
105
+ def _py_ed25519_sign(seed: bytes, message: bytes) -> bytes:
106
+ h = hashlib.sha512(seed).digest()
107
+ a_bytes = bytearray(h[:32])
108
+ a_bytes[0] &= 248
109
+ a_bytes[31] &= 127
110
+ a_bytes[31] |= 64
111
+ a = int.from_bytes(a_bytes, "little")
112
+ prefix = h[32:]
113
+ A = _compress(_point_mul(a, _G_BASE))
114
+ r = int.from_bytes(hashlib.sha512(prefix + message).digest(), "little") % _L
115
+ R = _compress(_point_mul(r, _G_BASE))
116
+ S = (r + int.from_bytes(hashlib.sha512(R + A + message).digest(), "little") * a) % _L
117
+ return R + int.to_bytes(S, 32, "little")
118
+
119
+
120
+ def _py_ed25519_verify(pub_bytes: bytes, message: bytes, sig: bytes) -> bool:
121
+ if len(sig) != 64 or len(pub_bytes) != 32:
122
+ return False
123
+ A = _decompress(pub_bytes)
124
+ if A is None:
125
+ return False
126
+ R_pt = _decompress(sig[:32])
127
+ if R_pt is None:
128
+ return False
129
+ s = int.from_bytes(sig[32:], "little")
130
+ if s >= _L:
131
+ return False
132
+ h = int.from_bytes(hashlib.sha512(sig[:32] + pub_bytes + message).digest(), "little")
133
+ sB = _point_mul(s, _G_BASE)
134
+ hA = _point_mul(h, A)
135
+ return _compress(sB) == _compress(_point_add(R_pt, hA))
136
+
137
+
138
+ # ---------------------------------------------------------------------------
139
+ # DER key extraction helpers
140
+ #
141
+ # WebCrypto exports Ed25519 keys as PKCS8 (private) and SPKI (public) DER.
142
+ # Both formats have a fixed structure for Ed25519 with known byte offsets:
143
+ #
144
+ # PKCS8 v0 (48 bytes):
145
+ # 30 2e 02 01 00 30 05 06 03 2b 65 70 04 22 04 20 [32-byte seed]
146
+ #
147
+ # SPKI (44 bytes):
148
+ # 30 2a 30 05 06 03 2b 65 70 03 21 00 [32-byte public key]
149
+ # ---------------------------------------------------------------------------
150
+
151
+ # fmt: off
152
+ _PKCS8_ED25519_LEN = 48 # 30 2e 02 01 00 30 05 06 03 2b 65 70 04 22 04 20 [32-byte seed]
153
+ _SPKI_ED25519_LEN = 44 # 30 2a 30 05 06 03 2b 65 70 03 21 00 [32-byte public key]
154
+ # fmt: on
155
+
156
+
157
+ def _seed_from_pkcs8(pkcs8_der: bytes) -> bytes:
158
+ if len(pkcs8_der) != 48:
159
+ raise ValueError(
160
+ f"Ed25519 PKCS8 DER must be 48 bytes, got {len(pkcs8_der)}. "
161
+ "Ensure the key was exported with SubtleCrypto.exportKey('pkcs8', key)."
162
+ )
163
+ return pkcs8_der[16:48]
164
+
165
+
166
+ def _pubkey_from_spki(spki_der: bytes) -> bytes:
167
+ if len(spki_der) != 44:
168
+ raise ValueError(
169
+ f"Ed25519 SPKI DER must be 44 bytes, got {len(spki_der)}. "
170
+ "Ensure the key was exported with SubtleCrypto.exportKey('spki', key)."
171
+ )
172
+ return spki_der[12:44]
173
+
174
+
175
+ # ---------------------------------------------------------------------------
176
+ # Protocol types
177
+ # ---------------------------------------------------------------------------
178
+
179
+
180
+ class ProtocolHeader(TypedDict, total=False):
181
+ version: str
182
+ messageId: str
183
+ timestampMs: int
184
+ ttlMs: int
185
+ sender: str
186
+ recipient: str
187
+ nonce: str
188
+
189
+
190
+ class ProtocolBody(TypedDict, total=False):
191
+ intent: str
192
+ content: str
193
+ capability: str
194
+ correlationId: str
195
+
196
+
197
+ class ProtocolSignature(TypedDict):
198
+ alg: str
199
+ keyId: str
200
+ value: str
201
+
202
+
203
+ class ProtocolEnvelope(TypedDict, total=False):
204
+ header: ProtocolHeader
205
+ body: ProtocolBody
206
+ signature: ProtocolSignature
207
+
208
+
209
+ @dataclass(frozen=True)
210
+ class ProtocolDiagnostic:
211
+ level: str
212
+ message: str
213
+
214
+
215
+ # ---------------------------------------------------------------------------
216
+ # Utility
217
+ # ---------------------------------------------------------------------------
218
+
219
+
220
+ def _json_string(value: Any) -> str:
221
+ return json.dumps(value, ensure_ascii=False, separators=(",", ":"))
222
+
223
+
224
+ def _base64url(data: bytes) -> str:
225
+ return base64.urlsafe_b64encode(data).decode("ascii").rstrip("=")
226
+
227
+
228
+ def _base64url_decode(value: str) -> bytes:
229
+ padding = "=" * ((4 - (len(value) % 4)) % 4)
230
+ return base64.urlsafe_b64decode(value + padding)
231
+
232
+
233
+ # ---------------------------------------------------------------------------
234
+ # Canonicalization
235
+ # ---------------------------------------------------------------------------
236
+
237
+
238
+ def canonicalize_envelope(envelope: Dict[str, Any]) -> str:
239
+ header = envelope["header"]
240
+ body = envelope["body"]
241
+
242
+ body_parts = []
243
+ if "capability" in body and body["capability"] is not None:
244
+ body_parts.append(f'"capability":{_json_string(body["capability"])}')
245
+ body_parts.append(f'"content":{_json_string(body["content"])}')
246
+ if "correlationId" in body and body["correlationId"] is not None:
247
+ body_parts.append(f'"correlationId":{_json_string(body["correlationId"])}')
248
+ body_parts.append(f'"intent":{_json_string(body["intent"])}')
249
+
250
+ header_parts = [
251
+ f'"messageId":{_json_string(header["messageId"])}',
252
+ f'"nonce":{_json_string(header["nonce"])}',
253
+ ]
254
+ if "recipient" in header and header["recipient"] is not None:
255
+ header_parts.append(f'"recipient":{_json_string(header["recipient"])}')
256
+ header_parts.extend(
257
+ [
258
+ f'"sender":{_json_string(header["sender"])}',
259
+ f'"timestampMs":{int(header["timestampMs"])}',
260
+ f'"ttlMs":{int(header["ttlMs"])}',
261
+ f'"version":{_json_string(header["version"])}',
262
+ ]
263
+ )
264
+
265
+ return (
266
+ f'{{"body":{{{",".join(body_parts)}}},"header":{{{",".join(header_parts)}}}}}'
267
+ )
268
+
269
+
270
+ # ---------------------------------------------------------------------------
271
+ # HMAC-SHA256 (HS256)
272
+ # ---------------------------------------------------------------------------
273
+
274
+
275
+ def sign_canonical_payload_hmac(canonical_payload: str, secret: str) -> str:
276
+ digest = hmac.new(
277
+ secret.encode("utf-8"), canonical_payload.encode("utf-8"), hashlib.sha256
278
+ ).digest()
279
+ return _base64url(digest)
280
+
281
+
282
+ def verify_canonical_payload_hmac(
283
+ canonical_payload: str, signature: str, secret: str
284
+ ) -> bool:
285
+ expected = sign_canonical_payload_hmac(canonical_payload, secret)
286
+ return hmac.compare_digest(signature, expected)
287
+
288
+
289
+ # ---------------------------------------------------------------------------
290
+ # Ed25519 — tries cryptography → PyNaCl → pure-Python in order
291
+ # ---------------------------------------------------------------------------
292
+
293
+
294
+ def sign_canonical_payload_ed25519(
295
+ canonical_payload: str, private_key_pkcs8_base64url: str
296
+ ) -> str:
297
+ private_der = _base64url_decode(private_key_pkcs8_base64url)
298
+ msg = canonical_payload.encode("utf-8")
299
+
300
+ if _HAS_CRYPTOGRAPHY:
301
+ key = serialization.load_der_private_key(private_der, password=None)
302
+ if not isinstance(key, Ed25519PrivateKey):
303
+ raise ValueError("DER key is not an Ed25519 private key")
304
+ return _base64url(key.sign(msg))
305
+
306
+ if _HAS_NACL:
307
+ seed = _seed_from_pkcs8(private_der)
308
+ sk = _nacl_signing.SigningKey(seed)
309
+ return _base64url(bytes(sk.sign(msg).signature))
310
+
311
+ # Pure-Python fallback
312
+ seed = _seed_from_pkcs8(private_der)
313
+ return _base64url(_py_ed25519_sign(seed, msg))
314
+
315
+
316
+ def verify_canonical_payload_ed25519(
317
+ canonical_payload: str, signature: str, public_key_spki_base64url: str
318
+ ) -> bool:
319
+ public_der = _base64url_decode(public_key_spki_base64url)
320
+ msg = canonical_payload.encode("utf-8")
321
+ sig_bytes = _base64url_decode(signature)
322
+
323
+ if _HAS_CRYPTOGRAPHY:
324
+ try:
325
+ key = serialization.load_der_public_key(public_der)
326
+ if not isinstance(key, Ed25519PublicKey):
327
+ return False
328
+ key.verify(sig_bytes, msg)
329
+ return True
330
+ except Exception:
331
+ return False
332
+
333
+ if _HAS_NACL:
334
+ try:
335
+ pub_bytes = _pubkey_from_spki(public_der)
336
+ vk = _nacl_signing.VerifyKey(pub_bytes)
337
+ vk.verify(msg, sig_bytes)
338
+ return True
339
+ except Exception:
340
+ return False
341
+
342
+ # Pure-Python fallback
343
+ try:
344
+ pub_bytes = _pubkey_from_spki(public_der)
345
+ return _py_ed25519_verify(pub_bytes, msg, sig_bytes)
346
+ except Exception:
347
+ return False
348
+
349
+
350
+ # ---------------------------------------------------------------------------
351
+ # Envelope-level sign/verify helpers
352
+ # ---------------------------------------------------------------------------
353
+
354
+
355
+ def sign_envelope_hmac(
356
+ envelope: Dict[str, Any], secret: str, key_id: str = "python-dev-key"
357
+ ) -> Dict[str, Any]:
358
+ canonical = canonicalize_envelope(envelope)
359
+ signature = sign_canonical_payload_hmac(canonical, secret)
360
+ return {
361
+ "header": dict(envelope["header"]),
362
+ "body": dict(envelope["body"]),
363
+ "signature": {"alg": "HS256", "keyId": key_id, "value": signature},
364
+ }
365
+
366
+
367
+ def verify_envelope_hmac(envelope: Dict[str, Any], secret: str) -> bool:
368
+ signature = envelope.get("signature")
369
+ if not signature:
370
+ return False
371
+ if signature.get("alg") != "HS256":
372
+ return False
373
+ unsigned = {"header": envelope["header"], "body": envelope["body"]}
374
+ canonical = canonicalize_envelope(unsigned)
375
+ return verify_canonical_payload_hmac(canonical, signature.get("value", ""), secret)
376
+
377
+
378
+ def sign_envelope_ed25519(
379
+ envelope: Dict[str, Any],
380
+ private_key_pkcs8_base64url: str,
381
+ key_id: str = "python-ed25519-key",
382
+ ) -> Dict[str, Any]:
383
+ canonical = canonicalize_envelope(envelope)
384
+ signature = sign_canonical_payload_ed25519(canonical, private_key_pkcs8_base64url)
385
+ return {
386
+ "header": dict(envelope["header"]),
387
+ "body": dict(envelope["body"]),
388
+ "signature": {"alg": "ED25519", "keyId": key_id, "value": signature},
389
+ }
390
+
391
+
392
+ def verify_envelope_ed25519(
393
+ envelope: Dict[str, Any], public_key_spki_base64url: str
394
+ ) -> bool:
395
+ signature = envelope.get("signature")
396
+ if not signature:
397
+ return False
398
+ if signature.get("alg") != "ED25519":
399
+ return False
400
+ unsigned = {"header": envelope["header"], "body": envelope["body"]}
401
+ canonical = canonicalize_envelope(unsigned)
402
+ return verify_canonical_payload_ed25519(
403
+ canonical, signature.get("value", ""), public_key_spki_base64url
404
+ )
405
+
406
+
407
+ # ---------------------------------------------------------------------------
408
+ # Wire encode/decode
409
+ # ---------------------------------------------------------------------------
410
+
411
+
412
+ def encode_envelope_compact(envelope: Dict[str, Any]) -> str:
413
+ header = envelope["header"]
414
+ body = envelope["body"]
415
+ compact: Dict[str, Any] = {
416
+ "v": header["version"],
417
+ "mid": header["messageId"],
418
+ "ts": header["timestampMs"],
419
+ "ttl": header["ttlMs"],
420
+ "s": header["sender"],
421
+ "n": header["nonce"],
422
+ "i": body["intent"],
423
+ "c": body["content"],
424
+ }
425
+ if "recipient" in header and header["recipient"] is not None:
426
+ compact["r"] = header["recipient"]
427
+ if "capability" in body and body["capability"] is not None:
428
+ compact["cap"] = body["capability"]
429
+ if "correlationId" in body and body["correlationId"] is not None:
430
+ compact["cid"] = body["correlationId"]
431
+
432
+ signature = envelope.get("signature")
433
+ if signature:
434
+ compact["sig"] = {
435
+ "a": signature.get("alg", "HS256"),
436
+ "k": signature["keyId"],
437
+ "v": signature["value"],
438
+ }
439
+
440
+ return _json_string(compact)
441
+
442
+
443
+ def decode_envelope(raw: str) -> Dict[str, Any]:
444
+ parsed = json.loads(raw)
445
+ if "header" in parsed and "body" in parsed:
446
+ return parsed
447
+
448
+ if parsed.get("v") != "7h3/0.1" or "mid" not in parsed or "i" not in parsed:
449
+ raise ValueError("Envelope JSON shape is not recognized")
450
+
451
+ header: Dict[str, Any] = {
452
+ "version": parsed["v"],
453
+ "messageId": parsed["mid"],
454
+ "timestampMs": parsed["ts"],
455
+ "ttlMs": parsed["ttl"],
456
+ "sender": parsed["s"],
457
+ "nonce": parsed["n"],
458
+ }
459
+ if "r" in parsed:
460
+ header["recipient"] = parsed["r"]
461
+
462
+ body: Dict[str, Any] = {
463
+ "intent": parsed["i"],
464
+ "content": parsed["c"],
465
+ }
466
+ if "cap" in parsed:
467
+ body["capability"] = parsed["cap"]
468
+ if "cid" in parsed:
469
+ body["correlationId"] = parsed["cid"]
470
+
471
+ envelope: Dict[str, Any] = {"header": header, "body": body}
472
+ if parsed.get("sig"):
473
+ envelope["signature"] = {
474
+ "alg": parsed["sig"].get("a", "HS256"),
475
+ "keyId": parsed["sig"]["k"],
476
+ "value": parsed["sig"]["v"],
477
+ }
478
+ return envelope
479
+
480
+
481
+ # ---------------------------------------------------------------------------
482
+ # Envelope validation
483
+ # ---------------------------------------------------------------------------
484
+
485
+
486
+ def validate_envelope(
487
+ envelope: Dict[str, Any], now_ms: Optional[int] = None
488
+ ) -> list[ProtocolDiagnostic]:
489
+ header = envelope["header"]
490
+ body = envelope["body"]
491
+ current = int(now_ms if now_ms is not None else 0)
492
+ diagnostics: list[ProtocolDiagnostic] = []
493
+
494
+ if header.get("version") != "7h3/0.1":
495
+ diagnostics.append(
496
+ ProtocolDiagnostic(
497
+ level="error",
498
+ message=f"Unsupported protocol version '{header.get('version')}'",
499
+ )
500
+ )
501
+ if not str(header.get("messageId", "")).strip():
502
+ diagnostics.append(
503
+ ProtocolDiagnostic(level="error", message="Missing messageId")
504
+ )
505
+ if not str(header.get("sender", "")).strip():
506
+ diagnostics.append(
507
+ ProtocolDiagnostic(level="error", message="Missing sender identity")
508
+ )
509
+ if int(header.get("ttlMs", 0)) <= 0:
510
+ diagnostics.append(
511
+ ProtocolDiagnostic(level="error", message="ttlMs must be greater than zero")
512
+ )
513
+
514
+ if now_ms is not None:
515
+ if int(header.get("timestampMs", 0)) + int(header.get("ttlMs", 0)) < current:
516
+ diagnostics.append(
517
+ ProtocolDiagnostic(level="error", message="Message TTL expired")
518
+ )
519
+
520
+ if not str(body.get("content", "")).strip():
521
+ diagnostics.append(
522
+ ProtocolDiagnostic(level="warning", message="Empty content payload")
523
+ )
524
+
525
+ return diagnostics
protocol_7h3/queue.py ADDED
@@ -0,0 +1,118 @@
1
+ """Queue message binding for 7h3 Protocol — Kafka/SQS/Pub-Sub/RabbitMQ."""
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ import secrets
6
+ import time
7
+ from typing import Any, Dict, List, Optional
8
+
9
+ QUEUE_DEFAULT_TTL_MS = 3_600_000 # 1 hour
10
+
11
+
12
+ def _create_envelope(
13
+ sender: str,
14
+ content: str,
15
+ ttl_ms: int,
16
+ *,
17
+ recipient: Optional[str] = None,
18
+ intent: str = "TASK",
19
+ ) -> Dict[str, Any]:
20
+ """Build a bare (unsigned) protocol envelope dict."""
21
+ now_ms = int(time.time() * 1000)
22
+ message_id = secrets.token_hex(16)
23
+ nonce = secrets.token_hex(8)
24
+ header: Dict[str, Any] = {
25
+ "version": "7h3/0.1",
26
+ "messageId": message_id,
27
+ "timestampMs": now_ms,
28
+ "ttlMs": ttl_ms,
29
+ "sender": sender,
30
+ "nonce": nonce,
31
+ }
32
+ if recipient is not None:
33
+ header["recipient"] = recipient
34
+
35
+ body: Dict[str, Any] = {
36
+ "intent": intent,
37
+ "content": content,
38
+ }
39
+ return {"header": header, "body": body}
40
+
41
+
42
+ def sign_queue_message(
43
+ payload: Any,
44
+ private_key: str,
45
+ sender: str,
46
+ *,
47
+ recipient: Optional[str] = None,
48
+ ttl_ms: int = QUEUE_DEFAULT_TTL_MS,
49
+ ) -> str:
50
+ """
51
+ Sign a payload for queue transit.
52
+ Returns a JSON string: {"envelope": {...}, "payload": payload}
53
+ """
54
+ from .protocol import sign_envelope_ed25519
55
+
56
+ content = payload if isinstance(payload, str) else json.dumps(payload, separators=(",", ":"))
57
+ envelope = _create_envelope(
58
+ sender=sender,
59
+ content=content,
60
+ ttl_ms=ttl_ms,
61
+ recipient=recipient,
62
+ )
63
+ signed = sign_envelope_ed25519(envelope, private_key)
64
+ return json.dumps({"envelope": signed, "payload": payload}, separators=(",", ":"))
65
+
66
+
67
+ def verify_queue_message(message: str, public_key: str) -> Dict[str, Any]:
68
+ """
69
+ Verify and unwrap a queue message.
70
+ Returns {"payload": Any, "envelope": dict}.
71
+ Raises ValueError on failure.
72
+ """
73
+ from .protocol import verify_envelope_ed25519, validate_envelope
74
+
75
+ try:
76
+ wrapper = json.loads(message)
77
+ except Exception as e:
78
+ raise ValueError(f"7h3: malformed queue message: {e}") from e
79
+
80
+ envelope = wrapper.get("envelope")
81
+ payload = wrapper.get("payload")
82
+ if not envelope or not isinstance(envelope, dict):
83
+ raise ValueError("7h3: missing envelope in queue message")
84
+
85
+ # validate_envelope returns list[ProtocolDiagnostic] (dataclass objects)
86
+ diags = validate_envelope(envelope)
87
+ errors = [d for d in diags if d.level == "error"]
88
+ if errors:
89
+ raise ValueError(
90
+ f"7h3: envelope validation failed: {'; '.join(d.message for d in errors)}"
91
+ )
92
+
93
+ valid = verify_envelope_ed25519(envelope, public_key)
94
+ if not valid:
95
+ raise ValueError("7h3: invalid signature on queue message")
96
+
97
+ return {"payload": payload, "envelope": envelope}
98
+
99
+
100
+ def verify_queue_batch(
101
+ messages: List[str],
102
+ public_key: str,
103
+ ) -> List[Dict[str, Any]]:
104
+ """
105
+ Verify a batch of queue messages without throwing.
106
+ Each result is {"ok": True, "payload": ..., "envelope": ...} or
107
+ {"ok": False, "raw": ..., "error": "..."}.
108
+ """
109
+ results = []
110
+ for msg in messages:
111
+ try:
112
+ result = verify_queue_message(msg, public_key)
113
+ results.append(
114
+ {"ok": True, "payload": result["payload"], "envelope": result["envelope"]}
115
+ )
116
+ except Exception as e:
117
+ results.append({"ok": False, "raw": msg, "error": str(e)})
118
+ return results
@@ -0,0 +1,116 @@
1
+ """Webhook binding for 7h3 Protocol — lightweight per-payload signing."""
2
+ from __future__ import annotations
3
+ import json
4
+ import time
5
+ from typing import Any, Dict, Optional, TypeVar, Union
6
+
7
+ WEBHOOK_SIG_HEADER = "x-7h3-sig"
8
+ WEBHOOK_TS_HEADER = "x-7h3-ts"
9
+ WEBHOOK_DEFAULT_TTL_MS = 300_000 # 5 minutes
10
+
11
+
12
+ def _webhook_signing_payload(timestamp_ms: int, body: str) -> str:
13
+ return f"{timestamp_ms}.{body}"
14
+
15
+
16
+ def sign_webhook(
17
+ payload: Union[str, bytes],
18
+ private_key: str,
19
+ *,
20
+ ttl_ms: int = WEBHOOK_DEFAULT_TTL_MS,
21
+ ) -> Dict[str, str]:
22
+ """Sign a webhook payload. Returns headers dict with x-7h3-sig and x-7h3-ts."""
23
+ from .protocol import sign_canonical_payload_ed25519
24
+
25
+ body = payload.decode("utf-8") if isinstance(payload, bytes) else payload
26
+ ts = int(time.time() * 1000)
27
+ signing_payload = _webhook_signing_payload(ts, body)
28
+ sig = sign_canonical_payload_ed25519(signing_payload, private_key)
29
+ return {WEBHOOK_SIG_HEADER: sig, WEBHOOK_TS_HEADER: str(ts)}
30
+
31
+
32
+ def sign_webhook_hmac(
33
+ payload: Union[str, bytes],
34
+ secret: str,
35
+ *,
36
+ ttl_ms: int = WEBHOOK_DEFAULT_TTL_MS,
37
+ ) -> Dict[str, str]:
38
+ """Sign a webhook payload with HMAC-SHA256 shared secret."""
39
+ from .protocol import sign_canonical_payload_hmac
40
+
41
+ body = payload.decode("utf-8") if isinstance(payload, bytes) else payload
42
+ ts = int(time.time() * 1000)
43
+ signing_payload = _webhook_signing_payload(ts, body)
44
+ sig = sign_canonical_payload_hmac(signing_payload, secret)
45
+ return {WEBHOOK_SIG_HEADER: sig, WEBHOOK_TS_HEADER: str(ts)}
46
+
47
+
48
+ def verify_webhook(
49
+ payload: Union[str, bytes],
50
+ headers: Dict[str, str],
51
+ public_key: str,
52
+ *,
53
+ max_age_ms: int = WEBHOOK_DEFAULT_TTL_MS,
54
+ ) -> bool:
55
+ """Verify an Ed25519 webhook signature. Returns True/False."""
56
+ from .protocol import verify_canonical_payload_ed25519
57
+
58
+ body = payload.decode("utf-8") if isinstance(payload, bytes) else payload
59
+ # normalise header keys
60
+ normalised = {k.lower(): v for k, v in headers.items()}
61
+ sig = normalised.get(WEBHOOK_SIG_HEADER.lower())
62
+ ts_str = normalised.get(WEBHOOK_TS_HEADER.lower())
63
+ if not sig or not ts_str:
64
+ return False
65
+ try:
66
+ ts = int(ts_str)
67
+ except ValueError:
68
+ return False
69
+ now_ms = int(time.time() * 1000)
70
+ if now_ms - ts > max_age_ms:
71
+ return False
72
+ signing_payload = _webhook_signing_payload(ts, body)
73
+ return verify_canonical_payload_ed25519(signing_payload, sig, public_key)
74
+
75
+
76
+ def verify_webhook_hmac(
77
+ payload: Union[str, bytes],
78
+ headers: Dict[str, str],
79
+ secret: str,
80
+ *,
81
+ max_age_ms: int = WEBHOOK_DEFAULT_TTL_MS,
82
+ ) -> bool:
83
+ """Verify an HMAC webhook signature."""
84
+ from .protocol import verify_canonical_payload_hmac
85
+
86
+ body = payload.decode("utf-8") if isinstance(payload, bytes) else payload
87
+ normalised = {k.lower(): v for k, v in headers.items()}
88
+ sig = normalised.get(WEBHOOK_SIG_HEADER.lower())
89
+ ts_str = normalised.get(WEBHOOK_TS_HEADER.lower())
90
+ if not sig or not ts_str:
91
+ return False
92
+ try:
93
+ ts = int(ts_str)
94
+ except ValueError:
95
+ return False
96
+ now_ms = int(time.time() * 1000)
97
+ if now_ms - ts > max_age_ms:
98
+ return False
99
+ signing_payload = _webhook_signing_payload(ts, body)
100
+ return verify_canonical_payload_hmac(signing_payload, sig, secret)
101
+
102
+
103
+ T = TypeVar("T")
104
+
105
+
106
+ def consume_webhook(
107
+ payload: str,
108
+ headers: Dict[str, str],
109
+ public_key: str,
110
+ *,
111
+ max_age_ms: int = WEBHOOK_DEFAULT_TTL_MS,
112
+ ) -> Any:
113
+ """Parse and verify a JSON webhook payload. Raises ValueError on failure."""
114
+ if not verify_webhook(payload, headers, public_key, max_age_ms=max_age_ms):
115
+ raise ValueError("7h3: webhook signature verification failed")
116
+ return json.loads(payload)