1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-24 09:18:09 +00:00

refactoring Address to Account

This commit is contained in:
Szymon Lesisz 2018-05-16 18:30:46 +02:00
parent b90ba80ec4
commit a5b6e20525
15 changed files with 54 additions and 48 deletions

View File

@ -32,7 +32,7 @@ export const init = (): ThunkAction => {
if (!coin) return; if (!coin) return;
const state: State = { const state: State = {
index: parseInt(urlParams.address), index: parseInt(urlParams.account),
deviceState: selected.state || '0', deviceState: selected.state || '0',
deviceId: selected.features ? selected.features.device_id : '0', deviceId: selected.features ? selected.features.device_id : '0',
deviceInstance: selected.instance, deviceInstance: selected.instance,

View File

@ -115,7 +115,7 @@ export const start = (device: TrezorDevice, network: string, ignoreCompleted?: b
// start from beginning // start from beginning
dispatch( begin(device, network) ); dispatch( begin(device, network) );
} else { } else {
dispatch( discoverAddress(device, discoveryProcess) ); dispatch( discoverAccount(device, discoveryProcess) );
} }
} }
} }
@ -189,7 +189,7 @@ const begin = (device: TrezorDevice, network: string): AsyncAction => {
} }
} }
const discoverAddress = (device: TrezorDevice, discoveryProcess: Discovery): AsyncAction => { const discoverAccount = (device: TrezorDevice, discoveryProcess: Discovery): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => { return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const derivedKey = discoveryProcess.hdKey.derive(`m/${discoveryProcess.accountIndex}`); const derivedKey = discoveryProcess.hdKey.derive(`m/${discoveryProcess.accountIndex}`);
@ -296,7 +296,7 @@ const discoverAddress = (device: TrezorDevice, discoveryProcess: Discovery): Asy
const addressIsEmpty = nonce < 1 && !balance.greaterThan(0); const addressIsEmpty = nonce < 1 && !balance.greaterThan(0);
if (!addressIsEmpty) { if (!addressIsEmpty) {
dispatch( discoverAddress(device, discoveryProcess) ); dispatch( discoverAccount(device, discoveryProcess) );
} else { } else {
// release acquired sesssion // release acquired sesssion
await TrezorConnect.getFeatures({ await TrezorConnect.getFeatures({

View File

@ -234,7 +234,7 @@ export const onSelectDevice = (device: TrezorDevice | Device): ThunkAction => {
const deviceId: string = device.features.device_id; const deviceId: string = device.features.device_id;
const urlParams: RouterLocationState = getState().router.location.state; const urlParams: RouterLocationState = getState().router.location.state;
// let url: string = `/device/${ device.features.device_id }/network/ethereum/address/0`; // let url: string = `/device/${ device.features.device_id }/network/ethereum/account/0`;
let url: string = `/device/${ deviceId }`; let url: string = `/device/${ deviceId }`;
let instance: ?number; let instance: ?number;
// check if device is not TrezorDevice type // check if device is not TrezorDevice type
@ -250,7 +250,7 @@ export const onSelectDevice = (device: TrezorDevice | Device): ThunkAction => {
} }
} }
// check if current location is not set to this device // check if current location is not set to this device
//dispatch( push(`/device/${ device.features.device_id }/network/etc/address/0`) ); //dispatch( push(`/device/${ device.features.device_id }/network/etc/account/0`) );
if (urlParams.deviceInstance !== instance || urlParams.device !== deviceId) { if (urlParams.deviceInstance !== instance || urlParams.device !== deviceId) {
dispatch( push(url) ); dispatch( push(url) );
@ -498,7 +498,7 @@ export const selectDuplicatedDevice = (): AsyncAction => {
} }
export function addAddress(): ThunkAction { export function addAccount(): ThunkAction {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const selected = findSelectedDevice(getState().connect); const selected = findSelectedDevice(getState().connect);
if (!selected) return; if (!selected) return;

View File

@ -71,7 +71,7 @@ export default class AbstractAccount<P> extends Component<Props & P, AccountStat
} }
if (discovery && !discovery.completed && !deviceStatusNotification) { if (discovery && !discovery.completed && !deviceStatusNotification) {
deviceStatusNotification = <Notification className="info" title="Loading accounts" />; deviceStatusNotification = <Notification className="info" title="Loading accounts..." />;
} }
this.setState({ this.setState({
@ -93,7 +93,7 @@ export default class AbstractAccount<P> extends Component<Props & P, AccountStat
const accountState = props.abstractAccount; const accountState = props.abstractAccount;
if (!accountState) { if (!accountState) {
return (<section><Notification className="info" title="Loading device" /></section>); return (<section><Notification className="info" title="Loading device..." /></section>);
} }
const { const {
@ -106,9 +106,12 @@ export default class AbstractAccount<P> extends Component<Props & P, AccountStat
return (<section>Device with state {accountState.deviceState} not found</section>); return (<section>Device with state {accountState.deviceState} not found</section>);
} }
// account not found. checking why...
if (!account) { if (!account) {
if (!discovery || discovery.waitingForDevice) { if (!discovery || discovery.waitingForDevice) {
if (device.connected) { if (device.connected) {
// case 1: device is connected but discovery not started yet (probably waiting for auth)
if (device.available) { if (device.available) {
return ( return (
<section> <section>
@ -116,6 +119,7 @@ export default class AbstractAccount<P> extends Component<Props & P, AccountStat
</section> </section>
); );
} else { } else {
// case 2: device is unavailable (created with different passphrase settings) account cannot be accessed
return ( return (
<section> <section>
<Notification <Notification
@ -127,6 +131,7 @@ export default class AbstractAccount<P> extends Component<Props & P, AccountStat
); );
} }
} else { } else {
// case 3: device is disconnected
return ( return (
<section> <section>
<Notification <Notification
@ -138,18 +143,21 @@ export default class AbstractAccount<P> extends Component<Props & P, AccountStat
); );
} }
} else if (discovery.waitingForBackend) { } else if (discovery.waitingForBackend) {
// case 4: backend is not working
return ( return (
<section> <section>
<Notification className="warning" title="Backend not working" /> <Notification className="warning" title="Backend not working" />
</section> </section>
); );
} else if (discovery.completed) { } else if (discovery.completed) {
// case 5: account not found and discovery is completed
return ( return (
<section> <section>
<Notification className="warning" title="Account does not exist" /> <Notification className="warning" title="Account does not exist" />
</section> </section>
); );
} else { } else {
// case 6: discovery is not completed yet
return ( return (
<section> <section>
<Notification className="info" title="Account is loading..." /> <Notification className="info" title="Account is loading..." />

View File

@ -79,8 +79,7 @@ class Indicator extends Component<Props, State> {
const AccountTabs = (props: any) => { const AccountTabs = (props: any) => {
const urlParams = props.match.params; const urlParams = props.match.params;
//const urlParams = props.match ? props.match.params : { address: '0' }; const basePath = `/device/${urlParams.device}/network/${urlParams.network}/account/${urlParams.account}`;
const basePath = `/device/${urlParams.device}/network/${urlParams.network}/address/${urlParams.address}`;
return ( return (
<div className="account-tabs"> <div className="account-tabs">

View File

@ -18,8 +18,7 @@ type State = {
const AccountTabs = (props: any): any => { const AccountTabs = (props: any): any => {
const urlParams = props.match.params; const urlParams = props.match.params;
//const urlParams = props.match ? props.match.params : { address: '0' }; const basePath = `/device/${urlParams.device}/network/${urlParams.network}/account/${urlParams.account}`;
const basePath = `/device/${urlParams.device}/network/${urlParams.network}/address/${urlParams.address}`;
return ( return (
<div className="account-tabs"> <div className="account-tabs">

View File

@ -29,36 +29,36 @@ const AccountSelection = (props: Props): ?React$Element<string> => {
const fiatRate = props.fiat.find(f => f.network === selectedCoin.network); const fiatRate = props.fiat.find(f => f.network === selectedCoin.network);
const deviceAddresses: Array<any> = findDeviceAccounts(accounts, selected, location.state.network); const deviceAccounts: Array<any> = findDeviceAccounts(accounts, selected, location.state.network);
let selectedAccounts = deviceAddresses.map((address, i) => { let selectedAccounts = deviceAccounts.map((account, i) => {
// const url: string = `${baseUrl}/network/${location.state.network}/address/${i}`; // const url: string = `${baseUrl}/network/${location.state.network}/account/${i}`;
const url: string = location.pathname.replace(/address+\/([0-9]*)/, `address/${i}`); const url: string = location.pathname.replace(/account+\/([0-9]*)/, `account/${i}`);
let balance: string = 'Loading...'; let balance: string = 'Loading...';
if (address.balance !== '') { if (account.balance !== '') {
if (fiatRate) { if (fiatRate) {
const accountBalance = new BigNumber(address.balance); const accountBalance = new BigNumber(account.balance);
const fiat = accountBalance.times(fiatRate.value).toFixed(2); const fiat = accountBalance.times(fiatRate.value).toFixed(2);
balance = `${ address.balance } ${ selectedCoin.symbol } / $${ fiat }`; balance = `${ account.balance } ${ selectedCoin.symbol } / $${ fiat }`;
} else { } else {
balance = `${ address.balance } ${ selectedCoin.symbol }`; balance = `${ account.balance } ${ selectedCoin.symbol }`;
} }
} }
return ( return (
<NavLink key={i} activeClassName="selected" className="account" to={ url }> <NavLink key={i} activeClassName="selected" className="account" to={ url }>
{ `Address #${(address.index + 1 )}` } { `Account #${(account.index + 1 )}` }
<span>{ address.loaded ? balance : "Loading..." }</span> <span>{ account.loaded ? balance : "Loading..." }</span>
</NavLink> </NavLink>
) )
}); });
if (selectedAccounts.length < 1) { if (selectedAccounts.length < 1) {
if (selected.connected) { if (selected.connected) {
const url: string = location.pathname.replace(/address+\/([0-9]*)/, `address/0`); const url: string = location.pathname.replace(/account+\/([0-9]*)/, `account/0`);
selectedAccounts = ( selectedAccounts = (
<NavLink activeClassName="selected" className="account" to={ url }> <NavLink activeClassName="selected" className="account" to={ url }>
Address #1 Account #1
<span>Loading...</span> <span>Loading...</span>
</NavLink> </NavLink>
) )
@ -72,17 +72,17 @@ const AccountSelection = (props: Props): ?React$Element<string> => {
if (discovery.completed) { if (discovery.completed) {
// TODO: add only if last one is not empty // TODO: add only if last one is not empty
//if (selectedAccounts.length > 0 && selectedAccounts[selectedAccounts.length - 1]) //if (selectedAccounts.length > 0 && selectedAccounts[selectedAccounts.length - 1])
const lastAccount = deviceAddresses[deviceAddresses.length - 1]; const lastAccount = deviceAccounts[deviceAccounts.length - 1];
if (lastAccount && (new BigNumber(lastAccount.balance).greaterThan(0) || lastAccount.nonce > 0)) { if (lastAccount && (new BigNumber(lastAccount.balance).greaterThan(0) || lastAccount.nonce > 0)) {
discoveryStatus = ( discoveryStatus = (
<div className="add-address" onClick={ props.addAddress }> <div className="add-account" onClick={ props.addAccount }>
Add address Add account
</div> </div>
); );
} else { } else {
const tooltip = ( const tooltip = (
<div className="aside-tooltip-wrapper"> <div className="aside-tooltip-wrapper">
To add a new address, last address must have some transactions. To add a new account, last account must have some transactions.
</div> </div>
) )
discoveryStatus = ( discoveryStatus = (
@ -90,17 +90,17 @@ const AccountSelection = (props: Props): ?React$Element<string> => {
arrowContent={<div className="rc-tooltip-arrow-inner"></div>} arrowContent={<div className="rc-tooltip-arrow-inner"></div>}
overlay={ tooltip } overlay={ tooltip }
placement="top"> placement="top">
<div className="add-address disabled"> <div className="add-account disabled">
Add address Add account
</div> </div>
</Tooltip> </Tooltip>
); );
} }
} else if (!selected.connected) { } else if (!selected.connected || !selected.available) {
discoveryStatus = ( discoveryStatus = (
<div className="discovery-status"> <div className="discovery-status">
Addresses could not be loaded Accounts could not be loaded
<span>{ `Connect ${ selected.instanceLabel } device` }</span> <span>{ `Connect ${ selected.instanceLabel } device` }</span>
</div> </div>
); );

View File

@ -21,7 +21,7 @@ const CoinSelection = (props: Props): React$Element<string> => {
} }
const walletCoins = config.coins.map(item => { const walletCoins = config.coins.map(item => {
const url = `${ baseUrl }/network/${ item.network }/address/0`; const url = `${ baseUrl }/network/${ item.network }/account/0`;
const className = `coin ${ item.network }` const className = `coin ${ item.network }`
return ( return (
<NavLink key={ item.network } to={ url } className={ className }> <NavLink key={ item.network } to={ url } className={ className }>

View File

@ -30,7 +30,7 @@ type StateProps = {
type DispatchProps = { type DispatchProps = {
toggleDeviceDropdown: typeof toggleDeviceDropdown, toggleDeviceDropdown: typeof toggleDeviceDropdown,
addAddress: typeof TrezorConnectActions.addAddress, addAccount: typeof TrezorConnectActions.addAccount,
acquireDevice: typeof TrezorConnectActions.acquire, acquireDevice: typeof TrezorConnectActions.acquire,
forgetDevice: typeof TrezorConnectActions.forget, forgetDevice: typeof TrezorConnectActions.forget,
duplicateDevice: typeof TrezorConnectActions.duplicateDevice, duplicateDevice: typeof TrezorConnectActions.duplicateDevice,
@ -56,7 +56,7 @@ const mapDispatchToProps: MapDispatchToProps<Dispatch, OwnProps, DispatchProps>
return { return {
//onAccountSelect: bindActionCreators(AccountActions.onAccountSelect, dispatch), //onAccountSelect: bindActionCreators(AccountActions.onAccountSelect, dispatch),
toggleDeviceDropdown: bindActionCreators(toggleDeviceDropdown, dispatch), toggleDeviceDropdown: bindActionCreators(toggleDeviceDropdown, dispatch),
addAddress: bindActionCreators(TrezorConnectActions.addAddress, dispatch), addAccount: bindActionCreators(TrezorConnectActions.addAccount, dispatch),
acquireDevice: bindActionCreators(TrezorConnectActions.acquire, dispatch), acquireDevice: bindActionCreators(TrezorConnectActions.acquire, dispatch),
forgetDevice: bindActionCreators(TrezorConnectActions.forget, dispatch), forgetDevice: bindActionCreators(TrezorConnectActions.forget, dispatch),
duplicateDevice: bindActionCreators(TrezorConnectActions.duplicateDevice, dispatch), duplicateDevice: bindActionCreators(TrezorConnectActions.duplicateDevice, dispatch),

View File

@ -25,7 +25,7 @@ const Content = (props: Props) => {
return ( return (
<article> <article>
<nav> <nav>
<Route path="/device/:device/network/:network/address/:address" component={ AccountTabs } /> <Route path="/device/:device/network/:network/account/:account" component={ AccountTabs } />
<Route path="/device/:device/device-settings" component={ DeviceSettingsTabs } /> <Route path="/device/:device/device-settings" component={ DeviceSettingsTabs } />
</nav> </nav>
<Notifications /> <Notifications />

View File

@ -54,7 +54,7 @@ const _render = (props: Props, state: AccountState): React$Element<string> => {
{ deviceStatusNotification } { deviceStatusNotification }
<h2 className={ `summary-header ${abstractAccount.network}` }> <h2 className={ `summary-header ${abstractAccount.network}` }>
Address #{ parseInt(abstractAccount.index) + 1 } Account #{ parseInt(abstractAccount.index) + 1 }
<a href={ explorerLink } className="gray" target="_blank" rel="noreferrer noopener">See full transaction history</a> <a href={ explorerLink } className="gray" target="_blank" rel="noreferrer noopener">See full transaction history</a>
</h2> </h2>

View File

@ -76,7 +76,7 @@ const complete = (state: State, action: DiscoveryCompleteAction): State => {
return newState; return newState;
} }
const addressCreate = (state: State, action: AccountCreateAction): State => { const accountCreate = (state: State, action: AccountCreateAction): State => {
const index: number = findIndex(state, action.network, action.device.state || '0'); const index: number = findIndex(state, action.network, action.device.state || '0');
const newState: State = [ ...state ]; const newState: State = [ ...state ];
newState[index].accountIndex++; newState[index].accountIndex++;
@ -159,7 +159,7 @@ export default function discovery(state: State = initialState, action: Action):
case DISCOVERY.START : case DISCOVERY.START :
return start(state, action); return start(state, action);
case ACCOUNT.CREATE : case ACCOUNT.CREATE :
return addressCreate(state, action); return accountCreate(state, action);
case DISCOVERY.STOP : case DISCOVERY.STOP :
return stop(state, action); return stop(state, action);
case DISCOVERY.COMPLETE : case DISCOVERY.COMPLETE :

View File

@ -35,11 +35,11 @@ export default (
<Route exact path="/device/:device/acquire" component={ AcquireContainer } /> <Route exact path="/device/:device/acquire" component={ AcquireContainer } />
<Route exact path="/device/:device/bootloader" component={ BootloaderContainer } /> <Route exact path="/device/:device/bootloader" component={ BootloaderContainer } />
<Route exact path="/device/:device/settings" component={ DeviceSettingsContainer } /> <Route exact path="/device/:device/settings" component={ DeviceSettingsContainer } />
<Route exact path="/device/:device/network/:network/address/:address" component={ SummaryContainer } /> <Route exact path="/device/:device/network/:network/account/:account" component={ SummaryContainer } />
<Route path="/device/:device/network/:network/address/:address/send" component={ SendFormContainer } /> <Route path="/device/:device/network/:network/account/:account/send" component={ SendFormContainer } />
<Route path="/device/:device/network/:network/address/:address/send/override" component={ SendFormContainer } /> <Route path="/device/:device/network/:network/account/:account/send/override" component={ SendFormContainer } />
<Route path="/device/:device/network/:network/address/:address/receive" component={ ReceiveContainer } /> <Route path="/device/:device/network/:network/account/:account/receive" component={ ReceiveContainer } />
<Route path="/device/:device/network/:network/address/:address/signverify" component={ SignVerifyContainer } /> <Route path="/device/:device/network/:network/account/:account/signverify" component={ SignVerifyContainer } />
</WalletContainer> </WalletContainer>
</Route> </Route>
</Switch> </Switch>

View File

@ -65,10 +65,10 @@ const validation = (api: MiddlewareAPI, params: RouterLocationState): boolean =>
const { config } = api.getState().localStorage; const { config } = api.getState().localStorage;
const coin = config.coins.find(coin => coin.network === params.network); const coin = config.coins.find(coin => coin.network === params.network);
if (!coin) return false; if (!coin) return false;
if (!params.address) return false; if (!params.account) return false;
} }
if (params.address) { if (params.account) {
} }

View File

@ -514,7 +514,7 @@ aside {
.add-address { .add-account {
position: relative; position: relative;
padding: 8px 0 8px 20px; padding: 8px 0 8px 20px;
cursor: pointer; cursor: pointer;