-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
191 lines (161 loc) · 5.62 KB
/
index.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
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
const sha256 = require("crypto-js/sha256");
const EC = require("elliptic").ec;
var ec = new EC("secp256k1");
//create Block
class Block {
constructor(timestamp,transactions,previousHash=""){
this.timestamp = timestamp;
this.transactions = transactions;
this.previousHash = previousHash;
this.hash = this.calculateHash();
this.nonce=0;
}
mineBlock(difficulty){
while(this.hash.substring(0,difficulty) !== Array(difficulty+1).join("0")){
this.nonce++;
this.hash=this.calculateHash();
}
//console.log("Mine Done: ",this.hash);
}
calculateHash(){
return sha256 (this.timestamp +
JSON.stringify(this.transactions) +
this.previousHash +
this.nonce).toString();
}
isValidTransaction(){
for(const tx of this.transactions){
if(tx.isValid === false){
return false;
}
}
return true;
}
}
//create Transaction
class Transaction{
constructor(fromAddress,toAddress,amount){
this.fromAddress=fromAddress;
this.toAddress=toAddress;
this.amount=amount;
}
/*-------------Making signature and key---------------*/
calculateHash(){
return sha256 (this.fromAddress + this.toAddress + this.amount).toString;
}
signTransaction(key){
if(key.getPublic("hex") !== this.fromAddress){
throw new Error("You do not have access!");
}
const hasTX = this.calculateHash();
const signature = key.sign(hasTX,"base64");
this.signature = signature.toDER();
}
isValid(){
if(this.fromAddress === null){
return true; // miner get reward from system
}
if(!this.signature || this.signature.length ===0){
throw new Error("No Signature Found!");
}
const key=ec.keyFromPrivate(this.fromAddress,"hex");
return key.verify(this.calculateHash(),this.signature);
}
}
//create BlockChain
class BlockChain {
constructor(){
this.chain = [this.generateGenesisBlock()]; // initialize array with first element
this.difficulty = 2; //declare by default difficulty
this.pendingTransactions = [];
this.miningReward=10;
}
generateGenesisBlock(){ //create first block
return new Block("2023-02-28","Genesis","0000");
}
getHashofLastBlock(){
return this.chain[this.chain.length-1];
}
addTransaction(transactionObj){
if(!transactionObj.fromAddress || !transactionObj.toAddress){
throw new Error("Can't Process Transaction!");
}
if(!transactionObj.isValid()){ //! Get Error
//throw new Error("Transaction is not valid!");
}
if(transactionObj.amount < 0){
throw new Error("Invalid Transaction Amount!");
}
if(transactionObj.amount>this.getBalanceOfAddress(this.fromAddress)){
throw new Error("Not have enough balance!");
}
this.pendingTransactions.push(transactionObj);
}
minePendingTransaction(minerAddress){
let block = new Block(Date.now(),this.pendingTransactions);
block.mineBlock(this.difficulty);
this.chain.push(block);
this.pendingTransactions = [new Transaction(null,minerAddress,this.miningReward)];
}
/*addBlock(newBlock){
newBlock.previousHash=this.getHashofLastBlock().hash;
//newBlock.hash=newBlock.calculateHash();
newBlock.mineBlock(this.difficulty);
this.chain.push(newBlock);
}*/
checkValidationofBlock(){
for(let i=1;i<this.chain.length;i++){
const currentBlock = this.chain[i];
const previousBlock = this.chain[i-1];
if(currentBlock.hash !== currentBlock.calculateHash()){
return false;
}
if(currentBlock.previousHash !== previousBlock.hash){
return false;
}
if(!currentBlock.isValidTransaction()){
return false;
}
}
return true;
}
//In one chain, there are many blocks and in one block there are many transactions
getBalanceOfAddress(address){
let balance = 0;
for(const block of this.chain){
for(const transaction of block.transactions){
if(transaction.fromAddress === address){
balance = balance - transaction.amount;
}
if(transaction.toAddress === address){
balance = transaction.amount + balance;
}
}
}
return balance;
}
}
/*const josscoin = new BlockChain();
const block = new Block("2023-02-28",{amount: 5});
josscoin.addBlock(block);
const block2 = new Block("2023-02-22",{amount: 10});
josscoin.addBlock(block2);
josscoin.createTransaction(new Transaction("sami","biva",100)); //sami=> -100+70=-30
josscoin.createTransaction(new Transaction("biva","sami",70)); //biva=> 100 -70 = 30
josscoin.minePendingTransaction("azraf");
console.log(josscoin);
console.log(josscoin.getBalanceOfAddress("azraf"));
console.log(josscoin.getBalanceOfAddress("sami"));
console.log(josscoin.getBalanceOfAddress("biva"));
josscoin.minePendingTransaction("azraf");
console.log(josscoin.getBalanceOfAddress("azraf"));*/
//console.log("After Invalid Change of Data");
//josscoin.chain[1].data="HACKED";
//console.log(josscoin.checkValidationofBlock());
//console.log(Array(6).join("0"));
//class exports
module.exports = {
Block,
Transaction,
BlockChain
};