fix pubkey param. add test

This commit is contained in:
Matias Alejo Garcia 2014-09-18 18:29:00 -03:00
parent 9bfc0dd193
commit 238d36c0a7
2 changed files with 32 additions and 19 deletions

View File

@ -114,12 +114,12 @@ WalletFactory.prototype.fromObj = function(inObj, skipFields) {
/** /**
* @desc Imports a wallet from an encrypted base64 object * @desc Imports a wallet from an encrypted base64 object
* @param {string} base64 - the base64 encoded 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 * @param {string[]} skipFields - fields to ignore when importing
* @return {Wallet} * @return {Wallet}
*/ */
WalletFactory.prototype.fromEncryptedObj = function(base64, password, skipFields) { WalletFactory.prototype.fromEncryptedObj = function(base64, passphrase, skipFields) {
this.storage.setPassphrase(password); this.storage.setPassphrase(passphrase);
var walletObj = this.storage.import(base64); var walletObj = this.storage.import(base64);
if (!walletObj) return false; if (!walletObj) return false;
return this.fromObj(walletObj, skipFields); 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}! * @TODO: this is essentialy the same method as {@link WalletFactory#fromEncryptedObj}!
* @desc Imports a wallet from an encrypted base64 object * @desc Imports a wallet from an encrypted base64 object
* @param {string} base64 - the base64 encoded 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 * @param {string[]} skipFields - fields to ignore when importing
* @return {Wallet} * @return {Wallet}
*/ */
WalletFactory.prototype.import = function(base64, password, skipFields) { WalletFactory.prototype.import = function(base64, passphrase, skipFields) {
var self = this; 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; return w;
}; };
@ -248,7 +248,6 @@ WalletFactory.prototype.create = function(opts, cb) {
}); });
log.debug('\t### TxProposals Initialized'); log.debug('\t### TxProposals Initialized');
this.storage.setPassphrase(opts.passphrase);
opts.storage = this.storage; opts.storage = this.storage;
opts.network = this.networks[opts.networkName]; opts.network = this.networks[opts.networkName];
@ -260,6 +259,7 @@ WalletFactory.prototype.create = function(opts, cb) {
opts.totalCopayers = totalCopayers; opts.totalCopayers = totalCopayers;
opts.version = opts.version || this.version; opts.version = opts.version || this.version;
this.storage.setPassphrase(opts.passphrase);
var w = this._getWallet(opts); var w = this._getWallet(opts);
var self = this; var self = this;
w.store(function(err) { w.store(function(err) {
@ -394,7 +394,7 @@ WalletFactory.prototype.joinCreateSession = function(opts, cb) {
//Create our PrivateK //Create our PrivateK
var privateKey = new PrivateKey(privOpts); var privateKey = new PrivateKey(privOpts);
log.debug('\t### PrivateKey Initialized'); log.debug('\t### PrivateKey Initialized');
var opts = { var joinOpts = {
copayerId: privateKey.getId(), copayerId: privateKey.getId(),
privkey: privateKey.getIdPriv(), privkey: privateKey.getIdPriv(),
key: privateKey.getIdKey(), key: privateKey.getIdKey(),
@ -414,23 +414,29 @@ WalletFactory.prototype.joinCreateSession = function(opts, cb) {
return cb('joinError'); return cb('joinError');
}); });
joinNetwork.start(opts, function() { joinNetwork.start(joinOpts, function() {
joinNetwork.greet(decodedSecret.pubKey, opts.secretNumber);
joinNetwork.greet(decodedSecret.pubKey, joinOpts.secretNumber);
joinNetwork.on('data', function(sender, data) { joinNetwork.on('data', function(sender, data) {
if (data.type === 'walletId' && data.opts) { if (data.type === 'walletId' && data.opts) {
if (data.networkName !== decodedSecret.networkName) { if (data.networkName !== decodedSecret.networkName) {
return cb('badNetwork'); return cb('badNetwork');
} }
data.opts.privateKey = privateKey; var walletOpts = _.clone(data.opts);
data.opts.nickname = opts.nickname; walletOpts.id = data.walletId;
data.opts.passphrase = opts.passphrase;
data.opts.id = data.walletId; walletOpts.privateKey = privateKey;
self.create(data.opts, function(err, w) { walletOpts.nickname = opts.nickname;
if (!err & w) { walletOpts.passphrase = opts.passphrase;
w.sendWalletReady(s.pubKey);
self.create(walletOpts, function(err, w) {
if (w) {
w.sendWalletReady(decodedSecret.pubKey);
} else { } else {
if (!err) err = 'walletFull'; if (!err) err = 'walletFull';
log.info(err);
} }
return cb(err, w); return cb(err, w);
}); });

View File

@ -443,10 +443,17 @@ describe('WalletFactory model', function() {
networkName: 'testnet', networkName: 'testnet',
opts: {}, 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) { wf.joinCreateSession(opts, function(err, w) {
net.start.calledOnce.should.equal(true); net.start.calledOnce.should.equal(true);
wf.create.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(); done();
}); });
}); });