-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBlockChain.ts
More file actions
34 lines (26 loc) · 876 Bytes
/
BlockChain.ts
File metadata and controls
34 lines (26 loc) · 876 Bytes
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
import { Block } from './Block';
export class BlockChain{
private chain:Array<Block>;
private miningDifficulty:number;
constructor(miningD:number){
this.chain=new Array<Block>(new Block(0,{ firstBlock:"Created By Aymen Naghmouchi"}));
this.miningDifficulty=miningD;
}
public getLastBlock():Block{
return this.chain[this.chain.length-1];
}
public addBlock(newBlock:Block){
newBlock.setPrecedentHash(this.getLastBlock().getHashBlock());
newBlock.mineBlock(this.miningDifficulty);
this.chain.push(newBlock);
}
public checkBlockChain():boolean{
for(let i:number=1;i<this.chain.length;i++){
let actualblock:Block=this.chain[i];
let precedentblock:Block=this.chain[i-1];
if(actualblock.getPrecedentHash()!==precedentblock.getHashBlock())
return false;
}
return true;
}
}