Class: MerkleTree
merkle.MerkleTree
Constructors​
constructor​
• new MerkleTree(leafHashes
, hashMethod?
): MerkleTree
Create a Merkle tree
Parameters​
Name | Type | Default value | Description |
---|---|---|---|
leafHashes | string [] | undefined | hex-string array |
hashMethod | (a : BigNumberish , b : BigNumberish ) => string | computePedersenHash | hash method to use, default: Pedersen |
Returns​
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​
Properties​
leaves​
• leaves: string
[]
Defined in​
branches​
• branches: string
[][] = []
Defined in​
root​
• root: string
Defined in​
hashMethod​
• hashMethod: (a
: BigNumberish
, b
: BigNumberish
) => string
Type declaration​
â–¸ (a
, b
): string
Parameters​
Name | Type |
---|---|
a | BigNumberish |
b | BigNumberish |
Returns​
string
Defined in​
Methods​
hash​
â–¸ hash(a
, b
, hashMethod?
): string
Calculate hash from ordered a and b, Pedersen hash default
Parameters​
Name | Type | Default value | Description |
---|---|---|---|
a | BigNumberish | undefined | first value |
b | BigNumberish | undefined | second value |
hashMethod | (a : BigNumberish , b : BigNumberish ) => string | computePedersenHash | hash 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​
getProof​
â–¸ getProof(leaf
, branch?
, hashPath?
): string
[]
Calculates the merkle membership proof path
Parameters​
Name | Type | Default value | Description |
---|---|---|---|
leaf | string | undefined | hex-string |
branch | string [] | undefined | hex-string array |
hashPath | string [] | [] | 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',
// ]