-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
63 lines (51 loc) · 1.43 KB
/
index.ts
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
// https://medium.com/the-capital/building-a-simple-blockchain-in-30-lines-of-typescript-9bc2963223fc
// https://github.com/soyjavi/vanilla-blockchain
import { SHA256 } from "crypto-js";
type Block = {
data: string;
timestamp: number;
nonce: number;
hash?: string;
previousHash: string;
};
let blockchain3: Block[] = [];
// The genesis block
// https://twitter.com/OnePeopei/status/1549726868081094656
blockchain3.push({
data: "ブロックチェーンで、OSは、作れますよ。",
timestamp: 1658351040,
nonce: 0,
hash: "7b160e1f030b8929bf48b0a2774942a23ab0e4a7e428da499c058e01924d6960",
previousHash: "",
});
const getBlockHash = (block: Block) => {
const blockValue =
block.previousHash +
block.timestamp +
JSON.stringify(block.data) +
block.nonce;
return SHA256(blockValue).toString();
};
export const mineNewBlock = (data: string, difficulty = 1) => {
const previousBlock = blockchain3[blockchain3.length - 1];
const previousHash = getBlockHash(previousBlock);
let nonce = 0;
let timestamp = new Date().getTime();
while (true) {
timestamp = new Date().getTime();
const block: Block = {
data,
timestamp,
nonce,
previousHash,
};
const blockHash = getBlockHash(block);
if (blockHash.startsWith(Array(difficulty + 1).join("0"))) {
block.hash = blockHash;
blockchain3.push(block);
break;
}
nonce += 1;
}
return blockchain3;
};