From 238d36c0a724dc881bb07f7f1653faf751623840 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 18 Sep 2014 18:29:00 -0300 Subject: [PATCH] fix pubkey param. add test --- js/models/core/WalletFactory.js | 42 +++++++++++++++++++-------------- test/test.WalletFactory.js | 9 ++++++- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/js/models/core/WalletFactory.js b/js/models/core/WalletFactory.js index b1594937c..88770d0d8 100644 --- a/js/models/core/WalletFactory.js +++ b/js/models/core/WalletFactory.js @@ -114,12 +114,12 @@ WalletFactory.prototype.fromObj = function(inObj, skipFields) { /** * @desc Imports a wallet from an encrypted base64 object * @param {string} base64 - the base64 encoded object - * @param {string} password - password to decrypt it + * @param {string} passphrase - passphrase to decrypt it * @param {string[]} skipFields - fields to ignore when importing * @return {Wallet} */ -WalletFactory.prototype.fromEncryptedObj = function(base64, password, skipFields) { - this.storage.setPassphrase(password); +WalletFactory.prototype.fromEncryptedObj = function(base64, passphrase, skipFields) { + this.storage.setPassphrase(passphrase); var walletObj = this.storage.import(base64); if (!walletObj) return false; return this.fromObj(walletObj, skipFields); @@ -130,15 +130,15 @@ WalletFactory.prototype.fromEncryptedObj = function(base64, password, skipFields * @TODO: this is essentialy the same method as {@link WalletFactory#fromEncryptedObj}! * @desc Imports a wallet from an encrypted base64 object * @param {string} base64 - the base64 encoded object - * @param {string} password - password to decrypt it + * @param {string} passphrase - passphrase to decrypt it * @param {string[]} skipFields - fields to ignore when importing * @return {Wallet} */ -WalletFactory.prototype.import = function(base64, password, skipFields) { +WalletFactory.prototype.import = function(base64, passphrase, skipFields) { var self = this; - var w = self.fromEncryptedObj(base64, password, skipFields); + var w = self.fromEncryptedObj(base64, passphrase, skipFields); - if (!w) throw new Error('Wrong password'); + if (!w) throw new Error('Wrong passphrase'); return w; }; @@ -248,7 +248,6 @@ WalletFactory.prototype.create = function(opts, cb) { }); log.debug('\t### TxProposals Initialized'); - this.storage.setPassphrase(opts.passphrase); opts.storage = this.storage; opts.network = this.networks[opts.networkName]; @@ -260,6 +259,7 @@ WalletFactory.prototype.create = function(opts, cb) { opts.totalCopayers = totalCopayers; opts.version = opts.version || this.version; + this.storage.setPassphrase(opts.passphrase); var w = this._getWallet(opts); var self = this; w.store(function(err) { @@ -394,7 +394,7 @@ WalletFactory.prototype.joinCreateSession = function(opts, cb) { //Create our PrivateK var privateKey = new PrivateKey(privOpts); log.debug('\t### PrivateKey Initialized'); - var opts = { + var joinOpts = { copayerId: privateKey.getId(), privkey: privateKey.getIdPriv(), key: privateKey.getIdKey(), @@ -414,23 +414,29 @@ WalletFactory.prototype.joinCreateSession = function(opts, cb) { return cb('joinError'); }); - joinNetwork.start(opts, function() { - joinNetwork.greet(decodedSecret.pubKey, opts.secretNumber); + joinNetwork.start(joinOpts, function() { + + joinNetwork.greet(decodedSecret.pubKey, joinOpts.secretNumber); joinNetwork.on('data', function(sender, data) { if (data.type === 'walletId' && data.opts) { if (data.networkName !== decodedSecret.networkName) { return cb('badNetwork'); } - data.opts.privateKey = privateKey; - data.opts.nickname = opts.nickname; - data.opts.passphrase = opts.passphrase; - data.opts.id = data.walletId; - self.create(data.opts, function(err, w) { - if (!err & w) { - w.sendWalletReady(s.pubKey); + var walletOpts = _.clone(data.opts); + walletOpts.id = data.walletId; + + walletOpts.privateKey = privateKey; + walletOpts.nickname = opts.nickname; + walletOpts.passphrase = opts.passphrase; + + self.create(walletOpts, function(err, w) { + + if (w) { + w.sendWalletReady(decodedSecret.pubKey); } else { if (!err) err = 'walletFull'; + log.info(err); } return cb(err, w); }); diff --git a/test/test.WalletFactory.js b/test/test.WalletFactory.js index 517bf44bd..0b1a16d3a 100644 --- a/test/test.WalletFactory.js +++ b/test/test.WalletFactory.js @@ -443,10 +443,17 @@ describe('WalletFactory model', function() { networkName: 'testnet', opts: {}, }); - wf.create = sinon.stub().yields(null, 'wallet'); + + var w = sinon.stub(); + w.sendWalletReady = sinon.spy(); + wf.create = sinon.stub().yields(null, w); wf.joinCreateSession(opts, function(err, w) { net.start.calledOnce.should.equal(true); wf.create.calledOnce.should.equal(true); + wf.create.calledOnce.should.equal(true); + + w.sendWalletReady.calledOnce.should.equal(true); + w.sendWalletReady.getCall(0).args[0].should.equal('03ddbc4711534bc62ccf576ab05f2a0afd11f9e2f4016781f3f5a88de9543a229a'); done(); }); });