Usage with Proofs

"Talk is cheap, show me the code."

The proof verification is done within the smart-contract side, so as a developer we will mostly be looking at the proof generation that happens on the client side. HollowDB Prover makes proving stuff dead-simple, just call a prove function and that is all.

import {SDK} from 'hollowdb';
import {Prover, computeKey} from 'hollowdb-prover';
import {WarpFactory, JWKInterface} from 'warp-contracts';
import fs from 'fs';

// read wallet
const walletPath = __dirname + '/wallet-name.json';
const wallet = JSON.parse(
  fs.readFileSync(walletPath).toString()
) as JWKInterface;

// instantiate SDK
const contractTxId = '<your-contract-txid>';
const sdk = new SDK(wallet, contractTxId, WarpFactory.forMainnet());

// instantiate the prover
const wasmCircuitPath = __dirname + '/circuit.wasm';
const proverKeyPath = __dirname + '/prover_key.zkey';
const prover = new Prover(wasmCircuitPath, proverKeyPath);

// compute your key from secret
const secret = BigInt("0xDEADBEEF");
const key = computeKey(secret);

// generate a proof for UPDATE
const currentValue = await sdk.get(key);
const nextValue = 'this is a new value!';
const {proof} = await prover.prove(secret, currentValue, nextValue);

// update
await sdk.update(key, nextValue, proof);

Let’s digest this code step by step:

  1. First, we read our Arweave wallet from file, to be used for our transactions. You could also provide the wallet as a JSON object within the code too (but you should be careful not to expose your wallet & accidentally commit them to your repo).

  2. Then, we create the HollowDB SDK object. For this, we provide our wallet, we specify the cache type to be LMDB, and we provide the contract transaction id along with a Warp instance. Basically, we are β€œconnecting” to our contract on the Mainnet.

  3. We now create our Prover object, which is a wrapper around a few SnarkJS functions to generate a proof. We have to provide a path to our WASM circuit and a prover key to create this object. You can obtain them from our repository, and host them on your side. For example, if you are writing a web application, you could host them under the public folder.

  4. We need to compute the key, which is the hash of our preimage. You could generate a dummy proof and read the key from it’s output, but that is not really efficient. Instead, HollowDB exports a computeKey function for this purpose.

  5. Then, we generate our proof by simply calling prove with the required arguments, that are the inputs we have shown in the above diagram.

  6. Finally, we call sdk.update to update the value at our key, using our zero-knowledge proof!

Last updated