From 6af4e7224e30ae435a24bd0e7bdabecb8237628c Mon Sep 17 00:00:00 2001 From: Shreerang Kale Date: Tue, 6 May 2025 15:15:25 +0530 Subject: [PATCH] Update sign message request handler --- src/App.tsx | 6 +++--- src/screens/SignRequestEmbed.tsx | 25 ++++++++++++++++++++----- src/utils/constants.ts | 4 +++- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 55a00b7..fd8c034 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -30,7 +30,7 @@ import { getSignParamsMessage } from "./utils/wallet-connect/helpers"; import ApproveTransfer from "./screens/ApproveTransfer"; import AddNetwork from "./screens/AddNetwork"; import EditNetwork from "./screens/EditNetwork"; -import { COSMOS, EIP155 } from "./utils/constants"; +import { CHECK_BALANCE, COSMOS, EIP155, IS_SUFFICIENT } from "./utils/constants"; import { useNetworks } from "./context/NetworksContext"; import { NETWORK_METHODS } from "./utils/wallet-connect/common-data"; import { COSMOS_METHODS } from "./utils/wallet-connect/COSMOSData"; @@ -232,7 +232,7 @@ const App = (): React.JSX.Element => { useEffect(() => { const handleCheckBalance = async (event: MessageEvent) => { - if (event.data.type !== 'CHECK_BALANCE') return; + if (event.data.type !== CHECK_BALANCE) return; const { chainId, amount } = event.data; const network = networksData.find(net => net.chainId === chainId); @@ -272,7 +272,7 @@ const App = (): React.JSX.Element => { const areFundsSufficient = checkSufficientFunds(amount, balance.amount); - sendMessage(event.source as Window, 'IS_SUFFICIENT', areFundsSufficient, event.origin); + sendMessage(event.source as Window, IS_SUFFICIENT, areFundsSufficient, event.origin); }; window.addEventListener('message', handleCheckBalance); diff --git a/src/screens/SignRequestEmbed.tsx b/src/screens/SignRequestEmbed.tsx index 0d7fc00..7a789d6 100644 --- a/src/screens/SignRequestEmbed.tsx +++ b/src/screens/SignRequestEmbed.tsx @@ -25,6 +25,7 @@ const SignRequestEmbed = ({ route }: SignRequestProps) => { const [displayAccount, setDisplayAccount] = useState(); const [message, setMessage] = useState(''); const [chainId, setChainId] = useState(''); + const [namespace, setNamespace] = useState(''); const [signDoc, setSignDoc] = useState(null); const [signerAddress, setSignerAddress] = useState(''); const [origin, setOrigin] = useState(''); @@ -41,8 +42,13 @@ const SignRequestEmbed = ({ route }: SignRequestProps) => { setIsApproving(true); try { - const requestAccount = await retrieveSingleAccount(COSMOS, chainId, signerAddress); - const path = (await getPathKey(`${COSMOS}:${chainId}`, requestAccount!.index)).path; + if (namespace !== COSMOS) { + // TODO: Support ETH namespace + throw new Error(`namespace ${namespace} is not supported`) + } + + const requestAccount = await retrieveSingleAccount(namespace, chainId, signerAddress); + const path = (await getPathKey(`${namespace}:${chainId}`, requestAccount!.index)).path; const mnemonic = await getMnemonic(); const requestedNetworkData = networksData.find(networkData => networkData.chainId === chainId) @@ -111,16 +117,25 @@ const SignRequestEmbed = ({ route }: SignRequestProps) => { try { const { signerAddress, signDoc } = event.data.params; + const receivedNamespace = event.data.chainId.split(':')[0] + const receivedChainId = event.data.chainId.split(':')[1] + + if (receivedNamespace !== COSMOS) { + // TODO: Support ETH namespace + throw new Error(`namespace ${receivedNamespace} is not supported`) + } + setSignerAddress(signerAddress); setSignDoc(signDoc); setMessage(signDoc.memo || ''); setOrigin(event.origin); setSourceWindow(event.source as Window); - setChainId(event.data.chainId); + setNamespace(receivedNamespace); + setChainId(receivedChainId); const requestAccount = await retrieveSingleAccount( - COSMOS, - event.data.chainId, + receivedNamespace, + receivedChainId, signerAddress, ); diff --git a/src/utils/constants.ts b/src/utils/constants.ts index c845a1a..4c537fe 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -82,9 +82,10 @@ export const REQUEST_SIGN_MESSAGE = 'REQUEST_SIGN_MESSAGE'; export const REQUEST_WALLET_ACCOUNTS = 'REQUEST_WALLET_ACCOUNTS'; export const REQUEST_CREATE_OR_GET_ACCOUNTS = 'REQUEST_CREATE_OR_GET_ACCOUNTS'; export const REQUEST_TX = 'REQUEST_TX'; -export const AUTO_SIGN_IN = 'AUTO_SIGN_IN'; export const REQUEST_ACCOUNT_PK = 'REQUEST_ACCOUNT_PK'; export const REQUEST_ADD_ACCOUNT = 'ADD_ACCOUNT'; +export const AUTO_SIGN_IN = 'AUTO_SIGN_IN'; +export const CHECK_BALANCE = 'CHECK_BALANCE'; // iframe response types export const COSMOS_ACCOUNTS_RESPONSE = 'COSMOS_ACCOUNTS_RESPONSE'; @@ -95,3 +96,4 @@ export const SIGN_IN_RESPONSE = 'SIGN_IN_RESPONSE'; export const ACCOUNT_PK_RESPONSE = 'ACCOUNT_PK_RESPONSE'; export const ADD_ACCOUNT_RESPONSE = 'ADD_ACCOUNT_RESPONSE'; export const WALLET_ACCOUNTS_DATA = 'WALLET_ACCOUNTS_DATA'; +export const IS_SUFFICIENT = 'IS_SUFFICIENT';