Get the code
npx scaffold-hbar@latest --template hedera-dev/hedera-finance-templates#templates/hcs-audit-trailOverview
An audit log kept in your own database can be edited, reordered or truncated by whoever runs the database. Regulators receive an export after the fact and have to trust it. The Hedera Consensus Service (HCS) removes that trust. A topic is an append-only log run by the network:- Every message gets a sequence number, a consensus timestamp and a running hash from the network. A missing message shows up as a gap; an edited one cannot exist.
- A topic created with a submit key accepts messages only from the holders of that key. Anyone else is rejected by the network.
- Anyone can read the topic through the public mirror node, with no account and no access to your systems.
What the topic holds that the ledger does not
A fair question to ask first: if a freeze is already aTokenFreeze transaction on a public ledger, what is the topic for? Logging the transaction id and its result again would make the topic a copy of the ledger with a hash chain on top, and the ledger would still be the source of truth.
So the entries carry what the ledger has no field for:
- The reason. The network records that a holder was frozen. It has no idea that it happened under case
CMP-2026-114, on a court order received that morning. An auditor asking why has nothing on chain to read. - The authority. Which officer or committee authorised it. The ledger records the key that signed, which in most operating models is a service account, not a person.
- The evidence. The SHA-256 of the document behind the action — the order, the instruction, the signed minute. The hash commits to that exact file at that exact position in the chain, so a document produced afterwards cannot match an entry already on the topic.
- The decisions that never reach the ledger. A KYC review, a sanctions screening, a board approval. Nothing executes, so there is no transaction, no receipt, nothing for a mirror node to show. These entries exist nowhere else, and they are the ones a compliance examination usually asks about first.
- Issuer: the operator account the app runs with. Creates the audit topic and a security token, performs token actions, records off-chain decisions, and publishes an audit entry for each one
- Compliance officer (optional): a HashPack account the Issuer authorises as a publisher. Signs and submits the audit entries instead of the Issuer
- Regulator: reads the trail from the mirror node. Holds no keys and needs no account
- The Issuer creates the audit topic with a submit key, and a security token with KYC and freeze keys
- The Issuer performs token actions on a target account: grant KYC, transfer, freeze, unfreeze
- Each action is logged as an audit entry carrying its transaction id and result, the reason and authority behind it, the hash of any supporting document, and the hash of the previous entry
- Decisions taken off chain — a KYC review, a sanctions screening, an approval — are recorded as entries with no transaction at all
- The Regulator’s feed reads the topic from the mirror node and verifies it: no sequence gaps, an unbroken hash chain, every referenced transaction real with the logged result, and every off-chain decision carrying its authority and evidence. It also says which authorities signed their own entries and which were only named
- Anyone else who tries to publish is rejected by the network
Prerequisites
- Node.js 22.23.4 or later on the 22.x line, and Yarn
- A Hedera testnet account from the Hedera Portal. It becomes the Issuer. Its key can be a raw ECDSA key in hex, or a DER-encoded ECDSA or ED25519 key.
- Testnet HBAR on that account. Setting up an audit trail costs about 14 HBAR, almost all of it the network fee for creating the token; each action and its entry cost a fraction of an HBAR.
- Optional, for Sign entries with HashPack: the HashPack browser extension with a testnet account other than the Issuer, and a WalletConnect project id from the Reown dashboard
Project structure
Configuration
Createpackages/nextjs/.env.local:
Set up the audit topic
Press Set up audit trail. The page shows each step as it runs: a spinner on the running step, then a check with the id it created.Step 1: Create the topic with a submit key
The submit key is what makes the log trustworthy: only messages signed by it are accepted. The admin key lets the Issuer change the submit key later, which is how a compliance officer gets authorised.Step 2: Create the security token and a holder
The token has 0 decimals, an initial supply of 1,000 held by the Issuer, and a KYC key and a freeze key, so there are compliance actions worth auditing. Setup also creates a demo holder account and associates it with the token, without KYC. Creating the token is the first audit entry: anissue entry whose prev is 64 zeros.
You should see the token id, the holder account id, a link to the regulator feed, and entry #1, issue, in the trail.
Log a token action
Step 1: Perform the action
Choose a target account: the demo holder by default, or any testnet account. Fill in the reason and the authority if you have them, and attach a supporting document if there is one. Then press Grant KYC, Freeze holder, Unfreeze holder or Transfer. Each is a real HTS transaction with its own transaction id and result. The three fields are what the entry adds to an action the ledger already has. They are optional here so the buttons stay one click away while you are trying the template; a real desk would require them. For a decision, which has nothing on the ledger behind it, they are required. The fields empty themselves once the entry is on the topic, so a court order attached to a freeze is not published again with the unfreeze that follows it. A refused action is still logged with the network’s result code. A transfer before KYC is granted returnsACCOUNT_KYC_NOT_GRANTED_FOR_TOKEN; a transfer to a frozen account returns ACCOUNT_FROZEN_FOR_TOKEN; any action on an account that has not associated the token returns TOKEN_NOT_ASSOCIATED_TO_ACCOUNT. The Issuer cannot target itself.
Step 2: Queue and publish the audit entry
The entry records the action, so it cannot be written first: it carries that action’s transaction id and result, and neither exists until the action has run. Which leaves exactly one gap to close — an action that succeeded while its entry did not. So the entry is queued the moment the action returns, and publishing is a separate, serialized step. A decision has no transaction to wait for, but it goes through the same queue, so both kinds share oneprev chain and one ordering.
The entry is JSON with a fixed key order, so the bytes published are exactly the bytes the next entry hashes:
previs read when an entry is published, not when it is built. Two actions in flight therefore cannot be chained onto the sameprev— which would land two messages claiming the same position and showBROKEN.- A submission keeps one transaction id across retries. A response lost after the network accepted it would otherwise publish the entry twice; the resubmission is refused as a duplicate instead, and the receipt is queried to find out what happened.
- Nothing discards a queued entry. A failed submission, a declined signature or an unreachable mirror node are retried until the entry is on the topic.
Record a decision the ledger never sees
The Off-chain decisions card records akyc_review, a sanctions_check or an approval. Nothing here touches the network: there is no transaction to perform, so the entry is the record, and tx is null.
Pick the decision and its outcome (PASSED or REFUSED), name the subject account, write the reason, name the authority, and attach the document. The last two are required — the button stays disabled without them — because an entry with neither says only that something happened, which is the part the ledger already does better.
The document never leaves your browser
The file is hashed in the page, with the Web Crypto API, and only the digest is sent:off chain where the transaction link usually is, and the document’s hash in its own column. off chain is only ever a decision: a message the feed cannot read at all shows a dash there, so a broken entry is never dressed up as one the desk meant to record.
Read and verify the regulator feed
Open Open regulator feed, or/regulator?topic=<topic id>. The page reads the topic from the mirror node directly in the browser. It works with the app’s server stopped, because it trusts nothing but the network. For every message it checks:
- Sequence: sequence numbers run 1, 2, 3… with no gap, so nothing was deleted
-
Chain: the entry’s
prevequals the SHA-256 of the previous message’s exact bytes, so nothing was edited or reordered -
Claim: the entry’s own fields agree with the kind of event it says it is. A ledger event has to cite a transaction; a decision has to cite none, and has to carry its authority, its evidence hash and a
PASSEDorREFUSEDoutcome. This closes the two ways the kinds could be mixed up — a decision citing a real transaction to borrow its credibility, and an action with no transaction to be checked against -
Transaction: for a ledger event, the referenced transaction exists on the mirror node with the result the entry claims, and is the kind of transaction the event implies, acting on what the entry says it acted on — a
freezeentry must point at aTOKENFREEZEof that account, a successfultransferat aCRYPTOTRANSFERwhose legs credit that amount of that token to that account. Checking the result code alone would let an authorised publisher logfreeze/SUCCESSagainst any successful transaction on the network Which id gets checked is whichever one the mirror node reports, and it is not always the token: aTOKENCREATIONnames the token, but aTOKENFREEZEorTOKENGRANTKYCnames the account and carries no token on the transaction at all. A transfer names neither, which is why its legs are checked instead — the strongest of the three, since they carry token, account and amount together.
signed by this account when the entry was paid for by the account it names, and publisher's claim when it was not:
BROKEN. It is the difference between a field that looks like evidence and a field that tells you when it is.
VERIFIED with the entry count, every row passing the checks that apply to it, and a dash in the transaction column for each decision. New entries appear within a few seconds, without a reload. A topic whose messages fail a check shows BROKEN and the first entry that failed, with the reason.
What the network refuses
Press Try to forge an entry. An account that is not an authorised publisher submits a message to the topic, and the network rejects it withINVALID_SIGNATURE. Nothing is added to the trail, and the topic’s message count does not change.
HCS guarantees order, timestamps and immutability; it cannot stop an authorised publisher from writing an inaccurate entry. That is why every ledger entry references a transaction the regulator checks independently: an entry claiming a freeze is only
VERIFIED if a freeze of that account exists on the network with that result. What an authorised publisher can still do is write a well-formed entry at the wrong position — and that is what BROKEN is for.The fields the ledger cannot check need one more distinction. authority is usually the publisher’s claim, not a proof: the submit key establishes who wrote the entry, not that the account it names agreed to anything. The exception is an entry published by the officer it names — their wallet signed and paid for that submission, and the mirror node says so, which is why the feed marks those signed by this account. Even then it is endorsement of the record, not authorisation of the action: the officer signed the entry, not the freeze. evidence is stronger still, because the hash is self-checking: anyone holding the document can confirm it is the one the entry committed to.Sign entries as a compliance officer with HashPack
WithNEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID set, switch Signing mode to HashPack and connect a testnet account that is not the Issuer.
-
Authorise as publisher. The Issuer reads the wallet’s public key from the mirror node and changes the topic’s submit key to a key list that accepts either key, signing with the topic’s admin key:
-
Perform an action. The Issuer still performs the token action on the server and builds the entry with the correct
prev. HashPack signs and submits the topic message as the payer. -
Confirmation. The prepared entry is bound to the exact transaction id frozen into the transaction the wallet was handed, and only that id can confirm it. The server then checks, from the mirror node, that the transaction was a message submit to this topic, paid by that wallet, and succeeded — and that the bytes now on the topic are the bytes it prepared. Only then does the chain advance.
A successful transaction from that account is not enough on its own. An older or different message from the same wallet would otherwise advance the chain to the hash of bytes that are not on the topic at all, and every entry after it would fail its
prevcheck. - If the wallet does not publish it, the Issuer does. A declined signature, a submission the network refuses, a mirror node that never shows the transaction: the entry stays queued and the Issuer — always an authorised publisher — publishes it instead. The action happened, so its entry is not optional. Before taking an entry back, the server checks the topic for the prepared bytes, because a confirmation that failed on a slow mirror node must not become a second copy of an entry that did land.
@hashgraph/hedera-wallet-connect: create the provider with HederaProvider.init({ projectId, metadata }), pass it to createAppKit as universalProvider alongside a HederaAdapter, and sign by calling hedera_signAndExecuteTransaction on that provider with { signerAccountId: "hedera:testnet:<accountId>", transactionList: <base64 transaction> }. The server builds and freezes each transaction with the wallet as payer; the app never holds the wallet’s key.
What this template simplifies
- The queue lives in memory: a server restart with entries still queued loses them, though everything that reached the topic stays verifiable. A real desk persists the queue, or derives what is missing by comparing the chain against its own records.
- Private payloads: the topic is public, and so is
reason. The template keeps documents off chain by publishing only their hash; apply the same discipline to the reason text, which can easily name a person, or encrypt entries to the regulator’s key. - Provable authority: having the authority publish its own entries proves it endorsed the record. Proving it authorised the action needs its signature on the action itself — the 2-of-3 threshold key in Regulated Token, so the token refuses the freeze without it.
- A document store that outlives the trail: an evidence hash is worth nothing if the document is lost. Pair the topic with retention on the document store, and record the storage reference alongside the hash.
- Admin key: hold the topic’s admin key in a threshold key, so no single person can change who may publish.
- Lower latency: subscribe to the topic over gRPC, or run your own mirror node, instead of polling the REST API.
- Shared plumbing: log the other Finance Workflows to the same topic, such as each DvP settlement or coupon payment.
- Custody: sign with a custody provider or HSM rather than a key in an environment variable.
Next steps
Create a topic
Topic keys, memos and options.
Submit a message
Message size, chunking and receipts.
Query the mirror node
Reading topic messages the way the regulator feed does.
Scaffold HBAR
The CLI and the other templates.