From c1288753146cb80fe57778439e676ccef258d318 Mon Sep 17 00:00:00 2001 From: Ben Kremer Date: Wed, 5 Apr 2023 13:50:32 +0200 Subject: [PATCH] fix: ensure wallet alerts visibly and sends JSON-RPC error response --- .../src/utils/EIP155RequestHandlerUtil.ts | 58 +++++++++++++------ 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/wallets/react-wallet-v2/src/utils/EIP155RequestHandlerUtil.ts b/wallets/react-wallet-v2/src/utils/EIP155RequestHandlerUtil.ts index 56ba361..aa8d0e7 100644 --- a/wallets/react-wallet-v2/src/utils/EIP155RequestHandlerUtil.ts +++ b/wallets/react-wallet-v2/src/utils/EIP155RequestHandlerUtil.ts @@ -20,30 +20,54 @@ export async function approveEIP155Request( switch (request.method) { case EIP155_SIGNING_METHODS.PERSONAL_SIGN: case EIP155_SIGNING_METHODS.ETH_SIGN: - const message = getSignParamsMessage(request.params) - const signedMessage = await wallet.signMessage(message) - return formatJsonRpcResult(id, signedMessage) + try { + const message = getSignParamsMessage(request.params) + const signedMessage = await wallet.signMessage(message) + return formatJsonRpcResult(id, signedMessage) + } catch (error: any) { + console.error(error) + alert(error.message) + return formatJsonRpcError(id, error.message) + } case EIP155_SIGNING_METHODS.ETH_SIGN_TYPED_DATA: case EIP155_SIGNING_METHODS.ETH_SIGN_TYPED_DATA_V3: case EIP155_SIGNING_METHODS.ETH_SIGN_TYPED_DATA_V4: - const { domain, types, message: data } = getSignTypedDataParamsData(request.params) - // https://github.com/ethers-io/ethers.js/issues/687#issuecomment-714069471 - delete types.EIP712Domain - const signedData = await wallet._signTypedData(domain, types, data) - return formatJsonRpcResult(id, signedData) + try { + const { domain, types, message: data } = getSignTypedDataParamsData(request.params) + // https://github.com/ethers-io/ethers.js/issues/687#issuecomment-714069471 + delete types.EIP712Domain + const signedData = await wallet._signTypedData(domain, types, data) + return formatJsonRpcResult(id, signedData) + } catch (error: any) { + console.error(error) + alert(error.message) + return formatJsonRpcError(id, error.message) + } case EIP155_SIGNING_METHODS.ETH_SEND_TRANSACTION: - const provider = new providers.JsonRpcProvider(EIP155_CHAINS[chainId as TEIP155Chain].rpc) - const sendTransaction = request.params[0] - const connectedWallet = wallet.connect(provider) - const { hash } = await connectedWallet.sendTransaction(sendTransaction) - return formatJsonRpcResult(id, hash) + try { + const provider = new providers.JsonRpcProvider(EIP155_CHAINS[chainId as TEIP155Chain].rpc) + const sendTransaction = request.params[0] + const connectedWallet = wallet.connect(provider) + const { hash } = await connectedWallet.sendTransaction(sendTransaction) + return formatJsonRpcResult(id, hash) + } catch (error: any) { + console.error(error) + alert(error.message) + return formatJsonRpcError(id, error.message) + } case EIP155_SIGNING_METHODS.ETH_SIGN_TRANSACTION: - const signTransaction = request.params[0] - const signature = await wallet.signTransaction(signTransaction) - return formatJsonRpcResult(id, signature) + try { + const signTransaction = request.params[0] + const signature = await wallet.signTransaction(signTransaction) + return formatJsonRpcResult(id, signature) + } catch (error: any) { + console.error(error) + alert(error.message) + return formatJsonRpcError(id, error.message) + } default: throw new Error(getSdkError('INVALID_METHOD').message) @@ -53,5 +77,5 @@ export async function approveEIP155Request( export function rejectEIP155Request(request: SignClientTypes.EventArguments['session_request']) { const { id } = request - return formatJsonRpcError(id, getSdkError('USER_REJECTED_METHODS').message) + return formatJsonRpcError(id, getSdkError('USER_REJECTED').message) }