Skip to main content
Version: 6.11.0

Class: MerkleTree

merkle.MerkleTree

Constructors​

constructor​

• new MerkleTree(leafHashes, hashMethod?): MerkleTree

Create a Merkle tree

Parameters​

NameTypeDefault valueDescription
leafHashesstring[]undefinedhex-string array
hashMethod(a: BigNumberish, b: BigNumberish) => stringcomputePedersenHashhash method to use, default: Pedersen

Returns​

MerkleTree

created Merkle tree

Example

const leaves = ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7'];
const tree = new MerkleTree(leaves);
// tree = {
// branches: [['0x5bb9440e2...', '0x262697b88...', ...], ['0x38118a340...', ...], ...],
// leaves: ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7'],
// root: '0x7f748c75e5bdb7ae28013f076b8ab650c4e01d3530c6e5ab665f9f1accbe7d4',
// hashMethod: [Function computePedersenHash],
// }

Defined in​

src/utils/merkle.ts:31

Properties​

leaves​

• leaves: string[]

Defined in​

src/utils/merkle.ts:5


branches​

• branches: string[][] = []

Defined in​

src/utils/merkle.ts:7


root​

• root: string

Defined in​

src/utils/merkle.ts:9


hashMethod​

• hashMethod: (a: BigNumberish, b: BigNumberish) => string

Type declaration​

â–¸ (a, b): string

Parameters​
NameType
aBigNumberish
bBigNumberish
Returns​

string

Defined in​

src/utils/merkle.ts:11

Methods​

hash​

â–¸ hash(a, b, hashMethod?): string

Calculate hash from ordered a and b, Pedersen hash default

Parameters​

NameTypeDefault valueDescription
aBigNumberishundefinedfirst value
bBigNumberishundefinedsecond value
hashMethod(a: BigNumberish, b: BigNumberish) => stringcomputePedersenHashhash method to use, default: Pedersen

Returns​

string

result of the hash function

Example

const result1 = MerkleTree.hash('0xabc', '0xdef');
// result1 = '0x484f029da7914ada038b1adf67fc83632364a3ebc2cd9349b41ab61626d9e82'

const customHashMethod = (a, b) => `custom_${a}_${b}`;
const result2 = MerkleTree.hash('0xabc', '0xdef', customHashMethod);
// result2 = 'custom_2748_3567'

Defined in​

src/utils/merkle.ts:76


getProof​

â–¸ getProof(leaf, branch?, hashPath?): string[]

Calculates the merkle membership proof path

Parameters​

NameTypeDefault valueDescription
leafstringundefinedhex-string
branchstring[]undefinedhex-string array
hashPathstring[][]hex-string array

Returns​

string[]

collection of merkle proof hex-string hashes

Example

const leaves = ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7'];
const tree = new MerkleTree(leaves);
const result = tree.getProof('0x3');
// result = [
// '0x4',
// '0x5bb9440e27889a364bcb678b1f679ecd1347acdedcbf36e83494f857cc58026',
// '0x8c0e46dd2df9aaf3a8ebfbc25408a582ad7fa7171f0698ddbbc5130b4b4e60',
// ]

Defined in​

src/utils/merkle.ts:104