forked from chuanwc/node-multi-hashing-1
-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.js
131 lines (107 loc) · 3.33 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
To test run the following
npm update
node-gyp clean
node-gyp configure
node-gyp build --debug
*/
var mh = require('./build/Release/multihashing.node');
var crypto = require('crypto');
// this needs more work... I need a something to pass into these hashers and get back and assert..
var nTime = "1472669240";
var merkleTree = Buffer.from('7eb35ada44', 'hex');
var nonce = "211447";
var headerBuffer = serializeHeader(
{
"bits": "1f01ffff",
"previousblockhash": "0",
"version": 1
},
merkleTree.toString('hex'), nTime, nonce
);
console.log("Testing ScryptN");
//console.log(merkleTree.toString('hex'));
//console.log(headerBuffer.toString('hex'));
console.log(mh.scryptn(headerBuffer, 20));
//console.log( reverseBuffer( sha256d(headerBuffer) ).toString('hex') );
console.log("Testing Skein");
console.log(mh.skein(Buffer.from('1234test1234test1234test1234dasd')));
console.log("Testing Groestl");
console.log(mh.groestl(Buffer.from('1234test1234test1234test1234dasd')));
console.log("Testing Neoscrypt");
console.log(mh.neoscrypt(Buffer.from('1234test1234test1234test1234dasd')));
// More test to follow when I work on fixing the rest
//testing a webhook
/// Functions for tests to use ///
function serializeHeader(rpcData, merkleRoot, nTime, nonce) {
var header = Buffer.alloc(80);
var position = 0;
header.write(nonce, position, 4, 'hex');
header.write(rpcData.bits, position += 4, 4, 'hex');
header.write(nTime, position += 4, 4, 'hex');
header.write(merkleRoot, position += 4, 32, 'hex');
header.write(rpcData.previousblockhash, position += 32, 32, 'hex');
header.writeUInt32BE(rpcData.version, position + 32);
var header = reverseBuffer(header);
return header;
};
function reverseBuffer(buff){
var reversed = Buffer.alloc(buff.length);
for (var i = buff.length - 1; i >= 0; i--)
reversed[buff.length - i - 1] = buff[i];
return reversed;
};
function sha256(buffer){
var hash1 = crypto.createHash('sha256');
hash1.update(buffer);
return hash1.digest();
};
function sha256d(buffer){
return sha256(sha256(buffer));
};
function merkleJoin(h1, h2){
var joined = Buffer.concat([h1, h2]);
var dhashed = sha256d(joined);
return dhashed;
};
function merkleCalculateSteps(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 = 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;
}
function range(start, stop, step){
if (typeof stop === 'undefined'){
stop = start;
start = 0;
}
if (typeof step === 'undefined'){
step = 1;
}
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)){
return [];
}
var result = [];
for (var i = start; step > 0 ? i < stop : i > stop; i += step){
result.push(i);
}
return result;
};