The PythonDocumentation Index
Fetch the complete documentation index at: https://docs.xandrlabs.ai/llms.txt
Use this file to discover all available pages before exploring further.
alx-protocol package implements kb_hash_from_envelope with identical semantics to the TypeScript reference implementation in @alx/protocol. Its primary purpose is conformance verification: given the same envelope input, it must produce the same kbHash output. The implementation is validated against the canonical test vectors in test-vectors/canonical/, the same vectors used by the TypeScript specification tests.
The Python implementation is for conformance verification. For building production integrations with ALX Protocol, use
@alx/protocol (TypeScript), which is the primary SDK.Requirements
- Python 3.11 or later
eth-hash[pycryptodome](installed automatically as a dependency)
Installation
Running the conformance suite
The conformance suite replays every vector undertest-vectors/canonical/ and asserts that kb_hash_from_envelope produces the expected kbHash for each one.
API
The public API is exported from the top-levelalx_protocol module:
| Export | Description |
|---|---|
kb_hash_from_envelope(envelope) | Derives kbHash: keccak256("KB_V1" + JCS(normalizedEnvelope)). Mirrors kbHashFromEnvelope in TypeScript. |
canonicalize(value) | RFC 8785-style (JCS) deterministic JSON serializer. Raises ValueError on None or non-finite floats. |
normalize_for_hash(envelope) | Extracts the hash-scoped fields and sorts sources. |
KB_DOMAIN_PREFIX | The string "KB_V1" prepended before hashing. |
Example: computing kbHash in Python
The following produces the samekbHash as the equivalent TypeScript call to kbHashFromEnvelope:
practice-minimal canonical test vector covers exactly this envelope.
How the hash is computed
The Python implementation follows the same three-step pipeline as the TypeScript reference:Normalize the envelope
Extract only the hash-scoped fields:
type, domain, sources, artifactHash, tier, payload, and derivation. Sort sources deterministically when there is more than one entry. Fall back to parents if sources is absent.Canonicalize
Serialize the normalized envelope using JCS (RFC 8785): object keys sorted lexicographically, no whitespace,
null values rejected.Canonical test vectors
The vectors intest-vectors/canonical/ each contain two files:
envelope.json— the input envelopeexpected.json— the expectedkbHash(and related fields)
tests/test_vectors.py loads each pair and asserts equality. Covered cases include all KB types, edge cases (unicode content, large payloads, max sources), and derivation scenarios (single parent, multi-parent, unsorted parent lists).