From 1a2e5098c360c72adb97894a1b2cf427c16f1311 Mon Sep 17 00:00:00 2001 From: Ben Kremer Date: Thu, 10 Feb 2022 14:34:32 +0100 Subject: [PATCH] refactor: creates `getAddressByChainId` helper fn --- .../src/contexts/JsonRpcContext.tsx | 33 ++++++++----------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx b/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx index 35fd8cc..9f5d035 100644 --- a/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx +++ b/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx @@ -84,6 +84,15 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea setChainData(chainData); }; + const getAddressByChainId = (chainId: string) => { + const account = accounts.find(account => account.startsWith(chainId)); + if (account === undefined) throw new Error(`Account for chainId ${chainId} not found.`); + const address = account.split(":").pop(); + if (address === undefined) throw new Error(`Address for account ${account} is invalid`); + + return address; + }; + const _createJsonRpcRequestHandler = (rpcRequest: (...requestArgs: [any]) => Promise) => async (chainId: string) => { @@ -185,11 +194,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea // encode message (hex) const hexMsg = encoding.utf8ToHex(message, true); - // get ethereum address - const account = accounts.find(account => account.startsWith(chainId)); - if (account === undefined) throw new Error("Account is not found"); - const address = account.split(":").pop(); - if (address === undefined) throw new Error("Address is invalid"); + const address = getAddressByChainId(chainId); // personal_sign params const params = [hexMsg, address]; @@ -231,11 +236,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea // test message const message = JSON.stringify(eip712.example); - // get ethereum address - const account = accounts.find(account => account.startsWith(chainId)); - if (account === undefined) throw new Error("Account is not found"); - const address = account.split(":").pop(); - if (address === undefined) throw new Error("Address is invalid"); + const address = getAddressByChainId(chainId); // eth_signTypedData params const params = [address, message]; @@ -306,11 +307,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea reference, ); - // get cosmos address - const account = accounts.find(account => account.startsWith(chainId)); - if (account === undefined) throw new Error("Account is not found"); - const address = account.split(":").pop(); - if (address === undefined) throw new Error("Address is invalid"); + const address = getAddressByChainId(chainId); // cosmos_signDirect params const params = { @@ -359,11 +356,7 @@ export function JsonRpcContextProvider({ children }: { children: ReactNode | Rea sequence: "54", }; - // get cosmos address - const account = accounts.find(account => account.startsWith(chainId)); - if (account === undefined) throw new Error("Account is not found"); - const address = account.split(":").pop(); - if (address === undefined) throw new Error("Address is invalid"); + const address = getAddressByChainId(chainId); // cosmos_signAmino params const params = { signerAddress: address, signDoc };