diff --git a/src/keyring/keyring.js b/src/keyring/keyring.js index ad13793c..84233492 100644 --- a/src/keyring/keyring.js +++ b/src/keyring/keyring.js @@ -67,7 +67,7 @@ Keyring.prototype.clear = function() { function emailCheck(email, key) { email = email.toLowerCase(); var keyEmails = key.getUserIds(); - for (var i; i < keyEmails.length; i++) { + for (var i = 0; i < keyEmails.length; i++) { //we need to get just the email from the userid key keyEmail = keyEmails[i].split('<')[1].split('>')[0].trim().toLowerCase(); if (keyEmail == email) { diff --git a/src/keyring/localstore.js b/src/keyring/localstore.js index 587a0430..4d169646 100644 --- a/src/keyring/localstore.js +++ b/src/keyring/localstore.js @@ -19,25 +19,34 @@ * The class that deals with storage of the keyring. Currently the only option is to use HTML5 local storage. * @requires openpgp * @module keyring/localstore + * @param {String} item itemname in localstore */ module.exports = LocalStore; var openpgp = require('../'); -function LocalStore() { +function LocalStore(item) { if (typeof window != 'undefined' && window.localStorage) { this.storage = window.localStorage; } else { this.storage = new (require('node-localstorage').LocalStorage)(openpgp.config.node_store); } + if(typeof item == 'string') { + this.item = item; + } } +/* + * Declare the localstore itemname + */ +LocalStore.prototype.item = 'armoredKeys'; + /** * Load the keyring from HTML5 local storage and initializes this instance. * @return {Array} array of keys retrieved from localstore */ LocalStore.prototype.load = function () { - var armoredKeys = JSON.parse(this.storage.getItem('armoredKeys')); + var armoredKeys = JSON.parse(this.storage.getItem(this.item)); var keys = []; if (armoredKeys !== null && armoredKeys.length !== 0) { var key; @@ -59,5 +68,5 @@ LocalStore.prototype.store = function (keys) { for (var i = 0; i < keys.length; i++) { armoredKeys.push(keys[i].armor()); } - this.storage.setItem('armoredKeys', JSON.stringify(armoredKeys)); + this.storage.setItem(this.item, JSON.stringify(armoredKeys)); }; diff --git a/test/general/keyring.js b/test/general/keyring.js index 31979f90..cab9715c 100644 --- a/test/general/keyring.js +++ b/test/general/keyring.js @@ -87,5 +87,15 @@ describe("Keyring", function() { expect(keys).to.exist.and.have.length(1); done(); }); + it('customize localstorage itemname', function() { + var localstore1 = new openpgp.Keyring.localstore('my-custom-name'); + var localstore2 = new openpgp.Keyring.localstore('my-custom-name'); + var localstore3 = new openpgp.Keyring.localstore(); + localstore3.store([]); + var key = openpgp.key.readArmored(pubkey).keys[0]; + localstore1.store([key]); + expect(localstore2.load()[0].primaryKey.getKeyId().equals(key.primaryKey.getKeyId())).to.be.true; + expect(localstore3.load()).to.have.length(0); + }); });