Skip to main content

Class: ChronicleServerClient

Represents a client for interacting with the Chronicle server.

Example

import { ChronicleServerClient, getFreebeeAuth } from "@chroniclewtf/client";

async function main() {
const client = new ChronicleServerClient({url: "http://localhost:8080", authorization: getFreebeeAuth()});
const leaf = client.hash("gm chronicle");
const leafIdx = await client.postLeaf(leaf);
console.log(`Posted leaf ${leaf} at index ${leafIdx}`);
const { hash, treeSize } = await client.waitForCheckpointedLeaf(leafIdx);
console.log(`Leaf ${leaf} was included in checkpoint ${hash} at tree size ${treeSize}`);
const { left, right } = await client.getProofForIndexAndTreeSize(
leafIdx,
treeSize
);
console.log(`Got proof for leaf ${leaf} at index ${leafIdx} and tree size ${treeSize}`);
// Verify proof. Note that this checks the proof against the Chronicle server, but it's
// typically recommended to use the Chronicle contract on-chain for verification.
const valid = await client.verifyProof({
leaf,
leafIdx,
left,
right,
targetRoot: hash,
});
console.log(`Proof is valid: ${valid}`);
}

main();

Constructors​

constructor​

β€’ new ChronicleServerClient(Β«destructuredΒ»?)

Constructs an instance of ChronicleServerClient.

Parameters​

NameType
Β«destructuredΒ»ServerClientParams

Defined in​

packages/client/src/ChronicleServerClient.ts:104

Methods​

getCheckpointCoveringLeaf​

β–Έ getCheckpointCoveringLeaf(leafIdx): Promise<undefined | Checkpoint>

Gets the earliest checkpoint that covers the specified leaf index.

See

Checkpoint

Parameters​

NameTypeDescription
leafIdxbigintThe index of the leaf to search for.

Returns​

Promise<undefined | Checkpoint>

  • The earliest checkpoint that covers the specified leaf.

Defined in​

packages/client/src/ChronicleServerClient.ts:218


getCurrentTreeState​

β–Έ getCurrentTreeState(): Promise<{ checkpointedSpanEnd: bigint ; id: bigint ; innerNodeCount: bigint ; leafCount: bigint ; merklizedRoot: `0x${string}` ; merklizedSpanEnd: bigint }>

Gets the current tree state.

See

TreeState

Returns​

Promise<{ checkpointedSpanEnd: bigint ; id: bigint ; innerNodeCount: bigint ; leafCount: bigint ; merklizedRoot: `0x${string}` ; merklizedSpanEnd: bigint }>

  • The current tree state.

Defined in​

packages/client/src/ChronicleServerClient.ts:130


getLeafIdxForHash​

β–Έ getLeafIdxForHash(leaf): Promise<bigint>

Gets the index of a leaf with the specified hash.

Parameters​

NameTypeDescription
leaf`0x${string}`The hash of the leaf to search for.

Returns​

Promise<bigint>

  • The index of the leaf with the specified hash.

Defined in​

packages/client/src/ChronicleServerClient.ts:207


getProofForIndexAndTreeSize​

β–Έ getProofForIndexAndTreeSize(index, treeSize): Promise<{ left: `0x${string}`[] ; right: `0x${string}`[] ; targetRoot: `0x${string}` }>

Gets the Merkle proof for a leaf at the specified index and tree size.

See

Proof

Parameters​

NameTypeDescription
indexbigintThe index of the leaf to get the proof for.
treeSizebigintThe size of the Merkle tree.

Returns​

Promise<{ left: `0x${string}`[] ; right: `0x${string}`[] ; targetRoot: `0x${string}` }>

  • The Merkle proof for the specified leaf.

Defined in​

packages/client/src/ChronicleServerClient.ts:234


hash​

β–Έ hash(str): `0x${string}`

Hashes a string using the keccak256 algorithm.

Parameters​

NameTypeDescription
strstringThe string to hash.

Returns​

`0x${string}`

  • The resulting hash.

Defined in​

packages/client/src/ChronicleServerClient.ts:281


healthCheck​

β–Έ healthCheck(): Promise<string>

Performs a health check.

Returns​

Promise<string>

  • The health check response.

Defined in​

packages/client/src/ChronicleServerClient.ts:120


postLeaf​

β–Έ postLeaf(leaf): Promise<bigint>

Posts a leaf to the server.

Parameters​

NameTypeDescription
leaf`0x${string}`The leaf to post.

Returns​

Promise<bigint>

  • The index of the posted leaf.

Defined in​

packages/client/src/ChronicleServerClient.ts:197


postLeafAndGetProof​

β–Έ postLeafAndGetProof(leaf): Promise<{ leafIdx: bigint ; left: `0x${string}`[] ; right: `0x${string}`[] ; targetRoot: `0x${string}` }>

Posts a leaf and waits until it can return with a checkpointed proof.

Remarks

This is a convenience method that combines postLeaf and waitForCheckpointedLeaf. Because it waits for a checkpointed leaf, it may take up to a few minutes to return.

Parameters​

NameTypeDescription
leaf`0x${string}`The leaf to post.

Returns​

Promise<{ leafIdx: bigint ; left: `0x${string}`[] ; right: `0x${string}`[] ; targetRoot: `0x${string}` }>

  • The proof for the specified leaf.

Defined in​

packages/client/src/ChronicleServerClient.ts:171


verifyProof​

β–Έ verifyProof(proof): Promise<boolean>

Verifies a Merkle proof for a given leaf.

Remarks

This method calls out to the Chronicle server, to verify the proof provided corresponds to an actual checkpoint. It's recommended to cross-check the proof and checkpoint against the corresponding Chronicle contract on-chain.

See

Proof

Parameters​

NameTypeDescription
proofProof & { leaf: `0x${string}` ; leafIdx: bigint }The Merkle proof to verify.

Returns​

Promise<boolean>

  • A boolean indicating whether the proof is valid.

Defined in​

packages/client/src/ChronicleServerClient.ts:256


waitForCheckpointedLeaf​

β–Έ waitForCheckpointedLeaf(leafIdx): Promise<{ blockNumber: bigint ; hash: `0x${string}` ; timestamp: Date ; treeSize: bigint }>

Waits for a checkpointed covering the given leaf index.

See

Checkpoint

Parameters​

NameTypeDescription
leafIdxbigintThe index of the leaf to wait for.

Returns​

Promise<{ blockNumber: bigint ; hash: `0x${string}` ; timestamp: Date ; treeSize: bigint }>

  • The earliest checkpoint that covers the specified leaf.

Defined in​

packages/client/src/ChronicleServerClient.ts:148