-
Notifications
You must be signed in to change notification settings - Fork 3
/
block.js
60 lines (48 loc) · 1.64 KB
/
block.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
const SHA256 = require('crypto-js/sha256');
class Block {
constructor(timestamp, lastHash, hash, data, validator, signature,HTTP_PORT) {
this.timestamp = timestamp;
this.lastHash = lastHash;
this.hash = hash;
this.data = data;
this.validator = validator;
this.signature = signature;
this.HTTP_PORT = HTTP_PORT;
}
toString() {
return `Block -
Timestamp : ${this.timestamp}
Last Hash : ${this.lastHash}
Hash : ${this.hash}
Data : ${this.data}
Validator : ${this.validator}
Signature : ${this.signature}
Port : ${this.HTTP_PORT}`;
}
static genesis() {
return new this(`genesis time`, "----", "genesis-hash", []);
}
static hash(timestamp,lastHash,data){
return SHA256(`${timestamp}${lastHash}${data}`).toString();
}
static createBlock(lastBlock, data, wallet,HTTP_PORT) {
let hash;
let timestamp = Date.now();
const lastHash = lastBlock.hash;
hash = Block.hash(timestamp, lastHash, data);
// get the validators public key
let validator = wallet.getPublicKey();
// Sign the block
let signature = Block.signBlockHash(hash, wallet);
return new this(timestamp, lastHash, hash, data, validator, signature,HTTP_PORT);
}
static signBlockHash(hash, wallet) {
return wallet.sign(hash);
}
static blockHash(block){
//destructuring
const { timestamp, lastHash, data } = block;
return Block.hash(timestamp,lastHash,data);
}
}
module.exports = Block;