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);
|
2017-06-19 17:00:36 -07:00
|
|
|
//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) {
|
2016-11-27 08:55:01 -08:00
|
|
|
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;
|
|
|
|
};
|