enfinitos-sdk-auditor 0.0.1__tar.gz

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.
Files changed (30) hide show
  1. enfinitos_sdk_auditor-0.0.1/LICENSE +21 -0
  2. enfinitos_sdk_auditor-0.0.1/PKG-INFO +363 -0
  3. enfinitos_sdk_auditor-0.0.1/README.md +335 -0
  4. enfinitos_sdk_auditor-0.0.1/pyproject.toml +52 -0
  5. enfinitos_sdk_auditor-0.0.1/setup.cfg +4 -0
  6. enfinitos_sdk_auditor-0.0.1/src/enfinitos_auditor/__init__.py +109 -0
  7. enfinitos_sdk_auditor-0.0.1/src/enfinitos_auditor/auditor.py +409 -0
  8. enfinitos_sdk_auditor-0.0.1/src/enfinitos_auditor/canonical_json.py +204 -0
  9. enfinitos_sdk_auditor-0.0.1/src/enfinitos_auditor/errors.py +83 -0
  10. enfinitos_sdk_auditor-0.0.1/src/enfinitos_auditor/hashing.py +80 -0
  11. enfinitos_sdk_auditor-0.0.1/src/enfinitos_auditor/keys.py +343 -0
  12. enfinitos_sdk_auditor-0.0.1/src/enfinitos_auditor/metering_audit.py +292 -0
  13. enfinitos_sdk_auditor-0.0.1/src/enfinitos_auditor/proof_chain.py +169 -0
  14. enfinitos_sdk_auditor-0.0.1/src/enfinitos_auditor/proof_pack.py +501 -0
  15. enfinitos_sdk_auditor-0.0.1/src/enfinitos_auditor/py.typed +0 -0
  16. enfinitos_sdk_auditor-0.0.1/src/enfinitos_auditor/settlement_audit.py +304 -0
  17. enfinitos_sdk_auditor-0.0.1/src/enfinitos_auditor/tenant_chain.py +291 -0
  18. enfinitos_sdk_auditor-0.0.1/src/enfinitos_auditor/types.py +410 -0
  19. enfinitos_sdk_auditor-0.0.1/src/enfinitos_sdk_auditor.egg-info/PKG-INFO +363 -0
  20. enfinitos_sdk_auditor-0.0.1/src/enfinitos_sdk_auditor.egg-info/SOURCES.txt +28 -0
  21. enfinitos_sdk_auditor-0.0.1/src/enfinitos_sdk_auditor.egg-info/dependency_links.txt +1 -0
  22. enfinitos_sdk_auditor-0.0.1/src/enfinitos_sdk_auditor.egg-info/requires.txt +5 -0
  23. enfinitos_sdk_auditor-0.0.1/src/enfinitos_sdk_auditor.egg-info/top_level.txt +1 -0
  24. enfinitos_sdk_auditor-0.0.1/tests/test_auditor.py +115 -0
  25. enfinitos_sdk_auditor-0.0.1/tests/test_canonical_json.py +96 -0
  26. enfinitos_sdk_auditor-0.0.1/tests/test_keys.py +172 -0
  27. enfinitos_sdk_auditor-0.0.1/tests/test_metering_audit.py +71 -0
  28. enfinitos_sdk_auditor-0.0.1/tests/test_proof_chain.py +54 -0
  29. enfinitos_sdk_auditor-0.0.1/tests/test_proof_pack.py +151 -0
  30. enfinitos_sdk_auditor-0.0.1/tests/test_settlement_audit.py +90 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 EnfinitOS Ltd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,363 @@
