Skip to content

Latest commit

 

History

History

zkdrops-lib

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Zk Merkle Airdrop Library

Self-contained repo for interacting with the zkdrops-contracts. A browser usage example of this library can be found in zkdrops/client-ex.

Usage

Create MerkleTree from file (ex file)

import { MerkleTree } from 'zkdrops-lib';

let merkleTreeStorageString = ...; // Load the file from network or local into a string
let mt = MerkleTree.createFromStorageString(merkleTreeStorageString);

Create MerkleTree from leaves

import { MerkleTree } from 'zkdrops-lib';

let treeHeight = 2**13;
// Create an array of BigNumbers to enter as leaves
let leaves: BigNumber[] = new Array(treeHeight).fill(0).map(_ => randomBigNumber());
let mt = MerkleTree.createFromLeaves(leaves)

Export MerkleTree storage string

import { MerkleTree } from 'zkdrops-lib';

let mt: MerkleTree = ...;
let storageString = mt.getStorageString();
fs.WriteFileSync("./file/path/text.txt', storageString);

Generate proof

import { MerkleTree, poseidon2, generateProofCallData } from 'zkdrops-lib';

let KEY = "0x00d8d681994b73f635532cc3f0a7e3f02e10d55a6e107e1bd374b1c23bead940";
let SEC = "0x00b87f2a60e72cc5c6f334833c56aaed80aef550b76bd94837a86070c10dd061";
let airdropToAddress = "0x0"; // Reciever address -- caller of PrivateAirdrop.collectAirdrop()

// Optional: Compute commitment
let commitment: BigInt = poseidon2(BigInt(KEY), BigInt(SEC));

// Load tree into string
let merkleTreeStorageString: string = ...;
// Load dep files into buffers
let wasmBuffer: Buffer = ...;
let zkeyBuffer: Buffer = ...;

// Create MerkleTree
let mt = MerkleTree.createFromStorageString(merkleTreeStorageString);

// Optional: Check leaf exists
if (!mt.leafExists(commitment)) {
    alert("Commitment does not exist as leaf in tree!);
}

// Compute proof -- long running operation
let proof = await generateProofCallData(mt, BigInt(key), BigInt(secret), airdropToAddress, wasmBuffer, zkeyBuffer);

Cmds

  • Install: yarn install
  • Build: yarn build

Notes

  • witness_calculator is copied from those generated by the Circom library with a BigInt adjustment to work in the browser.
  • The circom/snarkjs dependencies rely on server only libraries (fs, stream, path, crypto, os), which will not exist in a browser environment. If webpacking, these must be ignored.