add store/restore passphrasse methods
This commit is contained in:
parent
d9cd2e95d4
commit
f1ae8f9c33
5 changed files with 32 additions and 7 deletions
|
|
@ -295,14 +295,17 @@ Identity.prototype.close = function(cb) {
|
||||||
* @return {Wallet}
|
* @return {Wallet}
|
||||||
*/
|
*/
|
||||||
Identity.prototype.importWallet = function(base64, password, skipFields, cb) {
|
Identity.prototype.importWallet = function(base64, password, skipFields, cb) {
|
||||||
|
preconditions.checkArgument(password);
|
||||||
preconditions.checkArgument(cb);
|
preconditions.checkArgument(cb);
|
||||||
|
|
||||||
|
this.storage.savePassphrase();
|
||||||
this.storage.setPassword(password);
|
this.storage.setPassword(password);
|
||||||
|
|
||||||
var obj = this.storage.decrypt(base64);
|
var obj = this.storage.decrypt(base64);
|
||||||
if (!obj) return false;
|
this.storage.restorePassphrase();
|
||||||
|
|
||||||
|
if (!obj) return false;
|
||||||
var w = Identity._walletFromObj(obj, this.storage, this.networkOpts, this.blockchainOpts);
|
var w = Identity._walletFromObj(obj, this.storage, this.networkOpts, this.blockchainOpts);
|
||||||
|
console.log('[Identity.js.307:Identity:]',w); //TODO
|
||||||
this._checkVersion(w.version);
|
this._checkVersion(w.version);
|
||||||
this.addWallet(w, function(err) {
|
this.addWallet(w, function(err) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
|
||||||
|
|
@ -45,8 +45,24 @@ Storage.prototype._getPassphrase = function() {
|
||||||
throw new Error('NOPASSPHRASE: No passphrase set');
|
throw new Error('NOPASSPHRASE: No passphrase set');
|
||||||
|
|
||||||
return pps[this.__uniqueid];
|
return pps[this.__uniqueid];
|
||||||
}
|
};
|
||||||
|
|
||||||
|
Storage.prototype.savePassphrase = function() {
|
||||||
|
if (!pps[this.__uniqueid])
|
||||||
|
throw new Error('NOPASSPHRASE: No passphrase set');
|
||||||
|
|
||||||
|
this.savedPassphrase = this.savedPassphrase || {};
|
||||||
|
this.savedPassphrase[this.__uniqueid] = pps[this.__uniqueid];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Storage.prototype.restorePassphrase = function() {
|
||||||
|
if (!this.savedPassphrase[this.__uniqueid])
|
||||||
|
throw new Error('NOSTOREDPASSPHRASE: No stored passphrase');
|
||||||
|
|
||||||
|
pps[this.__uniqueid] = this.savedPassphrase[this.__uniqueid];
|
||||||
|
this.savedPassphrase[this.__uniqueid] = undefined;
|
||||||
|
};
|
||||||
|
|
||||||
Storage.prototype.hasPassphrase = function() {
|
Storage.prototype.hasPassphrase = function() {
|
||||||
return pps[this.__uniqueid] ? true : false;
|
return pps[this.__uniqueid] ? true : false;
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,8 @@ describe('Identity model', function() {
|
||||||
beforeEach(function(done) {
|
beforeEach(function(done) {
|
||||||
storage = sinon.stub();
|
storage = sinon.stub();
|
||||||
storage.getItem = sinon.stub();
|
storage.getItem = sinon.stub();
|
||||||
|
storage.savePassphrase = sinon.spy();
|
||||||
|
storage.restorePassphrase = sinon.spy();
|
||||||
storage.setPassword = sinon.spy();
|
storage.setPassword = sinon.spy();
|
||||||
storage.hasPassphrase = sinon.stub().returns(true);
|
storage.hasPassphrase = sinon.stub().returns(true);
|
||||||
storage.getSessionId = sinon.spy();
|
storage.getSessionId = sinon.spy();
|
||||||
|
|
@ -127,7 +129,7 @@ describe('Identity model', function() {
|
||||||
|
|
||||||
describe('#open', function(done) {
|
describe('#open', function(done) {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
storage.getFirst = sinon.stub().yields('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);
|
||||||
|
|
@ -230,7 +232,7 @@ describe('Identity model', function() {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
iden.migrateWallet = sinon.stub().yields(null);
|
iden.migrateWallet = sinon.stub().yields(null);
|
||||||
storage.setPassword = sinon.spy();
|
storage.setPassword = sinon.spy();
|
||||||
storage.getFirst = sinon.stub().yields('wallet1234');
|
storage.getFirst = sinon.stub().yields(null, 'wallet1234');
|
||||||
|
|
||||||
var wallet = sinon.stub();
|
var wallet = sinon.stub();
|
||||||
wallet.store = sinon.stub().yields(null);
|
wallet.store = sinon.stub().yields(null);
|
||||||
|
|
@ -256,6 +258,7 @@ describe('Identity model', function() {
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
iden.migrateWallet = sinon.stub().yields(null);
|
iden.migrateWallet = sinon.stub().yields(null);
|
||||||
|
storage.getFirst = sinon.stub().yields(null, 'wallet1234');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create wallet from encrypted object', function(done) {
|
it('should create wallet from encrypted object', function(done) {
|
||||||
|
|
@ -266,9 +269,12 @@ 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);
|
||||||
|
|
||||||
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) {
|
||||||
|
iden.storage.savePassphrase.calledOnce.should.equal(true);
|
||||||
|
iden.storage.restorePassphrase.calledOnce.should.equal(true);
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
should.exist(w);
|
should.exist(w);
|
||||||
done();
|
done();
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ describe('Storage model', function() {
|
||||||
var s2 = new Storage(requireMock('FakeLocalStorage').storageParams);
|
var s2 = new Storage(requireMock('FakeLocalStorage').storageParams);
|
||||||
(function() {
|
(function() {
|
||||||
var params = _.clone(requireMock('FakeLocalStorage').storageParams);
|
var params = _.clone(requireMock('FakeLocalStorage').storageParams);
|
||||||
params.password = undefined;
|
params.passphrase = '1234';
|
||||||
new Storage(params);
|
new Storage(params);
|
||||||
}).should.throw('Illegal Argument');
|
}).should.throw('Illegal Argument');
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ module.exports.storageParams = {
|
||||||
password: '123',
|
password: '123',
|
||||||
db: new FakeLocalStorage(),
|
db: new FakeLocalStorage(),
|
||||||
sessionStorage: new FakeLocalStorage(),
|
sessionStorage: new FakeLocalStorage(),
|
||||||
passphrase: {
|
passphraseConfig: {
|
||||||
iterations: 1,
|
iterations: 1,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue