-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcli.js
81 lines (66 loc) · 2.28 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
#!/usr/bin/env node
const program = require('commander');
const txService = require('./src/service/tx');
const scriptService = require('./src/service/script');
program.version('0.1.0').description('₿itcoin Forge is a tool for the Bitcoin protocol.');
program
.command('tx <inputsQ> <outputsQ> [ios...]')
.description('Forge a TX')
.option('-t, --testnet', 'Indicates if "Testnet" should be used')
.action((inputsQ, outputsQ, ios, options) => {
// set the network to work with
const isTestnet = options.testnet === true;
txService.setTestnet(isTestnet);
let iosReadCount = 0;
const inputs = [];
const outputs = [];
// read inputs
for (let i = 0; i < inputsQ; i += 1) {
const inputTxhash = ios[iosReadCount];
const inputOutputIndex = ios[iosReadCount + 1];
const inputOutputPrivKey = ios[iosReadCount + 2];
iosReadCount += 3; // move 3 positions at a time
const input = {
prevTxHash: inputTxhash,
prevTxIndex: parseInt(inputOutputIndex, 10),
privateKey: inputOutputPrivKey,
};
// check if there is an additional field for the input amount
// (should be numeric only)
if (/^\d+$/.test(ios[iosReadCount])) {
input.amount = parseInt(ios[iosReadCount], 10);
iosReadCount += 1; // move 1 position for this field
}
inputs.push(input);
}
// read outputs
for (let o = 0; o < outputsQ; o += 1) {
const outputAddress = ios[iosReadCount];
const outputAmount = ios[iosReadCount + 1];
iosReadCount += 2; // move 2 positions at a time
outputs.push({
address: outputAddress,
amount: parseInt(outputAmount, 10),
});
}
const tx = txService.createTx(inputs, outputs);
// prepare for display
const txDisplay = {
id: tx.getId(),
size: tx.byteLength(),
virtualSize: tx.virtualSize(),
weight: tx.weight(),
inputsNum: tx.ins.length,
outputsNum: tx.outs.length,
hex: tx.toHex(),
};
console.log('TX:', txDisplay);
});
program
.command('decompileasm <scriptHex>')
.description('Decompile a script into the ASM')
.action((scriptHex) => {
const asm = scriptService.decompileScriptASM(scriptHex);
console.log('Script ASM: ', asm);
});
program.parse(process.argv);