Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
95 lines (80 loc) · 3.11 KB
/
Copy pathcli.js
File metadata and controls
95 lines (80 loc) · 3.11 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
// const {Command} = require("commander");
// const program = new Command();
// const Blockchain = require("./blockchain.js")
// const [Transaction, NewCoinbaseTX, NewUTXOTransaction] = require("./transaction");
import {Command} from "commander";
const program = new Command();
import Blockchain from "./blockchain.js";
import {NewUTXOTransaction} from './transaction.js'
class Cli {
printUsage() {
console.log("Usage:");
console.log(" getbalance -address ADDRESS -get balance of address");
console.log(" createblockchain -address ADDRESS -create a blockchain and send genesis reward to address");
console.log(" printchain - print all blcoks in the blockchain");
console.log(" send -from FROM -to TO -amount AMOUNT - send amount fo coins from FROM to TO");
}
validArgs() {
if (process.argv.length < 3) {
this.printUsage();
process.exit(1);
}
}
async createBlockchain(address) {
await Blockchain.createBlockchain(address);
console.log("Create blockchain done");
}
async getBalance(address) {
let bc = await Blockchain.newBlockchain(address);
let balance = 0;
let UTXOs = await bc.findUTXO(address);
for (const out of UTXOs) {
balance += out.value;
}
console.log(`${address} get balance: ${balance}`);
}
async printChain() {
console.log("printchain :")
let bc = await Blockchain.newBlockchain("");
let it = bc.iterator();
while (true) {
let block = await it.next();
console.log("Pre.hash:", block.preBlockHash);
console.log("Transaction:", JSON.stringify(block.transactions));
console.log("Hash:", block.hash);
console.log();
if (block.preBlockHash.length === 0) {
console.log("get end block");
break;
}
}
}
async send(from, to, amount) {
let bc = await Blockchain.newBlockchain(from);
let tx = await NewUTXOTransaction(from, to, amount, bc);
await bc.mineBlock([tx]);
console.log("Success!");
}
run() {
this.validArgs();
program.command("createblockchain")
.description("create blockchain")
.requiredOption("-address <address>", "address")
.action((address) => this.createBlockchain(address.Address));
program.command("getbalance")
.description("get balance")
.requiredOption("-address <address>", "address")
.action((address) => this.getBalance(address.Address));
program.command("send")
.description("sed coins")
.requiredOption("-from <from>", "sender")
.requiredOption("-to <to>", "receiver")
.requiredOption("-amount <amount>", "amount")
.action((param) => this.send(param.From, param.To, param.Amount));
program.command("printchain")
.description("prints blockchain")
.action(() => this.printChain())
program.parse(process.argv);
}
}
export default Cli;