Skip to content

Commit

Permalink
Merge pull request bitpay#10 from matiu/feat/cli-send2
Browse files Browse the repository at this point in the history
Feat/cli send2
  • Loading branch information
isocolsky committed Feb 14, 2015
2 parents cea5495 + 926406e commit b54187a
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 28 deletions.
21 changes: 17 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ app.use(bodyParser.json({
limit: POST_LIMIT
}));

app.use(require('morgan')('dev'));


var port = process.env.COPAY_PORT || 3001;
var router = express.Router();

Expand All @@ -59,7 +62,6 @@ function returnError(err, res, req) {
}
var m = message || err.toString();

console.log('[app.js.60]'); //TODO
log.error('Error: ' + req.url + ' :' + code + ':' + m);
res.status(code || 500).json({
error: m,
Expand Down Expand Up @@ -144,6 +146,17 @@ router.get('/v1/wallets/', function(req, res) {
});
});


router.get('/v1/txproposals/', function(req, res) {
getServerWithAuth(req, res, function(server) {
server.getPendingTxs({}, function(err, pendings) {
if (err) return returnError(err, res, req);
res.json(pendings);
});
});
});


router.post('/v1/txproposals/', function(req, res) {
getServerWithAuth(req, res, function(server) {
server.createTx(req.body, function(err, txp) {
Expand Down Expand Up @@ -181,9 +194,9 @@ router.get('/v1/balance/', function(req, res) {
});
});

router.post('/v1/txproposals/:id/signatures', function(req, res) {
req.body.txProposalId = req.params['id'];
router.post('/v1/txproposals/:id/signatures/', function(req, res) {
getServerWithAuth(req, res, function(server) {
req.body.txProposalId = req.params['id'];
server.signTx(req.body, function(err, txp) {
if (err) return returnError(err, res, req);
res.end();
Expand All @@ -192,8 +205,8 @@ router.post('/v1/txproposals/:id/signatures', function(req, res) {
});

router.post('/v1/txproposals/:id/rejections', function(req, res) {
req.body.txProposalId = req.params['id'];
getServerWithAuth(req, res, function(server) {
req.body.txProposalId = req.params['id'];
server.signTx(req.body, function(err, txp) {
if (err) return returnError(err, res, req);
res.end();
Expand Down
2 changes: 1 addition & 1 deletion bit-wallet/bit
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node

var program = require('commander');
var cli = require('../lib/clilib.js');

program
.version('0.0.1')
Expand All @@ -12,6 +11,7 @@ program
.command('addresses', 'list addresses')
.command('balance', 'wallet balance')
.command('send <address> <amount> <note>', 'send bitcoins')
.command('sign <txpId>', 'sign a Transaction Proposal')
.parse(process.argv);


Expand Down
4 changes: 2 additions & 2 deletions bit-wallet/bit-address
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

var program = require('commander');
var CliLib = require('../lib/clilib.js');
var ClientLib = require('../lib/clientlib.js');
var common = require('./common');

program
Expand All @@ -11,7 +11,7 @@ program
.parse(process.argv);

var args = program.args;
var cli = new CliLib({
var cli = new ClientLib({
filename: program.config
});

Expand Down
4 changes: 2 additions & 2 deletions bit-wallet/bit-addresses
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

var _ = require('lodash');
var program = require('commander');
var CliLib = require('../lib/clilib.js');
var common = require('./common');
var ClientLib = require('../lib/clientlib.js');

program
.version('0.0.1')
Expand All @@ -12,7 +12,7 @@ program
.parse(process.argv);

var args = program.args;
var cli = new CliLib({
var cli = new ClientLib({
filename: program.config
});

Expand Down
4 changes: 2 additions & 2 deletions bit-wallet/bit-balance
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

var program = require('commander');
var CliLib = require('../lib/clilib.js');
var ClientLib = require('../lib/clientlib.js');
var common = require('./common');

program
Expand All @@ -11,7 +11,7 @@ program
.parse(process.argv);

var args = program.args;
var cli = new CliLib({
var cli = new ClientLib({
filename: program.config
});

Expand Down
4 changes: 2 additions & 2 deletions bit-wallet/bit-create
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

var program = require('commander');
var CliLib = require('../lib/clilib.js');
var ClientLib = require('../lib/clientlib.js');
var common = require('./common');

program
Expand All @@ -21,7 +21,7 @@ var network = program.network;

var mn = common.parseMN(args[1]);

var cli = new CliLib({
var cli = new ClientLib({
filename: program.config
});
cli.createWallet(walletName, copayerName, mn[0], mn[1], network, function(err, secret) {
Expand Down
4 changes: 2 additions & 2 deletions bit-wallet/bit-join
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

var program = require('commander');
var CliLib = require('../lib/clilib.js');
var ClientLib = require('../lib/clientlib.js');
var common = require('./common');

program
Expand All @@ -17,7 +17,7 @@ if (!args[0])
var secret = args[0];
var copayerName = args[1] || process.env.USER;

var cli = new CliLib({
var cli = new ClientLib({
filename: program.config
});

Expand Down
12 changes: 6 additions & 6 deletions bit-wallet/bit-send
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

var program = require('commander');
var CliLib = require('../lib/clilib.js');
var ClientLib = require('../lib/clientlib.js');
var common = require('./common');

program
Expand All @@ -19,15 +19,15 @@ if (!args[0] || !args[1] || !args[2])
var amount = args[1];
var message = args[2];

var cli = new CliLib({
filename: program.config
var cli = new ClientLib({
filename: program.config
});

cli.send({toAddress: address, amount: amount, message:message}, function(err, x) {
common.die(err);
console.log(' * Tx created: ID %s [%s] RequiredSignatures:',
x.id, x.status, x.requiredSignatures);
x.id, x.status, x.requiredSignatures);

if (program.verbose)
console.log('* Raw Server Response:\n', x); //TODO
});
console.log('* Raw Server Response:\n', x); //TODO
});
52 changes: 52 additions & 0 deletions bit-wallet/bit-sign
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env node

var _ = require('lodash');
var program = require('commander');
var ClientLib = require('../lib/clientlib.js');
var common = require('./common');

program
.version('0.0.1')
.option('-c,--config [file]', 'Wallet config filename')
.option('-v,--verbose', 'be verbose')
.usage('[options] <txpid>')
.parse(process.argv);

var args = program.args;
if (!args[0])
program.help();

var txpid = args[0];

var cli = new ClientLib({
filename: program.config
});

cli.txProposals({}, function(err, x) {
common.die(err);

if (program.verbose)
console.log('* Raw Server Response:\n', x); //TODO

var txps = _.filter(x, function(x) {
return _.endsWith(common.shortID(x.id), txpid);
});

if (!txps.length)
common.die('Could not find TX Proposal:' + txpid);

if (txps.length > 1)
common.die('More that one TX Proposals match:' + txpid + ' : ' + _.map(txps, function(x) {
return x.id;
}).join(' '));;

var txp = txps[0];
cli.sign(txp, function(err, x) {
common.die(err);

if (program.verbose)
console.log('* Raw Server Response:\n', x); //TODO

console.log('Transaction signed.');
});
});
31 changes: 26 additions & 5 deletions bit-wallet/bit-status
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env node

var _ = require('lodash');
var program = require('commander');
var CliLib = require('../lib/clilib.js');

var ClientLib = require('../lib/clientlib.js');
var common = require('./common');

program
Expand All @@ -11,14 +13,33 @@ program
.parse(process.argv);

var args = program.args;
var cli = new CliLib({
var cli = new ClientLib({
filename: program.config
});

cli.status(function(err, x) {
cli.status(function(err, res) {
common.die(err);

var x = res.wallet;
console.log('* Wallet %s [%s]: %d-%d %s ', x.name, x.isTestnet ? 'testnet' : 'livenet', x.m, x.n, x.status);

if (program.verbose)
console.log('* Raw Server Response:\n', x); //TODO
var x = res.balance;
console.log('* Balance %d (Locked: %d)', x.totalAmount, x.lockedAmount);

if (!_.isEmpty(res.pendingTxps)) {
console.log("* TX Proposals:")
_.each(res.pendingTxps, function(x) {
console.log("\t%s [%s by %s] %dSAT => %s", common.shortID(x.id), x.message, x.creatorName, x.amount, x.toAddress);

if (!_.isEmpty(x.actions)) {
console.log('\t\t * Actions');
console.log('\t\t', _.map(x.actions, function(a) {
return a.copayerName + ': ' + a.type + ''
}).join('. '));
}

if (program.verbose)
console.log('* Raw Server Response:\n', res); //TODO
});
}
});
4 changes: 4 additions & 0 deletions bit-wallet/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ common.parseMN = function(MN) {
};


common.shortID = function(id) {
return id.substr(id.length - 4);
};

module.exports = common;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
},
"dependencies": {
"async": "^0.9.0",
"bitcore": "0.10.0",
"bitcore": "^0.10.3",
"bitcore-explorers": "^0.9.1",
"body-parser": "^1.11.0",
"commander": "^2.6.0",
"express": "^4.10.0",
"inherits": "^2.0.1",
"leveldown": "^0.10.0",
"levelup": "^0.19.0",
"lodash": "^2.4.1",
"lodash": "^3.2.0",
"morgan": "*",
"npmlog": "^0.1.1",
"preconditions": "^1.0.7",
"request": "^2.53.0",
Expand Down

0 comments on commit b54187a

Please sign in to comment.