API Reference

json-record exposes a minimal API for creating hash-linked blocks and verifying their integrity. All operations use deterministic SHA-256 hashing and return plain data structures containing Uint8Array fields.

createBlock(payload, prevBlock)

Creates a new hash-linked block. The payload must be a Uint8Array. If a previous block is provided, its blockHash becomes the prevHash of the new block.

Import

import { createBlock } from "json-record";

Usage

const enc = new TextEncoder();
const payload = enc.encode("hello");

const block = await createBlock(payload);

Returns

{
  index: number,
  timestamp: number,
  payload: Uint8Array,
  payloadHash: Uint8Array,
  prevHash: Uint8Array | null,
  blockHash: Uint8Array
}

Note: modifying a block or its payload after creation will invalidate its hashes.


verifyBlock(block)

Verifies the integrity of a single block. Returns true if the payloadHash and blockHash match the block contents.

Import

import { verifyBlock } from "json-record";

Usage

const ok = await verifyBlock(block);

Returns

boolean

verifyChain(blocks)

Verifies the integrity of a sequence of blocks. Ensures each block is valid and that each prevHash matches the previous blockHash.

Import

import { verifyChain } from "json-record";

Usage

const ok = await verifyChain([block0, block1, block2]);

Returns

boolean