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);
|
|
|
|
return result._65.root;
|
2014-01-13 16:45:10 -08:00
|
|
|
}
|
2016-11-16 14:27:48 -08:00
|
|
|
|
|
|
|
exports.getRoot = function (rpcData, generateTxRaw) {
|
2016-11-27 08:55:01 -08:00
|
|
|
hashes = [util.reverseBuffer(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;
|
|
|
|
};
|