-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnonce.html
More file actions
534 lines (490 loc) · 17.2 KB
/
nonce.html
File metadata and controls
534 lines (490 loc) · 17.2 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<!DOCTYPE html>
<html lang="en">
<head>
<title>Mining Simulator</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<base target="_blank"/>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="files/confetti.browser.min.js"></script>
<link rel="stylesheet" href="files/style.css">
<style>
@import url('https://fonts.googleapis.com/css2?family=Manrope:wght@300;400;500;600;700;800&display=swap');
@import url('https://fonts.googleapis.com/css2?family=PT+Sans+Narrow:wght@400;700&family=Roboto:wght@300;400;500&display=swap');
@import url('https://fonts.googleapis.com/css2?family=PT+Mono&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Mulish:ital,wght@0,300;0,400;0,600;0,700;0,800;1,400;1,600;1,700&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Mr+Dafoe&display=swap');
</style>
<style>
body {
background-color:black;
font-family: 'Manrope', sans-serif;
/* background:linear-gradient(68.78deg,#ff6ab4 -5.3%,#ffd569 124.36%); */
}
a {
font-weight:bold;
text-decoration:underline;
}
.divider {
text-align:center;
width:175px;
padding-bottom:15px!important;
border-bottom:1px solid white;
margin-bottom:15px!important;
margin:0 auto;
}
.gradient {
-webkit-background-clip: text;
background: linear-gradient(68.78deg,#175d6a -5.3%,#ffffff 124.36%)!important;
font-weight:600!important;
}
.my-button {
color:black;
background-color:#9dcdd6;
-webkit-background-clip: text;
background: linear-gradient(68.78deg,#175d6a -5.3%,#ffffff 124.36%)!important;
font-weight:600!important;
margin-top:6px!important;
width:calc(100% - 50px);
padding-left:8px!important;
margin-right:20px!important;
margin-left:11px!important;
margin-bottom:16px;
}
input:focus,
select:focus,
textarea:focus,
button:focus {
outline: none;
}
#close, #showMore, #my-logo {
cursor:pointer;
}
.code {
font-family: 'PT Mono', monospace;
font-size:14px;
}
#my-nonce {
font-weight:bold;
}
#main-div {
background-color:black; border:1px solid black; text-align:center; margin:0 auto;
}
input {
width:150px;
padding-left:20px!important;
}
input, button {
-webkit-border-radius:999px;
border-radius:999px;
border:0px;
font-size: inherit;
padding: 0.6em;
margin: 0.1em 0.2em;
-moz-box-sizing: content-box;
-webkit-box-sizing: content-box;
box-sizing: content-box;
}
button {
margin-left:-50px;
font-weight:500;
padding-left:20px;
padding-right:20px;
background-color:#9dcdd6!important;
}
.headers {
font-weight:800;
text-align:center;
margin-bottom:20px;
font-size:20px;
}
.gradient-text {
background: -webkit-linear-gradient(68.78deg,#b1000b -5.3%,#91c1ca 124.36%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
color:#e82838!important;
width:240px;
font-size:18px;
margin:0 auto;
margin-bottom:20px;
letter-spacing:1px;
text-transform:uppercase;
font-family: 'Mulish', sans-serif;
font-weight:700;
}
footer {
line-height:18px;
margin-top:-20px;
}
footer a {
color:white;
font-weight:500!important;
text-decoration:none;
}
footer a:hover {
color:white;
font-weight:500!important;
text-decoration:underline;
}
h1 {
text-shadow: 4px 4px 6px #4b4b4b;
}
#instructions-text b {
background: -webkit-linear-gradient(68.78deg,#b1000b -5.3%,#91c1ca 124.36%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
-webkit-box-decoration-break: clone;
font-weight:800;
}
/* Background medium view */
@media (min-width:631px) and (max-width: 985px) {
body {
background-size:cover!important;
}
}
/* Desktop View */
@media (min-width:631px) {
body {
background:url('images/grid-background-sm.png');
background-size:120% auto;
background-attachment:fixed;
}
#made-with {
margin-top:100px!important;
}
#instructions-text {
padding-left: 45px!important;
padding-right:45px!important;
}
.my-button {
font-weight:bold;
margin-top:6px!important;
width:300px;
padding-left:8px!important;
margin-right:20px!important;
margin-left:20px!important;
}
}
/* Mobile View */
@media (max-width:630px) {
body {
background-color:black;
}
#main-div {
max-width:320px;
}
h1 span {
font-size:42px!important;
}
h3 {
font-size:18px;
}
input {
width:calc(100% - 78px);
}
}
#logo {
width:250px;
margin-top:43px;
margin-bottom:40px;
}
</style>
</head>
<body>
<div class="container" style="max-width:625px; width:100%; margin-top:0px">
<div id="main-div">
<div>
<img src=images/Mining-logo.png style="width:450px; max-width:100%; padding-top:40px; background-color:black; padding-bottom:10px" id="my-logo">
</div>
<div class="office-banner" style="display:none">
<h1>
<span style="font-family: 'Mr Dafoe', cursive; color:#e71325; font-size:45px">You found the nonce!<span> <img src=https://i.imgur.com/kFfPU9D.png width=20 id="close" onclick="$('.office-banner').hide(); $('canvas').hide()">
</h1>
<div style="font-size:25px; margin-top:-15px; letter-spacing:2px">LUCKY NUMBER <span id="winner"></span></div>
</div>
<div id="instructions" style="position:relative; z-index:1;">
<div id="instructions-text" style="position:relative; z-index:1; color:white; margin-left: -1px; margin-right: -1px; margin-top: -1px; padding:15px; padding-top:30px; text-align:justify;">
<div class="headers gradient-text" style="width:110%; margin-left:-5%">YOUR MISSION, SHOULD YOU CHOOSE TO ACCEPT IT</div>
Bitcoin mining is simpler than you'd think! It's a guess and check game of searching for a magic number that when plugged into a block produces a valid <b>proof of work</b>.
<br><br>
The block goes through a <b>hash function</b>, reducing it into a fingerprint or summary. If that fingerprint (a number) is less than another number (called a <b>difficulty target</b>), the block is valid and we get rewarded with the glorious privilege of writing it to the immutable bitcoin blockchain. <!--This process can't be faked, keeping bitcoin honest forever, Laura!--> <!--Oh yeah, we also get 6.25 BTC!-->
<br><br>
For this challenge, we're going to guess that magic number, which has a special name: a <b>nonce</b>, or a number use once.
<br>
</div>
<center><br>
<button class="my-button" value="Verify My Node" id="next-page" style="letter-spacing:1.2px">LET'S MINE BITCOIN</button>
</div>
<div style="display:none" id="contents"> <!--div for the content that will be swapped out -->
<div style="color:white; margin-left: -1px; margin-right: -1px; margin-top: -1px; padding:15px; padding-top:30px; text-align:center;">
<div class="headers gradient-text">Bitcoin Block Header</div>
← <i>previous block hash</i><br>
<span class="code">00000000000000000007a63643ce3b8d0b9a28fde9084f99af08a9b8637cf16f</span><br>
<div class="divider"></div>
<i>difficulty target</i><br>
<span id="target" class="code"></span><br>
<div class="divider"></div>
<div id="more" style="display:none;">
<i>merkle root hash</i><br>
<span class="code">9d10aa52ee949386ca9385695f04ede270dda20810decd12bc9b048aaab31471</span><br>
<div class="divider"></div>
<i>timestamp</i>: <span id="epoch" class="code"></span><br>
<div class="divider"></div>
<i>version</i>: <span class="code">294ce004</span><br>
<div class="divider"></div>
</div>
<i><span onclick="$('#more').show(); $(this).hide();" id="showMore"><b>...more block metadata</b><br><br></span></i>
<!-- nonce can be 32 bits, largest unsigned integer is 4,294,967,295, around 4 billion values
technically, once these are quickly exhausted, the coinbase tx can be changed thus changing the merkle root in the block header
-->
<input type="tel" onkeypress="return event.charCode >= 48 && event.charCode <= 57" max=4294967295 min=0 id="nonce-input"
oninput="if (this.value < 0) this.value = 0; if (this.value > 4294967295) this.value = 4294967295" placeholder="Nonce"></input>
<button onclick="tryNonce()" style="color:black; background:#5ddd9d" class="gradient">Hash</button>
</div>
<div id="results-block" style="display:none; padding-top: 20px; padding-bottom: 20px">
<div class="jagged-border-top"></div>
<div style="color:black; background-color:white; padding:15px; padding-top:35px; padding-bottom:26px">
<div class="headers" style="color:black!important">Hash Result</div>
<i><div style="margin-bottom:10px;" id="hash-header"></div></i>
<span id="hash" class="code"></span>
<div style="margin-top:10px; margin-bottom:10px;"><i>is<span id="result-area" style="font-size:18px"></span> the target of</i></div>
<div style=""><span id="target-compare" class="code"></span></div>
<div style="display:none" id="rate"><br><i>Your hashrate</i>: <span id="hashrate" class="code"></span> hashes / second</div>
<b><div id="message" style="font-size:18px; padding-top:23px"></div></b>
<img id="celebrate" src=https://c.tenor.com/XspKDzYsphsAAAAC/bitcoin.gif style="max-width:100%; display:none; margin-top:28px;">
</div>
<div class="jagged-border-top-inverse"></div>
</div> <!-- closed results-block -->
<div style="color:white; padding-bottom:10px; text-align:center; z-index:1; position: relative">
<div style="background-color:black; color:white; padding:15px; padding-top:15px; padding-bottom:5px; padding-right:5px; border:1px solid black;"></div>
<div class="headers gradient-text" style="padding-top:14px">Transactions</div>
<center>
Transaction #1<br>
Transaction #2<br>
Transaction #3<br>
...<br>
Transaction #n
</div>
</div> <!-- end main div -->
<br><br><br>
<div style="margin-top:0px!important; z-index:0; position: relative;">
<img src=images/Mountain.png style="width:318px; -webkit-transform: scaleX(-1); transform: scaleX(-1)">
</div>
<div style="margin-top:-11px">
<img src=images/White-line.png style="width:400px; max-width:100%">
</div>
</div> <!-- end contents block -->
<footer style="font-weight:400; font-size:16px; padding-bottom:23px">
<div style="margin-bottom:14px; margin-top:14px!important" id="made-with">Made with <!--♥--><span style="color:#e8000d">❤</span> by <a href=https://twitter.com/d_plus__plus target="_blank">D++</a></div>
<div style="font-size:13px"><script src="files/my-footer.js"></script></div>
</footer>
</div> <!-- end container -->
<script>
// global variables
var confettiOn = false;
var colors = ["#b1000b", "#91c1ca"];
var hashRate = false;
var startTime;
var prevNonce;
var end;
var salt = "pacman!!!";
var difficulty;
var URL = location.href.split("?");
difficulty = URL[1];
if (!URL[1]) {
difficulty = "Error: No Difficulty Target is Set";
}
$('#target').html(difficulty);
function compareBig(hash, target) {
// expects two 32 byte buffers
// if the hash is greater than the target, return false, meaning if the hash equals the target we have found a block
for (var i = 0; i < 32; i++) {
if (hash[i] > target[i])
return false;
if (hash[i] < target[i])
return true;
}
return true;
}
function hex2buf(string) {
var result = new Uint8Array(32);
var hexCounter = 0;
for (var i = 0; i < 32; i++) {
var hex = string.substring(hexCounter, hexCounter + 2);
result[i] = parseInt(Number("0x" + hex), 10);
hexCounter += 2;
}
return result;
}
/*************************************************************************************/
// candidate block == block in progress being constructed / mined
// technically the bitcoin block header is 80 bytes
// it contains 6 different data fields in little endian format
// bitcoin mostly uses little endian. networking usually uses big endian
// big endian = most significant bytes are first (human readable)
// little endian = lest significant bytes are first (not human readable, it's reversed)
// mining actually uses double sha!
// extraNonce parameters usually go in the script signature (unlocking script)
// of the coinbase transaction. it is not formally defined, however many mining
// pools use an extraNonce of 8 bytes. this can vary from pool to pool.
// block weight must be under 4 MB
// ONE S19 can exhaust the entire 32 bit nonce search space in under 1 millisecond!!!
/*************************************************************************************/
async function simpleSha(message, salt) {
// encode as UTF-8
const msgBuffer = new TextEncoder().encode(message + salt);
// hash the message
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
// convert ArrayBuffer to Array
const hashArray = Array.from(new Uint8Array(hashBuffer));
// convert bytes to hex string
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex;
}
function frame() {
confetti({
particleCount: 2,
angle: 60,
spread: 55,
origin: { x: 0 },
colors: colors,
});
confetti({
particleCount: 2,
angle: 120,
spread: 55,
origin: { x: 1 },
colors: colors,
});
if (Date.now() < end) {
requestAnimationFrame(frame);
}
else {
confettiOn = false;
}
}
function goldenNonce() {
$('.office-banner').show();
if (!confettiOn) {
end = Date.now() + 5000;
frame();
$('canvas').show();
confettiOn = true;
}
}
async function tryNonce() {
const nonce = $('#nonce-input').val();
const difficultyBuffer = hex2buf(difficulty);
let nonceHash = await simpleSha(nonce, salt);
$('#hash').html(nonceHash);
$('#target-compare').html(difficulty);
if (nonce == "") {
$('#hash-header').html("block header hash");
}
else {
$('#hash-header').html('block hash with nonce: <span class="code" id="my-nonce"></span>');
$('#my-nonce').html(nonce);
}
$("#my-nonce").hide();
$("#my-nonce").fadeIn(400);
$('#hash').hide();
$('#hash').fadeIn(400);
$('#nonce-input').val("");
if (compareBig(hex2buf(nonceHash), difficultyBuffer)) {
$('#result-area').html("<i class='gradient-text'><b> less than</b></i> ");
$('#celebrate').show();
$('#message').hide();
$('#winner').html(nonce);
goldenNonce();
}
else {
$('#result-area').html("<i class='gradient-text'><b> not less than</b></i> ");
$('#message').html("<i class='gradient-text'>TRY AGAIN!</i> 🙅♀️");
$('#celebrate').hide();
$('#message').show();
}
// calculate the hashrate
if (!hashRate) {
// first time calculating hashrate, so let's set the start time to now
startTime = $('#epoch').html();
hashRate = 1;
}
else if (nonce != prevNonce){
// only increment if current hash is not the same as previous hash
var currentTime = $('#epoch').html();
hashRate++;
$('#hashrate').html((hashRate / (currentTime - startTime)).toFixed(2));
$('#rate').show();
}
prevNonce = nonce;
$('#results-block').show();
doLines();
}
function autoNonce() {
for (var counter = 0; counter < 100000; counter++) {
$('#nonce-input').val(counter);
tryNonce();
}
}
//autoNonce();
// Try nonce on enter
var input = document.getElementById("nonce-input");
input.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
tryNonce();
}
});
$('#next-page').click(function() {
$('#instructions').hide();
$('#contents').show();
document.body.scrollTop = document.documentElement.scrollTop = 0;
});
$('#my-logo').click(function() {
$('#instructions').show();
$('#contents').hide();
document.body.scrollTop = document.documentElement.scrollTop = 0;
});
// Show timestamp couter
function timestamp() {
const d = new Date();
let time = d.getTime();
$('#epoch').html(parseInt(time/1000))
setTimeout(timestamp, 1000);
}
timestamp();
// On mobile, break lines of hashes exactly halfway through
function breakLines() {
$('.code').each(function() {
var html = $(this).html();
if (html.length > 32 && html.includes("<br>") == false) {
html = html.substring(0, 32) + "<br>" + html.substring(32);
$(this).html(html);
}
});
}
function glueLines() {
$('.code').each(function() {
var html = $(this).html();
html = html.replace('<br>','');
$(this).html(html);
});
}
function doLines() {
var width = $(window).width();
if (width <= 630) {
breakLines();
}
else if (width > 630) {
glueLines();
}
}
$(window).resize(function() {
doLines();
});
doLines();
</script>
</body>
</html>