-
Notifications
You must be signed in to change notification settings - Fork 1
/
tchain.js
158 lines (144 loc) · 4.27 KB
/
tchain.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"use strict";
//@ts-check
var createHash = require('create-hash');
/**
* @typedef block
* @property {string} algorithm
* @property {number} difficulty
* @property {string} hash
* @property {string} nonse
* @property {number} start
* @property {number} testCount
* @property {number} end
**/
// * @property {string} data
const hashMethods = {
/**
* @param {string} data
*/
md5: function(data) {
return createHash('md5').update(data).digest('hex');
},
/**
* @param {string} data
*/
sha256: function(data) {
return createHash('sha256').update(data).digest('hex');
},
};
/**
* @param {string} hash
* @param {string} algorithm
* @param {number} difficulty
* @return {block}
*/
function mine(hash, algorithm, difficulty) {
var resStart = new Array(difficulty + 1).join('0');
var nonse;
var hashMethod = hashMethods[algorithm];
var testCount = 0
var start = Date.now()
while (true) {
testCount++;
nonse = (Math.random() + '').substr(2);
var signature = hashMethod(hash + '\n' + nonse);
if (signature.substr(0, difficulty) == resStart) {
return {
algorithm,
difficulty,
hash,
signature,
nonse,
start,
testCount,
end: Date.now() - start
};
}
}
}
/**
* @param {string} hash
* @param {string} algorithm
* @param {number} difficulty
* @return {block}
*/
function tryMine(hash, algorithm, difficulty, maxTry) {
var resStart = new Array(difficulty + 1).join('0');
var nonse;
var hashMethod = hashMethods[algorithm];
var testCount = 0
var start = Date.now()
while (testCount < maxTry) {
testCount++;
nonse = (Math.random() + '').substr(2);
var signature = hashMethod(hash + '\n' + nonse);
if (parseInt(signature.substr(0, difficulty), 16) === 0) {
return {
algorithm,
difficulty,
hash,
signature,
nonse,
start,
end: Date.now() - start
};
}
}
}
/**
*
* @param {block} block
*/
function testBlock(block) {
var resStart = new Array(block.difficulty + 1).join('0');
var shouldHash = hashObjectData(block, block.algorithm);
if (shouldHash !== block.hash) {
console.log(shouldHash, block.hash)
console.log('the hash is not right')
return false;
}
var hash = hashMethods[block.algorithm](block.hash + '\n' + block.nonse);
return hash.substr(0, block.difficulty) == resStart;
}
/**
* @function
* function that is able to consistently hash no metter in what order props where added
* @param {*} data
* @param {string} algorithm
* @return {string}
*/
function hashObjectData(block, algorithm) {
//console.log('hashBlock:', block)
return hashMethods[algorithm](JSON.stringify(sortOjectsPropsByName(block.data)) + ':' + (block.lastBlockHash || '') + ':' + block.time);
}
/**
* When use JSON.stringify, the order of properties is not guarantied. althou
* it is consistent in JS. The order of props in the JSON is the same order as they
* where added to the object you stringify.
* if you parse and stringify, the props in each object will remain the same order.
* But when there was used different methods to create objects, the order of properties
* might vary and also the implementations in other languages might differ, so for the
* blockchain it will be nessasary to guarantie an order of props, to hash the stringified data.
*
* The current implementation might not be fast, but it is reliable
*
*/
function sortOjectsPropsByName(object) {
if (typeof object !== 'object') {
return object; // not an object
}
if (Array.isArray(object)) {
return object.map(sortOjectsPropsByName);
}
var keys = Object.keys(object).sort();
var out = {};
keys.forEach(function(key) {
out[key] = sortOjectsPropsByName(object[key]);
});
return out;
}
module.exports.hashObjectData = hashObjectData;
module.exports.hashMethods = hashMethods;
module.exports.testBlock = testBlock;
module.exports.mine = mine;
module.exports.tryMine = tryMine;