Permalink
Browse files

Added API documentation for merkle proof generation/verification

  • Loading branch information...
holgerd77 committed Nov 30, 2017
1 parent df14ad1 commit 394ed84e118b3a9ce23d0c286b9e7f5daa6f3ea3
Showing with 61 additions and 18 deletions.
  1. +45 −17 docs/index.md
  2. +1 −1 package.json
  3. +15 −0 proof.js
View
@@ -156,6 +156,51 @@ Reverts the trie to the state it was at when `checkpoint` was first called.
- `cb` **Function** the callback
## findPath
[baseTrie.js:252-300](https://github.com/wanderer/merkle-patricia-tree/blob/dc436426d717fed408f4d46fed23f6d26d03d39d/baseTrie.js#L252-L300 "Source code on GitHub")
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`
# Merkle Proof
Static functions for creating/verifying a merkle proof.
## prove
[proof.js:12-29](https://github.com/ethereumjs/merkle-patricia-tree/blob/df14ad1fbf861160abdb5954973cf4a570473ce9/proof.js#L12-L29 "Source code on GitHub")
Returns a merkle proof for a given key
**Parameters**
- `trie` **Trie**
- `key` **String**
- `cb` **Function** A callback `Function` (arguments {Error} `err`, {Array.<TrieNode>} `proof`)
## verifyProof
[proof.js:39-95](https://github.com/ethereumjs/merkle-patricia-tree/blob/df14ad1fbf861160abdb5954973cf4a570473ce9/proof.js#L39-L95 "Source code on GitHub")
Verifies a merkle proof for a given key
**Parameters**
- `rootHash` **Buffer**
- `key` **String**
- `proof` **Array&lt;TrieNode&gt;**
- `cb` **Function** A callback `Function` (arguments {Error} `err`, {String} `val`)
# Internal Util Functions
These are not exposed.
@@ -220,23 +265,6 @@ Returns the number of in order matching nibbles of two give nibble arrayes
- `nib1` **Array**
- `nib2` **Array**
## _findPath
[baseTrie.js:252-300](https://github.com/wanderer/merkle-patricia-tree/blob/dc436426d717fed408f4d46fed23f6d26d03d39d/baseTrie.js#L252-L300 "Source code on GitHub")
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`
## _saveStack
[baseTrie.js:502-529](https://github.com/wanderer/merkle-patricia-tree/blob/dc436426d717fed408f4d46fed23f6d26d03d39d/baseTrie.js#L502-L529 "Source code on GitHub")
View
@@ -12,7 +12,7 @@
"test:browser": "karma start karma.conf.js",
"test:node": "tape ./test/*.js",
"build": "browserify --s Trie index.js > ./dist/trie.js",
"build:docs": "documentation --github -f md ./index.js ./secure.js > ./docs/index.md"
"build:docs": "documentation --github -f md ./index.js ./secure.js ./proof.js > ./docs/index-template.md"
},
"author": {
"name": "mjbecze",
View
@@ -2,6 +2,13 @@ const TrieNode = require('./trieNode')
const ethUtil = require('ethereumjs-util')
const matchingNibbleLength = require('./util').matchingNibbleLength
/**
* Returns a merkle proof for a given key
* @method Trie.prove
* @param {Trie} trie
* @param {String} key
* @param {Function} cb A callback `Function` (arguments {Error} `err`, {Array.<TrieNode>} `proof`)
*/
exports.prove = function (trie, key, cb) {
var nodes
@@ -21,6 +28,14 @@ exports.prove = function (trie, key, cb) {
})
}
/**
* Verifies a merkle proof for a given key
* @method Trie.verifyProof
* @param {Buffer} rootHash
* @param {String} key
* @param {Array.<TrieNode>} proof
* @param {Function} cb A callback `Function` (arguments {Error} `err`, {String} `val`)
*/
exports.verifyProof = function (rootHash, key, proof, cb) {
key = TrieNode.stringToNibbles(key)
var wantHash = ethUtil.toBuffer(rootHash)

0 comments on commit 394ed84

Please sign in to comment.