diff --git a/src/App.tsx b/src/App.tsx index 9cbe0bf..d7eb587 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -43,10 +43,11 @@ import useAccountsData from "./hooks/useAccountsData"; import { useWebViewHandler } from "./hooks/useWebViewHandler"; import SignRequestEmbed from "./screens/SignRequestEmbed"; import useAddAccountEmbed from "./hooks/useAddAccountEmbed"; -import useExportPKEmbed from "./hooks/useExportPrivateKeyEmbed"; import { SignTxEmbed } from "./screens/SignTxEmbed"; import useCreateNetwork from "./hooks/useCreateNetwork"; +const REACT_APP_ALLOWED_URLS = import.meta.env.REACT_APP_ALLOWED_URLS; + const Stack = createStackNavigator(); const App = (): React.JSX.Element => { @@ -235,6 +236,18 @@ const App = (): React.JSX.Element => { const handleCheckBalance = async (event: MessageEvent) => { if (event.data.type !== CHECK_BALANCE) return; + if (!REACT_APP_ALLOWED_URLS) { + console.log('Allowed URLs are not set'); + return; + } + + const allowedUrls = REACT_APP_ALLOWED_URLS.split(',').map((url: string) => url.trim()); + + if (!allowedUrls.includes(event.origin)) { + console.log('Unauthorized app.'); + return; + } + const { chainId, amount } = event.data; const network = networksData.find(net => net.chainId === chainId); @@ -287,7 +300,6 @@ const App = (): React.JSX.Element => { useWebViewHandler(); useAddAccountEmbed(); - useExportPKEmbed(); useCreateNetwork(); return ( diff --git a/src/hooks/useExportPrivateKeyEmbed.ts b/src/hooks/useExportPrivateKeyEmbed.ts deleted file mode 100644 index ec0e1cb..0000000 --- a/src/hooks/useExportPrivateKeyEmbed.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { useEffect } from 'react'; - -import { useAccounts } from '../context/AccountsContext'; -import { getPathKey, sendMessage } from '../utils/misc'; -import { ACCOUNT_PK_RESPONSE, REQUEST_ACCOUNT_PK } from '../utils/constants'; - -const useExportPKEmbed = () => { - const { accounts } = useAccounts(); - - useEffect(() => { - const handleMessage = async (event: MessageEvent) => { - const { type, chainId, address } = event.data; - - if (type !== REQUEST_ACCOUNT_PK) return; - - try { - const selectedAccount = accounts.find(account => account.address === address); - if (!selectedAccount) { - throw new Error("Account not found") - } - - const pathKey = await getPathKey(chainId, selectedAccount.index); - const privateKey = pathKey.privKey; - - sendMessage( - event.source as Window, - ACCOUNT_PK_RESPONSE, - { privateKey }, - event.origin, - ); - } catch (error) { - console.error('Error fetching private key:', error); - } - }; - - window.addEventListener('message', handleMessage); - return () => { - window.removeEventListener('message', handleMessage); - }; - }, [accounts]); -}; - -export default useExportPKEmbed; diff --git a/src/screens/WalletEmbed.tsx b/src/screens/WalletEmbed.tsx index 9b0d73d..cebc002 100644 --- a/src/screens/WalletEmbed.tsx +++ b/src/screens/WalletEmbed.tsx @@ -28,6 +28,8 @@ import useGetOrCreateAccounts from '../hooks/useGetOrCreateAccounts'; import useAccountsData from '../hooks/useAccountsData'; import { REQUEST_TX, REQUEST_WALLET_ACCOUNTS, TRANSACTION_RESPONSE, WALLET_ACCOUNTS_DATA } from '../utils/constants'; +const REACT_APP_ALLOWED_URLS = import.meta.env.REACT_APP_ALLOWED_URLS; + type TransactionDetails = { chainId: string; fromAddress: string; @@ -54,6 +56,18 @@ export const WalletEmbed = () => { const handleGetAccounts = async (event: MessageEvent) => { if (event.data.type !== REQUEST_WALLET_ACCOUNTS) return; + if (!REACT_APP_ALLOWED_URLS) { + console.log('Allowed URLs are not set'); + return; + } + + const allowedUrls = REACT_APP_ALLOWED_URLS.split(',').map((url: string) => url.trim()); + + if (!allowedUrls.includes(event.origin)) { + console.log('Unauthorized app.'); + return; + } + const accountsData = await getAccountsData(event.data.chainId); if (accountsData.length === 0) { @@ -84,6 +98,18 @@ export const WalletEmbed = () => { try { if (event.data.type !== REQUEST_TX) return; + if (!REACT_APP_ALLOWED_URLS) { + console.log('Allowed URLs are not set'); + return; + } + + const allowedUrls = REACT_APP_ALLOWED_URLS.split(',').map((url: string) => url.trim()); + + if (!allowedUrls.includes(event.origin)) { + console.log('Unauthorized app.'); + return; + } + txEventRef.current = event; const { chainId, fromAddress, toAddress, amount } = event.data; diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 7dd3fbf..e25150d 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -70,7 +70,6 @@ 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 REQUEST_ACCOUNT_PK = 'REQUEST_ACCOUNT_PK'; export const REQUEST_ADD_ACCOUNT = 'REQUEST_ADD_ACCOUNT'; export const AUTO_SIGN_IN = 'AUTO_SIGN_IN'; export const CHECK_BALANCE = 'CHECK_BALANCE'; @@ -82,7 +81,6 @@ export const SIGN_TX_RESPONSE = 'SIGN_TX_RESPONSE'; export const SIGN_MESSAGE_RESPONSE = 'SIGN_MESSAGE_RESPONSE'; export const TRANSACTION_RESPONSE = 'TRANSACTION_RESPONSE'; 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';