import React, { useEffect, useState } from 'react'; import { ScrollView, TouchableOpacity, View } from 'react-native'; import { Button, List, Text, useTheme } from 'react-native-paper'; import { useNavigation } from '@react-navigation/native'; import { NativeStackNavigationProp } from '@react-navigation/native-stack'; import { AccountsProps, StackParamsList, Account } from '../types'; import { addAccount } from '../utils/accounts'; import styles from '../styles/stylesheet'; import HDPathDialog from './HDPathDialog'; import AccountDetails from './AccountDetails'; import { useAccounts } from '../context/AccountsContext'; import { web3wallet } from '../utils/wallet-connect/WalletConnectUtils'; const Accounts = ({ network, currentIndex, updateIndex: updateId, }: AccountsProps) => { const navigation = useNavigation>(); const { accounts, setAccounts } = useAccounts(); const [expanded, setExpanded] = useState(false); const [isAccountCreating, setIsAccountCreating] = useState(false); const [hdDialog, setHdDialog] = useState(false); const [pathCode, setPathCode] = useState(''); const theme = useTheme(); const handlePress = () => setExpanded(!expanded); const updateAccounts = (account: Account) => { switch (network) { case 'eth': setAccounts({ ...accounts, ethAccounts: [...accounts.ethAccounts, account], }); break; case 'cosmos': setAccounts({ ...accounts, cosmosAccounts: [...accounts.cosmosAccounts, account], }); break; default: console.error('Select a valid network!'); } }; useEffect(() => { const updateSessions = async () => { const sessions = web3wallet.getActiveSessions(); for (const sessionId in sessions) { const session = sessions[sessionId]; const requiredNamespaces = session.requiredNamespaces; const namespaces = session.namespaces; if (namespaces.hasOwnProperty('eip155')) { requiredNamespaces.eip155.chains?.forEach(chainId => { namespaces.eip155.accounts = accounts.ethAccounts.map( ethAccount => `${chainId}:${ethAccount.address}`, ); }); } if (namespaces.hasOwnProperty('cosmos')) { requiredNamespaces?.cosmos.chains?.forEach(chainId => { namespaces.cosmos.accounts = accounts.cosmosAccounts.map( cosmosAccount => `${chainId}:${cosmosAccount.address}`, ); }); } await web3wallet.updateSession({ topic: sessionId, namespaces }); } }; updateSessions(); }, [accounts]); const addAccountHandler = async () => { setIsAccountCreating(true); const newAccount = await addAccount(network); setIsAccountCreating(false); if (newAccount) { updateAccounts(newAccount); updateId(newAccount.counterId); } }; const selectedAccounts: Account[] = (() => { switch (network) { case 'eth': return accounts.ethAccounts; case 'cosmos': return accounts.cosmosAccounts; default: return []; } })(); const renderAccountItems = () => selectedAccounts.map(account => ( { updateId(account.counterId); setExpanded(false); }} /> )); return ( setHdDialog(false)} updateAccounts={updateAccounts} updateIndex={updateId} pathCode={pathCode} /> {renderAccountItems()} { navigation.navigate('SignMessage', { selectedNetwork: network, accountInfo: selectedAccounts[currentIndex], }); }}> Sign Message ); }; export default Accounts;