-
Notifications
You must be signed in to change notification settings - Fork 7
/
Block.ts
39 lines (34 loc) · 901 Bytes
/
Block.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
import SHA256 =require('crypto-js/sha256');
export class Block{
private index:number;
private timestamp:Date;
private data:any;
private precedentHash:any;
private hashBlock:any;
private nonce:number;
public constructor(index,data){
this.index=index;
this.timestamp=new Date();
this.data=data;
this.generateHash();
this.nonce=0;
}
public getHashBlock(){
return this.hashBlock;
}
public getPrecedentHash(){
return this.precedentHash;
}
public setPrecedentHash(hash:any){
this.precedentHash=hash;
}
public generateHash(){
this.hashBlock=SHA256(this.index+this.precedentHash+this.timestamp+this.data+this.nonce).toString();
}
public mineBlock(miningDifficulty:number){
while(this.hashBlock.substring(0,miningDifficulty)!=Array(miningDifficulty+1).join('0')){
this.nonce++;
this.generateHash();
}
}
}