-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
112 lines (88 loc) · 2.71 KB
/
index.ts
File metadata and controls
112 lines (88 loc) · 2.71 KB
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
import * as crypto from 'crypto';
class Transaction {
constructor(
public amount: number,
public payer: string, // public key
public payee: string, // private key
) { }
toString() {
return JSON.stringify(this);
}
}
class Block {
nonce = Math.round(Math.random() * 999999999);
constructor(
public previousHash: string,
public transaction: Transaction,
public timestamp = Date.now()
) { }
get hash() {
const str = JSON.stringify(this);
const hash = crypto.createHash('SHA256');
hash.update(str).end();
return hash.digest('hex');
}
}
class Chain {
static instance = new Chain(); // Singleton
chain: Block[];
constructor() { // set the genesis block
this.chain = [new Block(null, new Transaction(100, 'genesis', 'satoshi'))];
}
get previousBlock() {
return this.chain[this.chain.length - 1];
}
addBlock(transaction: Transaction, senderPublicKey: string, signature: Buffer) {
const verifier = crypto.createVerify('SHA256');
verifier.update(transaction.toString());
const isValid = verifier.verify(senderPublicKey, signature);
if (isValid) {
const newBlock = new Block(this.previousBlock.hash, transaction);
this.mine(newBlock.nonce);
this.chain.push(newBlock);
}
}
mine(nonce: number) {
let solution = 1;
console.log('Mining... ⛏');
while(true) {
// MD5 is very similar to SHA256, but only 128 bits and faster to compute
const hash = crypto.createHash('MD5');
hash.update((nonce + solution).toString()).end();
const attempt = hash.digest('hex');
if (attempt.substr(0, 4) === '0000') {
console.log(`Solved: ${solution}`);
return solution;
}
++solution;
}
}
}
class Wallet {
public publicKey: string;
public privateKey: string;
constructor() {
const keyPair = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: { type: 'spki', format: 'pem' },
privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
});
this.publicKey = keyPair.publicKey;
this.privateKey = keyPair.privateKey;
}
sendCrypto(amount: number, payeePublicKey: string) {
const transaction = new Transaction(amount, this.publicKey, payeePublicKey);
const sign = crypto.createSign('SHA256');
sign.update(transaction.toString()).end();
const signature = sign.sign(this.privateKey);
Chain.instance.addBlock(transaction, this.publicKey, signature);
}
}
// Example usage
const satoshi = new Wallet();
const jake = new Wallet();
const gabi = new Wallet();
satoshi.sendCrypto(50, gabi.publicKey);
gabi.sendCrypto(30, jake.publicKey);
jake.sendCrypto(5, gabi.publicKey);
console.log(Chain.instance);