aip7h3 0.1.2__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.
- aip7h3/__init__.py +14 -0
- aip7h3/protocol.py +525 -0
- aip7h3-0.1.2.dist-info/METADATA +47 -0
- aip7h3-0.1.2.dist-info/RECORD +5 -0
- aip7h3-0.1.2.dist-info/WHEEL +4 -0
aip7h3/__init__.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
)
|
aip7h3/protocol.py
ADDED
|
@@ -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") != "aip/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") != "aip/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
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: aip7h3
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: AIP (Aurelion Interaction Protocol) — Python SDK. Deterministic, signed, replay-safe AI-to-AI messaging. Wire version aip/0.1.
|
|
5
|
+
Project-URL: Homepage, https://github.com/IceMasterT/7h3-protocol-aip
|
|
6
|
+
Project-URL: Repository, https://github.com/IceMasterT/7h3-protocol-aip
|
|
7
|
+
Project-URL: Documentation, https://github.com/IceMasterT/7h3-protocol-aip/tree/main/sdk/python
|
|
8
|
+
Project-URL: Changelog, https://github.com/IceMasterT/7h3-protocol-aip/blob/main/CHANGELOG.md
|
|
9
|
+
License: MIT
|
|
10
|
+
Keywords: agent,aip,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,5 @@
|
|
|
1
|
+
aip7h3/__init__.py,sha256=IfaMm3N0peCq-iDCPGQ92exx7TLsH5y6N4h2C7XQd9k,388
|
|
2
|
+
aip7h3/protocol.py,sha256=QYAPV0g_KvCpmJI6TNOV31v1gfT3V0R5WQ_UXO1WBcQ,16833
|
|
3
|
+
aip7h3-0.1.2.dist-info/METADATA,sha256=jv0s4TDdC8dKhnsMwhgLEgXry0AnCkQtz_OVWdMji_g,1947
|
|
4
|
+
aip7h3-0.1.2.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
5
|
+
aip7h3-0.1.2.dist-info/RECORD,,
|