diff --git a/bit-wallet/cli-utils.js b/bit-wallet/cli-utils.js index 429423b..91dc89f 100644 --- a/bit-wallet/cli-utils.js +++ b/bit-wallet/cli-utils.js @@ -53,12 +53,30 @@ Utils.getClient = function(args) { if (args.password) { return cb(args.password); } else { - read({ prompt: 'Password: ', silent: true }, function(er, password) { + read({ + prompt: 'Password for ' + args.file + ' : ', + silent: true + }, function(er, password) { return cb(password); }) } }); + c.on('needNewPassword', function(cb) { + if (args.password) { + return cb(args.password); + } else { + read({ + prompt: 'New Password: ', + silent: true + }, function(er, password) { + return cb(password); + }) + } + }); + + + return c; } diff --git a/lib/client/api.js b/lib/client/api.js index 0077028..2dfc0f7 100644 --- a/lib/client/api.js +++ b/lib/client/api.js @@ -238,7 +238,7 @@ API.prototype._processWcdBeforeWrite = function(wcd, cb) { if (this.noPasswdAccess == 'full') { return cb(null, wcd); } else { - this.emit('needPassword', function(password) { + this.emit('needNewPassword', function(password) { if (!password) return cb('No password given'); var ewcd = WalletUtils.encryptWallet(wcd, self.noPasswdAccess, password); return cb(null, ewcd); @@ -254,7 +254,8 @@ API.prototype._load = function(opts, cb) { this.storage.load(function(err, rawdata) { if (err || !rawdata) { - return cb(err || 'wcd file not found.'); + if (err && err.code == 'ENOENT') err = 'NOTFOUND'; + return cb(err || 'NOTFOUND'); } self._processWcdAfterRead(rawdata, opts.requiredAccess, cb); }); @@ -382,7 +383,12 @@ API.prototype.createWallet = function(walletName, copayerName, m, n, network, cb if (!_.contains(['testnet', 'livenet'], network)) return cb('Invalid network'); - this.storage.load(function(err, wcd) { + this._load({ + requiredAccess: 'readonly', + }, function(err, wcd) { + if (err && err != 'NOTFOUND') + return cb(err); + if (wcd && wcd.n) return cb(self.storage.getName() + ' already contains a wallet'); @@ -457,7 +463,12 @@ API.prototype.reCreateWallet = function(walletName, cb) { API.prototype.joinWallet = function(secret, copayerName, cb) { var self = this; - this.storage.load(function(err, wcd) { + this._load({ + requiredAccess: 'readonly' + }, function(err, wcd) { + if (err && err != 'NOTFOUND') + return cb(err); + if (wcd && wcd.n) return cb(self.storage.getName() + ' already contains a wallet'); diff --git a/test/integration/clientApi.js b/test/integration/clientApi.js index c132c0d..11d8cbb 100644 --- a/test/integration/clientApi.js +++ b/test/integration/clientApi.js @@ -65,7 +65,7 @@ var fsmock = {}; var content = {}; fsmock.readFile = function(name, enc, cb) { if (!content || _.isEmpty(content[name])) - return cb('empty'); + return cb('NOTFOUND'); return cb(null, content[name]); }; @@ -227,6 +227,10 @@ describe('client API ', function() { clients[i].on('needPassword', function(cb) { return cb('1234#$@#%F,./.**'); }); + clients[i].on('needNewPassword', function(cb) { + return cb('1234#$@#%F,./.**'); + }); + }); }); @@ -637,7 +641,6 @@ describe('client API ', function() { }, function(err, str) { proxy.import(str, function(err) { should.not.exist(err); - proxy.createWallet('1', '2', 1, 1, 'testnet', function(err) { should.not.exist(err);