merkletreejs › Globals › "src/Base" › Base
-
Base
- bigNumberify
- binarySearch
- bufferIndexOf
- bufferToHex
- bufferify
- bufferifyFn
- isHexString
- linearSearch
- log2
- zip
- bigNumberify
- binarySearch
- bufferToHex
- bufferify
- hexZeroPad
- isHexString
- linearSearch
▸ bigNumberify(value
: any): BigInt
Parameters:
Name | Type |
---|---|
value |
any |
Returns: BigInt
▸ binarySearch(array
: Buffer[], element
: Buffer, compareFunction
: function): number
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
▸ bufferIndexOf(array
: Buffer[], element
: Buffer, isSorted
: boolean): number
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(value
: Buffer, withPrefix
: boolean): string
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(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(f
: any): any
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
▸ isHexString(value
: string): boolean
isHexString
desc
Returns true if value is a hex string.
example
console.log(MerkleTree.isHexString('0x1234'))
Parameters:
Name | Type |
---|---|
value |
string |
Returns: boolean
▸ linearSearch(array
: Buffer[], element
: Buffer, eqChecker
: function): number
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
▸ 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
▸ 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[][]
▸ bigNumberify(value
: any): BigInt
Parameters:
Name | Type |
---|---|
value |
any |
Returns: BigInt
▸ binarySearch(array
: Buffer[], element
: Buffer, compareFunction
: function): number
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
▸ bufferToHex(value
: Buffer, withPrefix
: boolean): string
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
▸ 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
▸ hexZeroPad(hexStr
: string, length
: number): string
Parameters:
Name | Type |
---|---|
hexStr |
string |
length |
number |
Returns: string
▸ isHexString(v
: string): boolean
isHexString
desc
Returns true if value is a hex string.
example
console.log(MerkleTree.isHexString('0x1234'))
Parameters:
Name | Type |
---|---|
v |
string |
Returns: boolean
▸ linearSearch(array
: Buffer[], element
: Buffer, eqChecker
: function): number
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
▸ 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