diff --git a/src/js/actions/DiscoveryActions.js b/src/js/actions/DiscoveryActions.js index dacff461..39834052 100644 --- a/src/js/actions/DiscoveryActions.js +++ b/src/js/actions/DiscoveryActions.js @@ -192,20 +192,16 @@ const begin = (device: TrezorDevice, network: string): AsyncAction => { const discoverAccount = (device: TrezorDevice, discoveryProcess: Discovery): AsyncAction => { return async (dispatch: Dispatch, getState: GetState): Promise => { + const completed: boolean = discoveryProcess.completed; + discoveryProcess.completed = false; + const derivedKey = discoveryProcess.hdKey.derive(`m/${discoveryProcess.accountIndex}`); const path = discoveryProcess.basePath.concat(discoveryProcess.accountIndex); const publicAddress: string = EthereumjsUtil.publicToAddress(derivedKey.publicKey, true).toString('hex'); const ethAddress: string = EthereumjsUtil.toChecksumAddress(publicAddress); const network = discoveryProcess.network; - dispatch({ - type: ACCOUNT.CREATE, - device, - network, - index: discoveryProcess.accountIndex, - path, - address: ethAddress - }); + // TODO: check if address was created before @@ -285,19 +281,30 @@ const discoverAccount = (device: TrezorDevice, discoveryProcess: Discovery): Asy const balance = await getBalanceAsync(web3instance.web3, ethAddress); if (discoveryProcess.interrupted) return; - dispatch( - AccountsActions.setBalance(ethAddress, network, device.state || 'undefined', web3instance.web3.fromWei(balance.toString(), 'ether')) - ); - const nonce: number = await getNonceAsync(web3instance.web3, ethAddress); if (discoveryProcess.interrupted) return; - dispatch(AccountsActions.setNonce(ethAddress, network, device.state || 'undefined', nonce)); const addressIsEmpty = nonce < 1 && !balance.greaterThan(0); - if (!addressIsEmpty) { - dispatch( discoverAccount(device, discoveryProcess) ); - } else { + if (!addressIsEmpty || (addressIsEmpty && completed) || (addressIsEmpty && discoveryProcess.accountIndex === 0)) { + dispatch({ + type: ACCOUNT.CREATE, + device, + network, + index: discoveryProcess.accountIndex, + path, + address: ethAddress + }); + dispatch( + AccountsActions.setBalance(ethAddress, network, device.state || 'undefined', web3instance.web3.fromWei(balance.toString(), 'ether')) + ); + dispatch(AccountsActions.setNonce(ethAddress, network, device.state || 'undefined', nonce)); + + if (!completed) + dispatch( discoverAccount(device, discoveryProcess) ); + } + + if (addressIsEmpty) { // release acquired sesssion await TrezorConnect.getFeatures({ device: { diff --git a/src/js/components/wallet/aside/AccountSelection.js b/src/js/components/wallet/aside/AccountSelection.js index 3e210302..688b9fd6 100644 --- a/src/js/components/wallet/aside/AccountSelection.js +++ b/src/js/components/wallet/aside/AccountSelection.js @@ -11,7 +11,7 @@ import Loader from '~/js/components/common/LoaderCircle'; import Tooltip from 'rc-tooltip'; import type { Props } from './index'; -import type { TrezorDevice } from '~/flowtype'; +import type { TrezorDevice, Accounts } from '~/flowtype'; const AccountSelection = (props: Props): ?React$Element => { @@ -29,7 +29,8 @@ const AccountSelection = (props: Props): ?React$Element => { const fiatRate = props.fiat.find(f => f.network === selectedCoin.network); - const deviceAccounts: Array = findDeviceAccounts(accounts, selected, location.state.network); + const deviceAccounts: Accounts = findDeviceAccounts(accounts, selected, location.state.network); + let selectedAccounts = deviceAccounts.map((account, i) => { // const url: string = `${baseUrl}/network/${location.state.network}/account/${i}`; const url: string = location.pathname.replace(/account+\/([0-9]*)/, `account/${i}`);