-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
151 lines (124 loc) · 3.35 KB
/
Copy pathindex.js
File metadata and controls
151 lines (124 loc) · 3.35 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
const SHA256 = require("crypto-js/sha256");
const figlet = require("figlet");
const chalk = require("chalk");
const ora = require('ora');
const Database = require("@replit/database");
const db = new Database();
console.log(chalk.bold.green('npm start'));
console.log('\n\n\n---\n\n\n');
console.log(chalk.cyanBright('ThedispardCoin\n'));
console.log(chalk.cyanBright('License MIT - 2021'));
console.log('\n\n\n---\n\n\n');
class CryptoBlock {
constructor(index, timestamp, data, precedingHash = " ") {
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.precedingHash = precedingHash;
this.hash = this.computeHash();
this.nonce = 0;
}
computeHash() {
return SHA256(
this.index +
this.precedingHash +
this.timestamp +
JSON.stringify(this.data) +
this.nonce
).toString();
}
proofOfWork(difficulty) {
console.log(chalk.yellowBright('Mining Block ⛏ ...'));
while (
this.hash.substring(0, difficulty) !== Array(difficulty + 1).join("0")
) {
this.nonce++;
this.hash = this.computeHash();
}
console.log(chalk.cyanBright(`Solved: ${this.nonce}`));
console.log(chalk.green(`Hash: ${this.hash}`))
}
}
class CryptoBlockchain {
constructor() {
this.blockchain = [this.startGenesisBlock()];
this.difficulty = 4;
}
startGenesisBlock() {
return new CryptoBlock(0, "11/06/2021", "Initial Block in the Chain", "0");
}
obtainLatestBlock() {
return this.blockchain[this.blockchain.length - 1];
}
addNewBlock(newBlock) {
newBlock.precedingHash = this.obtainLatestBlock().hash;
//newBlock.hash = newBlock.computeHash();
newBlock.proofOfWork(this.difficulty);
this.blockchain.push(newBlock);
console.log(newBlock);
console.log("\n");
}
checkChainValidity() {
for (let i = 1; i < this.blockchain.length; i++) {
const currentBlock = this.blockchain[i];
const precedingBlock = this.blockchain[i - 1];
if (currentBlock.hash !== currentBlock.computeHash()) {
return false;
}
if (currentBlock.precedingHash !== precedingBlock.hash) return false;
}
return true;
}
}
let thedispardCoin = new CryptoBlockchain();
let today = new Date();
let dd = today.getDate();
let mm = today.getMonth()+1;
let yyyy = today.getFullYear();
if(dd<10)
{
dd="0"+dd;
};
if(mm<10)
{
mm="0"+mm;
} ;
today = mm+"/"+dd+"/"+yyyy;
thedispardCoin.addNewBlock(
new CryptoBlock(1, today, {
sender: "Adrien Dumont",
recipient: "Gaspard Vandenbulcke",
quantity: 50
})
);
thedispardCoin.addNewBlock(
new CryptoBlock(2, today, {
sender: "Gaspard Vandenbulcke",
recipient: "Théo Gravé",
quantity: 100
})
);
thedispardCoin.addNewBlock(
new CryptoBlock(2, today, {
sender: "Théo Gravé",
recipient: "Adrien Dumont",
quantity: 100
})
);
let blockchain = JSON.stringify(thedispardCoin, null, 4);
console.log(`\n\n${chalk.cyanBright(JSON.stringify(thedispardCoin, null, 4))}\n\n`);
db.set("blockchain", blockchain).then(() => {});
figlet.text("ThedispardCoin", {
font: "Big",
horizontalLayout: "default",
verticalLayout: "default",
width: 55,
whitespaceBreak: true
}, (err, data) => {
if (err) {
console.log(chalk.bold.red("Error"));
console.dir(err);
return;
}
console.log(chalk.yellowBright(data));
});