You can create a secure Trie where the keys are automatically hashed using SHA3 by using require('merkle-patricia-tree/secure')
. It has the same methods and constuctor as Trie
Use require('merkel-patricia-tree')
for the base interface. In Ethereum applications stick with the Secure Trie Overlay require('merkel-patricia-tree/secure')
. The API for the raw and the secure interface are about the same
Parameters
db
[Object] An instance of levelup or a compatible API. If the db isnull
or left undefined, then the trie will be stored in memory via memdownroot
[Buffer or String]A hex
Stringor
Buffer` for the root of a previously stored trie
Properties
root
Buffer The current root of thetrie
isCheckpoint
Boolean determines if you are saving to a checkpoint or directly to the dbEMPTY_TRIE_ROOT
Buffer the Root for an empty trie
The given hash of operations (key additions or deletions) are executed on the DB
Parameters
ops
Arraycb
Function
Examples
var ops = [
{ type: 'del', key: 'father' }
, { type: 'put', key: 'name', value: 'Yuri Irsenovich Kim' }
, { type: 'put', key: 'dob', value: '16 February 1941' }
, { type: 'put', key: 'spouse', value: 'Kim Young-sook' }
, { type: 'put', key: 'occupation', value: 'Clown' }
]
trie.batch(ops)
Checks if a given root exists
Parameters
root
Buffercb
Function
The data
event is given an Object
hat has two properties; the key
and the value
. Both should be Buffers.
Returns stream.Readable Returns a stream of the contents of the trie
deletes a value given a key
Parameters
key
Buffer or Stringcb
Function the callbackFunction
Removes a raw value in the underlying db
Parameters
key
Buffer or Stringcb
Function A callbackFunction
, which is given the argumenterr
- for errors that may have occured
Gets a value given a key
Parameters
key
Buffer or String the key to search forcallback
Function A callbackFunction
which is given the argumentserr
- for errors that may have occured andvalue
- the found value in aBuffer
or if no value was foundnull
Retrieves a raw value in the underlying db
Parameters
key
Buffercb
Function A callbackFunction
, which is given the argumentserr
- for errors that may have occured andvalue
- the found value in aBuffer
or if no value was foundnull
.
Stores a given value
at the given key
Parameters
key
Buffer or StringValue
Buffer or Stringcallback
Function A callbackFunction
which is given the argumenterr
- for errors that may have occured
Writes a value directly to the underlining db
Parameters
key
Buffer or String The key as aBuffer
orString
value
Buffer The value to be storedcallback
Function A callbackFunction
, which is given the argumenterr
- for errors that may have occured
Creates a checkpoint that can later be reverted to or committed. After this is called, no changes to the trie will be permanently saved until commit
is called
commits a checkpoint to disk
Reverts the trie to the state it was at when checkpoint
was first called.
Parameters
cb
Function the callback
Trys to find a path to the node for the given key.
It returns a stack
of nodes to the closet node.
Parameters
Function
cb - the callback function. Its is given the following arguments- err - any errors encontered- node - the last node found
- keyRemainder - the remaining key nibbles not accounted for
- stack - an array of nodes that forms the path to node we are searching for
targetKey
cb
Static functions for creating/verifying a merkle proof.
Returns a merkle proof for a given key
Parameters
trie
Triekey
Stringcb
Function A callbackFunction
(arguments {Error}err
, {Array.}proof
)
Verifies a merkle proof for a given key
Parameters
rootHash
Bufferkey
Stringproof
Array<TrieNode>cb
Function A callbackFunction
(arguments {Error}err
, {String}val
)
These are not exposed.
Parameters
dataArr
Arraykey
terminator
Returns Buffer returns buffer of encoded data hexPrefix
Take a collection of async fns, call the cb on the first to return a truthy value. If all run without a truthy result, return undefined
Parameters
array
iterator
cb
Take two or more functions and returns a function that will execute all of the given functions
Parameters
cb
Function the callback
Compare two 'nibble array' keys
Parameters
keyA
keyB
Returns the number of in order matching nibbles of two give nibble arrayes
Parameters
nib1
Arraynib2
Array
saves a stack
Parameters
key
Array the key. Should follow the stackstack
Array a stack of nodes to the value given by the keyopStack
Array a stack of levelup operations to commit at the end of this funcitoncb
Function
Updates a node
Parameters
key
Buffervalue
Buffer or StringkeyRemainder
Arraystack
Array -cb
Function the callback