fix address creation when wallet not complete

This commit is contained in:
Ivan Socolsky 2015-02-08 11:47:04 -03:00
parent 70475fa17c
commit 1ba97a3883
3 changed files with 50 additions and 13 deletions

View File

@ -2,6 +2,7 @@
var _ = require('lodash'); var _ = require('lodash');
var util = require('util'); var util = require('util');
var $ = require('preconditions').singleton();
var Bitcore = require('bitcore'); var Bitcore = require('bitcore');
var BitcoreAddress = Bitcore.Address; var BitcoreAddress = Bitcore.Address;
@ -111,7 +112,13 @@ Wallet.prototype.getPublicKey = function(copayerId, path) {
return copayer.getPublicKey(path); return copayer.getPublicKey(path);
}; };
Wallet.prototype.isComplete = function() {
return this.status == 'complete';
};
Wallet.prototype.createAddress = function(isChange) { Wallet.prototype.createAddress = function(isChange) {
$.checkState(this.isComplete());
var path = this.addressManager.getNewAddressPath(isChange); var path = this.addressManager.getNewAddressPath(isChange);
var publicKeys = _.map(this.copayers, function(copayer) { var publicKeys = _.map(this.copayers, function(copayer) {

View File

@ -113,7 +113,7 @@ CopayServer.prototype.createWallet = function(opts, cb) {
}); });
self.storage.storeWallet(wallet, function(err) { self.storage.storeWallet(wallet, function(err) {
return cb(err,wallet.id); return cb(err, wallet.id);
}); });
}; };
@ -200,6 +200,7 @@ CopayServer.prototype.createAddress = function(opts, cb) {
Utils.runLocked(self.walletId, cb, function(cb) { Utils.runLocked(self.walletId, cb, function(cb) {
self.getWallet({}, function(err, wallet) { self.getWallet({}, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
if (!wallet.isComplete()) return cb(new ClientError('Wallet is not complete'));
var address = wallet.createAddress(opts.isChange); var address = wallet.createAddress(opts.isChange);

View File

@ -349,7 +349,7 @@ describe('Copay server', function() {
n: 1, n: 1,
pubKey: keyPair.pub, pubKey: keyPair.pub,
}; };
server.createWallet(walletOpts, function(err,walletId) { server.createWallet(walletOpts, function(err, walletId) {
should.not.exist(err); should.not.exist(err);
var copayer1Opts = { var copayer1Opts = {
walletId: walletId, walletId: walletId,
@ -568,7 +568,37 @@ describe('Copay server', function() {
}); });
}); });
it.skip('should fail to create address when wallet is not complete', function(done) {}); it('should fail to create address when wallet is not complete', function(done) {
var server = new CopayServer();
var walletOpts = {
name: 'my wallet',
m: 2,
n: 3,
pubKey: keyPair.pub,
};
server.createWallet(walletOpts, function(err, walletId) {
should.not.exist(err);
var copayerOpts = {
walletId: walletId,
name: 'me',
xPubKey: aXPubKey,
xPubKeySignature: aXPubKeySignature,
};
server.joinWallet(copayerOpts, function(err, copayerId) {
should.not.exist(err);
helpers.getAuthServer(copayerId, function(server) {
server.createAddress({
isChange: false,
}, function(err, address) {
should.not.exist(address);
err.should.exist;
err.message.should.contain('not complete');
done();
});
});
});
});
});
it('should create many addresses on simultaneous requests', function(done) { it('should create many addresses on simultaneous requests', function(done) {
async.map(_.range(10), function(i, cb) { async.map(_.range(10), function(i, cb) {
@ -1006,8 +1036,7 @@ describe('Copay server', function() {
}); });
}, function(err) { }, function(err) {
return done(err); return done(err);
} });
);
}); });
}); });
}); });