Skip to main content

Create and Validate a Block

Create an ALX Block from JSON content and verify the deterministic contentHash and blockHash.

Prerequisites​

  • Node.js 20, 21, or 22
  • ALX JavaScript SDK (@alx-protocol/sdk)

See Quick Start for installation instructions. Block creation and validation run locally without an API key, network connection, or blockchain.

Workflow​

  1. Create a root Block from JSON content with an empty parent set.
  2. Inspect the generated contentHash and blockHash.
  3. Validate the Block using validateBlock().
  4. Modify the Block content, parent set, or stored hash without recomputing the identities.
  5. Run validateBlock() again to identify the mismatch.

Validation Requirements​

validateBlock() requires:

  • contentHash — Matches the canonicalized content.
  • blockHash — Matches the canonicalized content and normalized parent set.
  • parentHashes — Already normalized, without the Block's own blockHash.
  • Block structure — Exactly the five protocol fields, with protocolVersion "1".

Validation runs locally using Block data. Conforming implementations produce equivalent results from equivalent protocol inputs. Block defines the complete validation rules.

Create a root Block, validate both deterministic identities, and fail the process if validation does not pass.

Create validate-block.mjs and run the script with node validate-block.mjs:

validate-block.mjs
import {
createBlock,
validateBlock,
} from '@alx-protocol/sdk';

const content = {type: 'example', value: 1};
const block = createBlock(content, []);
const result = validateBlock(block);

console.log(JSON.stringify({
contentHash: result.contentHash,
blockHash: result.blockHash,
ok: result.ok,
}, null, 2));

if (!result.ok) process.exitCode = 1;

// Steps 4 and 5: change the content without recomputing the hashes.
const tampered = {...block, content: {type: 'example', value: 2}};
console.log(validateBlock(tampered).ok);
Terminal
node validate-block.mjs

Continue with the protocol rules for Block structure, Block identity, canonicalization, and validation.