From f34811546a70f077e782b31707f0c4434aa5dde3 Mon Sep 17 00:00:00 2001 From: Daniel Ternyak Date: Mon, 11 Sep 2017 20:29:07 -0500 Subject: [PATCH] Fix Unlock Bugs (#183) * Remove unused imports. * Create and use .toPrecision forwarding method for `Unit` * Error handling when unlocking trezor devices. * Use translateRaw to fulfill string req; --- common/components/BalanceSidebar/AccountInfo.jsx | 1 - common/components/BalanceSidebar/index.jsx | 1 - .../WalletDecrypt/DeterministicWalletsModal.jsx | 4 +--- common/components/WalletDecrypt/Trezor.jsx | 2 +- common/components/WalletDecrypt/index.jsx | 9 ++++++++- common/libs/units.js | 4 ++++ common/reducers/wallet.js | 1 - 7 files changed, 14 insertions(+), 8 deletions(-) diff --git a/common/components/BalanceSidebar/AccountInfo.jsx b/common/components/BalanceSidebar/AccountInfo.jsx index 69a221d3..e23a6d9a 100644 --- a/common/components/BalanceSidebar/AccountInfo.jsx +++ b/common/components/BalanceSidebar/AccountInfo.jsx @@ -4,7 +4,6 @@ import React from 'react'; import translate from 'translations'; import { Identicon } from 'components/ui'; import { formatNumber } from 'utils/formatters'; -import type Big from 'bignumber.js'; import type { IWallet } from 'libs/wallet'; import type { NetworkConfig } from 'config/data'; import { Ether } from 'libs/units'; diff --git a/common/components/BalanceSidebar/index.jsx b/common/components/BalanceSidebar/index.jsx index d41a2992..1c3e9848 100644 --- a/common/components/BalanceSidebar/index.jsx +++ b/common/components/BalanceSidebar/index.jsx @@ -1,6 +1,5 @@ // @flow import React from 'react'; -import Big from 'bignumber.js'; import { IWallet } from 'libs/wallet'; import type { NetworkConfig } from 'config/data'; import type { State } from 'reducers'; diff --git a/common/components/WalletDecrypt/DeterministicWalletsModal.jsx b/common/components/WalletDecrypt/DeterministicWalletsModal.jsx index d0f18482..06c94429 100644 --- a/common/components/WalletDecrypt/DeterministicWalletsModal.jsx +++ b/common/components/WalletDecrypt/DeterministicWalletsModal.jsx @@ -142,9 +142,7 @@ class DeterministicWalletsModal extends React.Component { const { selectedAddress } = this.state; // Get renderable values, but keep 'em short - const value = wallet.value - ? toUnit(wallet.value, 'wei', 'ether').toPrecision(4) - : ''; + const value = wallet.value ? wallet.value.toEther().toPrecision(4) : ''; const tokenValue = wallet.tokenValues[desiredToken] ? wallet.tokenValues[desiredToken].toPrecision(4) : ''; diff --git a/common/components/WalletDecrypt/Trezor.jsx b/common/components/WalletDecrypt/Trezor.jsx index 28a0ec12..df0d111f 100644 --- a/common/components/WalletDecrypt/Trezor.jsx +++ b/common/components/WalletDecrypt/Trezor.jsx @@ -116,7 +116,7 @@ export default class TrezorDecrypt extends Component { onCancel={this._handleCancel} onConfirmAddress={this._handleUnlock} onPathChange={this._handlePathChange} - walletType={translate('x_Trezor')} + walletType={translate('x_Trezor', true)} /> ); diff --git a/common/components/WalletDecrypt/index.jsx b/common/components/WalletDecrypt/index.jsx index 31f19f11..b3919813 100644 --- a/common/components/WalletDecrypt/index.jsx +++ b/common/components/WalletDecrypt/index.jsx @@ -11,6 +11,7 @@ import ViewOnlyDecrypt from './ViewOnly'; import map from 'lodash/map'; import { unlockPrivateKey, unlockKeystore, setWallet } from 'actions/wallet'; import { connect } from 'react-redux'; +import isEmpty from 'lodash/isEmpty'; const WALLETS = { 'keystore-file': { @@ -164,8 +165,14 @@ export class WalletDecrypt extends Component { }; onUnlock = (payload: any) => { + // some components (TrezorDecrypt) don't take an onChange prop, and thus this.state.value will remain unpopulated. + // in this case, we can expect the payload to contain the unlocked wallet info. + const unlockValue = + this.state.value && !isEmpty(this.state.value) + ? this.state.value + : payload; this.props.dispatch( - WALLETS[this.state.selectedWalletKey].unlock(this.state.value || payload) + WALLETS[this.state.selectedWalletKey].unlock(unlockValue) ); }; } diff --git a/common/libs/units.js b/common/libs/units.js index 26fa4cf7..c99f31f3 100644 --- a/common/libs/units.js +++ b/common/libs/units.js @@ -47,6 +47,10 @@ class Unit { return this.amount.toString(base); } + toPrecision(precision?: number) { + return this.amount.toPrecision(precision); + } + toWei(): Wei { return new Wei(toWei(this.amount, this.unit)); } diff --git a/common/reducers/wallet.js b/common/reducers/wallet.js index 28ea3680..4a9c8221 100644 --- a/common/reducers/wallet.js +++ b/common/reducers/wallet.js @@ -6,7 +6,6 @@ import type { SetTokenBalancesAction } from 'actions/wallet'; import { IWallet } from 'libs/wallet'; -import { toUnit } from 'libs/units'; import Big from 'bignumber.js'; import { getTxFromBroadcastTransactionStatus } from 'selectors/wallet'; import type { BroadcastTransactionStatus } from 'libs/transaction';