From 2ccb3968894fa870500c0a7196670fff4162cb4c Mon Sep 17 00:00:00 2001 From: shreerang6921 <68148922+shreerang6921@users.noreply.github.com> Date: Thu, 4 Jul 2024 18:45:13 +0530 Subject: [PATCH] Show private key of selected account (#117) * Display private key of selected account * Update dialog box UI * Refactor show private key dialog * Refactor code to use context variables --- src/components/Accounts.tsx | 15 +++-- src/components/HDPath.tsx | 6 +- src/components/HDPathDialog.tsx | 2 - src/components/ShowPKDialog.tsx | 97 +++++++++++++++++++++++++++++++++ src/screens/HomeScreen.tsx | 9 +-- src/types.ts | 6 -- 6 files changed, 111 insertions(+), 24 deletions(-) create mode 100644 src/components/ShowPKDialog.tsx diff --git a/src/components/Accounts.tsx b/src/components/Accounts.tsx index 84cd84f..ede9122 100644 --- a/src/components/Accounts.tsx +++ b/src/components/Accounts.tsx @@ -6,7 +6,7 @@ import { setInternetCredentials } from 'react-native-keychain'; import { useNavigation } from '@react-navigation/native'; import { NativeStackNavigationProp } from '@react-navigation/native-stack'; -import { AccountsProps, StackParamsList, Account } from '../types'; +import { StackParamsList, Account } from '../types'; import { addAccount } from '../utils/accounts'; import styles from '../styles/stylesheet'; import HDPathDialog from './HDPathDialog'; @@ -16,12 +16,14 @@ import { web3wallet } from '../utils/wallet-connect/WalletConnectUtils'; import { useNetworks } from '../context/NetworksContext'; import ConfirmDialog from './ConfirmDialog'; import { getNamespaces } from '../utils/wallet-connect/helpers'; +import ShowPKDialog from './ShowPKDialog'; -const Accounts = ({ currentIndex, updateIndex }: AccountsProps) => { +const Accounts = () => { const navigation = useNavigation>(); - const { accounts, setAccounts, setCurrentIndex } = useAccounts(); + const { accounts, setAccounts, setCurrentIndex, currentIndex } = + useAccounts(); const { networksData, selectedNetwork, setNetworksData, setSelectedNetwork } = useNetworks(); const [expanded, setExpanded] = useState(false); @@ -80,7 +82,7 @@ const Accounts = ({ currentIndex, updateIndex }: AccountsProps) => { setIsAccountCreating(false); if (newAccount) { updateAccounts(newAccount); - updateIndex(newAccount.index); + setCurrentIndex(newAccount.index); } }; @@ -90,7 +92,7 @@ const Accounts = ({ currentIndex, updateIndex }: AccountsProps) => { key={account.index} title={`Account ${account.index + 1}`} onPress={() => { - updateIndex(account.index); + setCurrentIndex(account.index); setExpanded(false); }} /> @@ -120,7 +122,6 @@ const Accounts = ({ currentIndex, updateIndex }: AccountsProps) => { visible={hdDialog} hideDialog={() => setHdDialog(false)} updateAccounts={updateAccounts} - updateIndex={updateIndex} pathCode={pathCode} /> { hideDialog={hideDeleteNetworkDialog} onConfirm={handleRemove} /> + + ); diff --git a/src/components/HDPath.tsx b/src/components/HDPath.tsx index b590d1e..a49ca3c 100644 --- a/src/components/HDPath.tsx +++ b/src/components/HDPath.tsx @@ -5,20 +5,20 @@ import { Button, TextInput } from 'react-native-paper'; import { addAccountFromHDPath } from '../utils/accounts'; import { Account, NetworksDataState, PathState } from '../types'; import styles from '../styles/stylesheet'; +import { useAccounts } from '../context/AccountsContext'; const HDPath = ({ pathCode, updateAccounts, - updateIndex, hideDialog, selectedNetwork, }: { pathCode: string; - updateIndex: (index: number) => void; updateAccounts: (account: Account) => void; hideDialog: () => void; selectedNetwork: NetworksDataState; }) => { + const { setCurrentIndex } = useAccounts(); const [isAccountCreating, setIsAccountCreating] = useState(false); const [path, setPath] = useState({ firstNumber: '', @@ -46,7 +46,7 @@ const HDPath = ({ const newAccount = await addAccountFromHDPath(hdPath, selectedNetwork); if (newAccount) { updateAccounts(newAccount); - updateIndex(newAccount.index); + setCurrentIndex(newAccount.index); hideDialog(); } } catch (error) { diff --git a/src/components/HDPathDialog.tsx b/src/components/HDPathDialog.tsx index 6a8b168..2bd1f54 100644 --- a/src/components/HDPathDialog.tsx +++ b/src/components/HDPathDialog.tsx @@ -8,7 +8,6 @@ import { useNetworks } from '../context/NetworksContext'; const HDPathDialog = ({ visible, hideDialog, - updateIndex, updateAccounts, pathCode, }: HDPathDialogProps) => { @@ -22,7 +21,6 @@ const HDPathDialog = ({ diff --git a/src/components/ShowPKDialog.tsx b/src/components/ShowPKDialog.tsx new file mode 100644 index 0000000..6fdf3b3 --- /dev/null +++ b/src/components/ShowPKDialog.tsx @@ -0,0 +1,97 @@ +import { TouchableOpacity, View } from 'react-native'; +import React, { useState } from 'react'; +import { Button, Text, Portal, Dialog, useTheme } from 'react-native-paper'; + +import styles from '../styles/stylesheet'; +import { getPathKey } from '../utils/misc'; +import { useNetworks } from '../context/NetworksContext'; +import { useAccounts } from '../context/AccountsContext'; + +const ShowPKDialog = () => { + const { currentIndex } = useAccounts(); + const { selectedNetwork } = useNetworks(); + + const [privateKey, setprivateKey] = useState(); + const [showPKDialog, setShowPKDialog] = useState(false); + + const theme = useTheme(); + + const handleShowPrivateKey = async () => { + const pathKey = await getPathKey( + `${selectedNetwork!.namespace}:${selectedNetwork!.chainId}`, + currentIndex, + ); + + setprivateKey(pathKey.privKey); + }; + + const hideShowPKDialog = () => { + setShowPKDialog(false); + setprivateKey(undefined); + }; + + return ( + <> + + { + setShowPKDialog(true); + }}> + + Show Private Key + + + + + + + + {!privateKey ? ( + Show Private Key? + ) : ( + Private Key + )} + + + {privateKey && ( + + + {privateKey} + + + )} + + + + Warning: + + Never disclose this key. Anyone with your private keys can + steal any assets held in your account. + + + + + {!privateKey ? ( + <> + + + + ) : ( + + )} + + + + + + ); +}; + +export default ShowPKDialog; diff --git a/src/screens/HomeScreen.tsx b/src/screens/HomeScreen.tsx index 1e09b37..af8dc61 100644 --- a/src/screens/HomeScreen.tsx +++ b/src/screens/HomeScreen.tsx @@ -29,8 +29,7 @@ const WCLogo = () => { }; const HomeScreen = () => { - const { accounts, setAccounts, currentIndex, setCurrentIndex } = - useAccounts(); + const { accounts, setAccounts, setCurrentIndex } = useAccounts(); const { networksData, selectedNetwork, setSelectedNetwork, setNetworksData } = useNetworks(); @@ -123,10 +122,6 @@ const HomeScreen = () => { setCurrentIndex(0); }; - const updateIndex = (index: number) => { - setCurrentIndex(index); - }; - useEffect(() => { fetchAccounts(); }, [networksData, setAccounts, selectedNetwork, fetchAccounts]); @@ -142,7 +137,7 @@ const HomeScreen = () => { <> - +