From fd47a329135808ab27d3659e81b952304c29c5b4 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Mon, 15 Nov 2021 14:00:00 +0100 Subject: [PATCH 1/4] Add Alchemy Rinkeby key Signed-off-by: Sebastian Martinez --- src/config.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/config.json b/src/config.json index 5f5f7cf5d..1e240d15a 100644 --- a/src/config.json +++ b/src/config.json @@ -63,7 +63,8 @@ "reverseRegistrar": { "address": "0x6F628b68b30Dc3c17f345c9dbBb1E483c2b7aE5c" }, - "tokens": [] + "tokens": [], + "alchemy": { "key": "1T6h-0rxu7SRzKEtmukIoxaJOXazLDNs" } }, "walletConnect": { "bridge": "https://radicle.bridge.walletconnect.org" }, "ceramic": { From 54df789c959cd200d0b39f75aa99677ab073a4e5 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Mon, 15 Nov 2021 14:47:17 +0100 Subject: [PATCH 2/4] Use Alchemy as only provider Signed-off-by: Sebastian Martinez --- src/config.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config.ts b/src/config.ts index c7b257ab6..c13e2df6d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -220,8 +220,8 @@ function getProvider( config: Record, metamask: ethers.providers.JsonRpcProvider | null, ): ethers.providers.JsonRpcProvider { - // Use Alchemy in production, on mainnet. Otherwise use Metamask if installed. - if (network.name === "homestead" && import.meta.env.PROD) { + // Use Alchemy in production. Otherwise use Metamask if installed. + if (import.meta.env.PROD) { return new ethers.providers.AlchemyWebSocketProvider(network.name, config.alchemy.key); } else if (metamask) { return metamask; From c5172b8cc5682e7a159b7e0141815a2e307c38d1 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Mon, 15 Nov 2021 14:48:21 +0100 Subject: [PATCH 3/4] Allow WalletConnect to change accounts and network Signed-off-by: Sebastian Martinez --- src/config.ts | 7 ++++++- src/session.ts | 40 ++++++++++++++++++++++++++++++++-------- src/utils.ts | 9 --------- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/config.ts b/src/config.ts index c13e2df6d..29612e8d4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -7,6 +7,7 @@ import { Core } from '@self.id/core'; import WalletConnect from "@walletconnect/client"; import config from "@app/config.json"; import { WalletConnectSigner } from "./WalletConnectSigner"; +import { disconnectWallet } from "./session"; declare global { interface Window { @@ -129,6 +130,10 @@ export class Config { ); } + changeNetwork(chainId: number): void { + this.network = ethers.providers.getNetwork(chainId); + } + setSigner(signer: ethers.Signer & TypedDataSigner | WalletConnectSigner): void { this.signer = signer; } @@ -210,7 +215,7 @@ export class Config { } } -function isMetamaskInstalled(): boolean { +export function isMetamaskInstalled(): boolean { const { ethereum } = window; return Boolean(ethereum && ethereum.isMetaMask); } diff --git a/src/session.ts b/src/session.ts index 765e33854..5e01c0407 100644 --- a/src/session.ts +++ b/src/session.ts @@ -2,9 +2,8 @@ import { get, writable, derived } from "svelte/store"; import type { Readable } from "svelte/store"; import type { BigNumber } from 'ethers'; import type { TransactionReceipt, TransactionResponse } from '@ethersproject/providers'; -import { Config, getConfig } from "@app/config"; +import { Config, getConfig, isMetamaskInstalled } from "@app/config"; import { Unreachable, assert, assertEq } from "@app/error"; -import { formatNetwork } from "@app/utils"; import * as ethers from "ethers"; export enum Connection { @@ -105,12 +104,34 @@ export const loadState = (initial: State): Store => { signer.walletConnect.chainId ); + // Instead of killing the WalletConnect session, we force the UI to change network if (network.chainId !== config.network.chainId) { - config.walletConnect.client.killSession(); - throw new Error( - `Network mismatch. Please switch your wallet to ${formatNetwork(config.network)}.` - ); + config.changeNetwork(network.chainId); } + + config.walletConnect.client.on("session_update", async (error, { params: [{ accounts, chainId }] }: { params: [{ accounts: [string]; chainId: number }] }) => { + if (error) { + throw error; + } + + try { + // We only change accounts if the address has been changed, to avoid unnecessary refreshing. + if (address !== accounts[0]) changeAccounts(accounts[0]); + // Check the current chainId, and request Metamask to change, or reload the window to get the correct chain. + if (chainId !== config.network.chainId) { + if (isMetamaskInstalled()) { + await window.ethereum.request({ + method: 'wallet_switchEthereumChain', + params: [{ chainId: ethers.utils.hexValue(chainId) }] + }); + } else { + config.changeNetwork(chainId); + window.location.reload(); + } + } + } catch (e) { console.error(e); } + }); + store.set({ connection: Connection.Connected, session }); } catch (e) { console.log("WalletConnect: connection failed."); @@ -245,15 +266,18 @@ window.ethereum?.on('chainChanged', () => { // We disconnect the wallet to avoid out of sync state // between the account address and IDX DIDs disconnectMetamask(); - location.reload(); }); // Updates state when user changes accounts window.ethereum?.on("accountsChanged", async ([address]: string) => { + changeAccounts(address); +}); + +export async function changeAccounts(address: string): Promise { const config = await getConfig(); state.setChangedAccount(address); state.refreshBalance(config); -}); +} state.subscribe(s => { console.log("session.state", s); diff --git a/src/utils.ts b/src/utils.ts index 017cebf50..b708df093 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -107,15 +107,6 @@ export function formatHash(hash: string): string { + hash.substring(hash.length - 4, hash.length); } -export function formatNetwork(input: { name: string }): string { - let name = input.name; - - if (name === "homestead") { - name = "mainnet"; - } - return capitalize(name); -} - export function formatOrg(input: string, config: Config): string { if (isAddress(input)) { return ethers.utils.getAddress(input); From 137cbe82f950104ce7bdafec7bc9208e67483c3a Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Mon, 15 Nov 2021 14:48:34 +0100 Subject: [PATCH 4/4] Fix available wallets link Signed-off-by: Sebastian Martinez --- src/components/Modal/ConnectWallet.svelte | 2 +- src/config.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/Modal/ConnectWallet.svelte b/src/components/Modal/ConnectWallet.svelte index eab7fd2c7..7e49b0f77 100644 --- a/src/components/Modal/ConnectWallet.svelte +++ b/src/components/Modal/ConnectWallet.svelte @@ -53,7 +53,7 @@
diff --git a/src/config.ts b/src/config.ts index 29612e8d4..dd2d7f824 100644 --- a/src/config.ts +++ b/src/config.ts @@ -7,7 +7,6 @@ import { Core } from '@self.id/core'; import WalletConnect from "@walletconnect/client"; import config from "@app/config.json"; import { WalletConnectSigner } from "./WalletConnectSigner"; -import { disconnectWallet } from "./session"; declare global { interface Window {