diff --git a/src/global.d.ts b/src/global.d.ts index dfb6ad7..e9d6af1 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -14,10 +14,22 @@ declare global { // Called when accounts are ready for use onAccountsReady?: () => void; + + // Called when transfer is successfully completed + onTransferComplete?: (result: string) => void; + + // Called when transfer fails + onTransferError?: (error: string) => void; + + // Called when transfer is cancelled + onTransferCancelled?: () => void; }; // Handles incoming signature requests from Android receiveSignRequestFromAndroid?: (message: string) => void; + + // Handles incoming transfer requests from Android + receiveTransferRequestFromAndroid?: (to: string, amount: string) => void; } } diff --git a/src/hooks/useWebViewHandler.ts b/src/hooks/useWebViewHandler.ts index a4151ea..409c735 100644 --- a/src/hooks/useWebViewHandler.ts +++ b/src/hooks/useWebViewHandler.ts @@ -6,6 +6,7 @@ import { useAccounts } from '../context/AccountsContext'; import { useNetworks } from '../context/NetworksContext'; import { StackParamsList } from '../types'; import useGetOrCreateAccounts from './useGetOrCreateAccounts'; +import { retrieveAccountsForNetwork } from '../utils/accounts'; export const useWebViewHandler = () => { // Navigation and context hooks @@ -70,12 +71,106 @@ export const useWebViewHandler = () => { } }, [selectedNetwork, accounts, currentIndex, navigation]); + // Handle incoming transfer requests + const navigateToTransfer = useCallback(async (to: string, amount: string) => { + if (!accounts || accounts.length === 0) { + console.error('No accounts available'); + if (window.Android?.onTransferError) { + window.Android.onTransferError('No accounts available'); + } + return; + } + + const currentAccount = accounts[currentIndex]; + if (!currentAccount) { + console.error('Current account not found'); + if (window.Android?.onTransferError) { + window.Android.onTransferError('Current account not found'); + } + return; + } + + // Use Cosmos Hub Testnet network + const cosmosHubTestnet = { + namespace: 'cosmos', + chainId: 'provider', + addressPrefix: 'cosmos' + }; + + try { + // Get all accounts for Cosmos Hub Testnet + const cosmosAccounts = await retrieveAccountsForNetwork( + `${cosmosHubTestnet.namespace}:${cosmosHubTestnet.chainId}`, + '0' // Use the first account + ); + + if (!cosmosAccounts || cosmosAccounts.length === 0) { + console.error('No Cosmos Hub Testnet accounts found'); + if (window.Android?.onTransferError) { + window.Android.onTransferError('No Cosmos Hub Testnet accounts found'); + } + return; + } + + const cosmosAccount = cosmosAccounts[0]; // Use the first account + + const path = `/transfer/${cosmosHubTestnet.namespace}/${cosmosHubTestnet.chainId}/${cosmosAccount.address}/${to}/${amount}`; + + const pathRegex = /^\/transfer\/(eip155|cosmos)\/(.+)\/(.+)\/(.+)\/(.+)$/; + if (!pathRegex.test(path)) { + console.error('Path does not match expected pattern:', path); + if (window.Android?.onTransferError) { + window.Android.onTransferError('Invalid path format'); + } + return; + } + + const match = path.match(pathRegex); + if (!match) { + console.error('Failed to parse path:', path); + if (window.Android?.onTransferError) { + window.Android.onTransferError('Failed to parse path'); + } + return; + } + + navigation.reset({ + index: 0, + routes: [ + { + name: 'ApproveTransfer', + path: `/transfer/${cosmosHubTestnet.namespace}/${cosmosHubTestnet.chainId}/${cosmosAccount.address}/${to}/${amount}`, + params: { + namespace: cosmosHubTestnet.namespace, + chainId: `${cosmosHubTestnet.namespace}:${cosmosHubTestnet.chainId}`, + transaction: { + from: cosmosAccount.address, + to: to, + value: amount, + data: '' + }, + accountInfo: cosmosAccount, + }, + }, + ], + }); + } catch (error) { + console.error('Navigation error:', error); + if (window.Android?.onTransferError) { + window.Android.onTransferError(`Navigation error: ${error}`); + } + } + }, [accounts, currentIndex, navigation]); + useEffect(() => { // Assign the function to the window object window.receiveSignRequestFromAndroid = navigateToSignRequest; + + window.receiveTransferRequestFromAndroid = navigateToTransfer; return () => { window.receiveSignRequestFromAndroid = undefined; + window.receiveTransferRequestFromAndroid = undefined; }; - }, [navigateToSignRequest]); // Only the function reference as dependency + }, [navigateToSignRequest, navigateToTransfer]); // Only the function reference as dependency };