1.0.1 • Published 5 years ago

@shyftnetwork/shyft_merkle-lib v1.0.1

Weekly downloads
1
License
ISC
Repository
-
Last release
5 years ago

Shyft Merkle Tree Library

Basic libary for Shyft-specific merkle tree operations.

Installing

npm install @shyftnetwork/shyft_merkle-lib

Re-generating Docs

npm install && npm run docs

Functions

hashAB(a, b) ⇒ string

Hashes two values together.

Kind: global function

ParamTypeDescription
astringa bytes32 appropriate value
bstringa bytes32 appropriate value

hashHeader() ⇒ string

Hashes a block header: {previousHeader, timestamp, blockNumber, transactionRoot}

Kind: global function

ParamTypeDescription
obj.previousHeaderstringprevious block header
obj.timestampnumberblock timestamp
obj.blockNumbernumbercurrent block number
obj.transactionsRootstringcurrent transaction root
obj.receiptsRootstringcurrent receipts root

isPowerOfTwo(n) ⇒ boolean

Checks if a number is a power of two (ie. valid leaf array size)

Kind: global function

ParamType
nnumber

setupleafs(leafs) ⇒ Array.<Array.<string>>

Creates first two layers of a tree, ie. the leafs and their hashes

Kind: global function

ParamType
leafsArray.<string>

treePad(tree) ⇒ Array.<Array.<string>>

Pads the leaves of a tree with null to ensure its length is a power of two

Kind: global function

ParamType
treeArray.<Array.<string>>

buildTree(height) ⇒ Tree

Constructs a merkle tree recursively as a 2d array.

Kind: global function

ParamTypeDescription
Array.<string> | Array.<Array.<string>>the current tree. Initially a single dimensional array of leaves
heightcurrent height of the tree (starts at 0)

getProof(tree, leaf) ⇒ Array.<any>

Assembles a proof from a given tree for a given leaf. This produces a proof of the format boolean, leaf, boolean, leaf ...

Kind: global function

ParamType
treeArray.<Array.<string>>
leafstring

verifyProof(proof, leaf, root) ⇒ boolean

Verifies that the leaf and the proof do in fact hash to the root

Kind: global function

ParamType
proofArray.<any>
leafObject
rootstring

stringifyProof(proof) ⇒ string

Stringifies the proof by assmbling a bit array of the path indicators and appending it to the path segments.

Kind: global function

ParamType
proofArray.<any>

getRoot(tree) ⇒ string

Returns root element of tree.

Kind: global function

Param
tree

isValidLeaf(leaf, tree) ⇒ boolean

Checks if the leaf exists in the tree.

Kind: global function

ParamTypeDescription
leafObject
leaf.previousHeaderstringprevious block header
leaf.timestampnumberblock timestamp
leaf.blockNumbernumbercurrent block number
leaf.transactionsRootstringcurrent transaction root
leaf.receiptsRootstringcurrent receipts root
treeValid merkle tree