cosmos-fundraiser-cli
Version:
CLI tool for purchasing Atoms in the Cosmos Fundraiser
468 lines (385 loc) • 13.8 kB
JavaScript
'use strict';
var _stringify = require('babel-runtime/core-js/json/stringify');
var _stringify2 = _interopRequireDefault(_stringify);
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
var _promise = require('babel-runtime/core-js/promise');
var _promise2 = _interopRequireDefault(_promise);
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var readWallet = function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(prmpt) {
var seed, wallet;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return readSeed(prmpt);
case 2:
seed = _context.sent;
wallet = cfr.deriveWallet(seed);
return _context.abrupt('return', wallet);
case 5:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
return function readWallet(_x) {
return _ref.apply(this, arguments);
};
}();
var readSeed = function () {
var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(prmpt) {
var seed, rl;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (!prmpt) {
prmpt = 'Please enter your wallet seed phrase';
}
seed = void 0;
if (!process.stdin.isTTY) {
_context2.next = 11;
break;
}
process.stdout.write(prmpt + ':\n> ');
rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
_context2.next = 7;
return new _promise2.default(function (resolve) {
return rl.once('line', resolve);
});
case 7:
seed = _context2.sent;
rl.close();
_context2.next = 15;
break;
case 11:
_context2.next = 13;
return new _promise2.default(function (resolve, reject) {
var data = [];
process.stdin.on('data', function (chunk) {
return data.push(chunk);
});
process.stdin.once('end', function () {
return resolve(Buffer.concat(data));
});
process.stdin.once('error', function (err) {
return reject(err);
});
});
case 13:
seed = _context2.sent;
seed = seed.toString('utf8').trim();
case 15:
return _context2.abrupt('return', seed);
case 16:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
return function readSeed(_x2) {
return _ref2.apply(this, arguments);
};
}();
var runCommand = function () {
var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(commandName, args) {
var command;
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
command = commands[commandName];
if (!command) {
fail('"' + commandName + '" is not a valid command');
}
_context3.next = 4;
return command.apply(undefined, (0, _toConsumableArray3.default)(args));
case 4:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
return function runCommand(_x3, _x4) {
return _ref3.apply(this, arguments);
};
}();
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var promisify = require('bluebird').promisify;
var cfr = require('cosmos-fundraiser');
var _require = require('bitcoinjs-lib'),
Transaction = _require.Transaction;
var readline = require('readline');
cfr.fetchStatus = promisify(cfr.fetchStatus);
cfr.bitcoin.pushTx = promisify(cfr.bitcoin.pushTx);
cfr.bitcoin.fetchUtxos = promisify(cfr.bitcoin.fetchUtxos);
function fail(message) {
console.log(message);
process.exit(1);
}
var commands = {
status: function status() {
var _this = this;
return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4() {
var status, fundraiserEnded;
return _regenerator2.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return cfr.fetchStatus();
case 2:
status = _context4.sent;
fundraiserEnded = status.fundraiserEnded;
console.log(fundraiserEnded ? status : { fundraiserEnded: fundraiserEnded });
case 5:
case 'end':
return _context4.stop();
}
}
}, _callee4, _this);
}))();
},
genwallet: function genwallet() {
var _this2 = this;
return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5() {
var mnemonic;
return _regenerator2.default.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
mnemonic = cfr.generateMnemonic();
console.log(mnemonic);
case 2:
case 'end':
return _context5.stop();
}
}
}, _callee5, _this2);
}))();
},
btcaddress: function btcaddress() {
var _this3 = this;
return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6() {
var wallet;
return _regenerator2.default.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_context6.prev = 0;
_context6.next = 3;
return readWallet();
case 3:
wallet = _context6.sent;
console.log(wallet.addresses.bitcoin);
_context6.next = 11;
break;
case 7:
_context6.prev = 7;
_context6.t0 = _context6['catch'](0);
console.log('error:', _context6.t0);
fail('Usage: cosmos-fundraiser btcaddress < walletSeed');
case 11:
case 'end':
return _context6.stop();
}
}
}, _callee6, _this3, [[0, 7]]);
}))();
},
cosmosaddress: function cosmosaddress() {
var _this4 = this;
return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7() {
var wallet;
return _regenerator2.default.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_context7.prev = 0;
_context7.next = 3;
return readWallet();
case 3:
wallet = _context7.sent;
console.log(wallet.addresses.cosmos);
_context7.next = 11;
break;
case 7:
_context7.prev = 7;
_context7.t0 = _context7['catch'](0);
console.log('error:', _context7.t0);
fail('Usage: cosmos-fundraiser cosmosaddress < walletSeed');
case 11:
case 'end':
return _context7.stop();
}
}
}, _callee7, _this4, [[0, 7]]);
}))();
},
buildtx: function buildtx(btcAddress) {
var _this5 = this;
var feeRate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 300;
return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8() {
var _ref4, utxos, tx;
return _regenerator2.default.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
if (!btcAddress) {
fail('\nUsage:\n cosmos-fundraiser buildtx <bitcoinAddress> [feeRate]\n\n \'feeRate\' is used to calculate the Bitcoin transaction fee,\n measured in satoshis per byte\n ');
}
_context8.next = 3;
return cfr.bitcoin.fetchUtxos(btcAddress);
case 3:
_ref4 = _context8.sent;
utxos = _ref4.utxos;
if (utxos.length === 0) {
fail('Address has no unspent outputs\n\nPlease send some BTC to your intermediate address, then run this\ncommand again.\n ');
}
try {
tx = cfr.bitcoin.createFinalTx(utxos, feeRate).tx;
console.log(tx.toHex());
} catch (err) {
fail(err.message);
}
case 7:
case 'end':
return _context8.stop();
}
}
}, _callee8, _this5);
}))();
},
signtx: function signtx(txHex) {
var _this6 = this;
return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee9() {
var tx, wallet, signedTx;
return _regenerator2.default.wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
if (!txHex) {
fail('Usage: cosmos-fundraiser signtx <txHex> < walletSeed');
}
tx = Transaction.fromHex(txHex);
_context9.next = 4;
return readWallet();
case 4:
wallet = _context9.sent;
signedTx = cfr.bitcoin.signFinalTx(wallet, tx);
console.log(signedTx.toHex());
case 7:
case 'end':
return _context9.stop();
}
}
}, _callee9, _this6);
}))();
},
broadcasttx: function broadcasttx(txHex) {
var _this7 = this;
return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee10() {
return _regenerator2.default.wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
_context10.next = 2;
return cfr.bitcoin.pushTx(txHex);
case 2:
return _context10.abrupt('return', _context10.sent);
case 3:
case 'end':
return _context10.stop();
}
}
}, _callee10, _this7);
}))();
},
ethtx: function ethtx() {
var _this8 = this;
return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee11() {
var wallet, tx;
return _regenerator2.default.wrap(function _callee11$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
_context11.next = 2;
return readWallet();
case 2:
wallet = _context11.sent;
tx = cfr.ethereum.getTransaction(wallet.addresses.cosmos, wallet.addresses.ethereum);
console.log((0, _stringify2.default)(tx, null, ' '));
case 5:
case 'end':
return _context11.stop();
}
}
}, _callee11, _this8);
}))();
},
splitseed: function splitseed() {
var _this9 = this;
return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12() {
var seed, _cfr$splitMnemonic, one, two;
return _regenerator2.default.wrap(function _callee12$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
_context12.next = 2;
return readSeed('Please enter the seed phrase to split');
case 2:
seed = _context12.sent;
if (!seed) {
fail('Usage: cosmos-fundraiser splitseed < walletSeed');
}
_cfr$splitMnemonic = cfr.splitMnemonic(seed), one = _cfr$splitMnemonic.one, two = _cfr$splitMnemonic.two;
console.log("one: " + one + "\ntwo: " + two);
case 6:
case 'end':
return _context12.stop();
}
}
}, _callee12, _this9);
}))();
},
joinseed: function joinseed() {
var _this10 = this;
return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee13() {
var one, two, seed;
return _regenerator2.default.wrap(function _callee13$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
_context13.next = 2;
return readSeed('Please enter seed phrase 1');
case 2:
one = _context13.sent;
_context13.next = 5;
return readSeed('Please enter seed phrase 2');
case 5:
two = _context13.sent;
seed = cfr.joinMnemonic(one, two);
console.log("one: " + one + "\ntwo: " + two + "\nseed: " + seed);
case 8:
case 'end':
return _context13.stop();
}
}
}, _callee13, _this10);
}))();
}
};
module.exports = runCommand;