-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCLI.js
More file actions
174 lines (159 loc) · 3.99 KB
/
Copy pathCLI.js
File metadata and controls
174 lines (159 loc) · 3.99 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
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
const P2P = require("./P2P.js");
const Blockchain = require("./Blockchain.js");
const blockchain = new Blockchain();
const p2p = new P2P(blockchain);
const Wallet = require("./Wallet.js");
const wallet = new Wallet(blockchain,p2p);
const Output = require("./Transaction.js").Output;
function cli(vorpal) {
vorpal
.use(welcome)
.use(printBlockchain)
.use(createTransactionOutput)
.use(printTransactionOutputs)
.use(createTransaction)
.use(printTransactionPool)
.use(mineBlock)
.use(printTransaction)
.use(getBalance)
.use(importPublicKey)
.use(startServer)
.use(connectPeer)
.delimiter('bc_wallet ->')
.show();
}
module.exports = cli;
function welcome(vorpal) {
vorpal.log("Welcome to Blockchain Wallet");
vorpal.exec("help");
}
function printBlockchain(vorpal) {
vorpal
.command("blockchain","View the current blockchain")
.alias("b")
.action(function(args,callback) {
this.log(blockchain)
callback();
})
}
function createTransactionOutput(vorpal) {
vorpal
.command("output <dst> <amount>","Define a transaction output")
.alias("o")
.action(function(args,callback) {
wallet.addOutput(args.dst, args.amount);
callback();
})
}
function printTransactionOutputs(vorpal) {
vorpal
.command("show_outputs","Print current transaction outputs")
.alias("so")
.action(function(args,callback) {
wallet.printUnusedOutputs();
callback();
})
}
function createTransaction(vorpal) {
vorpal
.command("transaction <fee> [outs...]","Create a transaction with a certain fee and predefined outputs")
.alias("t")
.action(function(args,callback) {
if (args.fee && args.outs) {
const outputs = wallet.selectOutputs(args.outs);
const trans = wallet.buildTransaction(outputs,args.fee);
if (trans == []) {
this.log("Transaction creation failed");
} else {
p2p.sendTransaction(trans);
}
} else {
this.log("Invalid request");
}
callback();
})
}
function printTransactionPool(vorpal) {
vorpal
.command("transactionpool","Print the current contents of the transaction pool")
.alias("tp")
.action(function(args,callback) {
const transactionPool = blockchain.transactionPool;
for (let i = 0; i < transactionPool.length; i++) {
let transaction = transactionPool[i];
wallet.printTransaction(transaction);
}
callback();
})
}
function printTransaction(vorpal) {
vorpal
.command("show_trans <block> <trans>","Print a particular transaction")
.alias("st")
.action(function(args,callback) {
const transaction = blockchain.blockchain[args.block].transactions[args.trans];
wallet.printTransaction(transaction);
callback();
})
}
function mineBlock(vorpal) {
vorpal
.command("mine","Create a new block")
.alias("m")
.action(function(args,callback) {
const trans = blockchain.createRewardTransaction(wallet.addr);
blockchain.transactionPool.unshift(trans);
blockchain.createNextBlock();
p2p.sendBlock(blockchain.latestBlock);
callback();
})
}
function getBalance(vorpal) {
vorpal
.command("balance","Get wallet ballance")
.alias("wb")
.action(function(args,callback) {
const balance = wallet.getBalance();
this.log("Balance: "+balance);
callback();
})
}
function importPublicKey(vorpal) {
vorpal
.command("import_key <keyfile>","Import public key from file")
.alias("i")
.action(function(args,callback) {
if (args.keyfile) {
wallet.importKey(args.keyfile);
} else {
this.log("Invalid import");
}
callback();
})
}
function startServer(vorpal) {
vorpal
.command("start_server <port>","Start server")
.alias("s")
.action(function(args,callback) {
if (args.port) {
p2p.startServer(args.port);
} else {
this.log("Missing port number");
}
callback();
})
}
function connectPeer(vorpal) {
vorpal
.command("connect <ip> <port>","Connect to peer")
.alias("c")
.action(function(args,callback) {
if (args.ip && args.port) {
p2p.connectToPeer(args.ip,args.port);
} else {
this.log("Missing IP or port");
}
callback();
})
}