/
script.js
123 lines (102 loc) · 3.05 KB
/
script.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
const addButton = document.getElementById('add-button');
const mineButton = document.getElementById('mine-button');
const blockTxt = document.getElementById('block-txt');
const dataTxt = document.getElementById('data-input');
const hashTxt = document.getElementById('hash-info');
const prevHash = document.getElementById('p-hash');
const nonce = document.getElementById('nonce-info');
const allInput = document.querySelectorAll('input[type=text]');
const date = document.getElementById('date');
//particles.js object variables
window.onload = function() {
Particles.init({
selector: '.background',
maxParticles: 100,
color: ['#69ffb9', '#76ecfb', '#c1fda0', '#9386e6', '#f298f4'],
connectParticles: true
});
};
//variable for counting past Genesis block
let block = 1;
//holds previous hash values, defaulted to null in the event Add Block is pressed before current block is mined
let encryptArr = [null];
//holds and updates nonce for blocks
let nonceArr = [];
//sets first nonce if mine button is pressed without input to encrypt
nonceArr[0] = 2345;
//get the current date and time
const getTodayDate = () => {
let today = new Date();
let utcDate = today.toUTCString();
return utcDate;
}
//set the current date and time on the block(s)
const setDate = () => {
date.innerHTML = getTodayDate();
}
setDate();
//create new block and clear all previous data
const addBlock = () => {
let block = document.getElementById('box-wrapper');
let clone = block.cloneNode(true);
block.parentNode.insertBefore(clone, block.nextSibling);
dataTxt.disabled = false;
clearInputData();
}
//clear block data
const clearInputData = () => {
dataTxt.value = '';
hashTxt.value = '';
nonce.value = '';
}
//increment block numbers when new block is added
const changeBlockNum = () => {
blockTxt.innerHTML = 'Block #' + block++;
}
//encrypt text input field
const encryptData = (str) => {
str = dataTxt.value;
let encrypt = CryptoJS.SHA256(str);
let encryptStr = hashTxt.value = encrypt.toString();
encryptArr.push(encryptStr);
}
//push encrypted text in to array
const getHash = () => {
encryptArr[0] = hashTxt.value;
}
//set previous encrypted string to current previous hash field
const setPrevHash = () => {
prevHash.value = encryptArr[0];
encryptArr[0] = null;
}
//calculate the nonce (number only used once)
const calcNonce = () => {
str = '';
const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
while (str.length < 4) {
str += possible.charCodeAt(Math.floor(Math.random() * possible.length));
}
return str;
}
//assign calculated nonce in nonceArr
const setNonce = () => {
nonceArr[0] = calcNonce();
}
//set calculated nonce in array to nonce DOM element
const placeNonce = () => {
nonce.value = nonceArr[0];
}
mineButton.addEventListener('click', () => {
encryptData();
getHash();
placeNonce();
})
dataTxt.addEventListener('input', setNonce);
addButton.addEventListener('click', () => {
dataTxt.disabled = true;
addBlock();
setPrevHash();
changeBlockNum();
setDate();
setNonce();
})