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β
| Name | Type |
|---|---|
Β«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
Parametersβ
| Name | Type | Description |
|---|---|---|
leafIdx | bigint | The 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
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β
| Name | Type | Description |
|---|---|---|
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
Parametersβ
| Name | Type | Description |
|---|---|---|
index | bigint | The index of the leaf to get the proof for. |
treeSize | bigint | The 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β
| Name | Type | Description |
|---|---|---|
str | string | The 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β
| Name | Type | Description |
|---|---|---|
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β
| Name | Type | Description |
|---|---|---|
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
Parametersβ
| Name | Type | Description |
|---|---|---|
proof | Proof & { 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
Parametersβ
| Name | Type | Description |
|---|---|---|
leafIdx | bigint | The 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.