Last active
October 7, 2016 21:22
-
-
Save Matthew-Davey/45b999d8ce45086c752a6c72c57b9435 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash') | |
var crypto = require('crypto') | |
var reverse = require('buffer-reverse') | |
function sha256 (buffer) { | |
return crypto.createHash('sha256') | |
.update(buffer) | |
.digest() | |
} | |
var sha256d = _.flow(sha256, sha256) | |
function balanceLength (hashes) { | |
if (hashes.length % 2 === 0) { | |
return hashes | |
} | |
return hashes.concat([_.last(hashes)]) | |
} | |
function merkleRoot (hashes) { | |
if (hashes.length == 1) | |
return hashes[0] | |
return merkleRoot( | |
_.chain(balanceLength(hashes)) | |
.chunk(2) | |
.map(items => sha256d(Buffer.concat(items))) | |
.value() | |
) | |
} | |
function merkleRootLE (hashesLE) { | |
var hashesBE = _.map(hashesLE, reverse) | |
var merkleRootBE = merkleRoot(hashesBE) | |
var merkleRootLE = reverse(merkleRootBE) | |
return merkleRootLE | |
} | |
// Using tx hashes and expected merkle root from block #424328 | |
var hashesLE = [ | |
Buffer.from('8873f4e0179f3793f7057c061ab0d47ef88f0ac4bdf196db18bf8ebb595c2ee7', 'hex'), | |
Buffer.from('76d3724bb5ab9e2f6da7f0bb6d957f9741b804707b5ab5e959b82179a15f9cd2', 'hex'), | |
Buffer.from('8de666102756be4e5e6421ba545f1a4d751cc3fa5b525a4892f34ef32319bfc9', 'hex'), | |
Buffer.from('ade74bf39185afde6c9eff306cd979b7ce50921c38d7b5aabfa8954623e3c2a7', 'hex'), | |
Buffer.from('731dc0d08447feffef1de2a96fdf68fc201405b4e9083b0b8949845b93810862', 'hex'), | |
Buffer.from('f6f730aaab7a2c032cc6643cedf7bf9b496fe90d77d456a8333417b50183cbf7', 'hex'), | |
Buffer.from('d7775f6a9b955cc9f9606686b05ee837138c66a04eb8f5c59c281ed9ab7c8022', 'hex'), | |
Buffer.from('b9322e6588f1403dd82503fd398194410f46825f9d4ad62b34c1fd3414ba9e0c', 'hex'), | |
Buffer.from('f287b65f4b601dfb8e53b4183348e1583acf890ae8e0b86948fd30858d2de95d', 'hex'), | |
Buffer.from('660129cf91810f66280c47e092ac296d8c6118b65cf2cb60c0fab7fbed3af771', 'hex'), | |
Buffer.from('6fc53070af853023e5421ee0f383460b23f03dee1ed130b4f7368e83cbd38fed', 'hex') | |
] | |
var expected = Buffer.from('c7b70acf9c0fedd23cfaf6dd89ef9c65944b168ebf47569024854ee779ddf721', 'hex') | |
var actual = merkleRootLE(hashesLE) | |
console.log('expected :', expected.toString('hex')) | |
console.log('actual :', actual.toString('hex')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "merkle-root", | |
"version": "0.1.0", | |
"description": "", | |
"main": "merkle.js", | |
"author": "Matt Davey <matt.davey@fsfe.org>", | |
"license": "ISC", | |
"dependencies": { | |
"buffer-reverse": "^1.0.1", | |
"lodash": "^4.16.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment