-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
104 lines (78 loc) · 2.97 KB
/
app.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
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
'use strict'
/*
START OF DUMMY DATA
*/
// imports
import {Blockchain} from './blockchain.js';
import {Transaction} from './transaction.js';
import * as wallet from './wallet.js';
// if there are no blocks build a genesis block
export const blockchain = new Blockchain();
// create some wallets
const miningWallet = wallet.createWallet();
const walletOne = wallet.createWallet();
const walletTwo = wallet.createWallet();
const walletThree = wallet.createWallet();
console.log(`private key: ${walletOne.privateKey}`);
console.log(`public key: ${walletOne.publicKey}`);
// give all wallets a balance of 200
blockchain.mempool = [new Transaction('reward', walletOne.publicKey, 200), new Transaction('reward', walletTwo.publicKey, 200), new Transaction('reward', walletThree.publicKey, 200)];
// mine block with transactions
blockchain.buildNewBlock(miningWallet.publicKey);
// create some transactions
const tx1 = new Transaction(walletOne.publicKey, walletTwo.publicKey, 10);
tx1.signTransaction(walletOne.publicKey, walletOne.privateKey);
blockchain.addToMempool(tx1);
// mine block with transactions
blockchain.buildNewBlock(miningWallet.publicKey);
// create MORE transactions
const tx2 = new Transaction(walletThree.publicKey, walletOne.publicKey, 5);
tx2.signTransaction(walletThree.publicKey, walletThree.privateKey);
blockchain.addToMempool(tx2);
const tx3 = new Transaction(walletTwo.publicKey, walletThree.publicKey, 12);
tx3.signTransaction(walletTwo.publicKey, walletTwo.privateKey);
blockchain.addToMempool(tx3);
// mine block with transactions
blockchain.buildNewBlock(miningWallet.publicKey);
// add another transaction to the mempool
const tx4 = new Transaction(walletOne.publicKey, walletThree.publicKey, 1);
tx4.signTransaction(walletOne.publicKey, walletOne.privateKey);
blockchain.addToMempool(tx4);
/*
END OF DUMMY DATA
*/
// imports
import {Controller} from './controller.js';
import express from 'express';
// spin up the app
const app = express();
app.use(express.json());
app.use(express.urlencoded({extended: true}));
// grab endpoint behaviour from the controller
const controller = new Controller();
// get the whole chain
app.get('/blockchain', (req, res) => {
res.json(controller.getChain()).end();
});
// get pending transactions
app.get('/mempool', (req, res) => {
res.json(controller.getMempool()).end();
});
// get a specific block
app.get('/block/:hash', (req, res) => {
res.json(controller.getBlock(req.params.hash)).end();
});
// get a specific transaction
app.get('/transaction/:txid', (req, res) => {
res.json(controller.getTransaction(req.params.txid)).end();
});
// get amount of coins in wallet
app.get('/balance/:walletAddress', (req, res) => {
res.json(controller.getBalance(req.params.walletAddress)).end();
});
// post transaction
app.post('/transaction', (req, res) => {
res.json(controller.postTransaction(req.body.fromAddress, req.body.toAddress, req.body.amount, req.body.publicKey, req.body.privateKey)).end();
});
// exports
export {app};