Add hooks to add accounts and export private key from iframe (#26)
Co-authored-by: Shreerang Kale <shreerangkale@gmail.com> Co-authored-by: IshaVenikar <ishavenikar7@gmail.com> Co-authored-by: AdityaSalunkhe21 <adityasalunkhe2204@gmail.com> Reviewed-on: LaconicNetwork/laconic-wallet-web#26 Co-authored-by: ishavenikar <ishavenikar@noreply.git.vdb.to> Co-committed-by: ishavenikar <ishavenikar@noreply.git.vdb.to>
This commit is contained in:
parent
713f8bc0bb
commit
b2eafe59b3
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "web-wallet",
|
"name": "web-wallet",
|
||||||
"version": "0.1.3",
|
"version": "0.1.4",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cerc-io/registry-sdk": "^0.2.5",
|
"@cerc-io/registry-sdk": "^0.2.5",
|
||||||
|
@ -41,7 +41,9 @@ import { AutoSignIn } from "./screens/AutoSignIn";
|
|||||||
import { checkSufficientFunds, getPathKey, sendMessage } from "./utils/misc";
|
import { checkSufficientFunds, getPathKey, sendMessage } from "./utils/misc";
|
||||||
import useAccountsData from "./hooks/useAccountsData";
|
import useAccountsData from "./hooks/useAccountsData";
|
||||||
import { useWebViewHandler } from "./hooks/useWebViewHandler";
|
import { useWebViewHandler } from "./hooks/useWebViewHandler";
|
||||||
import SignMessageEmbed from "./screens/SignMessageEmbed";
|
import SignRequestEmbed from "./screens/SignRequestEmbed";
|
||||||
|
import useAddAccountEmbed from "./hooks/useAddAccountEmbed";
|
||||||
|
import useExportPKEmbed from "./hooks/useExportPrivateKeyEmbed";
|
||||||
|
|
||||||
const Stack = createStackNavigator<StackParamsList>();
|
const Stack = createStackNavigator<StackParamsList>();
|
||||||
|
|
||||||
@ -282,6 +284,8 @@ const App = (): React.JSX.Element => {
|
|||||||
const showWalletConnect = useMemo(() => accounts.length > 0, [accounts]);
|
const showWalletConnect = useMemo(() => accounts.length > 0, [accounts]);
|
||||||
|
|
||||||
useWebViewHandler();
|
useWebViewHandler();
|
||||||
|
useAddAccountEmbed();
|
||||||
|
useExportPKEmbed();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Surface style={styles.appSurface}>
|
<Surface style={styles.appSurface}>
|
||||||
@ -393,7 +397,7 @@ const App = (): React.JSX.Element => {
|
|||||||
/>
|
/>
|
||||||
<Stack.Screen
|
<Stack.Screen
|
||||||
name="sign-request-embed"
|
name="sign-request-embed"
|
||||||
component={SignMessageEmbed}
|
component={SignRequestEmbed}
|
||||||
options={{
|
options={{
|
||||||
header: () => <Header title="Wallet" />,
|
header: () => <Header title="Wallet" />,
|
||||||
}}
|
}}
|
||||||
|
@ -4,7 +4,7 @@ import { Account } from '../types';
|
|||||||
|
|
||||||
const AccountsContext = createContext<{
|
const AccountsContext = createContext<{
|
||||||
accounts: Account[];
|
accounts: Account[];
|
||||||
setAccounts: (account: Account[]) => void;
|
setAccounts: React.Dispatch<React.SetStateAction<Account[]>>;
|
||||||
currentIndex: number;
|
currentIndex: number;
|
||||||
setCurrentIndex: (index: number) => void;
|
setCurrentIndex: (index: number) => void;
|
||||||
}>({
|
}>({
|
||||||
|
58
src/hooks/useAddAccountEmbed.ts
Normal file
58
src/hooks/useAddAccountEmbed.ts
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
import { useEffect, useCallback } from 'react';
|
||||||
|
|
||||||
|
import { useNetworks } from '../context/NetworksContext';
|
||||||
|
import { sendMessage } from '../utils/misc';
|
||||||
|
import useAccountsData from '../hooks/useAccountsData';
|
||||||
|
import { addAccount } from '../utils/accounts';
|
||||||
|
import { useAccounts } from '../context/AccountsContext';
|
||||||
|
import { Account, NetworksDataState } from '../types';
|
||||||
|
|
||||||
|
const REACT_APP_ALLOWED_URLS = process.env.REACT_APP_ALLOWED_URLS;
|
||||||
|
|
||||||
|
const useAddAccountEmbed = () => {
|
||||||
|
const { networksData } = useNetworks();
|
||||||
|
const { setAccounts, setCurrentIndex } = useAccounts();
|
||||||
|
const { getAccountsData } = useAccountsData();
|
||||||
|
|
||||||
|
const addAccountHandler = useCallback(async (network: NetworksDataState) => {
|
||||||
|
const newAccount = await addAccount(network);
|
||||||
|
if (newAccount) {
|
||||||
|
setAccounts(prev => [...prev, newAccount]);
|
||||||
|
setCurrentIndex(newAccount.index);
|
||||||
|
}
|
||||||
|
}, [setAccounts, setCurrentIndex]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleAddAccount = async (event: MessageEvent) => {
|
||||||
|
if (event.data.type !== 'ADD_ACCOUNT') return;
|
||||||
|
|
||||||
|
if (!REACT_APP_ALLOWED_URLS) {
|
||||||
|
console.log('Unauthorized app origin:', event.origin);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const allowedUrls = REACT_APP_ALLOWED_URLS.split(',').map(url => url.trim());
|
||||||
|
|
||||||
|
if (!allowedUrls.includes(event.origin)) {
|
||||||
|
console.log('Unauthorized app.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const network = networksData.find(network => network.chainId === event.data.chainId);
|
||||||
|
|
||||||
|
await addAccountHandler(network!);
|
||||||
|
const updatedAccounts = await getAccountsData(event.data.chainId);
|
||||||
|
const addresses = updatedAccounts.map((account: Account) => account.address);
|
||||||
|
|
||||||
|
sendMessage(event.source as Window, 'ADD_ACCOUNT_RESPONSE', addresses, event.origin);
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('message', handleAddAccount);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('message', handleAddAccount);
|
||||||
|
};
|
||||||
|
}, [networksData, getAccountsData, addAccountHandler]);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useAddAccountEmbed;
|
42
src/hooks/useExportPrivateKeyEmbed.ts
Normal file
42
src/hooks/useExportPrivateKeyEmbed.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
|
import { useAccounts } from '../context/AccountsContext';
|
||||||
|
import { getPathKey, sendMessage } from '../utils/misc';
|
||||||
|
|
||||||
|
const useExportPKEmbed = () => {
|
||||||
|
const { accounts } = useAccounts();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleMessage = async (event: MessageEvent) => {
|
||||||
|
const { type, chainId, address } = event.data;
|
||||||
|
|
||||||
|
if (type !== 'REQUEST_ACCOUNT_PK') return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const selectedAccount = accounts.find(account => account.address === address);
|
||||||
|
if (!selectedAccount) {
|
||||||
|
throw new Error("Account not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
const pathKey = await getPathKey(chainId, selectedAccount.index);
|
||||||
|
const privateKey = pathKey.privKey;
|
||||||
|
|
||||||
|
sendMessage(
|
||||||
|
event.source as Window,
|
||||||
|
'ACCOUNT_PK_DATA',
|
||||||
|
{ privateKey },
|
||||||
|
event.origin,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching private key:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('message', handleMessage);
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('message', handleMessage);
|
||||||
|
};
|
||||||
|
}, [accounts]);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useExportPKEmbed;
|
@ -34,7 +34,7 @@ const useGetOrCreateAccounts = () => {
|
|||||||
if (event.data.type !== 'REQUEST_CREATE_OR_GET_ACCOUNTS') return;
|
if (event.data.type !== 'REQUEST_CREATE_OR_GET_ACCOUNTS') return;
|
||||||
|
|
||||||
if (!REACT_APP_ALLOWED_URLS) {
|
if (!REACT_APP_ALLOWED_URLS) {
|
||||||
console.log('allowed URLs are not set.');
|
console.log('Allowed URLs are not set');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ export const AutoSignIn = () => {
|
|||||||
if (event.data.type !== 'AUTO_SIGN_IN') return;
|
if (event.data.type !== 'AUTO_SIGN_IN') return;
|
||||||
|
|
||||||
if (!REACT_APP_ALLOWED_URLS) {
|
if (!REACT_APP_ALLOWED_URLS) {
|
||||||
console.log('allowed URLs are not set.');
|
console.log('Allowed URLs are not set');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import React, { useEffect, useState } from 'react';
|
import React, { useCallback, useEffect, useState } from 'react';
|
||||||
import { ScrollView, View } from 'react-native';
|
import { ScrollView, View } from 'react-native';
|
||||||
import { ActivityIndicator, Button, Text, Appbar } from 'react-native-paper';
|
import { ActivityIndicator, Button, Text, Appbar } from 'react-native-paper';
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ const REACT_APP_ALLOWED_URLS = process.env.REACT_APP_ALLOWED_URLS;
|
|||||||
|
|
||||||
type SignRequestProps = NativeStackScreenProps<StackParamsList, 'sign-request-embed'>;
|
type SignRequestProps = NativeStackScreenProps<StackParamsList, 'sign-request-embed'>;
|
||||||
|
|
||||||
const SignMessageEmbed = ({ route }: SignRequestProps) => {
|
const SignRequestEmbed = ({ route }: SignRequestProps) => {
|
||||||
const [displayAccount, setDisplayAccount] = useState<Account>();
|
const [displayAccount, setDisplayAccount] = useState<Account>();
|
||||||
const [message, setMessage] = useState<string>('');
|
const [message, setMessage] = useState<string>('');
|
||||||
const [chainId, setChainId] = useState<string>('');
|
const [chainId, setChainId] = useState<string>('');
|
||||||
@ -72,7 +72,7 @@ const SignMessageEmbed = ({ route }: SignRequestProps) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const rejectRequestHandler = async () => {
|
const rejectRequestHandler = useCallback(async () => {
|
||||||
if (sourceWindow && origin) {
|
if (sourceWindow && origin) {
|
||||||
sendMessage(
|
sendMessage(
|
||||||
sourceWindow,
|
sourceWindow,
|
||||||
@ -81,8 +81,7 @@ const SignMessageEmbed = ({ route }: SignRequestProps) => {
|
|||||||
origin,
|
origin,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
navigation.navigate('Home');
|
}, [sourceWindow, origin]);
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleCosmosSignMessage = async (event: MessageEvent) => {
|
const handleCosmosSignMessage = async (event: MessageEvent) => {
|
||||||
@ -90,7 +89,7 @@ const SignMessageEmbed = ({ route }: SignRequestProps) => {
|
|||||||
|
|
||||||
|
|
||||||
if (!REACT_APP_ALLOWED_URLS) {
|
if (!REACT_APP_ALLOWED_URLS) {
|
||||||
console.log('allowed URLs are not set.');
|
console.log('Allowed URLs are not set');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,8 +149,7 @@ const SignMessageEmbed = ({ route }: SignRequestProps) => {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
}, [navigation, rejectRequestHandler]);
|
||||||
}, [navigation]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@ -188,4 +186,4 @@ const SignMessageEmbed = ({ route }: SignRequestProps) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default SignMessageEmbed;
|
export default SignRequestEmbed;
|
Loading…
Reference in New Issue
Block a user