From 49cada4fc8e75e02226d0e268c63f316a5021859 Mon Sep 17 00:00:00 2001 From: AdityaSalunkhe21 Date: Sat, 12 Apr 2025 12:00:30 +0530 Subject: [PATCH] Update useWebViewHandler for token transfer --- src/global.d.ts | 12 ++++ src/hooks/useWebViewHandler.ts | 109 +++++++++++++++++++++++++++++---- 2 files changed, 109 insertions(+), 12 deletions(-) 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 cca1a20..1e08ccb 100644 --- a/src/hooks/useWebViewHandler.ts +++ b/src/hooks/useWebViewHandler.ts @@ -6,11 +6,9 @@ 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 = () => { - const [isReady, setIsReady] = useState(false); - useGetOrCreateAccounts(); - // Navigation and context hooks const navigation = useNavigation>(); const { selectedNetwork } = useNetworks(); @@ -22,13 +20,6 @@ export const useWebViewHandler = () => { // Core navigation handler const navigateToSignRequest = useCallback((message: string) => { try { - // Wait for accounts to be ready - if (!isReady) { - console.log('Accounts not yet ready'); - window.Android?.onSignatureError?.('Accounts not yet ready'); - return; - } - // Validation checks if (!selectedNetwork?.namespace || !selectedNetwork?.chainId) { window.Android?.onSignatureError?.('Invalid network configuration'); @@ -78,14 +69,108 @@ export const useWebViewHandler = () => { } catch (error) { window.Android?.onSignatureError?.(`Navigation error: ${error}`); } - }, [selectedNetwork, accounts, currentIndex, navigation, isReady]); + }, [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 };