From 8d382c1e88e85cb4557592c83627d56f9503dac4 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sun, 22 Feb 2015 01:51:24 -0300 Subject: [PATCH] add txproposal -i --- bit-wallet/bit-txproposals | 22 ++++++++++++++++++---- lib/client/api.js | 32 +++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/bit-wallet/bit-txproposals b/bit-wallet/bit-txproposals index 9285dc1..89c2607 100755 --- a/bit-wallet/bit-txproposals +++ b/bit-wallet/bit-txproposals @@ -3,22 +3,36 @@ var _ = require('lodash'); var fs = require('fs'); var program = require('commander'); -var utils = require('./cli-utils'); +var utils = require('./cli-utils'); program = utils.configureCommander(program); program + .option('-i, --input [filename]', 'input file') .option('-o, --output [filename]', 'output file') .parse(process.argv); var args = program.args; var client = utils.getClient(program); -client.getTxProposals({}, function(err, txps) { +var txData; + +function end(err, txps, rawtxps) { utils.die(err); + if (program.input) { + console.log('\n* From File : %s\n', program.input); + } utils.renderTxProposals(txps); if (program.output) { - fs.writeFileSync(program.output, JSON.stringify(txps)); + fs.writeFileSync(program.output, JSON.stringify(rawtxps)); console.log(' * Proposals Saved to: %s\n', program.output); } +}; + -}); +if (program.input) { + var txData = fs.readFileSync(program.input); + txData = JSON.parse(txData); + client.parseTxProposals(txData, end); +} else { + client.getTxProposals({getRawTxps: !!program.output}, end); +} diff --git a/lib/client/api.js b/lib/client/api.js index 1caa8a8..9aef259 100644 --- a/lib/client/api.js +++ b/lib/client/api.js @@ -483,9 +483,35 @@ API.prototype.import = function(str, cb) { }); }; +/** + * + */ + +API.prototype.parseTxProposals = function(txps, cb) { + var self = this; + + this._loadAndCheck(function(err, data) { + if (err) return cb(err); + + _processTxps(txps, data.sharedEncryptingKey); + + var fake = _.any(txps, function(txp) { + return (!Verifier.checkTxProposal(data, txp)); + }); + + if (fake) + return cb(new ServerCompromisedError('Server sent fake transaction proposal')); + + return cb(null, txps); + }); +}; + + + /** * * opts.doNotVerify + * opts.getRawTxps * @return {undefined} */ @@ -498,6 +524,10 @@ API.prototype.getTxProposals = function(opts, cb) { self._doGetRequest(url, data, function(err, txps) { if (err) return cb(err); + var rawTxps; + if (opts.getRawTxps) + rawTxps = JSON.parse(JSON.stringify(txps)); + _processTxps(txps, data.sharedEncryptingKey); var fake = _.any(txps, function(txp) { @@ -507,7 +537,7 @@ API.prototype.getTxProposals = function(opts, cb) { if (fake) return cb(new ServerCompromisedError('Server sent fake transaction proposal')); - return cb(null, txps); + return cb(null, txps, rawTxps); }); }); };