Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mafintosh/merkle-tree-stream
A stream that generates a merkle tree based on the incoming data.
https://github.com/mafintosh/merkle-tree-stream
Last synced: 6 days ago
JSON representation
A stream that generates a merkle tree based on the incoming data.
- Host: GitHub
- URL: https://github.com/mafintosh/merkle-tree-stream
- Owner: mafintosh
- License: mit
- Created: 2015-11-14T17:42:25.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2022-02-25T08:01:45.000Z (almost 3 years ago)
- Last Synced: 2025-01-02T00:07:49.322Z (13 days ago)
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 105
- Watchers: 5
- Forks: 12
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-peer-to-peer - merkle-stream
- awesome-peer-to-peer - merkle-stream
README
# merkle-tree-stream
A stream that generates a merkle tree based on the incoming data.
```
npm install merkle-tree-stream
```[![build status](http://img.shields.io/travis/mafintosh/merkle-tree-stream.svg?style=flat)](http://travis-ci.org/mafintosh/merkle-tree-stream)
## Usage
``` js
var MerkleTreeStream = require('merkle-tree-stream')
var crypto = require('crypto')var stream = new MerkleTreeStream({
leaf: function (leaf, roots) {
// this function should hash incoming data
// roots in the current partial roots of the merkle tree
return crypto.createHash('sha256').update(leaf.data).digest()
},
parent: function (a, b) {
// hash two merkle tree node hashes into a new parent hash
return crypto.createHash('sha256').update(a.hash).update(b.hash).digest()
}
})stream.write('hello')
stream.write('hashed')
stream.write('world')stream.on('data', function (data) {
console.log(data)
})
```Running the above will print
```
{ index: 0,
parent: 1,
hash: ,
data: } // 'hello' as buffer
{ index: 2,
parent: 1,
hash: ,
data: } // 'hashed' as buffer
{ index: 1,
parent: 3,
hash: ,
data: null }
{ index: 4,
parent: 5,
hash: ,
data: } // 'world' as buffer
````index` is the tree node index. all even numbers are data nodes (will have a non-null `data` property).
`parent` is the index of a tree node's parent node.
`hash` is the hash of a tree node.
You can always access the current partial roots of the merkle tree by accessing `stream.roots`.
If the number of nodes written to the stream is not a power of `2` then `stream.roots` will
contain more than 1 node (at most `log2(number-of-nodes-written)`). Otherwise it will contain just a single root.Optionally you can also pass in an array of `roots` in the options map as `{roots: roots}` to continue adding data
to a previously generated merkle tree.## Low-level interface
A non stream low-level interface can required by doing `require('merkle-tree-stream/generator')`.
``` js
var MerkleGenerator = require('merkle-tree-stream/generator')
var gen = new MerkleGenerator({leaf: ..., parent: ...}) // same options as abovevar nodes = gen.next('some data')
console.log(nodes) // returns the tree nodes generated, similar to the stream output
console.log(gen.roots) // contains the current roots nodes
```## Related
See [mafintosh/flat-tree](https://github.com/mafintosh/flat-tree) for more information about
how node/parent indexes are calculated.## License
MIT