Compare commits

..
Author SHA1 Message Date
Shreerang Kale 2d3242aad3 Return boolean for new user 2025-02-10 12:24:39 +05:30
4 changed files with 27 additions and 77 deletions
-49
View File
@@ -4,8 +4,6 @@ import { TxBody, AuthInfo } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import { SignClientTypes } from "@walletconnect/types";
import { useNavigation } from "@react-navigation/native";
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing";
import { SigningStargateClient } from "@cosmjs/stargate";
import {
createStackNavigator,
StackNavigationProp,
@@ -38,8 +36,6 @@ import styles from "./styles/stylesheet";
import { Header } from "./components/Header";
import { WalletEmbed } from "./screens/WalletEmbed";
import { AutoSignIn } from "./screens/AutoSignIn";
import { checkSufficientFunds, getPathKey, sendMessage } from "./utils/misc";
import { retrieveSingleAccount } from "./utils/accounts";
const Stack = createStackNavigator<StackParamsList>();
@@ -223,51 +219,6 @@ const App = (): React.JSX.Element => {
};
});
useEffect(() => {
const handleCheckBalance = async (event: MessageEvent) => {
if (event.data.type !== 'CHECK_BALANCE') return;
const { chainId, address, amount } = event.data;
const network = networksData.find(net => net.chainId === chainId);
if (!network) {
console.error('Network not found');
throw new Error('Requested network not supported.');
}
const account = await retrieveSingleAccount(network.namespace, network.chainId, address);
if (!account) {
throw new Error('Account not found for the requested address.');
}
const cosmosPrivKey = (
await getPathKey(`${network.namespace}:${chainId}`, account.index)
).privKey;
const sender = await DirectSecp256k1Wallet.fromKey(
Buffer.from(cosmosPrivKey.split('0x')[1], 'hex'),
network.addressPrefix
);
const client = await SigningStargateClient.connectWithSigner(network.rpcUrl!, sender);
const balance = await client.getBalance(
account.address,
network.nativeDenom!.toLowerCase()
);
const areFundsSufficient = !checkSufficientFunds(amount, balance.amount);
sendMessage(event.source as Window, 'IS_SUFFICIENT', areFundsSufficient, event.origin);
};
window.addEventListener('message', handleCheckBalance);
return () => {
window.removeEventListener('message', handleCheckBalance);
};
}, [networksData]);
const showWalletConnect = useMemo(() => accounts.length > 0, [accounts]);
return (
+4 -2
View File
@@ -59,7 +59,7 @@ export const AutoSignIn = () => {
useEffect(() => {
const getAccountAddress = async (event: MessageEvent) => {
if (event.data.type !== 'GET_ACCOUNT_ADDRESS') return;
let isNewUser = false;
if (event.data.secret !== process.env.REACT_APP_AUTH_SECRET) {
console.log('Unauthorized app.');
@@ -72,6 +72,8 @@ export const AutoSignIn = () => {
console.log("Accounts not found, creating wallet...");
await createWallet(networksData);
isNewUser = true
// Re-fetch newly created accounts
accountsData = await getAccountsData(event.data.chainId);
}
@@ -80,7 +82,7 @@ export const AutoSignIn = () => {
return;
}
sendMessage(event.source as Window, 'ACCOUNT_ADDRESS_RESPONSE', accountsData[0].address, event.origin);
sendMessage(event.source as Window, 'ACCOUNT_ADDRESS_RESPONSE', { address: accountsData[0].address, isNewUser }, event.origin);
};
window.addEventListener('message', getAccountAddress);
+19 -4
View File
@@ -19,7 +19,7 @@ import { createWallet, retrieveAccounts, retrieveSingleAccount } from '../utils/
import AccountDetails from '../components/AccountDetails';
import styles from '../styles/stylesheet';
import DataBox from '../components/DataBox';
import { checkSufficientFunds, getPathKey, sendMessage } from '../utils/misc';
import { getPathKey } from '../utils/misc';
import { useNetworks } from '../context/NetworksContext';
import TxErrorDialog from '../components/TxErrorDialog';
import { MEMO } from '../screens/ApproveTransfer';
@@ -62,6 +62,22 @@ export const WalletEmbed = () => {
return accounts.map(account => account.address);
}, [networksData]);
const sendMessage = (
source: Window | null,
type: string,
data: any,
origin: string
): void => {
source?.postMessage({ type, data }, origin);
};
const checkSufficientFunds = (amount: string, balance: string) => {
const amountBigNum = BigNumber.from(String(amount));
const balanceBigNum = BigNumber.from(balance);
return balanceBigNum.gt(amountBigNum);
};
useEffect(() => {
const handleGetAccounts = async (event: MessageEvent) => {
if (event.data.type !== 'REQUEST_WALLET_ACCOUNTS') return;
@@ -168,8 +184,7 @@ export const WalletEmbed = () => {
});
if (!checkSufficientFunds(amount, balance.amount)) {
console.log("Insufficient funds detected");
sendMessage(event.source as Window, 'INSUFFICIENT_FUNDS', null, event.origin);
console.log("Insufficient funds detected. Throwing error.");
throw new Error('Insufficient funds');
}
@@ -327,7 +342,7 @@ export const WalletEmbed = () => {
</>
) : (
<View style={styles.spinnerContainer}>
<View style={{ marginTop: 50 }}></View>
<View style={{marginTop: 50}}></View>
<ActivityIndicator size="large" color="#0000ff" />
</View>
)}
+4 -22
View File
@@ -1,11 +1,7 @@
/* Importing this library provides react native with a secure random source.
For more information, "visit https://docs.ethers.org/v5/cookbook/react-native/#cookbook-reactnative-security" */
import 'react-native-get-random-values';
import { BigNumber } from 'ethers';
import { AccountData } from '@cosmjs/amino';
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
import { stringToPath } from '@cosmjs/crypto';
import '@ethersproject/shims';
import {
@@ -13,6 +9,10 @@ import {
resetInternetCredentials,
setInternetCredentials,
} from './key-store';
import { AccountData } from '@cosmjs/amino';
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
import { stringToPath } from '@cosmjs/crypto';
import { EIP155 } from './constants';
import { NetworksDataState } from '../types';
@@ -149,28 +149,10 @@ const resetKeyServers = async (namespace: string) => {
});
};
const sendMessage = (
source: Window | null,
type: string,
data: any,
origin: string
): void => {
source?.postMessage({ type, data }, origin);
};
const checkSufficientFunds = (amount: string, balance: string) => {
const amountBigNum = BigNumber.from(String(amount));
const balanceBigNum = BigNumber.from(balance);
return balanceBigNum.gt(amountBigNum);
};
export {
getMnemonic,
getPathKey,
updateAccountIndices,
getHDPath,
resetKeyServers,
sendMessage,
checkSufficientFunds,
};