1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-28 09:58:23 +00:00

Merge pull request #284 from trezor/fix/account-refactoring

Fix/account refactoring
This commit is contained in:
Vladimir Volek 2019-01-03 14:33:18 +01:00 committed by GitHub
commit c8b850dbd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 117 additions and 186 deletions

View File

@ -4,59 +4,13 @@ import * as ACCOUNT from 'actions/constants/account';
import type { Action } from 'flowtype'; import type { Action } from 'flowtype';
import type { Account, State } from 'reducers/AccountsReducer'; import type { Account, State } from 'reducers/AccountsReducer';
export type AccountFromStorageAction = { export type AccountAction = {
type: typeof ACCOUNT.FROM_STORAGE, type: typeof ACCOUNT.FROM_STORAGE,
payload: State payload: State
} } | {
type: typeof ACCOUNT.CREATE | typeof ACCOUNT.UPDATE,
export type AccountCreateAction = {
type: typeof ACCOUNT.CREATE,
payload: Account, payload: Account,
} };
export type AccountUpdateAction = {
type: typeof ACCOUNT.UPDATE,
payload: Account,
}
export type AccountSetBalanceAction = {
type: typeof ACCOUNT.SET_BALANCE,
address: string,
network: string,
deviceState: string,
balance: string
}
export type AccountSetNonceAction = {
type: typeof ACCOUNT.SET_NONCE,
address: string,
network: string,
deviceState: string,
nonce: number
}
export type AccountAction =
AccountFromStorageAction
| AccountCreateAction
| AccountUpdateAction
| AccountSetBalanceAction
| AccountSetNonceAction;
export const setBalance = (address: string, network: string, deviceState: string, balance: string): Action => ({
type: ACCOUNT.SET_BALANCE,
address,
network,
deviceState,
balance,
});
export const setNonce = (address: string, network: string, deviceState: string, nonce: number): Action => ({
type: ACCOUNT.SET_NONCE,
address,
network,
deviceState,
nonce,
});
export const update = (account: Account): Action => ({ export const update = (account: Account): Action => ({
type: ACCOUNT.UPDATE, type: ACCOUNT.UPDATE,

View File

@ -12,7 +12,7 @@ import { httpRequest } from 'utils/networkUtils';
import * as buildUtils from 'utils/build'; import * as buildUtils from 'utils/build';
import * as storageUtils from 'utils/storage'; import * as storageUtils from 'utils/storage';
import { findAccountTokens } from 'reducers/TokensReducer'; import { getAccountTokens } from 'reducers/utils';
import type { Account } from 'reducers/AccountsReducer'; import type { Account } from 'reducers/AccountsReducer';
import type { Token } from 'reducers/TokensReducer'; import type { Token } from 'reducers/TokensReducer';
import type { Discovery } from 'reducers/DiscoveryReducer'; import type { Discovery } from 'reducers/DiscoveryReducer';
@ -59,11 +59,11 @@ const KEY_BETA_MODAL: string = '/betaModalPrivacy'; // this key needs to be comp
const findAccounts = (devices: Array<TrezorDevice>, accounts: Array<Account>): Array<Account> => devices.reduce((arr, dev) => arr.concat(accounts.filter(a => a.deviceState === dev.state)), []); const findAccounts = (devices: Array<TrezorDevice>, accounts: Array<Account>): Array<Account> => devices.reduce((arr, dev) => arr.concat(accounts.filter(a => a.deviceState === dev.state)), []);
const findTokens = (accounts: Array<Account>, tokens: Array<Token>): Array<Token> => accounts.reduce((arr, account) => arr.concat(findAccountTokens(tokens, account)), []); const findTokens = (accounts: Array<Account>, tokens: Array<Token>): Array<Token> => accounts.reduce((arr, account) => arr.concat(getAccountTokens(tokens, account)), []);
const findDiscovery = (devices: Array<TrezorDevice>, discovery: Array<Discovery>): Array<Discovery> => devices.reduce((arr, dev) => arr.concat(discovery.filter(d => d.deviceState === dev.state && d.completed)), []); const findDiscovery = (devices: Array<TrezorDevice>, discovery: Array<Discovery>): Array<Discovery> => devices.reduce((arr, dev) => arr.concat(discovery.filter(d => d.deviceState === dev.state && d.completed)), []);
const findPendingTxs = (accounts: Array<Account>, pending: Array<Transaction>): Array<Transaction> => accounts.reduce((result, account) => result.concat(pending.filter(p => p.address === account.address && p.network === account.network)), []); const findPendingTxs = (accounts: Array<Account>, pending: Array<Transaction>): Array<Transaction> => accounts.reduce((result, account) => result.concat(pending.filter(p => p.address === account.descriptor && p.network === account.network)), []);
export const save = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => { export const save = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
const devices: Array<TrezorDevice> = getState().devices.filter(d => d.features && d.remember === true); const devices: Array<TrezorDevice> = getState().devices.filter(d => d.features && d.remember === true);

View File

@ -210,7 +210,7 @@ export const observe = (prevState: State, action: Action): PayloadAction<boolean
const account = reducerUtils.getSelectedAccount(state); const account = reducerUtils.getSelectedAccount(state);
const network = reducerUtils.getSelectedNetwork(state); const network = reducerUtils.getSelectedNetwork(state);
const discovery = reducerUtils.getDiscoveryProcess(state); const discovery = reducerUtils.getDiscoveryProcess(state);
const tokens = reducerUtils.getAccountTokens(state, account); const tokens = reducerUtils.getAccountTokens(state.tokens, account);
const pending = reducerUtils.getAccountPendingTx(state.pending, account); const pending = reducerUtils.getAccountPendingTx(state.pending, account);
// prepare new state for "selectedAccount" reducer // prepare new state for "selectedAccount" reducer

View File

@ -1,6 +1,6 @@
/* @flow */ /* @flow */
import * as storageUtils from 'utils/storage'; import * as storageUtils from 'utils/storage';
import { findToken } from 'reducers/TokensReducer'; import { findToken } from 'reducers/utils';
import type { State as EthereumSendFormState } from 'reducers/SendFormEthereumReducer'; import type { State as EthereumSendFormState } from 'reducers/SendFormEthereumReducer';
import type { State as RippleSendFormState } from 'reducers/SendFormRippleReducer'; import type { State as RippleSendFormState } from 'reducers/SendFormRippleReducer';
@ -44,7 +44,7 @@ export const loadEthereumDraftTransaction = (): PayloadAction<?EthereumSendFormS
if (state.currency !== state.networkSymbol) { if (state.currency !== state.networkSymbol) {
const { account, tokens } = getState().selectedAccount; const { account, tokens } = getState().selectedAccount;
if (!account) return null; if (!account) return null;
const token = findToken(tokens, account.address, state.currency, account.deviceState); const token = findToken(tokens, account.descriptor, state.currency, account.deviceState);
if (!token) { if (!token) {
storageUtils.remove(TYPE, key); storageUtils.remove(TYPE, key);
return null; return null;

View File

@ -76,7 +76,7 @@ export const add = (token: NetworkToken, account: Account): AsyncAction => async
name: token.name, name: token.name,
symbol: token.symbol, symbol: token.symbol,
address: token.address, address: token.address,
ethAddress: account.address, ethAddress: account.descriptor,
decimals: token.decimals, decimals: token.decimals,
balance: '0', balance: '0',
}; };
@ -87,7 +87,7 @@ export const add = (token: NetworkToken, account: Account): AsyncAction => async
}); });
const tokenBalance = await dispatch(BlockchainActions.getTokenBalance(tkn)); const tokenBalance = await dispatch(BlockchainActions.getTokenBalance(tkn));
dispatch(setBalance(token.address, account.address, tokenBalance)); dispatch(setBalance(token.address, account.descriptor, tokenBalance));
}; };
export const remove = (token: Token): Action => ({ export const remove = (token: Token): Action => ({

View File

@ -193,9 +193,10 @@ export const getTxInput = (): PromiseAction<void> => async (dispatch: Dispatch):
export const updateAccount = (account: Account, newAccount: EthereumAccount, network: string): PromiseAction<void> => async (dispatch: Dispatch): Promise<void> => { export const updateAccount = (account: Account, newAccount: EthereumAccount, network: string): PromiseAction<void> => async (dispatch: Dispatch): Promise<void> => {
const instance: Web3Instance = await dispatch(initWeb3(network)); const instance: Web3Instance = await dispatch(initWeb3(network));
const balance = await instance.web3.eth.getBalance(account.address); const balance = await instance.web3.eth.getBalance(account.descriptor);
const nonce = await instance.web3.eth.getTransactionCount(account.address); const nonce = await instance.web3.eth.getTransactionCount(account.descriptor);
dispatch(AccountsActions.update({ dispatch(AccountsActions.update({
networkType: 'ethereum',
...account, ...account,
...newAccount, ...newAccount,
nonce, nonce,
@ -208,7 +209,7 @@ export const updateAccount = (account: Account, newAccount: EthereumAccount, net
}; };
export const updateAccountTokens = (account: Account): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => { export const updateAccountTokens = (account: Account): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const tokens = getState().tokens.filter(t => t.network === account.network && t.ethAddress === account.address); const tokens = getState().tokens.filter(t => t.network === account.network && t.ethAddress === account.descriptor);
tokens.forEach(async (token) => { tokens.forEach(async (token) => {
const balance = await dispatch(getTokenBalance(token)); const balance = await dispatch(getTokenBalance(token));
// const newBalance: string = balance.dividedBy(Math.pow(10, token.decimals)).toString(10); // const newBalance: string = balance.dividedBy(Math.pow(10, token.decimals)).toString(10);

View File

@ -85,7 +85,7 @@ export const estimateGasLimit = (network: string, data: string, value: string, g
}; };
export const subscribe = (network: string): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => { export const subscribe = (network: string): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const accounts: Array<string> = getState().accounts.filter(a => a.network === network).map(a => a.address); // eslint-disable-line no-unused-vars const accounts: Array<string> = getState().accounts.filter(a => a.network === network).map(a => a.descriptor); // eslint-disable-line no-unused-vars
const response = await TrezorConnect.blockchainSubscribe({ const response = await TrezorConnect.blockchainSubscribe({
accounts, accounts,
coin: network, coin: network,
@ -129,7 +129,7 @@ export const onBlockMined = (network: string): PromiseAction<void> => async (dis
export const onNotification = (payload: $ElementType<BlockchainNotification, 'payload'>): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => { export const onNotification = (payload: $ElementType<BlockchainNotification, 'payload'>): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const { notification } = payload; const { notification } = payload;
const account = getState().accounts.find(a => a.address === notification.address); const account = getState().accounts.find(a => a.descriptor === notification.address);
if (!account) return; if (!account) return;
if (notification.status === 'pending') { if (notification.status === 'pending') {

View File

@ -8,13 +8,13 @@ import * as BlockchainActions from 'actions/ethereum/BlockchainActions';
import type { import type {
PromiseAction, PromiseAction,
Dispatch, Dispatch,
GetState,
TrezorDevice, TrezorDevice,
Network, Network,
Account, Account,
} from 'flowtype'; } from 'flowtype';
import type { Discovery } from 'reducers/DiscoveryReducer'; import type { Discovery } from 'reducers/DiscoveryReducer';
export type DiscoveryStartAction = { export type DiscoveryStartAction = {
type: typeof DISCOVERY.START, type: typeof DISCOVERY.START,
networkType: 'ethereum', networkType: 'ethereum',
@ -61,33 +61,38 @@ export const begin = (device: TrezorDevice, network: Network): PromiseAction<Dis
}; };
}; };
export const discoverAccount = (device: TrezorDevice, discoveryProcess: Discovery): PromiseAction<Account> => async (dispatch: Dispatch): Promise<Account> => { export const discoverAccount = (device: TrezorDevice, discoveryProcess: Discovery): PromiseAction<Account> => async (dispatch: Dispatch, getState: GetState): Promise<Account> => {
const { config } = getState().localStorage;
const network = config.networks.find(c => c.shortcut === discoveryProcess.network);
if (!network) throw new Error('Discovery network not found');
const derivedKey = discoveryProcess.hdKey.derive(`m/${discoveryProcess.accountIndex}`); const derivedKey = discoveryProcess.hdKey.derive(`m/${discoveryProcess.accountIndex}`);
const path = discoveryProcess.basePath.concat(discoveryProcess.accountIndex); const path = discoveryProcess.basePath.concat(discoveryProcess.accountIndex);
const publicAddress: string = EthereumjsUtil.publicToAddress(derivedKey.publicKey, true).toString('hex'); const publicAddress: string = EthereumjsUtil.publicToAddress(derivedKey.publicKey, true).toString('hex');
const ethAddress: string = EthereumjsUtil.toChecksumAddress(publicAddress); const ethAddress: string = EthereumjsUtil.toChecksumAddress(publicAddress);
const { network } = discoveryProcess;
// TODO: check if address was created before // TODO: check if address was created before
const account = await dispatch(BlockchainActions.discoverAccount(device, ethAddress, network)); const account = await dispatch(BlockchainActions.discoverAccount(device, ethAddress, network.shortcut));
// const accountIsEmpty = account.transactions <= 0 && account.nonce <= 0 && account.balance === '0'; // const accountIsEmpty = account.transactions <= 0 && account.nonce <= 0 && account.balance === '0';
const empty = account.nonce <= 0 && account.balance === '0'; const empty = account.nonce <= 0 && account.balance === '0';
return { return {
imported: false,
index: discoveryProcess.accountIndex, index: discoveryProcess.accountIndex,
loaded: true, network: network.shortcut,
network,
deviceID: device.features ? device.features.device_id : '0', deviceID: device.features ? device.features.device_id : '0',
deviceState: device.state || '0', deviceState: device.state || '0',
addressPath: path, accountPath: path,
address: ethAddress, descriptor: ethAddress,
balance: account.balance, balance: account.balance,
availableBalance: account.balance, availableBalance: account.balance,
sequence: account.nonce,
nonce: account.nonce,
block: account.block, block: account.block,
transactions: account.transactions, transactions: account.transactions,
empty, empty,
networkType: 'ethereum',
nonce: account.nonce,
}; };
}; };

View File

@ -8,7 +8,6 @@ import * as NOTIFICATION from 'actions/constants/notification';
import * as SEND from 'actions/constants/send'; import * as SEND from 'actions/constants/send';
import * as WEB3 from 'actions/constants/web3'; import * as WEB3 from 'actions/constants/web3';
import { initialState } from 'reducers/SendFormEthereumReducer'; import { initialState } from 'reducers/SendFormEthereumReducer';
import { findToken } from 'reducers/TokensReducer';
import * as reducerUtils from 'reducers/utils'; import * as reducerUtils from 'reducers/utils';
import * as ethUtils from 'utils/ethUtils'; import * as ethUtils from 'utils/ethUtils';
@ -76,7 +75,7 @@ export const observe = (prevState: ReducersState, action: Action): ThunkAction =
// make sure that this token is added into account // make sure that this token is added into account
const { account, tokens } = getState().selectedAccount; const { account, tokens } = getState().selectedAccount;
if (!account) return; if (!account) return;
const token = findToken(tokens, account.address, currentState.sendFormEthereum.currency, account.deviceState); const token = reducerUtils.findToken(tokens, account.descriptor, currentState.sendFormEthereum.currency, account.deviceState);
if (!token) { if (!token) {
// token not found, re-init form // token not found, re-init form
dispatch(init()); dispatch(init());
@ -456,7 +455,7 @@ export const onSend = (): AsyncAction => async (dispatch: Dispatch, getState: Ge
pending, pending,
} = getState().selectedAccount; } = getState().selectedAccount;
if (!account || !network) return; if (!account || account.networkType !== 'ethereum' || !network) return;
const currentState: State = getState().sendFormEthereum; const currentState: State = getState().sendFormEthereum;
@ -466,8 +465,8 @@ export const onSend = (): AsyncAction => async (dispatch: Dispatch, getState: Ge
const txData = await dispatch(prepareEthereumTx({ const txData = await dispatch(prepareEthereumTx({
network: network.shortcut, network: network.shortcut,
token: isToken ? findToken(getState().tokens, account.address, currentState.currency, account.deviceState) : null, token: isToken ? reducerUtils.findToken(getState().tokens, account.descriptor, currentState.currency, account.deviceState) : null,
from: account.address, from: account.descriptor,
to: currentState.address, to: currentState.address,
amount: currentState.amount, amount: currentState.amount,
data: currentState.data, data: currentState.data,
@ -487,7 +486,7 @@ export const onSend = (): AsyncAction => async (dispatch: Dispatch, getState: Ge
}, },
// useEmptyPassphrase: !selected.instance, // useEmptyPassphrase: !selected.instance,
useEmptyPassphrase: selected.useEmptyPassphrase, useEmptyPassphrase: selected.useEmptyPassphrase,
path: account.addressPath, path: account.accountPath,
transaction: txData, transaction: txData,
}); });
@ -533,10 +532,10 @@ export const onSend = (): AsyncAction => async (dispatch: Dispatch, getState: Ge
type: 'send', type: 'send',
status: 'pending', status: 'pending',
confirmations: 0, confirmations: 0,
address: account.address, address: account.descriptor,
inputs: [ inputs: [
{ {
addresses: [account.address], addresses: [account.descriptor],
amount: currentState.amount, amount: currentState.amount,
fee, fee,
total: currentState.total, total: currentState.total,

View File

@ -3,8 +3,7 @@
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
import EthereumjsUtil from 'ethereumjs-util'; import EthereumjsUtil from 'ethereumjs-util';
import EthereumjsUnits from 'ethereumjs-units'; import EthereumjsUnits from 'ethereumjs-units';
import { findToken } from 'reducers/TokensReducer'; import { findDevice, getPendingAmount, findToken } from 'reducers/utils';
import { findDevice, getPendingAmount } from 'reducers/utils';
import * as SEND from 'actions/constants/send'; import * as SEND from 'actions/constants/send';
import * as ethUtils from 'utils/ethUtils'; import * as ethUtils from 'utils/ethUtils';
@ -114,7 +113,7 @@ export const recalculateTotalAmount = ($state: State): PayloadAction<State> => (
if (state.setMax) { if (state.setMax) {
const pendingAmount = getPendingAmount(pending, state.currency, isToken); const pendingAmount = getPendingAmount(pending, state.currency, isToken);
if (isToken) { if (isToken) {
const token = findToken(tokens, account.address, state.currency, account.deviceState); const token = findToken(tokens, account.descriptor, state.currency, account.deviceState);
if (token) { if (token) {
state.amount = new BigNumber(token.balance).minus(pendingAmount).toString(10); state.amount = new BigNumber(token.balance).minus(pendingAmount).toString(10);
} }
@ -173,7 +172,7 @@ export const addressLabel = ($state: State): PayloadAction<State> => (dispatch:
if (!account || !network) return state; if (!account || !network) return state;
const { address } = state; const { address } = state;
const savedAccounts = getState().accounts.filter(a => a.address.toLowerCase() === address.toLowerCase()); const savedAccounts = getState().accounts.filter(a => a.descriptor.toLowerCase() === address.toLowerCase());
if (savedAccounts.length > 0) { if (savedAccounts.length > 0) {
// check if found account belongs to this network // check if found account belongs to this network
const currentNetworkAccount = savedAccounts.find(a => a.network === network.shortcut); const currentNetworkAccount = savedAccounts.find(a => a.network === network.shortcut);
@ -221,7 +220,7 @@ export const amountValidation = ($state: State): PayloadAction<State> => (dispat
const pendingAmount: BigNumber = getPendingAmount(pending, state.currency, isToken); const pendingAmount: BigNumber = getPendingAmount(pending, state.currency, isToken);
if (isToken) { if (isToken) {
const token = findToken(tokens, account.address, state.currency, account.deviceState); const token = findToken(tokens, account.descriptor, state.currency, account.deviceState);
if (!token) return state; if (!token) return state;
const decimalRegExp = dynamicRegexp(parseInt(token.decimals, 0)); const decimalRegExp = dynamicRegexp(parseInt(token.decimals, 0));
@ -304,7 +303,7 @@ export const nonceValidation = ($state: State): PayloadAction<State> => (dispatc
const { const {
account, account,
} = getState().selectedAccount; } = getState().selectedAccount;
if (!account) return state; if (!account || account.networkType !== 'ethereum') return state;
const { nonce } = state; const { nonce } = state;
if (nonce.length < 1) { if (nonce.length < 1) {

View File

@ -16,7 +16,7 @@ import type {
const DECIMALS: number = 6; const DECIMALS: number = 6;
export const subscribe = (network: string): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => { export const subscribe = (network: string): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const accounts: Array<string> = getState().accounts.filter(a => a.network === network).map(a => a.address); const accounts: Array<string> = getState().accounts.filter(a => a.network === network).map(a => a.descriptor);
await TrezorConnect.blockchainSubscribe({ await TrezorConnect.blockchainSubscribe({
accounts, accounts,
coin: network, coin: network,
@ -68,7 +68,7 @@ export const onBlockMined = (network: string): PromiseAction<void> => async (dis
export const onNotification = (payload: $ElementType<BlockchainNotification, 'payload'>): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => { export const onNotification = (payload: $ElementType<BlockchainNotification, 'payload'>): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const { notification } = payload; const { notification } = payload;
const account = getState().accounts.find(a => a.address === notification.address); const account = getState().accounts.find(a => a.descriptor === notification.address);
if (!account) return; if (!account) return;
if (notification.status === 'pending') { if (notification.status === 'pending') {
@ -95,7 +95,7 @@ export const onNotification = (payload: $ElementType<BlockchainNotification, 'pa
const updatedAccount = await TrezorConnect.rippleGetAccountInfo({ const updatedAccount = await TrezorConnect.rippleGetAccountInfo({
account: { account: {
address: account.address, address: account.descriptor,
from: account.block, from: account.block,
history: false, history: false,
}, },
@ -104,10 +104,12 @@ export const onNotification = (payload: $ElementType<BlockchainNotification, 'pa
if (!updatedAccount.success) return; if (!updatedAccount.success) return;
dispatch(AccountsActions.update({ dispatch(AccountsActions.update({
networkType: 'ripple',
...account, ...account,
balance: toDecimalAmount(updatedAccount.payload.balance, DECIMALS), balance: toDecimalAmount(updatedAccount.payload.balance, DECIMALS),
availableBalance: toDecimalAmount(updatedAccount.payload.availableBalance, DECIMALS), availableBalance: toDecimalAmount(updatedAccount.payload.availableBalance, DECIMALS),
block: updatedAccount.payload.block, block: updatedAccount.payload.block,
sequence: updatedAccount.payload.sequence, sequence: updatedAccount.payload.sequence,
reserve: '0',
})); }));
}; };

View File

@ -60,19 +60,22 @@ export const discoverAccount = (device: TrezorDevice, discoveryProcess: Discover
const empty = account.sequence <= 0 && account.balance === '0'; const empty = account.sequence <= 0 && account.balance === '0';
return { return {
imported: false,
index: discoveryProcess.accountIndex, index: discoveryProcess.accountIndex,
loaded: true,
network: network.shortcut, network: network.shortcut,
deviceID: device.features ? device.features.device_id : '0', deviceID: device.features ? device.features.device_id : '0',
deviceState: device.state || '0', deviceState: device.state || '0',
addressPath: account.path || [], accountPath: account.path || [],
address: account.address, descriptor: account.address,
balance: toDecimalAmount(account.balance, 6),
availableBalance: toDecimalAmount(account.availableBalance, 6), balance: toDecimalAmount(account.balance, network.decimals),
sequence: account.sequence, availableBalance: toDecimalAmount(account.availableBalance, network.decimals),
nonce: account.sequence,
block: account.block, block: account.block,
transactions: account.transactions, transactions: account.transactions,
empty, empty,
networkType: 'ripple',
sequence: account.sequence,
reserve: '0',
}; };
}; };

View File

@ -173,7 +173,7 @@ export const onSend = (): AsyncAction => async (dispatch: Dispatch, getState: Ge
const selected: ?TrezorDevice = getState().wallet.selectedDevice; const selected: ?TrezorDevice = getState().wallet.selectedDevice;
if (!selected) return; if (!selected) return;
if (!account || !network) return; if (!account || account.networkType !== 'ripple' || !network) return;
const blockchain = getState().blockchain.find(b => b.shortcut === account.network); const blockchain = getState().blockchain.find(b => b.shortcut === account.network);
if (!blockchain) return; if (!blockchain) return;
@ -188,7 +188,7 @@ export const onSend = (): AsyncAction => async (dispatch: Dispatch, getState: Ge
state: selected.state, state: selected.state,
}, },
useEmptyPassphrase: selected.useEmptyPassphrase, useEmptyPassphrase: selected.useEmptyPassphrase,
path: account.addressPath, path: account.accountPath,
transaction: { transaction: {
fee: blockchain.fee, // Fee must be in the range of 10 to 10,000 drops fee: blockchain.fee, // Fee must be in the range of 10 to 10,000 drops
flags: 0x80000000, flags: 0x80000000,

View File

@ -115,7 +115,7 @@ const addressValidation = ($state: State): PayloadAction<State> => (dispatch: Di
state.errors.address = 'Address is not set'; state.errors.address = 'Address is not set';
} else if (!AddressValidator.validate(address, 'XRP')) { } else if (!AddressValidator.validate(address, 'XRP')) {
state.errors.address = 'Address is not valid'; state.errors.address = 'Address is not valid';
} else if (address.toLowerCase() === account.address.toLowerCase()) { } else if (address.toLowerCase() === account.descriptor.toLowerCase()) {
state.errors.address = 'Cannot send to myself'; state.errors.address = 'Cannot send to myself';
} }
return state; return state;
@ -135,7 +135,7 @@ const addressLabel = ($state: State): PayloadAction<State> => (dispatch: Dispatc
if (!account || !network) return state; if (!account || !network) return state;
const { address } = state; const { address } = state;
const savedAccounts = getState().accounts.filter(a => a.address.toLowerCase() === address.toLowerCase()); const savedAccounts = getState().accounts.filter(a => a.descriptor.toLowerCase() === address.toLowerCase());
if (savedAccounts.length > 0) { if (savedAccounts.length > 0) {
// check if found account belongs to this network // check if found account belongs to this network
const currentNetworkAccount = savedAccounts.find(a => a.network === network.shortcut); const currentNetworkAccount = savedAccounts.find(a => a.network === network.shortcut);

View File

@ -45,7 +45,7 @@ const ConfirmAddress = (props: Props) => {
<P>Please compare your address on device with address shown bellow.</P> <P>Please compare your address on device with address shown bellow.</P>
</Header> </Header>
<Content> <Content>
<P>{ account.address }</P> <P>{ account.descriptor }</P>
<Label>{ network.symbol } account #{ (account.index + 1) }</Label> <Label>{ network.symbol } account #{ (account.index + 1) }</Label>
</Content> </Content>
</Wrapper> </Wrapper>

View File

@ -71,7 +71,7 @@ class ConfirmUnverifiedAddress extends PureComponent<Props> {
const { account, onCancel, showAddress } = this.props; const { account, onCancel, showAddress } = this.props;
if (!account) return; if (!account) return;
onCancel(); onCancel();
showAddress(account.addressPath); showAddress(account.accountPath);
} }
showUnverifiedAddress() { showUnverifiedAddress() {

View File

@ -6,27 +6,35 @@ import * as WALLET from 'actions/constants/wallet';
import * as ACCOUNT from 'actions/constants/account'; import * as ACCOUNT from 'actions/constants/account';
import type { Action, TrezorDevice } from 'flowtype'; import type { Action, TrezorDevice } from 'flowtype';
import type {
AccountSetBalanceAction,
AccountSetNonceAction,
} from 'actions/AccountsActions';
export type Account = { type AccountCommon = {
loaded: boolean; +imported: boolean,
+network: string; +index: number,
+deviceID: string; +network: string, // network id (shortcut)
+deviceState: string; +deviceID: string, // empty for imported accounts
+index: number; +deviceState: string, // empty for imported accounts
+addressPath: Array<number>; +accountPath: Array<number>, // empty for imported accounts
+address: string; +descriptor: string, // address or xpub
balance: string;
availableBalance: string; balance: string,
sequence: number; availableBalance: string, // balance - pending
nonce: number; block: number, // last known (synchronized) block
block: number; empty: boolean, // account without transactions
transactions: number;
empty: boolean; transactions: number, // deprecated
} };
export type Account = AccountCommon & {
networkType: 'ethereum',
nonce: number,
} | AccountCommon & {
networkType: 'ripple',
sequence: number,
reserve: string,
} | AccountCommon & {
networkType: 'bitcoin',
addressIndex: number,
};
export type State = Array<Account>; export type State = Array<Account>;
@ -44,7 +52,7 @@ export const findDeviceAccounts = (state: State, device: TrezorDevice, network:
const createAccount = (state: State, account: Account): State => { const createAccount = (state: State, account: Account): State => {
// TODO check with device_id // TODO check with device_id
// check if account was created before // check if account was created before
const exist: ?Account = state.find(a => a.address === account.address && a.network === account.network && a.deviceState === account.deviceState); const exist: ?Account = state.find(a => a.descriptor === account.descriptor && a.network === account.network && a.deviceState === account.deviceState);
if (exist) { if (exist) {
return state; return state;
} }
@ -55,7 +63,6 @@ const createAccount = (state: State, account: Account): State => {
const removeAccounts = (state: State, device: TrezorDevice): State => state.filter(account => account.deviceState !== device.state); const removeAccounts = (state: State, device: TrezorDevice): State => state.filter(account => account.deviceState !== device.state);
const clear = (state: State, devices: Array<TrezorDevice>): State => { const clear = (state: State, devices: Array<TrezorDevice>): State => {
let newState: State = [...state]; let newState: State = [...state];
devices.forEach((d) => { devices.forEach((d) => {
@ -65,29 +72,12 @@ const clear = (state: State, devices: Array<TrezorDevice>): State => {
}; };
const updateAccount = (state: State, account: Account): State => { const updateAccount = (state: State, account: Account): State => {
const index: number = state.findIndex(a => a.address === account.address && a.network === account.network && a.deviceState === account.deviceState); const index: number = state.findIndex(a => a.descriptor === account.descriptor && a.network === account.network && a.deviceState === account.deviceState);
const newState: State = [...state]; const newState: State = [...state];
newState[index] = account; newState[index] = account;
return newState; return newState;
}; };
const setBalance = (state: State, action: AccountSetBalanceAction): State => {
// const index: number = state.findIndex(account => account.address === action.address && account.network === action.network && account.deviceState === action.deviceState);
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network);
const newState: State = [...state];
newState[index].loaded = true;
newState[index].balance = action.balance;
return newState;
};
const setNonce = (state: State, action: AccountSetNonceAction): State => {
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network && account.deviceState === action.deviceState);
const newState: State = [...state];
newState[index].loaded = true;
newState[index].nonce = action.nonce;
return newState;
};
export default (state: State = initialState, action: Action): State => { export default (state: State = initialState, action: Action): State => {
switch (action.type) { switch (action.type) {
case ACCOUNT.CREATE: case ACCOUNT.CREATE:
@ -108,11 +98,6 @@ export default (state: State = initialState, action: Action): State => {
case ACCOUNT.UPDATE: case ACCOUNT.UPDATE:
return updateAccount(state, action.payload); return updateAccount(state, action.payload);
case ACCOUNT.SET_BALANCE:
return setBalance(state, action);
case ACCOUNT.SET_NONCE:
return setNonce(state, action);
case ACCOUNT.FROM_STORAGE: case ACCOUNT.FROM_STORAGE:
return action.payload; return action.payload;

View File

@ -6,7 +6,6 @@ import * as WALLET from 'actions/constants/wallet';
import * as TOKEN from 'actions/constants/token'; import * as TOKEN from 'actions/constants/token';
import type { Action, TrezorDevice } from 'flowtype'; import type { Action, TrezorDevice } from 'flowtype';
import type { Account } from './AccountsReducer';
export type Token = { export type Token = {
loaded: boolean; loaded: boolean;
@ -24,21 +23,6 @@ export type State = Array<Token>;
const initialState: State = []; const initialState: State = [];
// Helper for actions
export const findToken = (state: Array<Token>, address: string, symbol: string, deviceState: string): ?Token => state.find(t => t.ethAddress === address && t.symbol === symbol && t.deviceState === deviceState);
export const findAccountTokens = (state: Array<Token>, account: Account): Array<Token> => state.filter(t => t.ethAddress === account.address && t.network === account.network && t.deviceState === account.deviceState);
// const setBalance = (state: State, payload: any): State => {
// const newState: Array<Token> = [ ...state ];
// let index: number = state.findIndex(t => t.address === payload.address && t.ethAddress === payload.ethAddress);
// if (index >= 0) {
// newState[index].loaded = true;
// newState[index].balance = payload.balance;
// }
// return newState;
// }
const create = (state: State, token: Token): State => { const create = (state: State, token: Token): State => {
const newState: State = [...state]; const newState: State = [...state];
newState.push(token); newState.push(token);

View File

@ -86,7 +86,7 @@ export const getDiscoveryProcess = (state: State): ?Discovery => {
export const getAccountPendingTx = (pending: Array<Transaction>, account: ?Account): Array<Transaction> => { export const getAccountPendingTx = (pending: Array<Transaction>, account: ?Account): Array<Transaction> => {
const a = account; const a = account;
if (!a) return []; if (!a) return [];
return pending.filter(p => p.network === a.network && p.address === a.address); return pending.filter(p => p.network === a.network && p.address === a.descriptor);
}; };
export const getPendingSequence = (pending: Array<Transaction>): number => pending.reduce((value: number, tx: Transaction) => { export const getPendingSequence = (pending: Array<Transaction>): number => pending.reduce((value: number, tx: Transaction) => {
@ -101,10 +101,12 @@ export const getPendingAmount = (pending: Array<Transaction>, currency: string,
return value; return value;
}, new BigNumber('0')); }, new BigNumber('0'));
export const getAccountTokens = (state: State, account: ?Account): Array<Token> => { export const findToken = (state: Array<Token>, address: string, symbol: string, deviceState: string): ?Token => state.find(t => t.ethAddress === address && t.symbol === symbol && t.deviceState === deviceState);
export const getAccountTokens = (tokens: Array<Token>, account: ?Account): Array<Token> => {
const a = account; const a = account;
if (!a) return []; if (!a) return [];
return state.tokens.filter(t => t.ethAddress === a.address && t.network === a.network && t.deviceState === a.deviceState); return tokens.filter(t => t.ethAddress === a.descriptor && t.network === a.network && t.deviceState === a.deviceState);
}; };
export const getWeb3 = (state: State): ?Web3Instance => { export const getWeb3 = (state: State): ?Web3Instance => {

View File

@ -37,8 +37,7 @@ const LocalStorageService: Middleware = (api: MiddlewareAPI) => (next: Middlewar
break; break;
case ACCOUNT.CREATE: case ACCOUNT.CREATE:
case ACCOUNT.SET_BALANCE: case ACCOUNT.UPDATE:
case ACCOUNT.SET_NONCE:
api.dispatch(LocalStorageActions.save()); api.dispatch(LocalStorageActions.save());
break; break;

View File

@ -161,10 +161,8 @@ const AccountMenu = (props: Props) => {
const discovery = props.discovery.find(d => d.deviceState === selected.state && d.network === location.state.network); const discovery = props.discovery.find(d => d.deviceState === selected.state && d.network === location.state.network);
if (discovery && discovery.completed) { if (discovery && discovery.completed) {
// TODO: add only if last one is not empty
//if (selectedAccounts.length > 0 && selectedAccounts[selectedAccounts.length - 1])
const lastAccount = deviceAccounts[deviceAccounts.length - 1]; const lastAccount = deviceAccounts[deviceAccounts.length - 1];
if (lastAccount && (new BigNumber(lastAccount.balance).greaterThan(0) || lastAccount.nonce > 0)) { if (lastAccount && !lastAccount.empty) {
discoveryStatus = ( discoveryStatus = (
<Row onClick={props.addAccount}> <Row onClick={props.addAccount}>
<RowAddAccountWrapper> <RowAddAccountWrapper>

View File

@ -111,9 +111,9 @@ const AccountReceive = (props: Props) => {
const isAddressVerifying = props.modal.context === CONTEXT_DEVICE && props.modal.windowType === 'ButtonRequest_Address'; const isAddressVerifying = props.modal.context === CONTEXT_DEVICE && props.modal.windowType === 'ButtonRequest_Address';
const isAddressHidden = !isAddressVerifying && !addressVerified && !addressUnverified; const isAddressHidden = !isAddressVerifying && !addressVerified && !addressUnverified;
let address = `${account.address.substring(0, 20)}...`; let address = `${account.descriptor.substring(0, 20)}...`;
if (addressVerified || addressUnverified || isAddressVerifying) { if (addressVerified || addressUnverified || isAddressVerifying) {
({ address } = account); address = account.descriptor;
} }
return ( return (
@ -149,7 +149,7 @@ const AccountReceive = (props: Props) => {
/> />
)} )}
> >
<EyeButton onClick={() => props.showAddress(account.addressPath)}> <EyeButton onClick={() => props.showAddress(account.accountPath)}>
<Icon <Icon
icon={addressUnverified ? ICONS.EYE_CROSSED : ICONS.EYE} icon={addressUnverified ? ICONS.EYE_CROSSED : ICONS.EYE}
color={addressUnverified ? colors.ERROR_PRIMARY : colors.TEXT_PRIMARY} color={addressUnverified ? colors.ERROR_PRIMARY : colors.TEXT_PRIMARY}
@ -159,7 +159,7 @@ const AccountReceive = (props: Props) => {
)} )}
/> />
{!(addressVerified || addressUnverified) && ( {!(addressVerified || addressUnverified) && (
<ShowAddressButton onClick={() => props.showAddress(account.addressPath)} isDisabled={device.connected && !discovery.completed}> <ShowAddressButton onClick={() => props.showAddress(account.accountPath)} isDisabled={device.connected && !discovery.completed}>
<ShowAddressIcon icon={ICONS.EYE} color={colors.WHITE} />Show full address <ShowAddressIcon icon={ICONS.EYE} color={colors.WHITE} />Show full address
</ShowAddressButton> </ShowAddressButton>
)} )}
@ -172,7 +172,7 @@ const AccountReceive = (props: Props) => {
fgColor="#000000" fgColor="#000000"
level="Q" level="Q"
style={{ width: 150 }} style={{ width: 150 }}
value={account.address} value={account.descriptor}
/> />
</QrWrapper> </QrWrapper>
)} )}

View File

@ -111,9 +111,9 @@ const AccountReceive = (props: Props) => {
const isAddressVerifying = props.modal.context === CONTEXT_DEVICE && props.modal.windowType === 'ButtonRequest_Address'; const isAddressVerifying = props.modal.context === CONTEXT_DEVICE && props.modal.windowType === 'ButtonRequest_Address';
const isAddressHidden = !isAddressVerifying && !addressVerified && !addressUnverified; const isAddressHidden = !isAddressVerifying && !addressVerified && !addressUnverified;
let address = `${account.address.substring(0, 20)}...`; let address = `${account.descriptor.substring(0, 20)}...`;
if (addressVerified || addressUnverified || isAddressVerifying) { if (addressVerified || addressUnverified || isAddressVerifying) {
({ address } = account); address = account.descriptor;
} }
return ( return (
@ -149,7 +149,7 @@ const AccountReceive = (props: Props) => {
/> />
)} )}
> >
<EyeButton onClick={() => props.showAddress(account.addressPath)}> <EyeButton onClick={() => props.showAddress(account.accountPath)}>
<Icon <Icon
icon={addressUnverified ? ICONS.EYE_CROSSED : ICONS.EYE} icon={addressUnverified ? ICONS.EYE_CROSSED : ICONS.EYE}
color={addressUnverified ? colors.ERROR_PRIMARY : colors.TEXT_PRIMARY} color={addressUnverified ? colors.ERROR_PRIMARY : colors.TEXT_PRIMARY}
@ -159,7 +159,7 @@ const AccountReceive = (props: Props) => {
)} )}
/> />
{!(addressVerified || addressUnverified) && ( {!(addressVerified || addressUnverified) && (
<ShowAddressButton onClick={() => props.showAddress(account.addressPath)} isDisabled={device.connected && !discovery.completed}> <ShowAddressButton onClick={() => props.showAddress(account.accountPath)} isDisabled={device.connected && !discovery.completed}>
<ShowAddressIcon icon={ICONS.EYE} color={colors.WHITE} />Show full address <ShowAddressIcon icon={ICONS.EYE} color={colors.WHITE} />Show full address
</ShowAddressButton> </ShowAddressButton>
)} )}
@ -172,7 +172,7 @@ const AccountReceive = (props: Props) => {
fgColor="#000000" fgColor="#000000"
level="Q" level="Q"
style={{ width: 150 }} style={{ width: 150 }}
value={account.address} value={account.descriptor}
/> />
</QrWrapper> </QrWrapper>
)} )}

View File

@ -86,7 +86,7 @@ class SignVerify extends Component <Props> {
<Input <Input
topLabel="Address" topLabel="Address"
name="signAddress" name="signAddress"
value={account.address} value={account.descriptor}
type="text" type="text"
autoSelect autoSelect
readOnly readOnly
@ -122,7 +122,7 @@ class SignVerify extends Component <Props> {
>Clear >Clear
</Button> </Button>
<StyledButton <StyledButton
onClick={() => signVerifyActions.sign(account.addressPath, signMessage)} onClick={() => signVerifyActions.sign(account.accountPath, signMessage)}
>Sign >Sign
</StyledButton> </StyledButton>
</RowButtons> </RowButtons>

View File

@ -84,7 +84,7 @@ const AccountSummary = (props: Props) => {
return <Content loader={loader} exceptionPage={exceptionPage} isLoading />; return <Content loader={loader} exceptionPage={exceptionPage} isLoading />;
} }
const explorerLink: string = `${network.explorer.address}${account.address}`; const explorerLink: string = `${network.explorer.address}${account.descriptor}`;
const pendingAmount: BigNumber = stateUtils.getPendingAmount(pending, network.symbol); const pendingAmount: BigNumber = stateUtils.getPendingAmount(pending, network.symbol);
const balance: string = new BigNumber(account.balance).minus(pendingAmount).toString(10); const balance: string = new BigNumber(account.balance).minus(pendingAmount).toString(10);

View File

@ -74,7 +74,7 @@ const AccountSummary = (props: Props) => {
return <Content loader={loader} exceptionPage={exceptionPage} isLoading />; return <Content loader={loader} exceptionPage={exceptionPage} isLoading />;
} }
const explorerLink: string = `${network.explorer.address}${account.address}`; const explorerLink: string = `${network.explorer.address}${account.descriptor}`;
const pendingAmount: BigNumber = stateUtils.getPendingAmount(pending, network.symbol); const pendingAmount: BigNumber = stateUtils.getPendingAmount(pending, network.symbol);
const balance: string = new BigNumber(account.balance).minus(pendingAmount).toString(10); const balance: string = new BigNumber(account.balance).minus(pendingAmount).toString(10);