on identity open return most recently used wallet

This commit is contained in:
Ivan Socolsky 2014-10-15 10:59:09 -03:00 committed by Matias Alejo Garcia
commit aa9f16a7e9
2 changed files with 60 additions and 18 deletions

View file

@ -197,7 +197,8 @@ Identity.open = function(email, password, opts, cb) {
// Open All wallets from profile // Open All wallets from profile
//This could be optional, or opts.onlyOpen = wid //This could be optional, or opts.onlyOpen = wid
var firstWallet; var wallets = [];
var remaining = wids.length;
_.each(wids, function(wid) { _.each(wids, function(wid) {
iden.openWallet(wid, function(err, w) { iden.openWallet(wid, function(err, w) {
if (err) { if (err) {
@ -205,13 +206,16 @@ Identity.open = function(email, password, opts, cb) {
iden.profile.deleteWallet(wid, function() {}); iden.profile.deleteWallet(wid, function() {});
} else { } else {
log.info('Open wallet id:' + wid + ' opened'); log.info('Open wallet id:' + wid + ' opened');
if (!firstWallet) wallets.push(w);
firstWallet = w; }
if (--remaining == 0) {
var firstWallet = _.findWhere(wallets, {
id: wids[0]
});
return cb(err, iden, firstWallet);
} }
}) })
}); });
return cb(err, iden, firstWallet);
}); });
}; };

View file

