From 83e6d361372b7a864178d3eb5899ed782d18af87 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Fri, 24 Mar 2023 12:36:21 -0700 Subject: [PATCH 1/2] chain id check --- .../src/components/form/BridgeForm.svelte | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/bridge-ui/src/components/form/BridgeForm.svelte b/packages/bridge-ui/src/components/form/BridgeForm.svelte index 2a926671e3c..2cdd3e70706 100644 --- a/packages/bridge-ui/src/components/form/BridgeForm.svelte +++ b/packages/bridge-ui/src/components/form/BridgeForm.svelte @@ -217,6 +217,26 @@ throw Error('Invalid custom recipient address'); } + const signerChain = await $signer.getChainId(); + if (signerChain !== $fromChain.id) { + errorToast('You are connected to the wrong chain in your wallet'); + return; + } + + const bridgeAddress = chains[$fromChain.id].bridgeAddress; + const tokenVaultAddress = tokenVaults[$fromChain.id]; + + const codeNotDeployed = [bridgeAddress, tokenVaultAddress].find( + async (a) => { + return (await $signer.provider.getCode(a)) === '0x'; + }, + ); + + if (codeNotDeployed) { + errorToast('You are connected to the wrong chain in your wallet'); + return; + } + const amountInWei = ethers.utils.parseUnits(amount, $token.decimals); const provider = providers[$toChain.id]; @@ -236,8 +256,8 @@ tokenAddress: await addrForToken(), fromChainId: $fromChain.id, toChainId: $toChain.id, - tokenVaultAddress: tokenVaults[$fromChain.id], - bridgeAddress: chains[$fromChain.id].bridgeAddress, + tokenVaultAddress: tokenVaultAddress, + bridgeAddress: bridgeAddress, processingFeeInWei: getProcessingFee(), memo: memo, isBridgedTokenAlreadyDeployed, From 9e565b203b1454fa86ac915d95fc5af3c557fb26 Mon Sep 17 00:00:00 2001 From: Jeffery Walsh Date: Fri, 24 Mar 2023 12:52:51 -0700 Subject: [PATCH 2/2] block wrong chain ids --- .../src/components/Transaction.svelte | 13 +++++++++ .../src/components/form/BridgeForm.svelte | 21 ++++---------- .../modals/SwitchEthereumChainModal.svelte | 1 + .../bridge-ui/src/utils/isOnCorrectChain.ts | 28 +++++++++++++++++++ 4 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 packages/bridge-ui/src/utils/isOnCorrectChain.ts diff --git a/packages/bridge-ui/src/components/Transaction.svelte b/packages/bridge-ui/src/components/Transaction.svelte index e59c47302ab..10b1b205d70 100644 --- a/packages/bridge-ui/src/components/Transaction.svelte +++ b/packages/bridge-ui/src/components/Transaction.svelte @@ -25,6 +25,7 @@ import { providers } from '../provider/providers'; import { bridges } from '../bridge/bridges'; import { tokenVaults } from '../vault/tokenVaults'; + import { isOnCorrectChain } from '../utils/isOnCorrectChain'; export let transaction: BridgeTransaction; export let fromChain: Chain; @@ -77,6 +78,12 @@ await switchChainAndSetSigner(chain); } + // confirm after switch chain that it worked. + if (!isOnCorrectChain($signer, bridgeTx.toChainId)) { + errorToast('You are connected to the wrong chain in your wallet'); + return; + } + // For now just handling this case for when the user has near 0 balance during their first bridge transaction to L2 // TODO: estimate Claim transaction const userBalance = await $signer.getBalance('latest'); @@ -120,6 +127,12 @@ await switchChainAndSetSigner(chain); } + // confirm after switch chain that it worked. + if (!isOnCorrectChain($signer, bridgeTx.fromChainId)) { + errorToast('You are connected to the wrong chain in your wallet'); + return; + } + const tx = await bridges[ bridgeTx.message?.data === '0x' || !bridgeTx.message?.data ? BridgeType.ETH diff --git a/packages/bridge-ui/src/components/form/BridgeForm.svelte b/packages/bridge-ui/src/components/form/BridgeForm.svelte index 2cdd3e70706..add1a6502e9 100644 --- a/packages/bridge-ui/src/components/form/BridgeForm.svelte +++ b/packages/bridge-ui/src/components/form/BridgeForm.svelte @@ -38,6 +38,7 @@ import { chains } from '../../chain/chains'; import { providers } from '../../provider/providers'; import { tokenVaults } from '../../vault/tokenVaults'; + import { isOnCorrectChain } from '../../utils/isOnCorrectChain'; let amount: string; let amountInput: HTMLInputElement; @@ -217,22 +218,7 @@ throw Error('Invalid custom recipient address'); } - const signerChain = await $signer.getChainId(); - if (signerChain !== $fromChain.id) { - errorToast('You are connected to the wrong chain in your wallet'); - return; - } - - const bridgeAddress = chains[$fromChain.id].bridgeAddress; - const tokenVaultAddress = tokenVaults[$fromChain.id]; - - const codeNotDeployed = [bridgeAddress, tokenVaultAddress].find( - async (a) => { - return (await $signer.provider.getCode(a)) === '0x'; - }, - ); - - if (codeNotDeployed) { + if (!isOnCorrectChain($signer, $fromChain.id)) { errorToast('You are connected to the wrong chain in your wallet'); return; } @@ -250,6 +236,9 @@ $fromChain, ); + const bridgeAddress = chains[$fromChain.id].bridgeAddress; + const tokenVaultAddress = tokenVaults[$fromChain.id]; + const bridgeOpts: BridgeOpts = { amountInWei: amountInWei, signer: $signer, diff --git a/packages/bridge-ui/src/components/modals/SwitchEthereumChainModal.svelte b/packages/bridge-ui/src/components/modals/SwitchEthereumChainModal.svelte index 5fc7c9deb69..d26d0ba5e40 100644 --- a/packages/bridge-ui/src/components/modals/SwitchEthereumChainModal.svelte +++ b/packages/bridge-ui/src/components/modals/SwitchEthereumChainModal.svelte @@ -27,6 +27,7 @@
{$_('switchChainModal.subtitle')} diff --git a/packages/bridge-ui/src/utils/isOnCorrectChain.ts b/packages/bridge-ui/src/utils/isOnCorrectChain.ts new file mode 100644 index 00000000000..10d5fe2f0e7 --- /dev/null +++ b/packages/bridge-ui/src/utils/isOnCorrectChain.ts @@ -0,0 +1,28 @@ +import type { ethers } from 'ethers'; +import { chains } from '../chain/chains'; +import { tokenVaults } from '../vault/tokenVaults'; + +export async function isOnCorrectChain( + signer: ethers.Signer, + wantChain: number, +) { + const signerChain = await signer.getChainId(); + if (signerChain !== wantChain) { + return false; + } + + const bridgeAddress = chains[wantChain].bridgeAddress; + const tokenVaultAddress = tokenVaults[wantChain]; + + const bridgeAddressCode = await signer.provider.getCode(bridgeAddress); + + const tokenVaultAddressCode = await signer.provider.getCode( + tokenVaultAddress, + ); + + if (bridgeAddressCode === '0x' || tokenVaultAddressCode === '0x') { + return false; + } + + return true; +}