From 437eaa476b28f94efc31e127eba6d44d8dacd918 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sun, 15 Feb 2015 11:33:04 -0300 Subject: [PATCH] move / rename client files --- .gitignore | 3 ++- TODO | 13 +++++++++ bit-wallet/bit-address | 6 ++--- bit-wallet/bit-addresses | 6 ++--- bit-wallet/bit-balance | 6 ++--- bit-wallet/bit-create | 2 +- bit-wallet/bit-join | 2 +- bit-wallet/bit-reject | 8 +++--- bit-wallet/bit-send | 6 ++--- bit-wallet/bit-sign | 6 ++--- bit-wallet/bit-status | 6 ++--- lib/{clientlib.js => client/API.js} | 42 +++++++++++++++-------------- lib/client/index.js | 8 ++++++ lib/index.js | 0 14 files changed, 69 insertions(+), 45 deletions(-) create mode 100644 TODO rename lib/{clientlib.js => client/API.js} (88%) create mode 100644 lib/client/index.js create mode 100644 lib/index.js diff --git a/.gitignore b/.gitignore index 4e23ec8..9bf8925 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Logs logs *.log +*.sw* # Runtime data pids @@ -31,4 +32,4 @@ node_modules out/ db/ -.bit \ No newline at end of file +.bit diff --git a/TODO b/TODO new file mode 100644 index 0000000..9333c3c --- /dev/null +++ b/TODO @@ -0,0 +1,13 @@ +ClientLib +- check derive address +- check change address +- remove storage from clientlib +- check prposal signature +- check xpriv keys correspond to wallet's network +- check secret format in join +- test raw tx have signatures +- add broadcast API to server +- enhance 'no network (internet)' error +- /Users/ematiu/devel/bitcore-wallet-service/node_modules/bitcore/lib/transaction/transaction.js:137 + throw new errors.Transaction.DustOutputs(); + and others TX errores now appear at broadcast time. diff --git a/bit-wallet/bit-address b/bit-wallet/bit-address index 217b78e..e3f2f16 100755 --- a/bit-wallet/bit-address +++ b/bit-wallet/bit-address @@ -1,7 +1,7 @@ #!/usr/bin/env node var program = require('commander'); -var ClientLib = require('../lib/clientlib.js'); +var Client = require('../lib/client'); var common = require('./common'); program @@ -11,11 +11,11 @@ program .parse(process.argv); var args = program.args; -var cli = new ClientLib({ +var cli = new Client({ filename: program.config }); -cli.address(function(err, x) { +cli.createAddress(function(err, x) { common.die(err); console.log('* New Address %s ', x.address); diff --git a/bit-wallet/bit-addresses b/bit-wallet/bit-addresses index 19614b1..e2f6daf 100755 --- a/bit-wallet/bit-addresses +++ b/bit-wallet/bit-addresses @@ -3,7 +3,7 @@ var _ = require('lodash'); var program = require('commander'); var common = require('./common'); -var ClientLib = require('../lib/clientlib.js'); +var Client = require('../lib/client'); program .version('0.0.1') @@ -12,11 +12,11 @@ program .parse(process.argv); var args = program.args; -var cli = new ClientLib({ +var cli = new Client({ filename: program.config }); -cli.addresses(function(err, x) { +cli.getAddresses(function(err, x) { common.die(err); console.log('* Addresses:'); diff --git a/bit-wallet/bit-balance b/bit-wallet/bit-balance index 579bc07..e9f9fb9 100755 --- a/bit-wallet/bit-balance +++ b/bit-wallet/bit-balance @@ -1,7 +1,7 @@ #!/usr/bin/env node var program = require('commander'); -var ClientLib = require('../lib/clientlib.js'); +var Client = require('../lib/client'); var common = require('./common'); program @@ -11,11 +11,11 @@ program .parse(process.argv); var args = program.args; -var cli = new ClientLib({ +var cli = new Client({ filename: program.config }); -cli.balance(function(err, x) { +cli.getBalance(function(err, x) { common.die(err); console.log('* Wallet balance', x); diff --git a/bit-wallet/bit-create b/bit-wallet/bit-create index 710838e..4d2d33e 100755 --- a/bit-wallet/bit-create +++ b/bit-wallet/bit-create @@ -1,7 +1,7 @@ #!/usr/bin/env node var program = require('commander'); -var ClientLib = require('../lib/clientlib.js'); +var ClientLib = require('../lib/client'); var common = require('./common'); program diff --git a/bit-wallet/bit-join b/bit-wallet/bit-join index f2a3424..15c7ec2 100755 --- a/bit-wallet/bit-join +++ b/bit-wallet/bit-join @@ -1,7 +1,7 @@ #!/usr/bin/env node var program = require('commander'); -var ClientLib = require('../lib/clientlib.js'); +var ClientLib = require('../lib/client'); var common = require('./common'); program diff --git a/bit-wallet/bit-reject b/bit-wallet/bit-reject index 766f066..b4bb33f 100644 --- a/bit-wallet/bit-reject +++ b/bit-wallet/bit-reject @@ -2,7 +2,7 @@ var _ = require('lodash'); var program = require('commander'); -var ClientLib = require('../lib/clientlib.js'); +var Client = require('../lib/client'); var common = require('./common'); program @@ -18,11 +18,11 @@ if (!args[0]) var txpid = args[0]; -var cli = new ClientLib({ +var cli = new Client({ filename: program.config }); -cli.txProposals({}, function(err, x) { +cli.getTxProposals({}, function(err, x) { common.die(err); if (program.verbose) @@ -41,7 +41,7 @@ cli.txProposals({}, function(err, x) { }).join(' '));; var txp = txps[0]; - cli.reject(txp, function(err, x) { + cli.rejectTxProposal(txp, function(err, x) { common.die(err); if (program.verbose) diff --git a/bit-wallet/bit-send b/bit-wallet/bit-send index 7603bc5..3cadc28 100755 --- a/bit-wallet/bit-send +++ b/bit-wallet/bit-send @@ -1,7 +1,7 @@ #!/usr/bin/env node var program = require('commander'); -var ClientLib = require('../lib/clientlib.js'); +var Client = require('../lib/client'); var common = require('./common'); program @@ -19,11 +19,11 @@ if (!args[0] || !args[1] || !args[2]) var amount = args[1]; var message = args[2]; -var cli = new ClientLib({ +var cli = new Client({ filename: program.config }); -cli.send({toAddress: address, amount: amount, message:message}, function(err, x) { +cli.sendTxProposal({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); diff --git a/bit-wallet/bit-sign b/bit-wallet/bit-sign index c284490..68289a6 100755 --- a/bit-wallet/bit-sign +++ b/bit-wallet/bit-sign @@ -2,7 +2,7 @@ var _ = require('lodash'); var program = require('commander'); -var ClientLib = require('../lib/clientlib.js'); +var ClientLib = require('../lib/client'); var common = require('./common'); program @@ -22,7 +22,7 @@ var cli = new ClientLib({ filename: program.config }); -cli.txProposals({}, function(err, x) { +cli.getTxProposals({}, function(err, x) { common.die(err); if (program.verbose) @@ -41,7 +41,7 @@ cli.txProposals({}, function(err, x) { }).join(' '));; var txp = txps[0]; - cli.sign(txp, function(err, x) { + cli.signTxProposal(txp, function(err, x) { common.die(err); if (program.verbose) diff --git a/bit-wallet/bit-status b/bit-wallet/bit-status index 8fe542d..77bd5d6 100755 --- a/bit-wallet/bit-status +++ b/bit-wallet/bit-status @@ -3,7 +3,7 @@ var _ = require('lodash'); var program = require('commander'); -var ClientLib = require('../lib/clientlib.js'); +var Client = require('../lib/client'); var common = require('./common'); program @@ -13,11 +13,11 @@ program .parse(process.argv); var args = program.args; -var cli = new ClientLib({ +var cli = new Client({ filename: program.config }); -cli.status(function(err, res) { +cli.getStatus(function(err, res) { common.die(err); var x = res.wallet; diff --git a/lib/clientlib.js b/lib/client/API.js similarity index 88% rename from lib/clientlib.js rename to lib/client/API.js index 6993354..615c58c 100644 --- a/lib/clientlib.js +++ b/lib/client/API.js @@ -9,7 +9,7 @@ log.level = 'debug'; var fs = require('fs') var Bitcore = require('bitcore') -var SignUtils = require('./signutils'); +var SignUtils = require('../signutils'); var BASE_URL = 'http://localhost:3001/copay/api'; @@ -47,24 +47,24 @@ function _createXPrivKey(network) { return new Bitcore.HDPrivateKey(network).toString(); }; -function ClientLib(opts) { +function API(opts) { if (!opts.filename) { throw new Error('Please set the config filename'); } this.filename = opts.filename; }; -ClientLib.prototype._save = function(data) { +API.prototype._save = function(data) { fs.writeFileSync(this.filename, JSON.stringify(data)); }; -ClientLib.prototype._load = function() { +API.prototype._load = function() { try { return JSON.parse(fs.readFileSync(this.filename)); } catch (ex) {} }; -ClientLib.prototype._loadAndCheck = function() { +API.prototype._loadAndCheck = function() { var data = this._load(); if (!data) { log.error('Wallet file not found.'); @@ -107,17 +107,17 @@ ClientLib.prototype._doRequest = function(method, url, args, data, cb) { }; -ClientLib.prototype._doPostRequest = function(url, args, data, cb) { +API.prototype._doPostRequest = function(url, args, data, cb) { return this._doRequest('post', url, args, data, cb); }; -ClientLib.prototype._doGetRequest = function(url, data, cb) { +API.prototype._doGetRequest = function(url, data, cb) { return this._doRequest('get', url, {}, data, cb); }; -ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, network, cb) { +API.prototype.createWallet = function(walletName, copayerName, m, n, network, cb) { var self = this; network = network || 'livenet'; if (!_.contains(['testnet', 'livenet'], network)) @@ -146,6 +146,8 @@ ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, netwo var url = '/v1/wallets/'; this._doPostRequest(url, args, data, function(err, body) { + if (err) return cb(err); + var walletId = body.walletId; var secret = walletId + ':' + privKey.toString() + ':' + (network == 'testnet' ? 'T' : 'L'); data.secret = secret; @@ -159,7 +161,7 @@ ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, netwo }); }; -ClientLib.prototype._joinWallet = function(data, secret, copayerName, cb) { +API.prototype._joinWallet = function(data, secret, copayerName, cb) { var self = this; data = data || {}; @@ -196,7 +198,7 @@ ClientLib.prototype._joinWallet = function(data, secret, copayerName, cb) { }); }; -ClientLib.prototype.joinWallet = function(secret, copayerName, cb) { +API.prototype.joinWallet = function(secret, copayerName, cb) { var self = this; var data = this._load(); @@ -205,7 +207,7 @@ ClientLib.prototype.joinWallet = function(secret, copayerName, cb) { self._joinWallet(data, secret, copayerName, cb); }; -ClientLib.prototype.status = function(cb) { +API.prototype.getStatus = function(cb) { var self = this; var data = this._loadAndCheck(); @@ -249,7 +251,7 @@ ClientLib.prototype.status = function(cb) { * @param inArgs.amount * @param inArgs.message */ -ClientLib.prototype.send = function(inArgs, cb) { +API.prototype.sendTxProposal = function(inArgs, cb) { var self = this; var data = this._loadAndCheck(); @@ -260,7 +262,7 @@ ClientLib.prototype.send = function(inArgs, cb) { }; // Get addresses -ClientLib.prototype.addresses = function(cb) { +API.prototype.getAddresses = function(cb) { var self = this; var data = this._loadAndCheck(); @@ -272,7 +274,7 @@ ClientLib.prototype.addresses = function(cb) { // Creates a new address // TODO: verify derivation!! -ClientLib.prototype.address = function(cb) { +API.prototype.createAddress = function(cb) { var self = this; var data = this._loadAndCheck(); @@ -281,11 +283,11 @@ ClientLib.prototype.address = function(cb) { this._doPostRequest(url, {}, data, cb); }; -ClientLib.prototype.history = function(limit, cb) { +API.prototype.history = function(limit, cb) { }; -ClientLib.prototype.balance = function(cb) { +API.prototype.getBalance = function(cb) { var self = this; var data = this._loadAndCheck(); @@ -295,7 +297,7 @@ ClientLib.prototype.balance = function(cb) { }; -ClientLib.prototype.txProposals = function(opts, cb) { +API.prototype.getTxProposals = function(opts, cb) { var self = this; var data = this._loadAndCheck(); @@ -304,7 +306,7 @@ ClientLib.prototype.txProposals = function(opts, cb) { this._doGetRequest(url, data, cb); }; -ClientLib.prototype.sign = function(txp, cb) { +API.prototype.signTxProposal = function(txp, cb) { var self = this; var data = this._loadAndCheck(); @@ -346,7 +348,7 @@ ClientLib.prototype.sign = function(txp, cb) { this._doPostRequest(url, args, data, cb); }; -ClientLib.prototype.reject = function(txp, reason, cb) { +API.prototype.rejectTxProposal = function(txp, reason, cb) { var self = this; var data = this._loadAndCheck(); @@ -357,4 +359,4 @@ ClientLib.prototype.reject = function(txp, reason, cb) { this._doPostRequest(url, args, data, cb); }; -module.exports = ClientLib; +module.exports = API; diff --git a/lib/client/index.js b/lib/client/index.js new file mode 100644 index 0000000..098387b --- /dev/null +++ b/lib/client/index.js @@ -0,0 +1,8 @@ +//var client = ; + +module.exports = require('./API'); +//client.verificator = require('./API'); + + +// TODO +//module.exports.storage = require('./storage'); diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..e69de29