Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

fix: Relates to #138. Prevents user from importing account that is already in account list #327

Merged
Merged
Expand Up @@ -4,9 +4,14 @@
// SPDX-License-Identifier: BSD-3-Clause

import React, { Component } from 'react';
import { Card, Form as FetherForm } from 'fether-ui';
import { addressShort, Card, Form as FetherForm } from 'fether-ui';
import { accounts$, withoutLoading } from '@parity/light.js';
import light from '@parity/light.js-react';
import { inject, observer } from 'mobx-react';

@light({
accounts: () => accounts$().pipe(withoutLoading())
})
@inject('createAccountStore')
@observer
class AccountImportOptions extends Component {
Expand All @@ -32,19 +37,29 @@ class AccountImportOptions extends Component {
handleSubmitPhrase = async () => {
const phrase = this.state.phrase.trim();
const {
createAccountStore,
createAccountStore: { setPhrase }
} = this.props;

this.setState({ isLoading: true, phrase });

try {
await setPhrase(phrase);

const addressForPhrase = createAccountStore.address.toLowerCase();

if (this.hasExistingAddressForImport(addressForPhrase)) {
return;
}

this.handleNextStep();
} catch (e) {
} catch (error) {
this.setState({
isLoading: false,
error:
'The passphrase was not recognized. Please verify that you entered your passphrase correctly.'
});
console.error(error);
}
};

Expand All @@ -54,7 +69,15 @@ class AccountImportOptions extends Component {
} = this.props;

this.setState({ isLoading: true });

try {
const json = JSON.parse(jsonString);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not parse here.

3 lines below, the await setJsonString(jsonString); actually parses the JSON, and createAccountStore.address will hold the parsed address. Do the hasExistingAddressForImport then.

const jsonAddress = `0x${json['address'].toLowerCase()}`;

if (this.hasExistingAddressForImport(jsonAddress)) {
return;
}

await setJsonString(jsonString);
this.handleNextStep();
} catch (error) {
Expand All @@ -67,6 +90,22 @@ class AccountImportOptions extends Component {
}
};

hasExistingAddressForImport = addressForImport => {
const { accounts } = this.props;
const isExistingAddress = accounts
.map(address => address && address.toLowerCase())
.includes(addressForImport);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put .toLowecase() on this line. So that the input argument can be in any case.


if (isExistingAddress) {
this.setState({
isLoading: false,
error: `Account ${addressShort(addressForImport)} already listed`
});
}

return isExistingAddress;
};

render () {
const {
history,
Expand Down
8 changes: 3 additions & 5 deletions packages/fether-ui/src/AddressShort/AddressShort.js
Expand Up @@ -6,12 +6,10 @@
import React from 'react';
import PropTypes from 'prop-types';

import { addressShort } from '../utils/addressShort';

export const AddressShort = ({ address, as: T = 'span', ...otherProps }) => (
<T {...otherProps}>
{address.slice(0, 6)}
..
{address.slice(-4)}
</T>
<T {...otherProps}>{addressShort(address)}</T>
);

AddressShort.propTypes = {
Expand Down
1 change: 1 addition & 0 deletions packages/fether-ui/src/index.js
Expand Up @@ -11,3 +11,4 @@ export * from './Form';
export * from './Header';
export * from './Placeholder';
export * from './TokenCard';
export * from './utils';
7 changes: 7 additions & 0 deletions packages/fether-ui/src/utils/addressShort.js
@@ -0,0 +1,7 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause

export const addressShort = address =>
address ? `${address.slice(0, 6)}..${address.slice(-4)}` : '';
6 changes: 6 additions & 0 deletions packages/fether-ui/src/utils/index.js
@@ -0,0 +1,6 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause

export * from './addressShort';