TypeScript Reference
The JavaScript and TypeScript implementation is @alx-protocol/sdk, published on npm. The package exposes deterministic Block creation and validation alongside canonicalization, Graph verification, attribution tracing, signing, replay checks, and Merkle helpers, with TypeScript types included.
Implementation
| Resource | Location |
|---|---|
| Package | @alx-protocol/sdk |
| Registry | npm |
| Install | npm install @alx-protocol/sdk |
| Requirements | Node.js 20, 21, or 22; ESM. Every entry point except /middleware is free of Node.js built-in imports. |
Exports
The root export covers canonicalization, Block creation and validation, parent normalization, Graph verification, attribution tracing, signed Blocks, replay windows, hash utilities, and error codes. Task-specific subpaths (/block, /lineage, /trace, /canonicalization, /signatures, /replay, /hash-utils) re-export subsets of the root for narrower imports. The Ed25519, P-256, and EIP-712 signers and verifiers are exported from the root and from /ed25519, /p256, and /eip712. Merkle helpers and the middleware are exported only from /merkle and /middleware.
- Canonicalization and Identity Constants
- Block Exports
- Graph and Trace Exports
- Signed Blocks and Signing Suites
- Replay, Errors, and Byte Helpers
- Merkle Subpath
- Middleware Subpath
Serialize protocol JSON the one canonical way, and read the domains and version the hashes bind.
- Serialization:
canonicalizeProtocolJson,parseStrictJson,parseStrictJsonBytes - Constants:
CANONICALIZATION_ALGORITHM,ALX_PROTOCOL_VERSION,BLOCK_HASH_DOMAIN,CONTENT_HASH_DOMAIN,ATTESTATION_HASH_DOMAIN,MAX_PARENTS,HASH_PATTERN.
Create Blocks, derive both identities, normalize parent hashes, and validate a stored Block.
- Creation and identity:
createBlock,lock,deriveBlockHash,deriveContentHash - Validation:
validateBlock - Normalization:
normalizeHash,normalizeHashes,normalizeHashLenient,sanitizeImportedParentHashes,compareHashesConstantTime.
Build and validate lineage graphs, then trace attribution through declared parents.
The adjacency form validateGraph(graph, context) is deprecated; use validateGraph(seed, source, context) for canonical Graph verification. The /lineage and /trace subpaths export the same helpers for narrower imports.
- Graphs:
buildGraph,addBlockToGraph,validateGraph,validateGraphStructure,validateIncrementalBlock,validateLineage,verifyGraph - Traces:
traceAttribution,verifyTrace.
Attach attestations to Blocks and verify them with Ed25519, P-256, or EIP-712.
An attestation stays outside Block identity: signing a Block leaves the Block's blockHash unchanged. Optional peer dependencies: Ed25519 uses @noble/ed25519 or @noble/curves; P-256 uses @noble/curves; Eip712Verifier uses @noble/secp256k1, @noble/curves, or ethers; Eip712Signer takes an ethers-compatible wallet; verifySignedProtocolPayload uses ethers unless a typed-data verify function is passed.
- Signed Blocks:
createSignedBlock,validateSignedBlock,addAttestation,deriveAttestationDigest,BLOCK_ATTESTATION_TYPES - Signers and verifiers:
Ed25519Signer,Ed25519Verifier,P256Signer,P256Verifier,Eip712Signer,Eip712Verifier - Typed payloads:
buildProtocolSigningDomain,normalizeSignedProtocolPayload,verifySignedProtocolPayload,PROTOCOL_SIGNED_REQUEST_TYPES,PROTOCOL_SIGNING_DOMAIN_NAME,PROTOCOL_SIGNING_DOMAIN_VERSION - Deprecated, scheduled for removal in v2.0:
signBlock(usecreateSignedBlockwithEip712Signer) andverifyBlockSignature(usevalidateSignedBlockwithEip712Verifier) - Deprecated, scheduled for removal in v2.0: the singular
SignedBlock.attestationfield. New envelopes useattestations.
Bound repeated requests, read typed error codes, and move between hex and bytes.
- Replay:
buildReplayScopeKey,validateReplayWindow - Errors:
ErrorCode,protocolError(every code is prefixedALX_) - Bytes:
bytesToHex,hexToBytes,concatBytes.
Build sorted-pair Merkle trees and verify inclusion proofs for commitment workflows.
@alx-protocol/sdk/merkle exports buildMerkleTree, getMerkleProof, and verifyMerkleProof. Merkle root hashes belong to an optional extension for commitments and do not change Block identity.
Lock each prompt and response of an AI client into linked Blocks.
@alx-protocol/sdk/middleware exports alxMiddleware and lockBind. alxMiddleware wraps a client exposing messages.create and messages.stream and locks each prompt and response into linked Blocks. lockBind creates the prompt Block and the response Block for one exchange without wrapping a client. The response Block declares the prompt Block as its parent. With an outDir option, alxMiddleware writes prompt-<id>.block.json and response-<id>.block.json for each exchange; without one, the Blocks stay in memory. /middleware imports node:fs/promises and runs in Node.js only.
When to Use TypeScript
Choose this package for Node.js, serverless, and JavaScript-based agent workflows, and for browsers through an ESM bundler (every entry point except /middleware). Run the shared conformance material after changing canonicalization, hashing, lineage, signing, or Merkle behavior in an integration that wraps the package.