-
Notifications
You must be signed in to change notification settings - Fork 37
/
cli.ts
executable file
·228 lines (211 loc) · 5.42 KB
/
cli.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env ./node_modules/.bin/ts-node
import Peer from "./src/Peer";
import Message from "./src/Message";
import MessageType from "./src/MessageType";
import MessageCreator from "./src/MessageCreator";
import { List } from "immutable";
import Block from "./src/Block";
import Payment from "./src/Payment";
import Transaction from "./src/Transaction";
const peer = new Peer();
const vorpal = require("vorpal")();
import * as util from "util";
vorpal
.use(connect)
.use(discover)
.use(blockchain)
.use(peers)
.use(mine)
.use(open)
.use(transaction)
.use(newWallet)
.use(getPublicKey)
.use(pay)
.use(balance)
.use(welcome)
.delimiter("coin >")
.show();
// COMMANDS
function welcome(vorpal) {
this.log("Welcome to Coin CLI!");
vorpal.exec("help");
}
function connect(vorpal) {
vorpal
.command(
"connect <host> <port>",
"Connect to a new peer with <host> and <port>."
)
.alias("c")
.action((args, callback) => {
try {
peer.connectToPeer(args.host, args.port);
} catch (err) {
this.log(err);
} finally {
callback();
}
});
}
function discover(vorpal) {
vorpal
.command("discover", "Discover new peers from your connected peers.")
.alias("d")
.action((args, callback) => {
try {
peer.discoverPeers();
} catch (err) {
this.log(err);
} finally {
callback();
}
});
}
function blockchain(vorpal) {
vorpal
.command("blockchain", "See the current state of the blockchain.")
.alias("bc")
.action((args, callback) => {
this.log(peer.blockchain.toString());
callback();
});
}
function peers(vorpal) {
vorpal
.command("peers", "Get the list of connected peers.")
.alias("p")
.action((args, callback) => {
peer.connectedPeers.forEach(peer => {
this.log(`${JSON.stringify(peer.pxpPeer.socket.address())}\n`);
}, this);
callback();
});
}
function mine(vorpal) {
vorpal
.command("mine [address]", "Mine a new block with rewards going to optional [address].")
.alias("m")
.action((args, callback) => {
try {
peer.mine();
const latestBlock: Block = peer.blockchain.latestBlock
const blockMessage: Message = MessageCreator.sendLatestBlock(latestBlock);
peer.broadcast(blockMessage);
const txsToClear: List<Transaction> = latestBlock.transactions;
txsToClear.forEach(tx => peer.broadcast(MessageCreator.sendRemovedTransaction(tx)));
} catch (e) {
if (e instanceof TypeError) {
handleTypeError.call(this, e);
} else {
this.log(e);
}
} finally {
callback();
}
});
}
function open(vorpal) {
vorpal
.command("open <port>", "Open port <port> to accept incoming connections.")
.alias("o")
.action((args, callback) => {
try {
peer.startServer(args.port);
this.log(`Listening to peers on ${args.port}`);
} catch (err) {
this.log(err);
} finally {
callback();
}
});
}
function transaction(vorpal) {
vorpal
.command("transactions", "See unconfirmed transactions that can be mined.")
.alias("tx")
.action((args, callback) => {
this.log(peer.mempool.toString());
callback();
});
}
function newWallet(vorpal) {
vorpal
.command("wallet <password>", "Create a new wallet with <password>")
.alias("w")
.action((args, callback) => {
args.password = args.password.toString();
peer.newWallet(args.password);
const hidePass = args.password.replace(/./g, "*");
this.log(`Created new wallet with password ${hidePass}.\n`);
this.log(`Address: ${peer.wallet.publicKey}\n`);
callback();
});
}
function getPublicKey(vorpal) {
vorpal
.command("key", "Get your public key")
.alias("k")
.action((args, callback) => {
try {
this.log(peer.wallet.publicKey);
} catch (e) {
if (e instanceof TypeError) {
handleTypeError.call(this, e);
} else {
this.log(e);
}
} finally {
callback();
}
});
}
function pay(vorpal) {
vorpal
.command(
"pay <address> <amount> <fee> <password>",
"Make a payment to <address> with <amount> and <fee> using wallet <password>"
)
.action((args, callback) => {
const address = args.address;
const amount = args.amount;
const password = args.password;
const fee = args.fee;
try {
const transaction = peer.createTransaction(
List([{ amount, address, fee }]),
password
);
peer.mempool.addTransaction(transaction);
const action: Message = MessageCreator.sendLatestTransaction(transaction);
peer.broadcast(action);
} catch (err) {
this.log(err);
} finally {
callback();
}
});
}
function balance(vorpal) {
vorpal
.command("balance [address]", "Balance of optional [address]")
.alias("b")
.action((args, callback) => {
try {
this.log(peer.getBalance(args.address))
} catch (e) {
if (e instanceof TypeError) {
handleTypeError.call(this, e);
}
} finally {
callback();
}
})
}
// Error Handlers
function handleTypeError(e) {
if (e.message.includes("'publicKey'")) {
this.log('\nPlease create a wallet. Enter "help wallet" to see usage.\n');
} else {
this.log(e);
}
}