node-stratum-pool/merkleTree.js

67 lines
1.6 KiB
JavaScript

/*
Ported from https://github.com/slush0/stratum-mining/blob/master/lib/merkletree.py
*/
var util = require('./util.js');
var MerkleTree = module.exports = function MerkleTree(data){
function merkleJoin(h1, h2){
var joined = Buffer.concat([h1, h2]);
var dhashed = util.doublesha(joined);
return dhashed;
}
function calculateSteps(data){
var L = data;
var steps = [];
var PreL = [null];
var StartL = 2;
var Ll = L.length;
if (Ll > 1){
while (true){
if (Ll === 1)
break;
steps.push(L[1]);
if (Ll % 2)
L.push(L[L.length - 1]);
var Ld = [];
var r = util.range(StartL, Ll, 2);
r.forEach(function(i){
Ld.push(merkleJoin(L[i], L[i + 1]));
});
L = PreL.concat(Ld);
Ll = L.length;
}
}
return steps;
}
this.data = data;
this.steps = calculateSteps(data);
}
MerkleTree.prototype = {
hashSteps: function(){
if (!this.stepsHash)
this.stepsHash = util.doublesha(Buffer.concat(this.steps));
return this.stepsHash;
},
withFirst: function(f){
this.steps.forEach(function(s){
f = util.doublesha(Buffer.concat([f, s]));
});
return f;
},
merkleRoot: function(){
return this.withFirst(this.data[0]);
}
};