Skip to content

Commit

Permalink
Update merkleTree.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ROZ-MOFUMOFU-ME authored Apr 30, 2024
1 parent 1fc8c92 commit 27bde74
Showing 1 changed file with 33 additions and 43 deletions.
76 changes: 33 additions & 43 deletions lib/merkleTree.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,48 @@
/*
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){
this.data = data.filter(element => element != null);
this.steps = calculateSteps(this.data);
};

function merkleJoin(h1, h2){
var joined = Buffer.concat([h1, h2]);
var dhashed = util.sha256d(joined);
return dhashed;
function merkleJoin(h1, h2){
if (h1 === null || h2 === null) {
throw new Error('Merkle join received null input');
}
var joined = Buffer.concat([h1, h2]);
var dhashed = util.sha256d(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){
function calculateSteps(data){
var L = data;
var steps = [];
var Ll = L.length;

if (Ll === 1)
break;
if (Ll > 1){
while (true){
if (Ll === 1) break;

steps.push(L[1]);
steps.push(L[1]);

if (Ll % 2)
L.push(L[L.length - 1]);
if (Ll % 2) L.push(L[L.length - 1]);

var Ld = [];
var r = util.range(StartL, Ll, 2);
r.forEach(function(i){
var Ld = [];
for (var i = 0; i < L.length; i += 2) {
if (i + 1 < L.length) {
Ld.push(merkleJoin(L[i], L[i + 1]));
});
L = PreL.concat(Ld);
Ll = L.length;
}
}
L = Ld;
Ll = L.length;
}
return steps;
}

this.data = data;
this.steps = calculateSteps(data);

return steps;
}
MerkleTree.prototype = {
withFirst: function(f){
this.steps.forEach(function(s){
f = util.sha256d(Buffer.concat([f, s]));
});
return f;
}
};

MerkleTree.prototype.withFirst = function(f){
this.steps.forEach(function(s){
f = util.sha256d(Buffer.concat([f, s]));
});
return f;
};

0 comments on commit 27bde74

Please sign in to comment.