Add window function to get or create account
This commit is contained in:
parent
acf0d1fc17
commit
b06c56c41c
9
src/global.d.ts
vendored
9
src/global.d.ts
vendored
@ -23,6 +23,12 @@ declare global {
|
||||
|
||||
// Called when transfer is cancelled
|
||||
onTransferCancelled?: () => void;
|
||||
|
||||
// Called when account is created
|
||||
onAccountCreated?: (account: string) => void;
|
||||
|
||||
// Called when account creation fails
|
||||
onAccountError?: (error: string) => void;
|
||||
};
|
||||
|
||||
// Handles incoming signature requests from Android
|
||||
@ -30,6 +36,9 @@ declare global {
|
||||
|
||||
// Handles incoming transfer requests from Android
|
||||
receiveTransferRequestFromAndroid?: (to: string, amount: string, namespace: String, chainId: string, memo: string) => void;
|
||||
|
||||
// Handles account creation requests from Android
|
||||
receiveGetOrCreateAccountFromAndroid?: (namespace: string, chainId: string) => void;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -58,29 +58,14 @@ const useGetOrCreateAccounts = () => {
|
||||
);
|
||||
};
|
||||
|
||||
const autoCreateAccounts = async () => {
|
||||
const defaultChainId = networksData[0]?.chainId;
|
||||
|
||||
if (!defaultChainId) {
|
||||
console.log('useGetOrCreateAccounts: No default chainId found');
|
||||
return;
|
||||
}
|
||||
const accounts = await getOrCreateAccountsForChain(defaultChainId);
|
||||
|
||||
// Only notify Android when we actually have accounts
|
||||
if (accounts.length > 0 && window.Android?.onAccountsReady) {
|
||||
window.Android.onAccountsReady();
|
||||
} else {
|
||||
console.log('No accounts created or Android bridge not available');
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener('message', handleCreateAccounts);
|
||||
|
||||
const isAndroidWebView = !!(window.Android);
|
||||
|
||||
if (isAndroidWebView) {
|
||||
autoCreateAccounts();
|
||||
if (!isAndroidWebView) {
|
||||
const defaultChainId = networksData[0]?.chainId;
|
||||
if (defaultChainId) {
|
||||
getOrCreateAccountsForChain(defaultChainId);
|
||||
}
|
||||
}
|
||||
|
||||
return () => {
|
||||
|
||||
@ -4,15 +4,50 @@ import { NativeStackNavigationProp } from '@react-navigation/native-stack';
|
||||
|
||||
import { useAccounts } from '../context/AccountsContext';
|
||||
import { useNetworks } from '../context/NetworksContext';
|
||||
import useAccountsData from "../hooks/useAccountsData";
|
||||
|
||||
import { StackParamsList } from '../types';
|
||||
import useGetOrCreateAccounts from './useGetOrCreateAccounts';
|
||||
import { retrieveAccountsForNetwork } from '../utils/accounts';
|
||||
import { retrieveAccountsForNetwork, createWallet } from '../utils/accounts';
|
||||
|
||||
export const useWebViewHandler = () => {
|
||||
// Navigation and context hooks
|
||||
const navigation = useNavigation<NativeStackNavigationProp<StackParamsList>>();
|
||||
const { selectedNetwork } = useNetworks();
|
||||
const { selectedNetwork, networksData } = useNetworks();
|
||||
const { accounts, currentIndex } = useAccounts();
|
||||
const { getAccountsData } = useAccountsData();
|
||||
|
||||
const handleGetOrCreateAccount = useCallback(async (namespace: string, chainId: string) => {
|
||||
try {
|
||||
// Find the requested network
|
||||
const network = networksData.find(net =>
|
||||
net.namespace === namespace && net.chainId === chainId
|
||||
);
|
||||
|
||||
if (!network) {
|
||||
window.Android?.onAccountError?.('Network configuration not found');
|
||||
return;
|
||||
}
|
||||
|
||||
let accountsData = await getAccountsData(chainId);
|
||||
|
||||
if (accountsData.length === 0) {
|
||||
console.log("Account not found, creating wallet...");
|
||||
await createWallet(networksData);
|
||||
accountsData = await getAccountsData(chainId);
|
||||
}
|
||||
|
||||
if (!accountsData || accountsData.length === 0) {
|
||||
window.Android?.onAccountError?.('Failed to create/retrieve account');
|
||||
return;
|
||||
}
|
||||
|
||||
window.Android?.onAccountCreated?.(JSON.stringify(accountsData[0]));
|
||||
} catch (error) {
|
||||
console.error('Account operation error:', error);
|
||||
window.Android?.onAccountError?.(`Operation failed: ${error}`);
|
||||
}
|
||||
}, [networksData, getAccountsData]);
|
||||
|
||||
// Initialize accounts
|
||||
useGetOrCreateAccounts();
|
||||
@ -139,9 +174,12 @@ export const useWebViewHandler = () => {
|
||||
|
||||
window.receiveTransferRequestFromAndroid = navigateToTransfer;
|
||||
|
||||
window.receiveGetOrCreateAccountFromAndroid = handleGetOrCreateAccount;
|
||||
|
||||
return () => {
|
||||
window.receiveSignRequestFromAndroid = undefined;
|
||||
window.receiveTransferRequestFromAndroid = undefined;
|
||||
window.receiveGetOrCreateAccountFromAndroid = undefined;
|
||||
};
|
||||
}, [navigateToSignRequest, navigateToTransfer]); // Only the function reference as dependency
|
||||
}, [navigateToSignRequest, navigateToTransfer, handleGetOrCreateAccount]); // Only the function reference as dependency
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user