Update receiveTransferRequestFromAndroid
This commit is contained in:
parent
6515a22abc
commit
79eda18185
2
src/global.d.ts
vendored
2
src/global.d.ts
vendored
@ -29,7 +29,7 @@ declare global {
|
||||
receiveSignRequestFromAndroid?: (message: string) => void;
|
||||
|
||||
// Handles incoming transfer requests from Android
|
||||
receiveTransferRequestFromAndroid?: (to: string, amount: string) => void;
|
||||
receiveTransferRequestFromAndroid?: (to: string, amount: string, namespace: String, chainId: string, addressPrefix: String) => void;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -28,12 +28,14 @@ export const useWebViewHandler = () => {
|
||||
|
||||
if (!accounts?.length) {
|
||||
window.Android?.onSignatureError?.('No accounts available');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const currentAccount = accounts[currentIndex];
|
||||
if (!currentAccount) {
|
||||
window.Android?.onSignatureError?.('Current account not found');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -44,6 +46,7 @@ export const useWebViewHandler = () => {
|
||||
|
||||
if (!match) {
|
||||
window.Android?.onSignatureError?.('Invalid signing path');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -72,84 +75,58 @@ export const useWebViewHandler = () => {
|
||||
}, [selectedNetwork, accounts, currentIndex, navigation]);
|
||||
|
||||
// Handle incoming transfer requests
|
||||
const navigateToTransfer = useCallback(async (to: string, amount: string) => {
|
||||
const navigateToTransfer = useCallback(async (to: string, amount: string, namespace: String, chainId: string, addressPrefix: 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'
|
||||
const chainSpecs = {
|
||||
namespace,
|
||||
chainId,
|
||||
addressPrefix
|
||||
};
|
||||
|
||||
try {
|
||||
// Get all accounts for Cosmos Hub Testnet
|
||||
const cosmosAccounts = await retrieveAccountsForNetwork(
|
||||
`${cosmosHubTestnet.namespace}:${cosmosHubTestnet.chainId}`,
|
||||
// Get all accounts
|
||||
const chainAccounts = await retrieveAccountsForNetwork(
|
||||
`${chainSpecs.namespace}:${chainSpecs.chainId}`,
|
||||
'0' // Use the first account
|
||||
);
|
||||
|
||||
if (!cosmosAccounts || cosmosAccounts.length === 0) {
|
||||
console.error('No Cosmos Hub Testnet accounts found');
|
||||
if (!chainAccounts || chainAccounts.length === 0) {
|
||||
console.error('Accounts not found');
|
||||
if (window.Android?.onTransferError) {
|
||||
window.Android.onTransferError('No Cosmos Hub Testnet accounts found');
|
||||
window.Android.onTransferError('Accounts not found');
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const cosmosAccount = cosmosAccounts[0]; // Use the first account
|
||||
const chainAccount = chainAccounts[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;
|
||||
}
|
||||
const path = `/transfer/${chainSpecs.namespace}/${chainSpecs.chainId}/${chainAccount.address}/${to}/${amount}`;
|
||||
|
||||
navigation.reset({
|
||||
index: 0,
|
||||
routes: [
|
||||
{
|
||||
name: 'ApproveTransfer',
|
||||
path: `/transfer/${cosmosHubTestnet.namespace}/${cosmosHubTestnet.chainId}/${cosmosAccount.address}/${to}/${amount}`,
|
||||
path: path,
|
||||
params: {
|
||||
namespace: cosmosHubTestnet.namespace,
|
||||
chainId: `${cosmosHubTestnet.namespace}:${cosmosHubTestnet.chainId}`,
|
||||
namespace: chainSpecs.namespace,
|
||||
chainId: `${chainSpecs.namespace}:${chainSpecs.chainId}`,
|
||||
transaction: {
|
||||
from: cosmosAccount.address,
|
||||
from: chainAccount.address,
|
||||
to: to,
|
||||
value: amount,
|
||||
data: ''
|
||||
},
|
||||
accountInfo: cosmosAccount,
|
||||
accountInfo: chainAccount,
|
||||
},
|
||||
},
|
||||
],
|
||||
@ -160,7 +137,7 @@ export const useWebViewHandler = () => {
|
||||
window.Android.onTransferError(`Navigation error: ${error}`);
|
||||
}
|
||||
}
|
||||
}, [accounts, currentIndex, navigation]);
|
||||
}, [accounts, navigation]);
|
||||
|
||||
useEffect(() => {
|
||||
// Assign the function to the window object
|
||||
|
||||
@ -53,14 +53,14 @@ const ApproveTransfer = ({ route }: ApproveTransferProps) => {
|
||||
const { web3wallet } = useWalletConnect();
|
||||
|
||||
// Extract data from route params or path
|
||||
const requestSession = route.params?.requestSessionData;
|
||||
const requestName = requestSession?.peer?.metadata?.name;
|
||||
const requestIcon = requestSession?.peer?.metadata?.icons?.[0];
|
||||
const requestURL = requestSession?.peer?.metadata?.url;
|
||||
const transaction = route.params?.transaction;
|
||||
const requestEvent = route.params?.requestEvent;
|
||||
const chainId = requestEvent?.params?.chainId || route.params?.chainId;
|
||||
const requestMethod = requestEvent?.params?.request?.method;
|
||||
const requestSession = route.params.requestSessionData ?? null;
|
||||
const requestName = requestSession?.peer.metadata.name;
|
||||
const requestIcon = requestSession?.peer.metadata.icons?.[0];
|
||||
const requestURL = requestSession?.peer.metadata.url;
|
||||
const transaction = route.params.transaction;
|
||||
const requestEvent = route.params.requestEvent ?? null;
|
||||
const chainId = requestEvent?.params.chainId || route.params.chainId;
|
||||
const requestMethod = requestEvent?.params.request.method;
|
||||
|
||||
const [account, setAccount] = useState<Account>();
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
@ -23,8 +23,8 @@ export type StackParamsList = {
|
||||
ApproveTransfer: {
|
||||
chainId?: string;
|
||||
transaction: PopulatedTransaction;
|
||||
requestEvent: Web3WalletTypes.SessionRequest;
|
||||
requestSessionData: SessionTypes.Struct;
|
||||
requestEvent?: Web3WalletTypes.SessionRequest;
|
||||
requestSessionData?: SessionTypes.Struct;
|
||||
};
|
||||
InvalidPath: undefined;
|
||||
WalletConnect: undefined;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user