-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.ts
39 lines (32 loc) · 951 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 } from './sha256';
interface BlockContent {
serial: number;
previousHash: string;
data: string; // ledger goes here
guess: string;
}
export class Block {
private content: BlockContent;
constructor(content: BlockContent) {
this.content = content;
}
public getRawHashee(guess: string): string {
this.content.guess = guess;
return JSON.stringify(this.content);
}
public hash = (guess: string): string => {
const toHash = this.getRawHashee(guess);
const hashed = Sha256.hash(toHash);
return hashed;
}
public verify = (difficulty: number, nextSerial: number, guess: string): boolean => {
if (this.content.serial !== nextSerial) {
return false;
}
const hashed = this.hash(guess);
if (hashed.startsWith(`0`.repeat(difficulty))) {
return true;
}
return false;
}
}