import { Web3Provider } from '@ethersproject/providers' ; import { getApprovedAccounts , setupWeb3Node } from '@services/EthService' ; import { getNetworkByChainId } from '@services/Store' ; import { Network } from '@types' ; import { Web3Wallet } from '../non-deterministic' ; export const unlockWeb3 = async ( networks ) => { const { lib : nodeLib , chainId } = await setupWeb3Node ( ) ; const network = getNetworkByChainId ( chainId , networks ) ; if ( ! network ) { throw new Error ( `MyCrypto doesn’t support the network with chain ID '${ chainId }'` ) ; } try { const accounts = await getApprovedAccounts ( nodeLib ) ; if ( accounts ) { return accounts . map ( ( address ) => new Web3Wallet ( address , network . id ) ) ; } else { throw new Error ( 'Failed to get web3 accounts via wallet_getPermissions' ) ; } } catch { try { const legacyAccounts = await getLegacyAccounts ( nodeLib ) ; if ( legacyAccounts ) { return legacyAccounts . map ( ( address ) => new Web3Wallet ( address , network . id ) ) ; } throw new Error ( 'Could not get accounts' ) ; } catch { throw new Error ( 'Error fetching legacy web3 accounts' ) ; } } } ; const getLegacyAccounts = async ( nodeLib ) => await nodeLib . listAccounts ( ) ;