Skip to main content

Server Client

The server clients use Hedera operator credentials or an injected Hedera SDK client to create HCS-27 topics and publish checkpoints directly to the network.

Create a ClientDirect link to Create a Client

import { Client as HederaClient } from '@hashgraph/sdk';
import { HCS27Client } from '@hashgraphonline/standards-sdk';

const hederaClient = HederaClient.forTestnet();
hederaClient.setOperator(
process.env.HEDERA_ACCOUNT_ID!,
process.env.HEDERA_PRIVATE_KEY!,
);

const client = new HCS27Client({
operatorId: process.env.HEDERA_ACCOUNT_ID!,
operatorKey: process.env.HEDERA_PRIVATE_KEY!,
network: 'testnet',
client: hederaClient,
});

Create a Checkpoint TopicDirect link to Create a Checkpoint Topic

const topic = await client.createCheckpointTopic({
ttl: 3600,
adminKey: true,
submitKey: true,
});

console.log(topic.topicId, topic.transactionId);

Publish a CheckpointDirect link to Publish a Checkpoint

await client.publishCheckpoint(
topic.topicId,
{
type: 'ans-checkpoint-v1',
stream: { registry: 'ans', log_id: 'default' },
log: {
alg: 'sha-256',
leaf: 'sha256(jcs(event))',
merkle: 'rfc9162',
},
root: {
treeSize: '2',
rootHashB64u: 'mB6Q2Wv1hR2hRGYt3Ty1m9E0m7M1hB4wS-9DqR2xFZQ',
},
prev: {
treeSize: '1',
rootHashB64u: 'pD8Q2Wv1hR2hRGYt3Ty1m9E0m7M1hB4wS-9DqR2xFZA',
},
},
'checkpoint 2',
);

Overflow Metadata Uses an Inscription ReferenceDirect link to Overflow Metadata Uses an Inscription Reference

If the serialized HCS-27 payload exceeds 1024 bytes, publishCheckpoint(...) automatically stores the metadata as an inscription and publishes the smaller pointer message instead.

const result = await client.publishCheckpoint(
topic.topicId,
{
type: 'ans-checkpoint-v1',
stream: { registry: 'ans', log_id: 'overflow' },
log: {
alg: 'sha-256',
leaf: 'sha256(jcs(event))-'.repeat(90),
merkle: 'rfc9162',
},
root: {
treeSize: '1',
rootHashB64u: 'A7K9uA5RUL9Ko_BIO8l7ON3W5gQw6B6yv2nM3xABjS8',
},
},
'overflow checkpoint',
);

const checkpoints = await client.getCheckpoints(topic.topicId);
console.log(checkpoints[0]?.message.metadata); // hcs://1/<topic>

Read and Validate a Checkpoint ChainDirect link to Read and Validate a Checkpoint Chain

const checkpoints = await client.getCheckpoints(topic.topicId);
client.validateCheckpointChain(checkpoints);

for (const checkpoint of checkpoints) {
console.log(
checkpoint.sequence,
checkpoint.effectiveMetadata.root.treeSize,
checkpoint.effectiveMetadata.root.rootHashB64u,
);
}

Resolve an HCS-1 Reference DirectlyDirect link to Resolve an HCS-1 Reference Directly

const metadataBytes = await client.resolveHCS1Reference('hcs://1/0.0.123456');
console.log(metadataBytes.toString('utf8'));

Private Topics Created ElsewhereDirect link to Private Topics Created Elsewhere

If you create the topic outside the TypeScript SDK and still want to publish to a private submit-key topic, register the submit signer before calling publishCheckpoint(...):

client.registerTopicSubmitKey('0.0.123456', process.env.HCS27_SUBMIT_KEY!);