-
Notifications
You must be signed in to change notification settings - Fork 0
/
toybc.js
103 lines (91 loc) · 2.49 KB
/
toybc.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
/* Toy BC is a toy blockchain, just to play with the concept
TODO
make the block Id the hash of the block instead of having to pick it
*/
function sha256(str) {
var buffer = new TextEncoder("utf-8").encode(str);
return crypto.subtle.digest("SHA-256", buffer).then(function (hash) {
return hex(hash);
});
}
function hex(buffer) {
var hexCodes = [];
var view = new DataView(buffer);
for (var i = 0; i < view.byteLength; i += 4) {
var value = view.getUint32(i)
var stringValue = value.toString(16)
var padding = '00000000'
var paddedValue = (padding + stringValue).slice(-padding.length)
hexCodes.push(paddedValue);
}
return hexCodes.join("");
}
var difficulty = "00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
var Block = class {
constructor(id,parentBlock) {
this.id = id;
this.parentBlock = parentBlock;
this.nounce = 0;
this.message = null;
}
setMessage(message){
this.message = message;
};
isValid(){
var self = this;
sha256(JSON.stringify(self)).then(function(digest) {
if (digest < difficulty) {
return console.log("true");
}
return console.log("false");
});
};
mine() {
var self = this;
var nounces = [];
function checkShaForNounces(arr) {
return arr.reduce(function(promise, nounce) {
return promise.then(function() {
self.nounce = nounce;
return sha256(JSON.stringify(self)).then(function(res) {
if (res < difficulty) {
nounces.push(self.nounce);
}
});
});
}, Promise.resolve());
}
var listOfNounces = [];
for (var i=0;i<10000;i++) {
listOfNounces.push(i);
}
return checkShaForNounces(listOfNounces).then(function() {
if (nounces[0] == undefined) {
return console.log("Didn't find a valid nounce, look further!")
}
console.log('found a valid nounce: '+nounces[0]);
self.nounce = nounces[0];
});
}
showHash(){
var self = this;
sha256(JSON.stringify(this)).then(function(digest) {
console.log(digest);
});
};
getHash(){
var self = this;
return sha256(JSON.stringify(this)).then(function(digest) {
return digest;
});
};
}
var b1 = new Block(0);
var b2;
b1.setMessage("this is my origin block");
b1.mine().then(function(){
b1.getHash().then(function(h){
b2 = new Block(1,h);
b2.setMessage("this is my second block");
});
})