13 KiB
merkletreejs › Globals › "src/MerkleSumTree" › MerkleSumTree
Class: MerkleSumTree
Hierarchy
-
↳ MerkleSumTree
Index
Constructors
Properties
Methods
- bigNumberify
- binarySearch
- bufferIndexOf
- bufferToHex
- bufferify
- bufferifyFn
- getProof
- isHexString
- linearSearch
- log2
- sizeToBuffer
- sum
- verifyProof
- zip
- bigNumberify
- binarySearch
- bufferToHex
- bufferify
- checkConsecutive
- hexZeroPad
- isHexString
- linearSearch
Constructors
constructor
+ new MerkleSumTree(leaves
: Leaf[], hashFn
: THashFn): MerkleSumTree
Parameters:
Name | Type |
---|---|
leaves |
Leaf[] |
hashFn |
THashFn |
Returns: MerkleSumTree
Properties
buckets
• buckets: Bucket[]
hashFn
• hashFn: THashFn
leaves
• leaves: Leaf[]
root
• root: Bucket
Methods
bigNumberify
▸ bigNumberify(value
: any): BigInt
Inherited from Base.bigNumberify
Parameters:
Name | Type |
---|---|
value |
any |
Returns: BigInt
binarySearch
▸ binarySearch(array
: Buffer[], element
: Buffer, compareFunction
: function): number
Inherited from Base.binarySearch
binarySearch
desc
Returns the first index of which given item is found in array using binary search.
example
const index = tree.binarySearch(array, element, Buffer.compare)
Parameters:
▪ array: Buffer[]
Array of items.
▪ element: Buffer
Item to find.
▪ compareFunction: function
▸ (a
: unknown, b
: unknown): number
Parameters:
Name | Type |
---|---|
a |
unknown |
b |
unknown |
Returns: number
- Index number
Protected
bufferIndexOf
▸ bufferIndexOf(array
: Buffer[], element
: Buffer, isSorted
: boolean): number
Inherited from Base.bufferIndexOf
bufferIndexOf
desc
Returns the first index of which given buffer is found in array.
example
const index = tree.bufferIndexOf(haystack, needle)
Parameters:
Name | Type | Default |
---|---|---|
array |
Buffer[] | - |
element |
Buffer | - |
isSorted |
boolean | false |
Returns: number
- Index number
bufferToHex
▸ bufferToHex(value
: Buffer, withPrefix
: boolean): string
Inherited from Base.bufferToHex
bufferToHex
desc
Returns a hex string with 0x prefix for given buffer.
example
const hexStr = tree.bufferToHex(Buffer.from('A'))
Parameters:
Name | Type | Default |
---|---|---|
value |
Buffer | - |
withPrefix |
boolean | true |
Returns: string
bufferify
▸ bufferify(value
: any): Buffer
bufferify
desc
Returns a buffer type for the given value.
example
const buf = tree.bufferify('0x1234')
Parameters:
Name | Type |
---|---|
value |
any |
Returns: Buffer
bufferifyFn
▸ bufferifyFn(f
: any): any
Inherited from Base.bufferifyFn
bufferifyFn
desc
Returns a function that will bufferify the return value.
example
const fn = tree.bufferifyFn((value) => sha256(value))
Parameters:
Name | Type |
---|---|
f |
any |
Returns: any
getProof
▸ getProof(index
: number | BigInt): any[]
Parameters:
Name | Type |
---|---|
index |
number | BigInt |
Returns: any[]
Protected
isHexString
▸ isHexString(value
: string): boolean
Inherited from Base.isHexString
isHexString
desc
Returns true if value is a hex string.
example
console.log(MerkleTree.isHexString('0x1234'))
Parameters:
Name | Type |
---|---|
value |
string |
Returns: boolean
linearSearch
▸ linearSearch(array
: Buffer[], element
: Buffer, eqChecker
: function): number
Inherited from Base.linearSearch
linearSearch
desc
Returns the first index of which given item is found in array using linear search.
example
const index = tree.linearSearch(array, element, (a, b) => a === b)
Parameters:
▪ array: Buffer[]
Array of items.
▪ element: Buffer
Item to find.
▪ eqChecker: function
▸ (a
: unknown, b
: unknown): boolean
Parameters:
Name | Type |
---|---|
a |
unknown |
b |
unknown |
Returns: number
- Index number
Protected
log2
▸ log2(n
: number): number
log2
desc
Returns the log2 of number.
Parameters:
Name | Type |
---|---|
n |
number |
Returns: number
▸ print(): void
desc
Prints out a visual representation of the merkle tree.
example
tree.print()
Returns: void
sizeToBuffer
▸ sizeToBuffer(size
: BigInt): Buffer‹›
Parameters:
Name | Type |
---|---|
size |
BigInt |
Returns: Buffer‹›
sum
▸ sum(arr
: BigInt[]): bigint
Parameters:
Name | Type |
---|---|
arr |
BigInt[] |
Returns: bigint
verifyProof
▸ verifyProof(root
: Bucket, leaf
: Leaf, proof
: ProofStep[]): boolean
Parameters:
Name | Type |
---|---|
root |
Bucket |
leaf |
Leaf |
proof |
ProofStep[] |
Returns: boolean
Protected
zip
▸ zip(a
: any[], b
: any[]): any[][]
zip
desc
Returns true if value is a hex string.
example
const zipped = tree.zip(['a', 'b'],['A', 'B'])
console.log(zipped) // [ [ 'a', 'A' ], [ 'b', 'B' ] ]
Parameters:
Name | Type | Description |
---|---|---|
a |
any[] | first array |
b |
any[] | second array |
Returns: any[][]
Static
bigNumberify
▸ bigNumberify(value
: any): BigInt
Inherited from Base.bigNumberify
Parameters:
Name | Type |
---|---|
value |
any |
Returns: BigInt
Static
binarySearch
▸ binarySearch(array
: Buffer[], element
: Buffer, compareFunction
: function): number
Inherited from Base.binarySearch
binarySearch
desc
Returns the first index of which given item is found in array using binary search.
example
const index = MerkleTree.binarySearch(array, element, Buffer.compare)
Parameters:
▪ array: Buffer[]
Array of items.
▪ element: Buffer
Item to find.
▪ compareFunction: function
▸ (a
: unknown, b
: unknown): number
Parameters:
Name | Type |
---|---|
a |
unknown |
b |
unknown |
Returns: number
- Index number
Static
bufferToHex
▸ bufferToHex(value
: Buffer, withPrefix
: boolean): string
Inherited from Base.bufferToHex
bufferToHex
desc
Returns a hex string with 0x prefix for given buffer.
example
const hexStr = MerkleTree.bufferToHex(Buffer.from('A'))
Parameters:
Name | Type | Default |
---|---|---|
value |
Buffer | - |
withPrefix |
boolean | true |
Returns: string
Static
bufferify
▸ bufferify(value
: any): Buffer
bufferify
desc
Returns a buffer type for the given value.
example
const buf = MerkleTree.bufferify('0x1234')
Parameters:
Name | Type |
---|---|
value |
any |
Returns: Buffer
Static
checkConsecutive
▸ checkConsecutive(leaves
: Leaf[]): void
Parameters:
Name | Type |
---|---|
leaves |
Leaf[] |
Returns: void
Static
hexZeroPad
▸ hexZeroPad(hexStr
: string, length
: number): string
Inherited from Base.hexZeroPad
Parameters:
Name | Type |
---|---|
hexStr |
string |
length |
number |
Returns: string
Static
isHexString
▸ isHexString(v
: string): boolean
Inherited from Base.isHexString
isHexString
desc
Returns true if value is a hex string.
example
console.log(MerkleTree.isHexString('0x1234'))
Parameters:
Name | Type |
---|---|
v |
string |
Returns: boolean
Static
linearSearch
▸ linearSearch(array
: Buffer[], element
: Buffer, eqChecker
: function): number
Inherited from Base.linearSearch
linearSearch
desc
Returns the first index of which given item is found in array using linear search.
example
const index = MerkleTree.linearSearch(array, element, (a, b) => a === b)
Parameters:
▪ array: Buffer[]
Array of items.
▪ element: Buffer
Item to find.
▪ eqChecker: function
▸ (a
: unknown, b
: unknown): boolean
Parameters:
Name | Type |
---|---|
a |
unknown |
b |
unknown |
Returns: number
- Index number
Static
print
▸ print(tree
: any): void
desc
Prints out a visual representation of the given merkle tree.
example
MerkleTree.print(tree)
Parameters:
Name | Type | Description |
---|---|---|
tree |
any | Merkle tree instance. |
Returns: void