-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
63 lines (52 loc) · 1.64 KB
/
controller.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
'use strict'
/*
Blockchain can only CR and not UD
*/
// imports
// import {Blockchain} from './blockchain.js';
import {blockchain} from './app.js';
import {Transaction} from './transaction.js';
// controller for managing endpoint behaviour
class Controller {
// return all of the blocks in the chain
getChain = () => {
return blockchain.chain;
}
// return the mempool (all pending transactions)
getMempool = () => {
return blockchain.mempool;
}
// return block by hash
getBlock = (hash) => {
return blockchain.getBlockWithHash(hash) || `Block with hash ${hash} not found`;
}
// return all transactions in a block
getTransactions = (hash) => {
return blockchain.getBlockWithHash(hash).transactions;
}
// return transaction by id (hash)
getTransaction = (txid) => {
for (const block of blockchain.chain){
for (const transaction of block.transactions) {
if (transaction.txid === txid) return transaction;
}
}
return `Transaction with id ${txid} not found`;
}
// return amount of coins in wallet
getBalance = (walletAddress) => {
return blockchain.getAddressBalance(walletAddress) || 0;
}
/*
THIS WILL BE MOVED INTO THE WALLET MICROSERVICE EVENTUALLY
*/
// send a transaction
postTransaction = (fromAddress, toAddress, amount, publicKey, privateKey) => {
const tx = new Transaction(fromAddress, toAddress, amount);
tx.signTransaction(publicKey, privateKey);
blockchain.addToMempool(tx);
return tx;
}
}
// exports
export {Controller};