Socket
Socket
Sign inDemoInstall

@sismo-core/kv-merkle-tree

Package Overview
Dependencies
65
Maintainers
4
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @sismo-core/kv-merkle-tree

Implementation of Merkle tree for the Sismo protocole


Version published
Weekly downloads
28
increased by21.74%
Maintainers
4
Created
Weekly downloads
 

Readme

Source

Key Value Merkle tree

Merkle tree used in the Sismo ZK-SMPS to build account Merkle trees and world Merkle trees.

This implementation is a classic implementation of Merkle tree with features made for Sismo ZK-SMPS.

Generate your Merkle tree

From data

With the constructor you will be able to instanciate a MerkleTree from MerkleTreeData.

In the default beaviour this will create a tree where the leaves will be hash(key,value).

For example if we take the use case of an airdrop, this could allow you to store the amount of token (the value) associated to a user Ethereum account (the key) in the Merkle tree.

    const merkleTree = new KVMerkleTree({
      "0xa76f290c490c70f2d816d286efe47fd64a35800a": 1,
      "0x0085560b24769daa4ed057f1b2ae40746aa9aab6": 1,
      "0x0294350d7cf2c145446358b6461c161a927b3a87": 1,
      "0x4f9c798553a207536b79e886b54f169264a7a155": 1,
      "0xa1b04c9cbb449d13c4fca9c7e6be1f810e6f35e9": 1,
    }, poseidonHash);
ParamsDefaultTypeDescription
datanullMerkleTreeDataData used to generate your Merkle tree.
hashFunctionnullHashFunctionHash function used to generate your Merkle tree.
forceHeightnullnumberForce the number of levels in your Merkle tree.
hashLeavestruebooleanDefine if your leaf will be hashed or not.

The rest of params in the constructor are due to technicals needs, do not add them.

From leaves

    const merkleTree = KVMerkleTree.fromLeaves([
      "0xa76f290c490c70f2d816da86efe47fd64a35800b",
      "0x0085560b24769dacaed057f1b2ae40746aa9aab6",
      "0x0294350d7cf2c145446358b64a1c1610927b3a87",
      "0x4f9c798553d2075a6b79e886b54f169264a7a155",
      "0xa1b04c9cbb449d13c4fc29c7e6be1a810e6f35e9",
        ...
    ]);
ParamsDefaultTypeDescription
leavesstring[]MerkleTreeDataLeaves used to generate your Merkle tree.
hashFunctionnullHashFunctionhash function used to generate your Merkle tree.
forceHeightnullnumberForce the height of your merkle tree.
hashLeavesfalsebooleanDefine if your leaf will be hashed or not.

Usages

FunctionsDescription
getHeight(): numberReturn the height of your Merkle tree.
getRoot(): BigNumberReturn the root of your Merkle tree.
getValue(key: string): BigNumberReturn the value associated to a key. Not available in a Merkle tree create from leaves.
getMerklePathFromLeaf(leaf: string): MerklePathReturn MerklePath of a leaf.
getMerklePathFromKey(key: string): MerklePathMake the link between key and leaf. This allow you to retrieve the MerklePath without knowing the value associated to a key. Not available in a Merkle tree create from leaves.
toJson(): JsonMerkleTreeExport your Merkle tree in json format
KVMerkleTree.fromJson(jsonMerkleTree: JsonMerkleTree): KVMerkleTreeImport your Merkle tree from json format.

Types

JsonMerkleTree

type JsonMerkleTree = {
    root: string,
    height: number,
    pointers?:
      [key: string]: {
        leafValue: string,
        value: number | null
      }
    }
    tree: {
      [nodeValue: string]: { 
        p?: string,
        r?: string,
        l?: string
      }
    }
}

Description
rootMerkle root or "top hash" of the Merkle tree (see more)
heightNumber of level of the Merkle tree
pointersKey store that allow you to retrieve a leaf from a key without knowing the value. For example in an airdrop, the user will be able to retrieve his leaf from his Ethereum account without knowing the amount of token he deserve
treeMerkle tree where p is the parent of the current node, r the right child and l the left child

MerklePath

interface MerklePath {
    path: [BigNumber](https://docs.ethers.io/v5/api/utils/bignumber/)[]; 
    indices: number[]; // 0 if the has is on left, 1 if the has is on the right
}
Description
path[]List of nodes to get the Merkle root from a leaf
indices[]0 if the node is on left, 1 if the node is on the right

MerkleTreeData

type MerkleTreeData = { [key: string]: number | null };

HashFunction

type HashFunction = (inputs: any[]) => [BigNumber](https://docs.ethers.io/v5/api/utils/bignumber/);}

Keywords

FAQs

Last updated on 06 May 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc