-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
243 lines (203 loc) · 10.7 KB
/
index.html
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!DOCTYPE html>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
// b58 library from https://gist.github.com/diafygi/90a3e80ca1c2793220e5/
const MAP = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
var to_b58 = function (
B, //Uint8Array raw byte input
A //Base58 characters (i.e. "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
) {
var d = [], //the array for storing the stream of base58 digits
s = '', //the result string variable that will be returned
i, //the iterator variable for the byte input
j, //the iterator variable for the base58 digit array (d)
c, //the carry amount variable that is used to overflow from the current base58 digit to the next base58 digit
n; //a temporary placeholder variable for the current base58 digit
for (i in B) { //loop through each byte in the input stream
j = 0, //reset the base58 digit iterator
c = B[i]; //set the initial carry amount equal to the current byte amount
s += c || s.length ^ i ? '' : 1; //prepend the result string with a "1" (0 in base58) if the byte stream is zero and non-zero bytes haven't been seen yet (to ensure correct decode length)
while (j in d || c) { //start looping through the digits until there are no more digits and no carry amount
n = d[j]; //set the placeholder for the current base58 digit
n = n ? n * 256 + c : c; //shift the current base58 one byte and add the carry amount (or just add the carry amount if this is a new digit)
c = n / 58 | 0; //find the new carry amount (floored integer of current digit divided by 58)
d[j] = n % 58; //reset the current base58 digit to the remainder (the carry amount will pass on the overflow)
j++ //iterate to the next base58 digit
}
}
while (j--) //since the base58 digits are backwards, loop through them in reverse order
s += A[d[j]]; //lookup the character associated with each base58 digit
return s //return the final base58 string
}
function hexToArrayBuffer(hex) {
let view = new Uint8Array(hex.length / 2)
for (let i = 0; i < hex.length; i += 2) {
view[i / 2] = parseInt(hex.substring(i, i + 2), 16)
}
return view.buffer
}
function bufToHex(buffer) { // buffer is an ArrayBuffer
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}
const uint8ArrayfromHex = hex =>
new Uint8Array(hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
function getUrlvars() {
let vars = {};
let parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, (m, key, value) => {
vars[key] = value;
});
return vars;
}
Array.prototype.flatMap = function (f) {
return this.reduce((acc, x) => acc.concat(f(x)), [])
}
const combinations = (choices, n = 1) =>
n === 0
? [[]]
: combinations(choices, n - 1).flatMap(comb =>
choices.map(c => [c, ...comb]));
document.addEventListener('DOMContentLoaded', () => {
const urlvars = getUrlvars();
if (Boolean(6) && Boolean(urlvars.dice_number)) {
let resultBin = '';
let readyBits;
let maindiv = document.getElementById('main').hidden = false;
let paramsdiv = document.getElementById('params').hidden = true;
const diceCombos = combinations(['1', '2', '3', '4', '5', '6'], urlvars.dice_number);
// Set count of dice entries
let dice = document.getElementById('dice');
for (let i = 1; urlvars.dice_number >= i; i++) {
dice.innerHTML += '<p>Dice #' + i + ': <input class="number_input" type="number" min="1" max="6" id="dice' + i + '" result"></input></p>'
}
// Get roll bits count
let rollCombos;
let rollBits = function () {
rollCombos = Math.pow(6, urlvars.dice_number);
let rollBitsExp2, rollBits;
for (let i = 1; rollCombos >= Math.pow(2, i); i++) {
rollBitsExp2 = Math.pow(2, i);
rollBits = i;
}
return rollBits;
}();
dice.innerHTML += '<button id="submit_dice">Submit</button>';
document.getElementById('submit_dice').addEventListener('click', function () {
let result = document.getElementById('result');
let diceRoll = [];
for (let i = 1; i <= urlvars.dice_number; i++) {
diceRoll.push(document.getElementById('dice' + i).value)
}
if (Math.min.apply(null, diceRoll) >= 1 && Math.max.apply(null, diceRoll) <= 6 && !('' in diceRoll)) {
for (let i = 1; i <= urlvars.dice_number; i++) {
document.getElementById('dice' + i).value = '';
}
let diceNumber;
for (let i = 0; i <= diceCombos.length - 1; i++) {
if (diceRoll[0] == diceCombos[i][0]
&& diceRoll[1] == diceCombos[i][1]
&& diceRoll[2] == diceCombos[i][2]
&& diceRoll[3] == diceCombos[i][3]) { diceNumber = i; break; };
}
if (diceNumber >= Math.pow(2, rollBits)) {
let generated_bits = document.getElementById('generated_bits');
let gen_progress = document.getElementById('gen_progress');
let readyBits = gen_progress.value;
resultBin += diceNumber % 2;
readyBits = readyBits + 1;
gen_progress.value = readyBits;
generated_bits.textContent = readyBits;
result.innerHTML = '<p><div style="color: green">Your roll value is larger than 2^' + rollBits + '. 1 bit (even, odd) submitted.</div></p>';
} else {
// Get binary result
let diceBin = diceNumber.toString(2);
while (diceBin.length < rollBits) {
diceBin = '0' + diceBin;
}
// Increase progress
let gen_progress = document.getElementById('gen_progress');
let readyBits = gen_progress.value;
if (readyBits + rollBits < 256) {
let generated_bits = document.getElementById('generated_bits');
readyBits = readyBits + rollBits;
gen_progress.value = readyBits;
generated_bits.textContent = readyBits;
resultBin += diceBin;
result.innerHTML = '<p><div style="color: green">Roll submitted.</div></p>';
} else {
resultBin += diceBin.slice(rollBits - (256 - resultBin.length));
let binArray = resultBin.match(/.{1,8}/g);
for (let i = 0; binArray.length >= i; i++) {
if (binArray[i]) binArray[i] = parseInt(binArray[i], 2);
}
hex = '80';
for (let i = 0; i < binArray.length; i++) {
newHex = binArray[i].toString(16);
if (newHex.length < 2) newHex = '0' + newHex;
hex += newHex;
}
while (hex.length < 66) hex += '0';
hex +='01';
let bArray, checksum;
async function dualSha256(hex) {
let hash1, hash2;
await crypto.subtle.digest('SHA-256', hexToArrayBuffer(hex)).then((res) => {
hash1 = bufToHex(res);
});
await crypto.subtle.digest('SHA-256', hexToArrayBuffer(hash1)).then((res) => {
hash2 = bufToHex(res);
});
return hash2.slice(null, 8)
}
dualSha256(hex).then((checksum) => {
const rArray = uint8ArrayfromHex(hex);
const cArray = uint8ArrayfromHex(checksum);
console.log(cArray);
let fArray = new Uint8Array(rArray.length + cArray.length);
fArray.set(rArray);
fArray.set(cArray, rArray.length);
if (!alert('Your private key in WIF: ' + to_b58(fArray, MAP) + '\nHex: ' + hex.slice(2, -2))) { window.location.reload(); };
});
}
}
} else {
result.innerHTML = '<p><div style="color: crimson">Error!</div></p>';
}
});
}
});
</script>
<style src="text/css">
h2 {
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
.number_input {
width: 4ch;
}
</style>
<title>Dicecoin</title>
<header>
<h2>Bitcoin private key generator using dice for entropy</h2>
</header>
<body>
<div id="params">
<form>
<p>Number of dice: <input class="number_input" name="dice_number" value="2" type="number" min="1"
max="4"></input></p>
<input type="submit" value="Let's start!"></input>
</form>
</div>
<div id="main" hidden>
<p>You have generated <label id="generated_bits">0</label> bits of 256.</p>
<progress id="gen_progress" value=0 max=256></progress>
<p>
<div style="color: crimson;">Warning!</div> For 100% quality of entropy please mark every your dice and
enter results in valid order.
</p>
<div id="dice"></div>
<div id="result"></div>
</div>
</body>
<footer>
<p> Platofff 2020<br>v0.0.2</p>
</footer>