Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
*/
const currentAddress = getCachedAccount();
let accountWasCached = false;
if (currentAddress) {
accountWasCached = true;
// eslint-disable-next-line no-console
console.log(`using account from local storage: ${currentAddress}`);
}
this.props.setCurrentAccount(currentAddress);
/**
* Only supply currentAddress if it was obtained from a provider. The poll
* is only comparing changes with respect to the provider state. Passing it a cached state
* will only cause it to get the wrong impression and misbehave.
*/
pollForAccountChanges(accountWasCached ? null : currentAddress).subscribe(
(newAddress: Address | null): void => {
// eslint-disable-next-line no-console
console.log(`new account: ${newAddress}`);
this.props.setCurrentAccount(newAddress);
if (newAddress) {
cacheWeb3Info(newAddress);
} else {
uncacheWeb3Info();
gotoReadonly(this.props.showNotification);
}
});
}
console.log(`using address from cookie: ${currentAddressFromCookie}`);
this.props.setCurrentAccount(currentAddressFromCookie);
} else {
this.props.cookies.set("currentAddress", "", { path: "/"});
this.props.setCurrentAccount(undefined);
}
}
try {
metamask = await checkMetaMask();
} catch (err) {
console.log("MM not available or not set correctly: using default web3 provider: ", err.message);
}
if (metamask) {
pollForAccountChanges(currentAddress).subscribe(
(newAddress: Address) => {
if (newAddress && checkMetaMask()) {
console.log(`new address: ${newAddress}`);
this.props.setCurrentAccount(newAddress);
this.props.cookies.set("currentAddress", newAddress, { path: "/"});
// TODO: we reload on setting a new account,
// but it would be more elegant if we did not need to
window.location.reload();
}
});
}
}