Skip to content

Commit

Permalink
Fix for when localStorage is available but access is restricted (#3031)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyapotti authored and nivida committed Sep 11, 2019
1 parent ce27738 commit 8574bd3
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion packages/web3-eth-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,44 @@ Wallet.prototype.load = function (password, keyName) {
return this.decrypt(keystore || [], password);
};

if (typeof localStorage === 'undefined') {
if (!storageAvailable('localStorage')) {
delete Wallet.prototype.save;
delete Wallet.prototype.load;
}

/**
* Checks whether a storage type is available or not
* For more info on how this works, please refer to MDN documentation
* https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#Feature-detecting_localStorage
*
* @method storageAvailable
* @param {String} type the type of storage ('localStorage', 'sessionStorage')
* @returns {Boolean} a boolean indicating whether the specified storage is available or not
*/
function storageAvailable(type) {
var storage;
try {
storage = window[type];
var x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch(e) {
return e && (
// everything except Firefox
e.code === 22 ||
// Firefox
e.code === 1014 ||
// test name field too, because code might not be present
// everything except Firefox
e.name === 'QuotaExceededError' ||
// Firefox
e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
// acknowledge QuotaExceededError only if there's something already stored
(storage && storage.length !== 0);
}
}


module.exports = Accounts;

0 comments on commit 8574bd3

Please sign in to comment.