Skip to content

Commit

Permalink
Bugfix: android exception manager crashes (#1182)
Browse files Browse the repository at this point in the history
* fix address issues

* handle null input

* handle provider type undefined to defsult rpc

* handle selectedAddress as checksummed always

* use preferences controller setselectedaddress

* safetochecksumaddress when needed

* fix some merge conflicts

* comments
  • Loading branch information
estebanmino authored Nov 12, 2019
1 parent ebc2509 commit 160aa4d
Show file tree
Hide file tree
Showing 24 changed files with 56 additions and 57 deletions.
6 changes: 3 additions & 3 deletions app/components/Nav/Main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ import PaymentChannelDeposit from '../../Views/PaymentChannel/PaymentChannelDepo
import PaymentChannelSend from '../../Views/PaymentChannel/PaymentChannelSend';
import Networks from '../../../util/networks';
import { CONNEXT_DEPOSIT, getMethodData, TOKEN_METHOD_TRANSFER, decodeTransferData } from '../../../util/transactions';
import { BN, toChecksumAddress, isValidAddress } from 'ethereumjs-util';
import { isENS } from '../../../util/address';
import { BN, isValidAddress } from 'ethereumjs-util';
import { isENS, safeToChecksumAddress } from '../../../util/address';
import Logger from '../../../util/Logger';
import contractMap from 'eth-contract-metadata';
import MessageSign from '../../UI/MessageSign';
Expand Down Expand Up @@ -662,7 +662,7 @@ class Main extends PureComponent {
return;
}
// Check if it's a payment channel deposit transaction to sign
const to = toChecksumAddress(transactionMeta.transaction.to);
const to = safeToChecksumAddress(transactionMeta.transaction.to);
const networkId = Networks[this.props.providerType].networkId;
if (
this.props.paymentChannelsEnabled &&
Expand Down
6 changes: 3 additions & 3 deletions app/components/UI/AccountList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class AccountList extends PureComponent {
keyrings && keyrings.length ? keyrings : Engine.context.KeyringController.state.keyrings;
const accountsOrdered = allKeyrings.reduce((list, keyring) => list.concat(keyring.accounts), []);

await PreferencesController.update({ selectedAddress: accountsOrdered[newIndex] });
PreferencesController.setSelectedAddress(accountsOrdered[newIndex]);

this.props.onAccountChange();

Expand Down Expand Up @@ -190,7 +190,7 @@ class AccountList extends PureComponent {
await KeyringController.addNewAccount();
const { PreferencesController } = Engine.context;
const newIndex = Object.keys(this.props.identities).length - 1;
await PreferencesController.update({ selectedAddress: Object.keys(this.props.identities)[newIndex] });
PreferencesController.setSelectedAddress(Object.keys(this.props.identities)[newIndex]);
this.setState({ selectedAccountIndex: newIndex });
setTimeout(() => {
this.flatList && this.flatList.current && this.flatList.current.scrollToEnd();
Expand Down Expand Up @@ -262,7 +262,7 @@ class AccountList extends PureComponent {
const identity = identities[checksummedAddress];
const { name, address } = identity;
const identityAddressChecksummed = toChecksumAddress(address);
const isSelected = identityAddressChecksummed === toChecksumAddress(selectedAddress);
const isSelected = identityAddressChecksummed === selectedAddress;
const isImported = this.isImported(allKeyrings, identityAddressChecksummed);
let balance = 0x0;
if (accounts[identityAddressChecksummed]) {
Expand Down
4 changes: 2 additions & 2 deletions app/components/UI/AccountSelect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import PropTypes from 'prop-types';
import { StyleSheet, Text, TouchableOpacity, View, ScrollView } from 'react-native';
import { colors, fontStyles } from '../../../styles/common';
import { connect } from 'react-redux';
import { toChecksumAddress } from 'ethereumjs-util';
import { hexToBN, weiToFiat, renderFromWei } from '../../../util/number';
import { getTicker } from '../../../util/transactions';
import PaymentChannelsClient from '../../../core/PaymentChannelsClient';
import { strings } from '../../../../locales/i18n';
import { safeToChecksumAddress } from '../../../util/address';

const styles = StyleSheet.create({
root: {
Expand Down Expand Up @@ -148,7 +148,7 @@ class AccountSelect extends PureComponent {

renderActiveOption() {
const { selectedAddress, accounts, identities, value, isOpen, openAccountSelect } = this.props;
const targetAddress = toChecksumAddress(value || selectedAddress);
const targetAddress = safeToChecksumAddress(value) || selectedAddress;
const account = { ...identities[targetAddress], ...accounts[targetAddress] };
return (
<View style={styles.activeOption}>
Expand Down
4 changes: 2 additions & 2 deletions app/components/UI/AssetOverview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import AssetActionButtons from '../AssetActionButtons';
import { setTokensTransaction } from '../../../actions/transaction';
import { toggleReceiveModal } from '../../../actions/modals';
import { connect } from 'react-redux';
import { toChecksumAddress } from 'ethereumjs-util';
import { renderFromTokenMinimalUnit, balanceToFiat, renderFromWei, weiToFiat, hexToBN } from '../../../util/number';
import { safeToChecksumAddress } from '../../../util/address';

const styles = StyleSheet.create({
wrapper: {
Expand Down Expand Up @@ -148,7 +148,7 @@ class AssetOverview extends PureComponent {
currentCurrency
} = this.props;
let mainBalance, secondaryBalance;
const itemAddress = (asset.address && toChecksumAddress(asset.address)) || undefined;
const itemAddress = safeToChecksumAddress(asset.address);
let balance, balanceFiat;
if (asset.isETH) {
balance = renderFromWei(accounts[selectedAddress] && accounts[selectedAddress].balance);
Expand Down
3 changes: 1 addition & 2 deletions app/components/UI/DrawerView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { renderFromWei, renderFiat } from '../../../util/number';
import { strings } from '../../../../locales/i18n';
import { DrawerActions } from 'react-navigation-drawer'; // eslint-disable-line
import Modal from 'react-native-modal';
import { toChecksumAddress } from 'ethereumjs-util';
import SecureKeychain from '../../../core/SecureKeychain';
import { toggleNetworkModal, toggleAccountsModal, toggleReceiveModal } from '../../../actions/modals';
import { showAlert } from '../../../actions/alert';
Expand Down Expand Up @@ -1046,7 +1045,7 @@ class DrawerView extends PureComponent {

const mapStateToProps = state => ({
network: state.engine.backgroundState.NetworkController,
selectedAddress: toChecksumAddress(state.engine.backgroundState.PreferencesController.selectedAddress),
selectedAddress: state.engine.backgroundState.PreferencesController.selectedAddress,
accounts: state.engine.backgroundState.AccountTrackerController.accounts,
identities: state.engine.backgroundState.PreferencesController.identities,
frequentRpcList: state.engine.backgroundState.PreferencesController.frequentRpcList,
Expand Down
4 changes: 3 additions & 1 deletion app/components/UI/NavbarTitle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ class NavbarTitle extends PureComponent {
if (network.provider.nickname) {
name = network.provider.nickname;
} else {
name = Networks[network.provider.type].name || { ...Networks.rpc, color: null }.name;
name =
(Networks[network.provider.type] && Networks[network.provider.type].name) ||
{ ...Networks.rpc, color: null }.name;
}

const realTitle = translate ? strings(title) : title;
Expand Down
3 changes: 1 addition & 2 deletions app/components/UI/ReceiveRequest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { colors, fontStyles } from '../../../styles/common';
import ReceiveRequestAction from './ReceiveRequestAction';
import Logger from '../../../util/Logger';
import Share from 'react-native-share'; // eslint-disable-line import/default
import { toChecksumAddress } from 'ethereumjs-util';
import { connect } from 'react-redux';
import { toggleReceiveModal } from '../../../actions/modals';
import Modal from 'react-native-modal';
Expand Down Expand Up @@ -268,7 +267,7 @@ class ReceiveRequest extends PureComponent {
}

const mapStateToProps = state => ({
selectedAddress: toChecksumAddress(state.engine.backgroundState.PreferencesController.selectedAddress),
selectedAddress: state.engine.backgroundState.PreferencesController.selectedAddress,
receiveAsset: state.modals.receiveAsset
});

Expand Down
4 changes: 2 additions & 2 deletions app/components/UI/Tokens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { renderFromTokenMinimalUnit, balanceToFiat } from '../../../util/number'
import Engine from '../../../core/Engine';
import AssetElement from '../AssetElement';
import FadeIn from 'react-native-fade-in-image';
import { toChecksumAddress } from 'ethereumjs-util';
import { connect } from 'react-redux';
import { safeToChecksumAddress } from '../../../util/address';

const styles = StyleSheet.create({
wrapper: {
Expand Down Expand Up @@ -135,7 +135,7 @@ class Tokens extends PureComponent {

renderItem = asset => {
const { conversionRate, currentCurrency, tokenBalances, tokenExchangeRates, primaryCurrency } = this.props;
const itemAddress = (asset.address && toChecksumAddress(asset.address)) || undefined;
const itemAddress = safeToChecksumAddress(asset.address);
const logo = asset.logo || ((contractMap[itemAddress] && contractMap[itemAddress].logo) || undefined);
const exchangeRate = itemAddress in tokenExchangeRates ? tokenExchangeRates[itemAddress] : undefined;
const balance =
Expand Down
7 changes: 4 additions & 3 deletions app/components/UI/TransactionEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Engine from '../../../core/Engine';
import collectiblesTransferInformation from '../../../util/collectibles-transfer';
import contractMap from 'eth-contract-metadata';
import PaymentChannelsClient from '../../../core/PaymentChannelsClient';
import { safeToChecksumAddress } from '../../../util/address';

const EDIT = 'edit';
const REVIEW = 'review';
Expand Down Expand Up @@ -356,7 +357,7 @@ class TransactionEditor extends PureComponent {
const {
transaction: { value, gas, gasPrice, from }
} = this.props;
const checksummedFrom = from ? toChecksumAddress(from) : '';
const checksummedFrom = safeToChecksumAddress(from) || '';
const fromAccount = this.props.accounts[checksummedFrom];
if (!value || !gas || !gasPrice || !from) return strings('transaction.invalid_amount');
if (value && !isBN(value)) return strings('transaction.invalid_amount');
Expand Down Expand Up @@ -386,7 +387,7 @@ class TransactionEditor extends PureComponent {
transaction: { value, gas, gasPrice, from, selectedAsset },
contractBalances
} = this.props;
const checksummedFrom = from ? toChecksumAddress(from) : '';
const checksummedFrom = safeToChecksumAddress(from) || '';
const fromAccount = this.props.accounts[checksummedFrom];
if (!value || !gas || !gasPrice || !from) {
return strings('transaction.invalid_amount');
Expand Down Expand Up @@ -464,7 +465,7 @@ class TransactionEditor extends PureComponent {
if (gasPrice && !isBN(gasPrice)) return strings('transaction.invalid_gas_price');
if (gas.lt(new BN(21000)) || gas.gt(new BN(7920028))) return strings('custom_gas.warning_gas_limit');

const checksummedFrom = from ? toChecksumAddress(from) : '';
const checksummedFrom = safeToChecksumAddress(from) || '';
const fromAccount = this.props.accounts[checksummedFrom];
if (fromAccount && isBN(gas) && isBN(gasPrice) && hexToBN(fromAccount.balance).lt(gas.mul(gasPrice)))
return strings('transaction.insufficient');
Expand Down
7 changes: 3 additions & 4 deletions app/components/UI/TransactionElement/TransferElement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import {
renderToGwei,
weiToFiatNumber
} from '../../../../util/number';
import { toChecksumAddress } from 'ethereumjs-util';
import { getActionKey, decodeTransferData, isCollectibleAddress } from '../../../../util/transactions';
import { renderFullAddress } from '../../../../util/address';
import { renderFullAddress, safeToChecksumAddress } from '../../../../util/address';

const styles = StyleSheet.create({
row: {
Expand Down Expand Up @@ -135,8 +134,8 @@ export default class TransferElement extends PureComponent {

const amount = toBN(encodedAmount);

const userHasToken = toChecksumAddress(to) in tokens;
const token = userHasToken ? tokens[toChecksumAddress(to)] : null;
const userHasToken = safeToChecksumAddress(to) in tokens;
const token = userHasToken ? tokens[safeToChecksumAddress(to)] : null;
const renderActionKey = token ? strings('transactions.sent') + ' ' + token.symbol : actionKey;
const renderTokenAmount = token
? renderFromTokenMinimalUnit(amount, token.decimals) + ' ' + token.symbol
Expand Down
7 changes: 3 additions & 4 deletions app/components/UI/TransactionElement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import { colors, fontStyles } from '../../../styles/common';
import { strings } from '../../../../locales/i18n';
import { toLocaleDateTime } from '../../../util/date';
import { renderFromWei, weiToFiat, hexToBN, toBN, isBN, renderToGwei, balanceToFiat } from '../../../util/number';
import { toChecksumAddress } from 'ethereumjs-util';
import Identicon from '../Identicon';
import { getActionKey, decodeTransferData, getTicker } from '../../../util/transactions';
import TransactionDetails from './TransactionDetails';
import { renderFullAddress } from '../../../util/address';
import { renderFullAddress, safeToChecksumAddress } from '../../../util/address';
import FadeIn from 'react-native-fade-in-image';
import TokenImage from '../TokenImage';
import contractMap from 'eth-contract-metadata';
Expand Down Expand Up @@ -254,8 +253,8 @@ class TransactionElement extends PureComponent {

renderTxTime = () => {
const { tx, selectedAddress } = this.props;
const incoming = tx.transaction.to && toChecksumAddress(tx.transaction.to) === selectedAddress;
const selfSent = incoming && toChecksumAddress(tx.transaction.from) === selectedAddress;
const incoming = safeToChecksumAddress(tx.transaction.to) === selectedAddress;
const selfSent = incoming && safeToChecksumAddress(tx.transaction.from) === selectedAddress;
return (
<Text style={styles.date}>
{(!incoming || selfSent) && tx.transaction.nonce && `#${parseInt(tx.transaction.nonce, 16)} - `}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { strings } from '../../../../../locales/i18n';
import { connect } from 'react-redux';
import { APPROVE_FUNCTION_SIGNATURE, decodeTransferData, getTicker } from '../../../../util/transactions';
import contractMap from 'eth-contract-metadata';
import { toChecksumAddress } from 'ethereumjs-util';
import IonicIcon from 'react-native-vector-icons/Ionicons';
import { safeToChecksumAddress } from '../../../../util/address';

const styles = StyleSheet.create({
confirmBadge: {
Expand Down Expand Up @@ -113,9 +113,9 @@ class TransactionReviewSummary extends PureComponent {
let assetAmount, conversionRate, fiatValue;
const approveTransaction = data && data.substr(0, 10) === APPROVE_FUNCTION_SIGNATURE;
if (approveTransaction) {
let contract = contractMap[toChecksumAddress(to)];
let contract = contractMap[safeToChecksumAddress(to)];
if (!contract) {
contract = tokens.find(({ address }) => address === toChecksumAddress(to));
contract = tokens.find(({ address }) => address === safeToChecksumAddress(to));
}
const symbol = (contract && contract.symbol) || 'ERC20';
assetAmount = `${decodeTransferData('transfer', data)[1]} ${symbol}`;
Expand Down
5 changes: 2 additions & 3 deletions app/components/UI/TransactionReview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import DefaultTabBar from 'react-native-scrollable-tab-view/DefaultTabBar';
import TransactionReviewInformation from './TransactionReviewInformation';
import TransactionReviewData from './TransactionReviewData';
import TransactionReviewSummary from './TransactionReviewSummary';
import { renderAccountName } from '../../../util/address';
import { renderAccountName, safeToChecksumAddress } from '../../../util/address';
import Analytics from '../../../core/Analytics';
import ANALYTICS_EVENT_OPTS from '../../../util/analytics';
import contractMap from 'eth-contract-metadata';
import { toChecksumAddress } from 'ethereumjs-util';
import AssetIcon from '../AssetIcon';

const FONT_SIZE = PixelRatio.get() < 2 ? 12 : 16;
Expand Down Expand Up @@ -240,7 +239,7 @@ class TransactionReview extends PureComponent {
transaction: { from, to },
identities
} = this.props;
const contract = contractMap[toChecksumAddress(to)];
const contract = contractMap[safeToChecksumAddress(to)];
return (
<View style={styles.graphic}>
<View style={[styles.addressGraphic, styles.fromGraphic]}>
Expand Down
2 changes: 1 addition & 1 deletion app/components/UI/UrlAutocomplete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class UrlAutocomplete extends PureComponent {
};

render() {
if (this.props.input.length < 2) return <View style={styles.wrapper} />;
if (!this.props.input || this.props.input.length < 2) return <View style={styles.wrapper} />;
if (this.state.results.length === 0) {
return (
<View style={styles.wrapper}>
Expand Down
3 changes: 1 addition & 2 deletions app/components/Views/AddressQRCode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { TouchableOpacity, Dimensions, StyleSheet, View, Text, Clipboard } from 'react-native';
import { colors, fontStyles } from '../../../styles/common';
import { toChecksumAddress } from 'ethereumjs-util';
import { connect } from 'react-redux';
import QRCode from 'react-native-qrcode-svg';
import { strings } from '../../../../locales/i18n';
Expand Down Expand Up @@ -136,7 +135,7 @@ class AddressQRCode extends PureComponent {
}

const mapStateToProps = state => ({
selectedAddress: toChecksumAddress(state.engine.backgroundState.PreferencesController.selectedAddress)
selectedAddress: state.engine.backgroundState.PreferencesController.selectedAddress
});

const mapDispatchToProps = dispatch => ({
Expand Down
5 changes: 2 additions & 3 deletions app/components/Views/Asset/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { PureComponent } from 'react';
import { ActivityIndicator, InteractionManager, View, StyleSheet } from 'react-native';
import PropTypes from 'prop-types';
import { toChecksumAddress } from 'ethereumjs-util';
import Networks, { isKnownNetwork } from '../../../util/networks';
import { connect } from 'react-redux';
import { colors } from '../../../styles/common';
import AssetOverview from '../../UI/AssetOverview';
import Transactions from '../../UI/Transactions';
import { getNetworkNavbarOptions } from '../../UI/Navbar';
import Engine from '../../../core/Engine';
import { safeToChecksumAddress } from '../../../util/address';

const styles = StyleSheet.create({
wrapper: {
Expand Down Expand Up @@ -125,8 +125,7 @@ class Asset extends PureComponent {
transaction: { from, to }
} = tx;
return (
((from && toChecksumAddress(from) === selectedAddress) ||
(to && toChecksumAddress(to) === selectedAddress)) &&
(safeToChecksumAddress(from) === selectedAddress || safeToChecksumAddress(to) === selectedAddress) &&
((networkId && networkId.toString() === tx.networkID) ||
(networkType === 'rpc' && !isKnownNetwork(tx.networkID))) &&
tx.status !== 'unapproved'
Expand Down
4 changes: 2 additions & 2 deletions app/components/Views/ChoosePassword/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ class ChoosePassword extends PureComponent {

// Reselect previous selected account if still available
if (hdKeyring.accounts.includes(selectedAddress)) {
await PreferencesController.update({ selectedAddress });
PreferencesController.setSelectedAddress(selectedAddress);
} else {
await PreferencesController.update({ selectedAddress: hdKeyring[0] });
PreferencesController.setSelectedAddress(hdKeyring[0]);
}

if (this.state.biometryType && this.state.biometryChoice) {
Expand Down
2 changes: 1 addition & 1 deletion app/components/Views/ImportPrivateKeySuccess/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ImportPrivateKeySuccess extends PureComponent {
const allKeyrings =
keyrings && keyrings.length ? keyrings : Engine.context.KeyringController.state.keyrings;
const accountsOrdered = allKeyrings.reduce((list, keyring) => list.concat(keyring.accounts), []);
await PreferencesController.update({ selectedAddress: accountsOrdered[accountsOrdered.length - 1] });
PreferencesController.setSelectedAddress(accountsOrdered[accountsOrdered.length - 1]);
} catch (e) {
Logger.error('Error while refreshing imported pkey', e);
}
Expand Down
5 changes: 2 additions & 3 deletions app/components/Views/PaymentChannel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import Logger from '../../../util/Logger';
import { toBN, balanceToFiatNumber, BNToHex } from '../../../util/number';
import AssetCard from '../AssetCard';
import Engine from '../../../core/Engine';
import { toChecksumAddress } from 'ethereumjs-util';
import { setPaymentChannelTransaction } from '../../../actions/transaction';
import Transactions from '../../UI/Transactions';
import Networks from '../../../util/networks';
Expand Down Expand Up @@ -558,8 +557,8 @@ class PaymentChannel extends PureComponent {
const { TokenRatesController } = Engine.context;
const { nativeCurrency, currentCurrency, contractExchangeRates, conversionRate } = this.props;
let exchangeRate;
if (Object.keys(contractExchangeRates).includes(toChecksumAddress(DAI_ADDRESS))) {
exchangeRate = contractExchangeRates[toChecksumAddress(DAI_ADDRESS)];
if (Object.keys(contractExchangeRates).includes(DAI_ADDRESS)) {
exchangeRate = contractExchangeRates[DAI_ADDRESS];
} else {
const res = await TokenRatesController.fetchExchangeRate(
`contract_addresses=${DAI_ADDRESS}&vs_currencies=${nativeCurrency.toLowerCase()}`
Expand Down
Loading

0 comments on commit 160aa4d

Please sign in to comment.