Docs
Advanced
State proofs

Using State Proofs

For some application, it is importants to be able to create a off-chain proof of some state in the protokit state-tree. For example, for privacy-enabled application you want to prove that some data is in the tree, but don't want to reveal under which key this data is stored.

You could then attach a nullifier to that proof to prevent double-use.

Prerequisites

In order to create a state-proof, you need two things:

  1. The data
  2. A witness to that data

Fetching the Data

The data is very simple, you either already have it in your UI or you can fetch it via the query API

Client SDK
const state = await appchain.query.runtime.testModule.userState.get(address);

Fetching the witness

After we have the data, we need to prove the relationship between that data and the state-root of the last protokit proof. We do this by fetch the merkle witness and recalculating the state root inside a client-side proof.

Client SDK
const witness = await appchain.query.runtime.testModule.userState.witness(address);

Creating the state proof

For the state-proof we can then use the data and the witness to recalculate the root hash, which we attach to the publicOutput of our proof. Inside the protokit runtime, we can then assert that the state-proofs root hash equals to the last state root of the protokit runtime.

const rootHash = witness.computeRoot(
    Posiedon.hash(state.toFields())
);