diff --git a/app.js b/app.js index 4198b52..388a4d1 100644 --- a/app.js +++ b/app.js @@ -75,7 +75,7 @@ function getServerWithAuth(req, res, cb) { var credentials = getCredentials(req); var auth = { copayerId: credentials.copayerId, - message: req.url + '|' + JSON.stringify(req.body), + message: req.url + req.body ? '|' + JSON.stringify(req.body) : '', signature: credentials.signature, }; @@ -87,20 +87,20 @@ function getServerWithAuth(req, res, cb) { router.post('/v1/wallets/', function(req, res) { var server = CopayServer.getInstance(); - server.createWallet(req.body, function(err, wallet) { + server.createWallet(req.body, function(err, walletId) { if (err) returnError(err, res); - res.json(wallet); + res.json(walletId); }); }); router.post('/v1/wallets/:id/copayers/', function(req, res) { req.body.walletId = req.params['id']; var server = CopayServer.getInstance(); - server.joinWallet(req.body, function(err) { + server.joinWallet(req.body, function(err, copayerId) { if (err) returnError(err, res); - res.end(); + res.json(copayerId); }); }); diff --git a/cli.js b/cli.js new file mode 100644 index 0000000..39c7fd0 --- /dev/null +++ b/cli.js @@ -0,0 +1,24 @@ +var _ = require('lodash'); +var async = require('async'); +var log = require('npmlog'); +var fs = require('fs'); + +var clilib = require('./lib/clilib'); + +fs.unlinkSync('.bit'); + +clilib.createWallet('my wallet', 'me', 1, 1, function(err, secret) { + if (err) { + console.log(err); + process.exit(); + } + + clilib.status(function(err, status) { + if (err) { + console.log(err); + process.exit(); + } + + console.log(status); + }) +}); diff --git a/lib/clilib.js b/lib/clilib.js index 6c0a52a..3f87b77 100644 --- a/lib/clilib.js +++ b/lib/clilib.js @@ -4,7 +4,6 @@ var _ = require('lodash'); var async = require('async'); var log = require('npmlog'); var request = require('request') -var commander = require('commander') log.debug = log.verbose; log.level = 'debug'; var fs = require('fs') @@ -14,7 +13,7 @@ var SignUtils = require('./signutils'); var BASE_URL = 'http://localhost:3001/copay/api/'; -var cli = {}; +var clilib = {}; function _getUrl(path) { @@ -22,29 +21,33 @@ function _getUrl(path) { }; -function signRequest(url, args) { +function _signRequest(url, args) { }; -function save(data) { - fs.writeFileSync('./.bit', JSON.stringify(data)); +function _save(data) { + fs.writeFileSync('.bit', JSON.stringify(data)); }; -function load() { +function _load() { try { - return JSON.parse(fs.readFileSync('./.bit')); + return JSON.parse(fs.readFileSync('.bit')); } catch (ex) {} }; - +function _createXPrivKey() { + return new Bitcore.HDPrivateKey().toString(); +}; clilib.createWallet = function(walletName, copayerName, m, n, cb) { - var data = load(); - if (!data) { - data = {}; - data.xPrivKey = new Bitcore.HDPrivateKey().toString(); - data.m = m; - } + var data = _load(); + if (data) return cb('Only one wallet can exist'); + + data = { + xPrivKey: _createXPrivKey(), + m: m, + }; + var privKey = new Bitcore.PrivateKey(); var pubKey = privKey.toPublicKey(); @@ -63,23 +66,27 @@ clilib.createWallet = function(walletName, copayerName, m, n, cb) { }, function(err, res, body) { if (err) return cb(err); var walletId = body; - var secret = walletId + '|' + privKey.toString(); + data.secret = walletId + '|' + privKey.toString(); - joinWallet(secret, copayerName, function(err) { + _save(data); + + clilib.joinWallet(data.secret, copayerName, function(err) { if (err) return cb(err); - save(data); - return cb(null, secret); + return cb(null, data.secret); }); }); }; clilib.joinWallet = function(secret, copayerName, cb) { - var data = load(); + var data = _load(); + if (data && data.copayerId) return cb('Only one wallet can exist'); if (!data) { - data = {}; - data.xPrivKey = new Bitcore.HDPrivateKey().toString(); + data = { + xPrivKey: _createXPrivKey(), + }; } + var secretSplit = secret.split('|'); var walletId = secretSplit[0]; var privKey = Bitcore.PrivateKey.fromString(secretSplit[1]); @@ -102,23 +109,35 @@ clilib.joinWallet = function(secret, copayerName, cb) { json: true, }, function(err, res, body) { if (err) return cb(err); - var copayerId = body; data.copayerId = copayerId; - save(data); - return status(cb); + + _save(data); + + // TODO: call status to retrieve wallet.m + + return clilib.status(cb); }); }; clilib.status = function(cb) { + var data = _load(); + if (!data || !data.copayerId) return cb('Not a part of an active wallet'); + + var url = 'v1/dump/'; + var signature = _signRequest(url); + request({ + headers: { + 'x-identity': data.copayerId, + 'x-signature': signature, + }, method: 'get', - url: _getUrl('v1/dump/'), + url: _getUrl(url), }, function(err, res, body) { if (err) return cb(err); - console.log(body); - return cb(); + return cb(null, body); }); };