From 4746b5a79005d02295b9bd66cb4e7f289698a4c8 Mon Sep 17 00:00:00 2001 From: David Disu Date: Tue, 4 Oct 2022 12:42:41 +0100 Subject: [PATCH] Prevent proxy upgrade for invalid proxy address --- .../src/lib/components/contractGUI.tsx | 55 +++++++++++-------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/libs/remix-ui/run-tab/src/lib/components/contractGUI.tsx b/libs/remix-ui/run-tab/src/lib/components/contractGUI.tsx index cccbbbbb2e..f06a2e1d6c 100644 --- a/libs/remix-ui/run-tab/src/lib/components/contractGUI.tsx +++ b/libs/remix-ui/run-tab/src/lib/components/contractGUI.tsx @@ -1,6 +1,7 @@ // eslint-disable-next-line no-use-before-define import React, { useEffect, useRef, useState } from 'react' import * as remixLib from '@remix-project/remix-lib' +import Web3 from 'web3' import { ContractGUIProps } from '../types' import { CopyToClipboard } from '@remix-ui/clipboard' import { OverlayTrigger, Tooltip } from 'react-bootstrap' @@ -22,6 +23,7 @@ export function ContractGUI (props: ContractGUIProps) { const [deployState, setDeployState] = useState<{ deploy: boolean, upgrade: boolean }>({ deploy: false, upgrade: false }) const [useLastProxy, setUseLastProxy] = useState(false) const [proxyAddress, setProxyAddress] = useState('') + const [proxyAddressError, setProxyAddressError] = useState('') const multiFields = useRef>([]) const initializeFields = useRef>([]) const basicInputRef = useRef() @@ -174,7 +176,7 @@ export function ContractGUI (props: ContractGUIProps) { props.clickCallBack(props.initializerOptions.inputs.inputs, proxyInitializeString, ['Deploy with Proxy']) } else if (deployState.upgrade) { - props.clickCallBack(props.funcABI.inputs, proxyAddress, ['Upgrade with Proxy']) + !proxyAddressError && props.clickCallBack(props.funcABI.inputs, proxyAddress, ['Upgrade with Proxy']) } else { props.clickCallBack(props.funcABI.inputs, basicInput) } @@ -223,16 +225,36 @@ export function ContractGUI (props: ContractGUIProps) { const value = e.target.checked const address = props.savedProxyAddress + if (value) { + if (address) { + setProxyAddress(address) + setProxyAddressError('') + } else { + setProxyAddressError('No proxy address available') + setProxyAddress('') + } + } setUseLastProxy(value) - setProxyAddress(address || '') } const handleSetProxyAddress = (e) => { const value = e.target.value - + setProxyAddress(value) } + const validateProxyAddress = (address: string) => { + if (address === '') { + setProxyAddressError('proxy address cannot be empty') + } else { + if (Web3.utils.isAddress(address)) { + setProxyAddressError('') + } else { + setProxyAddressError('not a valid contract address') + } + } + } + return (
{!useLastProxy ? (
- - -
- ) : ( - - {proxyAddress || "No proxy address available"} - - )} + + validateProxyAddress(proxyAddress) } /> + { proxyAddressError && { proxyAddressError } } +
: + { proxyAddress || proxyAddressError } + }