Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #135 from isocolsky/try_join
Browse files Browse the repository at this point in the history
Dry join endpoint
  • Loading branch information
matiu committed Sep 18, 2015
2 parents b87ad09 + 8cd2088 commit ed984c7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
29 changes: 22 additions & 7 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
});
};

/**
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
35 changes: 23 additions & 12 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -397,18 +397,18 @@ 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();
});
});
});
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();
});
Expand All @@ -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();
});
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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) {
Expand All @@ -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');
Expand All @@ -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);
Expand Down Expand Up @@ -627,8 +640,6 @@ describe('client API', function() {
});
});
});


});

describe('Network fees', function() {
Expand Down

0 comments on commit ed984c7

Please sign in to comment.