-
Notifications
You must be signed in to change notification settings - Fork 33
fix: Relates to #138. Prevents user from importing account that is already in account list #327
Changes from 3 commits
cced0a0
2f22197
18c545d
3816b29
0d99be3
aaff1d2
341a124
929d29f
cff2a59
6296cc6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,13 @@ | |
|
||
import React, { Component } from 'react'; | ||
import { Card, Form as FetherForm } from 'fether-ui'; | ||
import { accountsInfo$, withoutLoading } from '@parity/light.js'; | ||
import light from '@parity/light.js-react'; | ||
import { inject, observer } from 'mobx-react'; | ||
|
||
@light({ | ||
accountsInfo: () => accountsInfo$().pipe(withoutLoading()) | ||
}) | ||
@inject('createAccountStore') | ||
@observer | ||
class AccountImportOptions extends Component { | ||
|
@@ -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); | ||
} | ||
}; | ||
|
||
|
@@ -54,7 +69,15 @@ class AccountImportOptions extends Component { | |
} = this.props; | ||
|
||
this.setState({ isLoading: true }); | ||
|
||
try { | ||
const json = JSON.parse(jsonString); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not parse here. 3 lines below, the |
||
const jsonAddress = `0x${json['address'].toLowerCase()}`; | ||
|
||
if (this.hasExistingAddressForImport(jsonAddress)) { | ||
return; | ||
} | ||
|
||
await setJsonString(jsonString); | ||
this.handleNextStep(); | ||
} catch (error) { | ||
|
@@ -67,6 +90,22 @@ class AccountImportOptions extends Component { | |
} | ||
}; | ||
|
||
hasExistingAddressForImport = addressForImport => { | ||
const { accountsInfo } = this.props; | ||
const isExistingAddress = Object.keys(accountsInfo) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use account$, no need for Object.keys anymore since it returns a string[] |
||
.map(address => address && address.toLowerCase()) | ||
.includes(addressForImport); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would put |
||
|
||
if (isExistingAddress) { | ||
this.setState({ | ||
isLoading: false, | ||
error: `Account already loaded. Address ${addressForImport} is already in the account list` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would go for something much shorter along the line: |
||
}); | ||
} | ||
|
||
return isExistingAddress; | ||
}; | ||
|
||
render () { | ||
const { | ||
history, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use
accounts$()
here: https://parity-js.github.io/light.js/api/modules/_rpc_eth_.html#accounts