node-stratum-pool/lib/merkleTree.js

23 lines
650 B
JavaScript
Raw Normal View History

2016-11-16 14:27:48 -08:00
var Promise = require('promise');
var merklebitcoin = Promise.denodeify(require('merkle-bitcoin'));
2014-01-13 16:45:10 -08:00
var util = require('./util.js');
2016-11-16 14:27:48 -08:00
function calcRoot(hashes) {
var result = merklebitcoin(hashes);
//console.log(Object.values(result)[2].root);
return Object.values(result)[2].root;
2014-01-13 16:45:10 -08:00
}
2016-11-16 14:27:48 -08:00
exports.getRoot = function (rpcData, generateTxRaw) {
2016-12-18 17:20:15 -08:00
hashes = [util.reverseBuffer(new Buffer(generateTxRaw, 'hex')).toString('hex')];
2016-11-16 14:27:48 -08:00
rpcData.transactions.forEach(function (value) {
hashes.push(value.hash);
});
if (hashes.length === 1) {
return hashes[0];
2014-01-13 16:45:10 -08:00
}
2016-11-16 14:27:48 -08:00
var result = calcRoot(hashes);
return result;
};