From ce8648a31df8e370dea1e583d85c881ac666b87f Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 12 Feb 2015 23:57:16 -0300 Subject: [PATCH] cli WIP --- app.js | 2 ++ bit-wallet/bit | 14 ++++++++++++++ bit-wallet/bit-create | 29 +++++++++++++++++++++++++++++ bit-wallet/bit-create.js | 10 ++++++++++ bit-wallet/bit-join | 26 ++++++++++++++++++++++++++ bit-wallet/bit-status | 20 ++++++++++++++++++++ bit-wallet/common.js | 27 +++++++++++++++++++++++++++ lib/clilib.js | 7 ++++++- lib/storage.js | 11 ++++++++--- 9 files changed, 142 insertions(+), 4 deletions(-) create mode 100755 bit-wallet/bit create mode 100755 bit-wallet/bit-create create mode 100644 bit-wallet/bit-create.js create mode 100644 bit-wallet/bit-join create mode 100644 bit-wallet/bit-status create mode 100644 bit-wallet/common.js diff --git a/app.js b/app.js index 440a1b7..a11d3e9 100644 --- a/app.js +++ b/app.js @@ -44,6 +44,8 @@ var router = express.Router(); function returnError(err, res) { if (err instanceof CopayServer.ClientError) { + +console.log('[app.js.47]'); //TODO var status = (err.code == 'NOTAUTHORIZED') ? 401 : 400; res.status(status).json({ code: err.code, diff --git a/bit-wallet/bit b/bit-wallet/bit new file mode 100755 index 0000000..2ac9fc0 --- /dev/null +++ b/bit-wallet/bit @@ -0,0 +1,14 @@ +#!/usr/bin/env node + +var program = require('commander'); +var cli = require('../lib/clilib.js'); + +program + .version('0.0.1') + .command('create [username]', 'creates a wallet') + .command('join [username]', 'join a wallet') + .command('status', 'get wallet status') + .parse(process.argv); + + + diff --git a/bit-wallet/bit-create b/bit-wallet/bit-create new file mode 100755 index 0000000..f5204ae --- /dev/null +++ b/bit-wallet/bit-create @@ -0,0 +1,29 @@ +#!/usr/bin/env node + +var program = require('commander'); +var cli = require('../lib/clilib.js'); +var common = require('./common'); + +program + .version('0.0.1') + .option('-c,--config [file]', 'Wallet config filename') + .usage('[options] [copayerName]') + .parse(process.argv); + +var args = program.args; +if (!args[0]) + program.help(); + +var walletName = args[0]; +var copayerName = args[2] || process.env.USER; + +var mn = common.parseMN(args[1]); + +cli.setFilename(program.config); + +cli.createWallet(walletName, copayerName, mn[0], mn[1], function(err, secret) { + common.die(err); + console.log(' * Wallet Created.'); + console.log(' - Secret to share:\n\t' + secret); +}); + diff --git a/bit-wallet/bit-create.js b/bit-wallet/bit-create.js new file mode 100644 index 0000000..1f02e8c --- /dev/null +++ b/bit-wallet/bit-create.js @@ -0,0 +1,10 @@ +#!/usr/bin/env node + +var program = require('commander'); +var cli = require('../lib/clilib.js'); + +program + .version('0.0.1') + .parse(process.argv); + + diff --git a/bit-wallet/bit-join b/bit-wallet/bit-join new file mode 100644 index 0000000..2c5f4e7 --- /dev/null +++ b/bit-wallet/bit-join @@ -0,0 +1,26 @@ +#!/usr/bin/env node + +var program = require('commander'); +var cli = require('../lib/clilib.js'); +var common = require('./common'); + +program + .version('0.0.1') + .option('-c,--config [file]', 'Wallet config filename') + .usage('[options] [copayerName]') + .parse(process.argv); + +var args = program.args; +if (!args[0]) + program.help(); + +var secret = args[0]; +var copayerName = args[1] || process.env.USER; + +cli.setFilename(program.config); + +cli.joinWallet(secret, copayerName, function(err, xx) { + common.die(err); + console.log(' * Wallet Joined.', xx); +}); + diff --git a/bit-wallet/bit-status b/bit-wallet/bit-status new file mode 100644 index 0000000..563d26e --- /dev/null +++ b/bit-wallet/bit-status @@ -0,0 +1,20 @@ +#!/usr/bin/env node + +var program = require('commander'); +var cli = require('../lib/clilib.js'); +var common = require('./common'); + +program + .version('0.0.1') + .option('-c,--config [file]', 'Wallet config filename') + .parse(process.argv); + +var args = program.args; + +cli.setFilename(program.config); + +cli.status(function(err, xx) { + common.die(err); + console.log(' * Status:', xx); +}); + diff --git a/bit-wallet/common.js b/bit-wallet/common.js new file mode 100644 index 0000000..7a6c256 --- /dev/null +++ b/bit-wallet/common.js @@ -0,0 +1,27 @@ +var common = function() {}; + + +var die = common.die = function(err) { + if (err) { + console.error(err); + process.exit(1); + } +}; + +common.parseMN = function(MN) { + if (!MN) + die('No m-n parameter'); + var mn = MN.split('-'); + + var m = parseInt(mn[0]); + var n = parseInt(mn[1]); + + if (!m || ! n) { + die('Bad m-n parameter'); + } + + return [m, n]; +}; + + +module.exports = common; diff --git a/lib/clilib.js b/lib/clilib.js index 759466f..b37bded 100644 --- a/lib/clilib.js +++ b/lib/clilib.js @@ -38,10 +38,14 @@ function _createXPrivKey() { }; function CliLib(opts) { + if (!opts.filename) { + throw new Error('Please set the config filename'); + } this.filename = opts.filename; }; -CliLib.prototype._save = function(data) { + +CliLib.prototype_save = function (data) { fs.writeFileSync(this.filename, JSON.stringify(data)); }; @@ -139,6 +143,7 @@ CliLib.prototype._joinWallet = function(data, secret, copayerName, cb) { body: args, json: true, }, function(err, res, body) { +console.log('[clilib.js.123:err:]',err, body); //TODO if (err) return cb(err); if (res.statusCode != 200) { _parseError(body); diff --git a/lib/storage.js b/lib/storage.js index 50bcf06..4971fee 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -5,6 +5,7 @@ var levelup = require('levelup'); var async = require('async'); var $ = require('preconditions').singleton(); var log = require('npmlog'); +var util = require('util'); log.debug = log.verbose; var Wallet = require('./model/wallet'); @@ -195,8 +196,8 @@ Storage.prototype.fetchNotifications = function(walletId, opts, cb) { var txs = []; opts = opts || {}; opts.limit = _.isNumber(opts.limit) ? parseInt(opts.limit) : -1; - opts.minTs = _.isNumber(opts.minTs) ? zeroPad(opts.minTs,11) : 0; - opts.maxTs = _.isNumber(opts.maxTs) ? zeroPad(opts.maxTs,11) : MAX_TS; + opts.minTs = _.isNumber(opts.minTs) ? zeroPad(opts.minTs, 11) : 0; + opts.maxTs = _.isNumber(opts.maxTs) ? zeroPad(opts.maxTs, 11) : MAX_TS; var key = KEY.NOTIFICATION(walletId, opts.minTs); var endkey = KEY.NOTIFICATION(walletId, opts.maxTs); @@ -380,7 +381,11 @@ Storage.prototype._dump = function(cb, fn) { fn = fn || console.log; this.db.readStream() - .on('data', fn) + .on('data', function(data) { + fn(util.inspect(data, { + depth: 10 + })); + }) .on('end', function() { if (cb) return cb(); });