1
+ Metadata-Version: 2.4
2
+ Name: enfinitos-sdk-auditor
3
+ Version: 0.0.1
4
+ Summary: EnfinitOS Auditor/Verifier SDK (Python) — cryptographic verification library that regulators, auditors, courts, and third parties use to verify signed proof packs issued by EnfinitOS without trusting the vendor. Parses signed proof packs, verifies Ed25519 signatures against published verification keys, walks the proof hash-chain, re-projects proof into metering, and re-computes settlement reconciliation.
5
+ Author: EnfinitOS
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/EnfinitOS/sdk-auditor-py
8
+ Project-URL: Issues, https://github.com/EnfinitOS/sdk-auditor-py/issues
9
+ Project-URL: Source, https://github.com/EnfinitOS/sdk-auditor-py
10
+ Keywords: audit,verification,proof,ed25519,hash-chain,compliance,enfinitos
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Topic :: Security :: Cryptography
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Classifier: Typing :: Typed
20
+ Requires-Python: >=3.10
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: cryptography>=44
24
+ Requires-Dist: httpx>=0.28
25
+ Provides-Extra: dev
26
+ Requires-Dist: pytest>=8.0; extra == "dev"
27
+ Dynamic: license-file
28
+
29
+ # enfinitos-sdk-auditor (Python)
30
+
31
+ EnfinitOS **Auditor / Verifier SDK** for Python — the cryptographic
32
+ verification library that regulators, auditors, courts, and third-party
33
+ compliance tools use to verify signed proof packs issued by EnfinitOS,
34
+ **without having to trust EnfinitOS as a vendor**.
35
+
36
+ Python port of the reference [`@enfinitos/sdk-auditor`](../auditor-ts/)
37
+ TypeScript implementation. The wire shapes, canonicalisation rules,
38
+ and verification semantics are deliberately identical: a regulator
39
+ auditing the same proof pack with either SDK MUST get the same
40
+ VALID/INVALID verdict on every step.
41
+
42
+ ## The trust model
43
+
44
+ EnfinitOS issues signed evidence as part of every spatial-chain run:
45
+ a proof receipt for every render, a metering summary projecting
46
+ those proofs into billable units, and a settlement summary
47
+ reconciling those units into invoiced amounts.
48
+
49
+ The trust model is **"don't trust us — verify"**:
50
+
51
+ 1. **We sign every record with our private key.** The corresponding
52
+ public key is published at `/v1/runtime-keys`, a deliberately
53
+ public, unauthenticated endpoint. The same endpoint is also
54
+ archived in a regulator-pinnable JSON snapshot, so an auditor can
55
+ verify a months-old proof pack using exactly the key set we
56
+ published at the time it was issued.
57
+
58
+ 2. **Every proof receipt is chained.** Each record carries a
59
+ `before_hash` (the previous record's `after_hash`) and an
60
+ `after_hash` (sha256 of its own canonical payload). The chain
61
+ makes single-record tampering detectable by any party walking the
62
+ chain in order.
63
+
64
+ 3. **Metering is a pure projection of proof.** No platform-side
65
+ alchemy: every meter record is `dwell_ms / 1000` (or one of a few
66
+ other deterministic policies). The auditor SDK ships the same
67
+ projection formulae and re-derives them, asserting equality.
68
+
69
+ 4. **Settlement is a pure projection of metering.** Same logic — a
70
+ share table, a gross price per unit, banker's rounding. The
71
+ auditor SDK ships the same formulae and reconciles.
72
+
73
+ 5. **The auditor has the full canonical-JSON encoder.** Whatever we
74
+ signed, the auditor recomputes from the wire payload, byte-exact.
75
+ A 1-bit divergence between our encoder and theirs would make every
76
+ verification fail — that's a feature: it surfaces immediately.
77
+
78
+ What this means in practice: an auditor running this SDK on a proof
79
+ pack we issued does **not** need access to our infrastructure (beyond
80
+ the public key directory), does **not** need credentials, and does
81
+ **not** need to take our word for anything. They get back a structured
82
+ `AuditReport` that says VALID, INVALID, or SKIPPED per step, with
83
+ stable reason codes.
84
+
85
+ ## Installation
86
+
87
+ ```bash
88
+ pip install enfinitos-sdk-auditor
89
+ ```
90
+
91
+ In an air-gapped environment, install from a wheel:
92
+
93
+ ```bash
94
+ pip install ./enfinitos_sdk_auditor-0.0.1-py3-none-any.whl --no-deps
95
+ pip install cryptography httpx # transitive deps
96
+ ```
97
+
98
+ Runtime dependencies (kept minimal):
99
+ - [`cryptography`](https://cryptography.io/) — Ed25519 verify primitive
100
+ - [`httpx`](https://www.python-httpx.org/) — only used when
101
+ `verification_key_source="platform"`; offline audits don't need it
102
+
103
+ ## Five-minute getting started
104
+
105
+ ```python
106
+ import json
107
+ from enfinitos_auditor import EnfinitOSAuditor
108
+
109
+ with open("./pack.json") as f:
110
+ pack = json.load(f)
111
+
112
+ auditor = EnfinitOSAuditor(
113
+ # "platform" fetches from https://api.enfinitos.com/v1/runtime-keys.
114
+ # "local" reads from local_keys (offline audit).
115
+ verification_key_source="platform",
116
+ )
117
+
118
+ report = auditor.verify_proof_pack(pack)
119
+ print(report.status) # "VALID" | "INVALID" | "SKIPPED"
120
+
121
+ if report.status != "VALID":
122
+ for step in report.steps:
123
+ if step.status == "INVALID":
124
+ print(f"[{step.reason}] {step.target}: {step.message}")
125
+ ```
126
+
127
+ ## Architecture
128
+
129
+ ```
130
+ ┌─────────────────────────────────────────┐
131
+ │ SignedProofPack JSON │
132
+ │ (envelope.v1, signed by EnfinitOS) │
133
+ └────────────────────┬────────────────────┘
134
+
135
+
136
+ ┌─────────────────────────────────────────┐
137
+ │ parse_signed_proof_pack (proof_pack) │
138
+ └────────────────────┬────────────────────┘
139
+
140
+ ┌────────────────────┴────────────────────┐
141
+ │ │
142
+ ▼ ▼
143
+ ┌────────────────────────────┐ ┌─────────────────────────┐
144
+ │ verify_proof_record × N │ │ verify_proof_chain │
145
+ │ (proof_pack) │ │ (proof_chain) │
146
+ │ │ │ │
147
+ │ • canonicalise payload │ │ • before_hash links │
148
+ │ • check after_hash │ │ • genesis null check │
149
+ │ • lookup key_id in dir │ │ • issued_at ordering │
150
+ │ • Ed25519 verify │ └─────────────────────────┘
151
+ └────────────────────────────┘
152
+
153
+
154
+ ┌────────────────────────────┐
155
+ │ verify_metering_projection│
156
+ │ (metering_audit) │
157
+ │ │
158
+ │ • idem_key reconstruct │
159
+ │ • unit_count re-project │
160
+ │ • totals reconcile │
161
+ └─────────────┬──────────────┘
162
+
163
+
164
+ ┌────────────────────────────┐
165
+ │ verify_settlement_reconcil.│
166
+ │ (settlement_audit) │
167
+ │ │
168
+ │ • idem_key reconstruct │
169
+ │ • share-sum == 1 │
170
+ │ • amount_cents recompute │
171
+ │ • totals reconcile │
172
+ └─────────────┬──────────────┘
173
+
174
+
175
+ ┌────────────────────────────┐
176
+ │ FullAuditReport │
177
+ │ │
178
+ │ status: VALID / INVALID │
179
+ │ + steps[] per primitive │
180
+ │ + reason codes (stable) │
181
+ └────────────────────────────┘
182
+ ```
183
+
184
+ ## Sample workflows
185
+
186
+ ### "I'm a regulator inspecting a campaign's evidence"
187
+
188
+ ```python
189
+ import json
190
+ from enfinitos_auditor import EnfinitOSAuditor, AuditBundle, VerificationKey
191
+
192
+ with open("./pinned-keys-2026-q1.json") as f:
193
+ keys_raw = json.load(f)
194
+ local_keys = [VerificationKey(**k) for k in keys_raw]
195
+
196
+ with open("./regulator-export.json") as f:
197
+ bundle_raw = json.load(f)
198
+
199
+ auditor = EnfinitOSAuditor(
200
+ verification_key_source="local",
201
+ local_keys=local_keys,
202
+ )
203
+
204
+ # AuditBundle from a parsed pack is built using the SDK's parser.
205
+ from enfinitos_auditor.proof_pack import parse_signed_proof_pack
206
+
207
+ pack = parse_signed_proof_pack(bundle_raw)
208
+ report = auditor.verify_all(AuditBundle(
209
+ pack=pack,
210
+ metering=pack.metering,
211
+ settlement=pack.settlement,
212
+ ))
213
+
214
+ print(f"Pack {report.pack_id} verdict: {report.status}")
215
+ for sub_name, sub in [
216
+ ("pack", report.pack),
217
+ ("chain", report.chain),
218
+ ("metering", report.metering),
219
+ ("settlement", report.settlement),
220
+ ]:
221
+ print(f" {sub_name}: {sub.status}")
222
+ for step in sub.steps:
223
+ if step.status == "INVALID":
224
+ print(f" [{step.reason}] {step.target}: {step.message}")
225
+ ```
226
+
227
+ ### "I'm a customer dispute team verifying delivery"
228
+
229
+ ```python
230
+ # We only have the proof pack (no metering/settlement); we just need
231
+ # to know the records weren't fabricated.
232
+ report = auditor.verify_proof_pack(pack)
233
+ # Walks each record's signature + canonicalisation + chain link.
234
+ # Reports VALID iff the platform's claims internally cohere.
235
+ ```
236
+
237
+ ### "I'm an external auditor confirming the operator's claims"
238
+
239
+ ```python
240
+ # Full pipeline including settlement reconciliation.
241
+ full = auditor.verify_all(AuditBundle(pack=pack,
242
+ metering=metering,
243
+ settlement=settlement))
244
+ # full.status == "VALID" means every line of every settlement row
245
+ # re-derives from a re-projected meter row that re-derives from a
246
+ # re-canonicalised proof receipt whose Ed25519 signature verifies
247
+ # against a public key we pulled from the platform's published
248
+ # directory.
249
+ ```
250
+
251
+ ## API reference
252
+
253
+ ### `EnfinitOSAuditor`
254
+
255
+ ```python
256
+ EnfinitOSAuditor(
257
+ verification_key_source: Literal["platform", "local"] = "platform",
258
+ platform_keys_url: str = "https://api.enfinitos.com/v1/runtime-keys",
259
+ local_keys: list[VerificationKey] | None = None,
260
+ http_fetch: Callable | None = None,
261
+ signature_verifier: SignatureVerifier | None = None,
262
+ )
263
+ ```
264
+
265
+ Methods:
266
+
267
+ - `verify_proof_pack(pack) → AuditReport`
268
+ - `verify_proof_chain(records) → ChainAuditReport`
269
+ - `verify_metering_projection(proof, metering) → ProjectionAuditReport`
270
+ - `verify_settlement_reconciliation(metering, settlement) → SettlementAuditReport`
271
+ - `verify_all(bundle) → FullAuditReport`
272
+ - `fetch_keys() → list[VerificationKey]`
273
+
274
+ ### `parse_signed_proof_pack(raw) → SignedProofPack`
275
+
276
+ Pure parsing + structural validation. Raises
277
+ `AuditorError(code="INVALID_INPUT")` on malformed input.
278
+
279
+ ### `verify_proof_chain(records) → ChainAuditReport`
280
+
281
+ Walks records in order, asserts genesis-null, link continuity, and
282
+ issued_at ordering.
283
+
284
+ ### `verify_metering_projection(proof_records, metering, pack_org_id=None) → ProjectionAuditReport`
285
+
286
+ Re-projects proof receipts into meter records using the same
287
+ deterministic formula the platform uses.
288
+
289
+ ### `verify_settlement_reconciliation(metering, settlement) → SettlementAuditReport`
290
+
291
+ Re-derives settlement lines from metering using the share table.
292
+
293
+ ### `load_key_directory(...) → KeyDirectory`
294
+
295
+ Fetches verification keys from `/v1/runtime-keys` or accepts a local
296
+ set. Validates key shape; caches in-process.
297
+
298
+ ### Canonical JSON helpers
299
+
300
+ - `canonicalise_proof_payload(payload) → str`
301
+ - `canonicalise_proof_signing_input(payload, key_id) → str`
302
+ - `canonical_sort_keys(value) → str`
303
+ - `sha256_prefixed(canonical) → str`
304
+ - `base64url_encode(bytes) → str`
305
+ - `base64url_decode(s) → bytes`
306
+
307
+ ## Error model
308
+
309
+ Two failure classes (identical to TS):
310
+
311
+ 1. **Audit failures** — pack contents fail verification. Returned
312
+ inside `AuditReport.steps[]` with a stable `reason` code. Never
313
+ raised.
314
+
315
+ 2. **Operational errors** — the SDK can't run. Raised as `AuditorError`
316
+ with a stable `code` (`INVALID_INPUT`, `KEYS_UNAVAILABLE`,
317
+ `KEYS_MALFORMED`, `PLATFORM_RESPONSE`, `INTERNAL`).
318
+
319
+ See the [TypeScript README](../auditor-ts/README.md#error-model) for
320
+ the full stable reason-code table — every code is identical between
321
+ the two SDKs.
322
+
323
+ ## Offline / pinned-key audit
324
+
325
+ A regulator auditing a proof pack issued months ago wants to use
326
+ **the same key set that was published at the time of issuance** — not
327
+ the current set (which may have been rotated). The audit run is
328
+ **reproducible**: months later, anyone with the same pack + the same
329
+ pinned key set will get exactly the same `FullAuditReport`.
330
+
331
+ ```python
332
+ from enfinitos_auditor import EnfinitOSAuditor, VerificationKey
333
+
334
+ local_keys = [
335
+ VerificationKey(
336
+ key_id="key_2026q1",
337
+ algorithm="ed25519",
338
+ public_key="6KQR9xKHdM1JCJ2GpWnHvWNd0vZkjjvbR9eEKwBPgJ4", # base64url
339
+ not_before="2026-01-01T00:00:00.000Z",
340
+ not_after="2026-04-01T00:00:00.000Z",
341
+ revoked_at=None,
342
+ ),
343
+ ]
344
+ auditor = EnfinitOSAuditor(
345
+ verification_key_source="local",
346
+ local_keys=local_keys,
347
+ )
348
+ report = auditor.verify_all(bundle)
349
+ ```
350
+
351
+ ## Verification
352
+
353
+ ```bash
354
+ cd packages/sdks/auditor-py
355
+ python -m pytest # runs the test suite (requires pytest)
356
+ ```
357
+
358
+ Cross-references to the platform-side counterpart:
359
+ - canonical.ts: `apps/api/src/services/spatialChain/canonicalise.ts`
360
+ - proof signing: `apps/api/src/services/spatialChain/proofService.ts`
361
+ - metering projection: `apps/api/src/services/spatialChain/meterService.ts`
362
+ - settlement projection: `apps/api/src/services/spatialChain/settlementService.ts`
363
+ - right/basis/offer hashes: `apps/api/src/modules/rights/service.ts`