txho
Version:
Get a Bitcoin transaction as JSON for CLI and node
124 lines (123 loc) • 6.12 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
exports.__esModule = true;
var dotenv = require("dotenv");
dotenv.config();
var JSON5 = require('json5');
var readline = require('readline');
var argv = require('minimist')(process.argv.slice(2));
var fromTxid_1 = require("./fromTxid");
var fromRawtx_1 = require("./fromRawtx");
var filterObj_1 = require("./filterObj");
var version = '3.1.0';
(function () {
if (argv.v || argv.version) {
console.log('v' + version);
process.exit(0);
}
var _a = argv._, command = _a[0], data = _a[1];
if (!command) {
kill('No command provided');
}
var conf = __assign(__assign({}, filterObj_1["default"](argv, { _: 0 })), { jsonRpc: {
protocol: process.env.BITCOIN_PROTOCOL || 'http',
user: process.env.BITCOIN_USERNAME || 'root',
pass: process.env.BITCOIN_PASSWORD || 'bitcoin',
host: process.env.BITCOIN_IP || '127.0.0.1',
port: process.env.BITCOIN_PORT || '8332'
} });
if (conf.help) {
return kill();
}
if (conf.txo) {
conf.txo = JSON5.parse("{" + conf.txo + "}");
}
if (conf.pool) {
conf.pool = JSON5.parse("{" + conf.pool + "}");
}
if (conf.debug) {
console.warn(__assign({}, filterObj_1["default"](conf, { jsonRpc: 0 })));
}
if (process && process.stdin && !process.stdin.isTTY) {
pipeData(command, conf);
return;
}
if (!data) {
kill('No data provided');
}
var res = new Promise(function () { });
switch (command) {
case 'txid':
res = fromTxid_1["default"](data, conf);
break;
case 'rawtx':
res = fromRawtx_1["default"](data, conf);
break;
default:
kill('Unknown command');
}
echo(res, conf);
})();
function pipeData(command, config) {
var conf = __assign({ compress: true, skip: 0 }, config);
var rl = readline.createInterface({
input: process.stdin,
//output: process.stdout,
terminal: false
});
var i_ = 0;
rl.on('line', function (line) {
var i = ++i_;
if (i <= (conf.skip | 0))
return;
line = line.trim();
if (!line.length)
return;
var res = new Promise(function () {
return '';
});
switch (command) {
case 'txid':
res = fromTxid_1["default"](line, conf);
break;
case 'rawtx':
res = fromRawtx_1["default"](line, conf);
break;
default:
kill('Unknown command');
}
echo(res, conf, "Problem on line " + i + ": ");
});
}
function echo(res, conf, prependErrMsg) {
if (prependErrMsg === void 0) { prependErrMsg = ''; }
console.log(JSON.stringify(res, null, conf.compress ? undefined : 2));
}
function kill(str) {
if (str === void 0) { str = ''; }
str.length && console.error('\n ❌ ' + str);
console.error('\nPlease make sure to provide one of the following combination of parameters');
console.error(' $ txho txid [data (64 char long hex)]');
console.error(' $ txho rawtx [data (raw tx)]');
console.error('\n Example: ');
console.error(' $ txho txid 0726963652a3...data...2274785e756d5f --cellStr --hide=data.b64');
console.error('\nYou can pipe in data from a file having txid or rawtx per line. Example: ');
console.error(' $ cat lostOfTxids.txt | txho txid ');
console.error(' $ txho rawtx < lostOfRawTxs.txt > output.txt');
console.error(' $ cat lostOfRawTxs.txt | txho rawtx --cellHash');
console.error("\nConfiguration\n network: string, // main or test \n maxDataSize: number, // What is the max size of data not hosted. Defaults to 512. If 0 no data is hoisted\n cellB64: flag; // Aways add a base 64 representation of data to each cell. Default true\n cellStr: flag; // Aways add a string representation of data to each cell. Default false\n cellHex: flag; // Aways add a hex representation of data to each cell. Default false\n cellHash: flag; // Aways add a buffer containing a data to each cell. Default false\n cellAuto: flag; // Try to guess the most human readable format of str/hex/b64. Will add iso representatoin of utc if string looks like a timestamp\n cellAll: flag; // Same as setting cellB64, cellStr, cellHex, cellHash and cellAuto to true\n cellBuf: flag; // Aways add a buffer containing raw data to each cell. Default false \n compress: boolean; // Compress the output to one line. Default false. If data is piped: default true. \t\n only: string // Comma seperated list of dot-notated elements to only include in output (like --only=tx.txid,data.sha256)\n hide: string // Comma seperated list of dot-notated elements to explude from output (like --hide=data.b64). \n skip: number // For piped data only: how many lines to skip before starting to execute. \n txo: object // The inside of a config object to manage TXO output. \n cleanData: string // For txid it will identify and use the first valid 64 char hex string from the input. For rawtx it will use the longest hex string found in the input. \n pool: object; // The inside of a config object to manage the pool size of rpc requests: See more about chiqq on npm.\n // Is merged with default: --pool=concurrency:5,retryMax:3,retryCooling:1000,retryFactor:2\n\n\t");
str.length && console.error('\n ❌ ' + str);
console.error();
process.exit(1);
}