forked from JoshOrndorff/BitStory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
185 lines (137 loc) · 4.22 KB
/
index.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"use strict"
window.addEventListener("DOMContentLoaded", () => {
// References to relevant DOM elements
var parentBox = document.getElementById("parent")
var wordBox = document.getElementById("word")
var nonceBox = document.getElementById("nonce")
var incButton = document.getElementById("increment")
var decButton = document.getElementById("decrement")
var randomButton = document.getElementById("random")
var autoDiv = document.getElementById("auto")
var autoButton = document.getElementById("auto-button")
var diffBox = document.getElementById("difficulty")
var hashDiv = document.getElementById("hash")
var nextButton = document.getElementById("publish")
// Populate default values if no values are entered or left over
if (parentBox.value === "" && wordBox.value === "") {
wordBox.value = "BitStory"
}
if (nonceBox.value === ""){
nonceBox.value = "0"
}
hashFromDOM();
// Update the hash whenever the text changes
parentBox.addEventListener("input", hashFromDOM)
wordBox .addEventListener("input", hashFromDOM)
nonceBox .addEventListener("input", hashFromDOM)
// Handle clicking the buttons
incButton.addEventListener("click", () => {changeBy( 1)})
decButton.addEventListener("click", () => {changeBy(-1)})
randomButton.addEventListener("click", genRandom)
autoButton.addEventListener("click", autoMine)
nextButton.addEventListener("click", nextBlock)
/**
* Resets the hasher to build on the existing block
*/
function nextBlock() {
parentBox.value = hashDiv.innerHTML
wordBox.focus()
wordBox.select()
}
/**
* Automatically mines until a block with appropriate difficulty
*/
function autoMine() {
// Variable difficulty for auto-mine
var diff = parseInt(diffBox.value, 10)
var parent = getParent()
var word = getWord()
var nonce = getNonce()
var oldHash
for (var hash = ""; hash !== oldHash && hash.slice(0, diff) !== "0".repeat(diff); nonce++) {
oldHash = hash
hash = getHash(parent, word, nonce)
}
nonceBox.value = --nonce
hashFromDOM()
}
/**
* Change the nonce in the DOM by n
* @param n The amount to change by
*/
function changeBy(n){
var nonce = parseInt(nonceBox.value, 10)
nonce += n
nonceBox.value = nonce
hashFromDOM()
}
/**
* Generates a random 32-bit nonce, puts it in the DOM, and updates hashes.
*/
function genRandom() {
var nonce = new Uint32Array(1)
window.crypto.getRandomValues(nonce)
nonceBox.value = nonce[0]
hashFromDOM()
}
/**
* Reads in and validates, values from the DOM.
* Enables auto-mine feature on secret code input
*/
function hashFromDOM() {
// Check for enabling auto-mine
if (parentBox.value === "auto-mine") {
autoDiv.style.display = "inline"
}
hashDiv.innerHTML = getHash(getParent(), getWord(), getNonce())
}
/**
* Reads in and validates the parent hash from the DOM.
* Treats parent case-insensatively because it most likely represents a hexadecimal number.
* @return The validated parent hash
*/
function getParent() {
var parent = parentBox.value.toLowerCase()
// No required validation yet
return parent
}
/**
* Reads in and validates the word from the DOM.
* @return The validated word
*/
function getWord() {
var word = wordBox.value
// Validate word: No spaces
if (word.indexOf(" ") !== -1) {
word = word.replace(" ", "");
console.warn("Invalid word. Using " + + " instead.")
}
return word
}
/**
* Reads in and validates the nonce from the DOM.
* @return The validated nonce
*/
function getNonce() {
var nonce = parseInt(nonceBox.value, 10)
// Validation nonce: Must be a number
if (isNaN(nonce)) {
nonce = 0
console.warn("Invalid nonce. Using 0 instead.")
}
return nonce
}
/**
* Calculate the sha256 hash of the parameters, and return the first
* 8 hexadigits as a string
* @param parent
* @param word
* @param nonce
* @return the hash
*/
function getHash(parent, word, nonce) {
var concat = parent + word + nonce
var hash = sha256(concat).slice(0, 8)
return hash
}
})