-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
184 lines (144 loc) · 4.46 KB
/
cli.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
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
const { program } = require("commander");
const { Wallet } = require("./wallet");
const {
startServer,
KnownNodes,
sendTx,
sendData,
cmdToBytes,
} = require("./network");
const { Blockchain } = require("./blockchain");
const { UTXOSet } = require("./utxo");
const { Wallets } = require("./wallets");
const { newTransaction, coinbaseTx, getBalance } = require("./transaction");
const { address } = require("bitcoinjs-lib");
class CommandLine {
async startNode(nodeID, minerAddress, address) {
console.log(`Starting Node ${nodeID}`);
if (minerAddress) {
if (Wallet.validateAddress(minerAddress)) {
console.log("Mining is on. Address to receive rewards:", minerAddress);
} else {
console.error("Wrong miner address!");
process.exit(1);
}
}
await startServer(nodeID, minerAddress, address);
}
async reindexUTXO(address, nodeID) {
const chain = new Blockchain(address, nodeID);
await chain.continueBlockchain(address);
const utxo = new UTXOSet(chain);
await utxo.reIndex();
const count = await utxo.countTransactions();
await chain.closeDB();
console.log(`Done! There are ${count} transactions in the UTXO set.`);
}
createWallet(nodeID) {
let wallets = new Wallets();
let address = wallets.addWallet();
wallets.saveFile(nodeID);
console.log(`New address is: ${address}`);
}
async printChain(address, nodeID) {
const chain = new Blockchain(address, nodeID);
await chain.continueBlockchain(true);
await chain.iterate();
}
async getBalanceUser(address, nodeID) {
const chain = new Blockchain(address, nodeID);
await chain.continueBlockchain(address);
let utxo = new UTXOSet(chain);
let balance = await getBalance(address, utxo, nodeID);
await chain.closeDB();
console.log("Balance of ", address, " : ", balance);
}
async send(from, to, amount, nodeID, mineNow) {
const payload = JSON.stringify({
from,
to,
amount,
nodeID,
});
const request = Buffer.concat([
cmdToBytes("getTxFromCmd"),
Buffer.from(payload),
]);
sendData(`localhost:${nodeID}`, request);
}
async returnChain(address, nodeID) {
const chain = new Blockchain(address, nodeID);
await chain.continueBlockchain(address);
const utxo = new UTXOSet(chain);
await utxo.returnChain("");
}
}
const cli = new CommandLine();
program
.command("startnode")
.description("Start a node")
.option("-n, --nodeid <nodeid>", "Set node ID")
.option("-m, --miner <miner>", "Set miner address")
.option("-a, --address <address>", "Set address")
.action((options) => {
cli.startNode(options.nodeid, options.miner, options.address);
});
//-------> node ./cli.js startnode -n 3000 -m
program
.command("reindex")
.description("reindex the database")
.option("-n, --nodeid <nodeid>", "Set node ID")
.option("-a, --address <address>", "Set address")
.action((options) => {
cli.reindexUTXO(options.address, options.nodeid);
});
program
.command("createwallet")
.description("create wallet")
.option("-n, --nodeid <nodeid>", "Set node ID")
.action((options) => {
cli.createWallet(options.nodeid);
});
//------> node ./cli.js createwallet -n 3000
program
.command("printchain")
.description("print chain")
.option("-n, --nodeid <nodeid>", "Set node ID")
.option("-a, --address <address>", "Set address")
.action((options) => {
cli.printChain(options.address, options.nodeid);
});
program
.command("returnchain")
.description("return chain")
.option("-n, --nodeid <nodeid>", "Set node ID")
.option("-a, --address <address>", "Set address")
.action((options) => {
cli.returnChain(options.address, options.nodeid);
});
program
.command("getuserbalance")
.description("get user balance")
.option("-a, --address <address>", "Set address")
.option("-n, --nodeid <nodeid>", "Set node ID")
.action((options) => {
cli.getBalanceUser(options.address, options.nodeid);
});
program
.command("send")
.description("create tx and send them to users")
.option("-f, --from <from>", "from address")
.option("-t, --to <to>", "to address")
.option("-m, --amount <amount>", "amount")
.option("-n, --nodeid <nodeid>", "Set node ID")
.option("-i, --mine <mineNow>", "mine now")
.action((options) => {
cli.send(
options.from,
options.to,
options.amount,
options.nodeid,
options.mineNow
);
});
program.parse(process.argv);