Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - allow web3 to work in iframes with third party cookies turned off #2203

Merged
merged 1 commit into from
Jan 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}