@ -54,11 +54,11 @@ describe('Identity model', function() {
profile.listWallets = sinon.stub().returns([]); profile.listWallets = sinon.stub().returns([]);
profile.setLastOpenedTs = sinon.stub().yields(null);; profile.setLastOpenedTs = sinon.stub().yields(null);;
profile.store = sinon.stub().yields(null);; profile.store = sinon.stub().yields(null);;
Identity._createProfile = sinon.stub().callsArgWith(3,null,profile); Identity._createProfile = sinon.stub().callsArgWith(3, null, profile);
Identity.create(email, password, config, function(err,i){ Identity.create(email, password, config, function(err, i) {
iden = i; iden = i;
done(); done();
}); });
@ -130,9 +130,11 @@ describe('Identity model', function() {
describe('#open', function(done) { describe('#open', function(done) {
beforeEach(function() { beforeEach(function() {
storage.getFirst = sinon.stub().yields(null, 'wallet1234'); storage.getFirst = sinon.stub().yields(null, 'wallet1234');
profile.listWallets = sinon.stub().returns([{id:'walletid'}]); profile.listWallets = sinon.stub().returns([{
id: 'walletid'
}]);
Identity._openProfile = sinon.stub().callsArgWith(3, null, profile); Identity._openProfile = sinon.stub().callsArgWith(3, null, profile);
Identity._walletRead = sinon.stub().callsArgWith(2, null, wallet); Identity._walletRead = sinon.stub().callsArgWith(2, null, wallet);
}); });
it('should call ._openProfile', function(done) { it('should call ._openProfile', function(done) {
@ -143,6 +145,32 @@ describe('Identity model', function() {
done(); done();
}); });
}); });
it('should return last used wallet', function(done) {
var wallets = [{
id: 'wallet1',
store: sinon.stub().yields(null),
netStart: sinon.stub(),
}, {
id: 'wallet2',
store: sinon.stub().yields(null),
netStart: sinon.stub(),
}, {
id: 'wallet3',
store: sinon.stub().yields(null),
netStart: sinon.stub(),
}];
profile.listWallets = sinon.stub().returns(wallets);
Identity._walletRead = sinon.stub();
Identity._walletRead.onCall(0).callsArgWith(2, null, wallets[0]);
Identity._walletRead.onCall(1).callsArgWith(2, null, wallets[1]);
Identity._walletRead.onCall(2).callsArgWith(2, null, wallets[2]);
Identity.open(email, password, config, function(err, iden, w) {
w.id.should.equal('wallet1');
done();
});
});
}); });
}); });
describe('#store', function() { describe('#store', function() {
@ -270,7 +298,7 @@ describe('Identity model', function() {
wallet.getId = sinon.stub().returns('ID123'); wallet.getId = sinon.stub().returns('ID123');
Identity._walletFromObj = sinon.stub().returns(wallet); Identity._walletFromObj = sinon.stub().returns(wallet);
Identity._walletRead = sinon.stub().yields(null,wallet); Identity._walletRead = sinon.stub().yields(null, wallet);
iden.importWallet("encrypted object", "xxx", [], function(err) { iden.importWallet("encrypted object", "xxx", [], function(err) {
iden.openWallet('ID123', function(err, w) { iden.openWallet('ID123', function(err, w) {
@ -344,7 +372,9 @@ describe('Identity model', function() {
networkName: 'aWeirdNetworkName', networkName: 'aWeirdNetworkName',
opts: {}, opts: {},
}); });
Identity._newAsync = function() { return net; }; Identity._newAsync = function() {
return net;
};
opts.privHex = undefined; opts.privHex = undefined;
iden.joinWallet(opts, function(err, w) { iden.joinWallet(opts, function(err, w) {
@ -367,7 +397,9 @@ describe('Identity model', function() {
type: 'walletId', type: 'walletId',
networkName: iden.networkName, networkName: iden.networkName,
}); });
Identity._newAsync = function() { return net; }; Identity._newAsync = function() {
return net;
};
iden.joinWallet(opts, function(err, w) { iden.joinWallet(opts, function(err, w) {
err.should.equal('joinError'); err.should.equal('joinError');
@ -390,7 +422,9 @@ describe('Identity model', function() {
networkName: 'testnet', networkName: 'testnet',
opts: {}, opts: {},
}); });
Identity._newAsync = function() { return net; }; Identity._newAsync = function() {
return net;
};
var w = sinon.stub(); var w = sinon.stub();
w.sendWalletReady = sinon.spy(); w.sendWalletReady = sinon.spy();
@ -420,7 +454,9 @@ describe('Identity model', function() {
networkName: 'testnet', networkName: 'testnet',
opts: {}, opts: {},
}); });
Identity._newAsync = function() { return net; }; Identity._newAsync = function() {
return net;
};
iden.createWallet = sinon.stub().yields(null, null); iden.createWallet = sinon.stub().yields(null, null);
iden.joinWallet(opts, function(err, w) { iden.joinWallet(opts, function(err, w) {
err.should.equal('walletFull'); err.should.equal('walletFull');
@ -431,7 +467,9 @@ describe('Identity model', function() {
it('should accept a priv key a input', function() { it('should accept a priv key a input', function() {
opts.privHex = 'tprv8ZgxMBicQKsPf7MCvCjnhnr4uiR2Z2gyNC27vgd9KUu98F9mM1tbaRrWMyddVju36GxLbeyntuSadBAttriwGGMWUkRgVmUUCg5nFioGZsd'; opts.privHex = 'tprv8ZgxMBicQKsPf7MCvCjnhnr4uiR2Z2gyNC27vgd9KUu98F9mM1tbaRrWMyddVju36GxLbeyntuSadBAttriwGGMWUkRgVmUUCg5nFioGZsd';
var net = sinon.stub(); var net = sinon.stub();
Identity._newAsync = function() { return net; }; Identity._newAsync = function() {
return net;
};
net.on = sinon.stub(); net.on = sinon.stub();
net.cleanUp = sinon.spy(); net.cleanUp = sinon.spy();
@ -446,12 +484,12 @@ describe('Identity model', function() {
net.cleanUp = sinon.spy(); net.cleanUp = sinon.spy();
net.on = sinon.stub(); net.on = sinon.stub();
net.start = sinon.spy(); net.start = sinon.spy();
Identity._newAsync = function() { return net; }; Identity._newAsync = function() {
return net;
};
iden.joinWallet(opts, function(err, w) { iden.joinWallet(opts, function(err, w) {
net.start.getCall(0).args[0].privkey.length.should.equal(64); //privkey is hex of private key buffer net.start.getCall(0).args[0].privkey.length.should.equal(64); //privkey is hex of private key buffer
}); });
}); });
}); });
}); });