-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErenChain.js
154 lines (126 loc) · 4.71 KB
/
ErenChain.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
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
const crypto = require("crypto"), SHA256 = message => crypto.createHash("sha256").update(message).digest("hex");
const EC = require("elliptic").ec, ec = new EC("secp256k1");
const MINT_PRIVATE_ADDRESS = "0700a1ad28a20e5b2a517c00242d3e25a88d84bf54dce9e1733e6096e6d6495e";
const MINT_KEY_PAIR = ec.keyFromPrivate(MINT_PRIVATE_ADDRESS, "hex");
const MINT_PUBLIC_ADDRESS = MINT_KEY_PAIR.getPublic("hex");
class Block {
constructor(timestamp = "", data = []) {
this.timestamp = timestamp;
this.data = data;
this.hash = Block.getHash(this);
this.prevHash = "";
this.nonce = 0;
}
static getHash(block) {
return SHA256(JSON.stringify(block.data) + block.timestamp + block.prevHash + block.nonce);
}
mine(difficulty) {
while(!this.hash.startsWith(Array(difficulty + 1).join("0"))) {
this.nonce++;
this.hash = Block.getHash(this);
}
}
static hasValidTransactions(block, chain) {
let gas = 0, reward = 0;
block.data.forEach(transaction => {
if (transaction.from !== MINT_PUBLIC_ADDRESS) {
gas += transaction.gas;
} else {
reward = transaction.amount;
}
});
return (
reward - gas ===chain.reward &&
block.data.every(transaction => Transaction.isValid(transaction, chain)) &&
block.data.filter(transaction => transaction.from === MINT_PUBLIC_ADDRESS).length ===1
);
}
}
class Blockchain {
constructor() {
const initalCoinRelease = new Transaction(MINT_PUBLIC_ADDRESS, "04719af634ece3e9bf00bfd7c58163b2caf2b8acd1a437a3e99a093c8dd7b1485c20d8a4c9f6621557f1d583e0fcff99f3234dd1bb365596d1d67909c270c16d64", 100000000);
this.chain = [new Block("", [initalCoinRelease])];
this.difficulty = 1;
this.blockTime = 30000;
this.transactions = [];
this.reward = 297;
}
getLastBlock() {
return this.chain[this.chain.length - 1];
}
getBalance(address) {
let balance = 0;
this.chain.forEach(block => {
block.data.forEach(transaction => {
if (transaction.from === address) {
balance -= transaction.amount;
balance -= transaction.gas;
}
if (transaction.to === address) {
balance += transaction.amount;
}
})
})
return balance;
}
addBlock(block) {
block.prevHash = this.getLastBlock().hash;
block.hash = Block.getHash(block);
block.mine(this.difficulty);
this.chain.push(block);
this.difficulty += Date.now() - parseInt(this.getLastBlock().timestamp) < this.blockTime ? 1 : -1;
}
addTransaction(transaction) {
if (Transaction.isValid(transaction, this)){
this.transactions.push(transaction);
}
}
mineTransaction(rewardAddress) {
let gas = 0;
this.transactions.forEach(transaction => {
gas += transaction.gas;
})
const rewardTransaction = new Transaction(MINT_PUBLIC_ADDRESS, rewardAddress, this.reward + gas);
rewardTransaction.sign(MINT_KEY_PAIR);
if(this.transactions.length !== 0) this.addBlock(new Block(Date.now().toString(), [rewardTransaction, ...this.transactions]));
this.transactions = [];
}
static isValid(blockchain) {
for (let i = 1; i < blockchain.chain.length; i++) {
const currentBlock = blockchain.chain[i];
const prevBlock = blockchain.chain[i-1];
if (
currentBlock.hash !== Block.getHash(currentBlock) ||
prevBlock.hash !== currentBlock.prevHash ||
!Block.hasValidTransactions(currentBlock, blockchain)
) {
return false;
}
}
return true;
}
}
class Transaction {
constructor(from, to, amount, gas = 0) {
this.from = from;
this.to = to;
this.amount = amount;
this.gas = gas;
}
sign(keyPair) {
if (keyPair.getPublic("hex") === this.from) {
this.signature = keyPair.sign(SHA256(this.from + this.to + this.amount + this.gas),"base64").toDER("hex");
}
}
static isValid(tx, chain) {
return(
tx.from &&
tx.to &&
tx.amount &&
(chain.getBalance(tx.from) >= tx.amount + tx.gas || tx.from === MINT_PUBLIC_ADDRESS ) &&
ec.keyFromPublic(tx.from, "hex").verify(SHA256(tx.from + tx.to + tx.amount + tx.gas), tx.signature)
)
}
}
const ErenChain = new Blockchain();
module.exports = {Block, Blockchain, ErenChain, Transaction};