From 8cd20881c532d90fb7b76839ec3d2157ac42bb0a Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Thu, 17 Sep 2015 16:00:40 -0300 Subject: [PATCH] joinWallet dry run --- lib/api.js | 29 ++++++++++++++++++++++------- package.json | 4 ++-- test/client.js | 35 +++++++++++++++++++++++------------ 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/lib/api.js b/lib/api.js index 4d6421e4..1ab2d1fe 100644 --- a/lib/api.js +++ b/lib/api.js @@ -591,6 +591,8 @@ API.prototype._doJoinWallet = function(walletId, walletPrivKey, xPubKey, request requestPubKey: requestPubKey, customData: encCustomData, }; + if (opts.dryRun) args.dryRun = true; + if (_.isBoolean(opts.supportBIP44AndP2PKH)) args.supportBIP44AndP2PKH = opts.supportBIP44AndP2PKH; @@ -787,16 +789,26 @@ API.prototype.createWallet = function(walletName, copayerName, m, n, opts, cb) { }; /** - * Join to an existent wallet + * Join an existent wallet * * @param {String} secret * @param {String} copayerName + * @param {Object} opts + * @param {Boolean} opts.dryRun[=false] - Simulate wallet join * @param {Callback} cb * @returns {Callback} cb - Returns the wallet */ -API.prototype.joinWallet = function(secret, copayerName, cb) { +API.prototype.joinWallet = function(secret, copayerName, opts, cb) { var self = this; + if (!cb) { + cb = opts; + opts = {}; + log.warn('DEPRECATED WARN: joinWallet should receive 4 parameters.') + } + + opts = opts || {}; + try { var secretData = WalletUtils.fromSecret(secret); } catch (ex) { @@ -807,12 +819,15 @@ API.prototype.joinWallet = function(secret, copayerName, cb) { self.seedFromRandom(secretData.network); } - self._doJoinWallet(secretData.walletId, secretData.walletPrivKey, self.credentials.xPubKey, self.credentials.requestPubKey, copayerName, {}, - function(err, wallet) { - if (err) return cb(err); + self._doJoinWallet(secretData.walletId, secretData.walletPrivKey, self.credentials.xPubKey, self.credentials.requestPubKey, copayerName, { + dryRun: !!opts.dryRun, + }, function(err, wallet) { + if (err) return cb(err); + if (!opts.dryRun) { self.credentials.addWalletInfo(wallet.id, wallet.name, wallet.m, wallet.n, secretData.walletPrivKey.toString(), copayerName); - return cb(null, wallet); - }); + } + return cb(null, wallet); + }); }; /** diff --git a/package.json b/package.json index 6fe553f6..e0d5aa6c 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "bitcore-wallet-client", "description": "Client for bitcore-wallet-service", "author": "BitPay Inc", - "version": "0.4.2", + "version": "0.5.0", "keywords": [ "bitcoin", "copay", @@ -35,7 +35,7 @@ "uglify": "^0.1.1" }, "devDependencies": { - "bitcore-wallet-service": "0.2.2", + "bitcore-wallet-service": "0.2.3", "chai": "^1.9.1", "coveralls": "^2.11.2", "grunt-jsdoc": "^0.5.8", diff --git a/test/client.js b/test/client.js index 715bdb9a..5d71a3e5 100644 --- a/test/client.js +++ b/test/client.js @@ -76,7 +76,7 @@ helpers.createAndJoinWallet = function(clients, m, n, cb) { function(next) { async.each(_.range(1, n), function(i, cb) { clients[i].seedFromRandomWithMnemonic('testnet'); - clients[i].joinWallet(secret, 'copayer ' + i, cb); + clients[i].joinWallet(secret, 'copayer ' + i, {}, cb); }, next); }, function(next) { @@ -382,7 +382,7 @@ describe('client API', function() { should.not.exist(err); clients[0].isComplete().should.equal(false); clients[0].credentials.isComplete().should.equal(false); - clients[1].joinWallet(secret, 'guest', function(err) { + clients[1].joinWallet(secret, 'guest', {}, function(err) { should.not.exist(err); clients[0].openWallet(function(err, walletStatus) { should.not.exist(err); @@ -397,7 +397,7 @@ describe('client API', function() { it('should not allow to join a full wallet ', function(done) { helpers.createAndJoinWallet(clients, 2, 2, function(w) { should.exist(w.secret); - clients[4].joinWallet(w.secret, 'copayer', function(err, result) { + clients[4].joinWallet(w.secret, 'copayer', {}, function(err, result) { err.code.should.contain('WALLET_FULL'); done(); }); @@ -405,10 +405,10 @@ describe('client API', function() { }); it('should fail with an invalid secret', function(done) { // Invalid - clients[0].joinWallet('dummy', 'copayer', function(err, result) { + clients[0].joinWallet('dummy', 'copayer', {}, function(err, result) { err.message.should.contain('Invalid secret'); // Right length, invalid char for base 58 - clients[0].joinWallet('DsZbqNQQ9LrTKU8EknR7gFKyCQMPg2UUHNPZ1BzM5EbJwjRZaUNBfNtdWLluuFc0f7f7sTCkh7T', 'copayer', function(err, result) { + clients[0].joinWallet('DsZbqNQQ9LrTKU8EknR7gFKyCQMPg2UUHNPZ1BzM5EbJwjRZaUNBfNtdWLluuFc0f7f7sTCkh7T', 'copayer', {}, function(err, result) { err.message.should.contain('Invalid secret'); done(); }); @@ -417,7 +417,7 @@ describe('client API', function() { it('should fail with an unknown secret', function(done) { // Unknown walletId var oldSecret = '3bJKRn1HkQTpwhVaJMaJ22KwsjN24ML9uKfkSrP7iDuq91vSsTEygfGMMpo6kWLp1pXG9wZSKcT'; - clients[0].joinWallet(oldSecret, 'copayer', function(err, result) { + clients[0].joinWallet(oldSecret, 'copayer', {}, function(err, result) { err.code.should.equal('WALLET_NOT_FOUND'); done(); }); @@ -476,6 +476,22 @@ describe('client API', function() { }); }); }); + it('should perform a dry join without actually joining', function(done) { + clients[0].createWallet('wallet name', 'creator', 1, 2, {}, function(err, secret) { + should.not.exist(err); + should.exist(secret); + clients[1].joinWallet(secret, 'dummy', { + dryRun: true + }, function(err, wallet) { + should.not.exist(err); + should.exist(wallet); + wallet.status.should.equal('pending'); + wallet.copayers.length.should.equal(1); + done(); + }); + }); + }); + it('should return wallet status even if wallet is not yet complete', function(done) { clients[0].createWallet('wallet name', 'creator', 1, 2, { network: 'testnet' @@ -542,7 +558,7 @@ describe('client API', function() { }); }); - it('should set walletPrivKey from BWS', function(done) { + it('should set walletPrivKey from BWS', function(done) { clients[0].createWallet('wallet name', 'creator', 1, 1, { network: 'testnet' }, function(err) { @@ -563,8 +579,6 @@ describe('client API', function() { }); }); - - it('should prepare wallet with external xpubkey', function(done) { var client = helpers.newClient(app); client.seedFromExtendedPublicKey('xpub661MyMwAqRbcGVyYUcHbZi9KNhN9Tdj8qHi9ZdoUXP1VeKiXDGGrE9tSoJKYhGFE2rimteYdwvoP6e87zS5LsgcEvsvdrpPBEmeWz9EeAUq', 'ledger', '1a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f001a1f00'); @@ -591,7 +605,6 @@ describe('client API', function() { }); }); - it('should create a 1-1 wallet with given mnemonic', function(done) { var words = 'forget announce travel fury farm alpha chaos choice talent sting eagle supreme'; clients[0].seedFromMnemonic(words); @@ -627,8 +640,6 @@ describe('client API', function() { }); }); }); - - }); describe('Network fees', function() {