Skip to content

Commit

Permalink
Merge pull request #2203 from joshstevens19/fix/allow-web3-to-work-in…
Browse files Browse the repository at this point in the history
…-iframes-with-3rd-party-cookies-turned-off

Fix - allow web3 to work in iframes with third party cookies turned off
  • Loading branch information
nivida authored Jan 21, 2019
2 parents 0610c89 + 000c68b commit d516d5e
Showing 1 changed file with 50 additions and 10 deletions.
60 changes: 50 additions & 10 deletions packages/web3-eth-accounts/src/Accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,21 @@ class Wallet {
* @returns {boolean}
*/
save(password, keyName) {
localStorage.setItem(keyName || this.defaultKeyName, JSON.stringify(this.encrypt(password)));
try {
localStorage.setItem(keyName || this.defaultKeyName, JSON.stringify(this.encrypt(password)));
} catch (error) {
// code 18 means trying to use local storage in a iframe
// with third party cookies turned off
// we still want to support using web3 in a iframe
// as by default safari turn these off for all iframes
// so mask the error
if (error.code === 18) {
return true;
} else {
// throw as normal if not
throw new Error(error);
}
}

return true;
}
Expand All @@ -703,19 +717,45 @@ class Wallet {
* @returns {Wallet}
*/
load(password, keyName) {
let keystore = localStorage.getItem(keyName || this.defaultKeyName);

if (keystore) {
try {
keystore = JSON.parse(keystore);
} catch (error) {}
let keystore
try {
keystore = localStorage.getItem(keyName || this.defaultKeyName);

if (keystore) {
try {
keystore = JSON.parse(keystore);
} catch (error) {}
}
} catch (error) {
// code 18 means trying to use local storage in a iframe
// with third party cookies turned off
// we still want to support using web3 in a iframe
// as by default safari turn these off for all iframes
// so mask the error
if (error.code === 18) {
keystore = this.defaultKeyName;
} else {
// throw as normal if not
throw new Error(error);
}
}

return this.decrypt(keystore || [], password);
}
}

if (typeof localStorage === 'undefined') {
delete Wallet.prototype.save;
delete Wallet.prototype.load;
try {
if (typeof localStorage === 'undefined') {
delete Wallet.prototype.save;
delete Wallet.prototype.load;
}
} catch (error) {
// code 18 means trying to use local storage in a iframe
// with third party cookies turned off
// we still want to support using web3 in a iframe
// as by default safari turn these off for all iframes
// so mask the error
if (error.code !== 18) {
throw new Error(error);
}
}

0 comments on commit d516d5e

Please sign in to comment.