diff --git a/src/components/AccountIconChooser.js b/src/components/AccountIconChooser.js index 72a02b14e8..b9c357dbf8 100644 --- a/src/components/AccountIconChooser.js +++ b/src/components/AccountIconChooser.js @@ -33,6 +33,7 @@ import { NetworkProtocols } from '../constants'; import fonts from "../fonts"; import { debounce } from '../util/debounce'; import { brainWalletAddress, substrateAddress, words } from '../util/native'; +import {constructSURI} from '../util/suri' export default class AccountIconChooser extends React.PureComponent { constructor(props) { @@ -48,7 +49,6 @@ export default class AccountIconChooser extends React.PureComponent { // clean previous selection onSelect({ newAddress: '', isBip39: false, newSeed: ''}); - try { const icons = await Promise.all( Array(4) @@ -65,12 +65,19 @@ export default class AccountIconChooser extends React.PureComponent { if (protocol === NetworkProtocols.ETHEREUM) { Object.assign(result, await brainWalletAddress(result.seed)); } else { + // Substrate try { - result.address = await substrateAddress(`${result.seed}${derivationPath}///${derivationPassword}`, prefix); + const suri = constructSURI({ + derivePath: derivationPath, + password: derivationPassword, + phrase: result.seed + }); + + result.address = await substrateAddress(suri, prefix); result.bip39 = true; } catch (e){ // invalid seed or derivation path - // console.error(e); + console.error(e); } } return result; diff --git a/src/components/DerivationPathField.js b/src/components/DerivationPathField.js index 792c5fc242..2e6b49bc14 100644 --- a/src/components/DerivationPathField.js +++ b/src/components/DerivationPathField.js @@ -25,7 +25,7 @@ import { } from 'react-native'; import Icon from 'react-native-vector-icons/MaterialIcons'; -import keyExtract from '../util/keyExtract' +import {parseDerivationPath} from '../util/suri' import TextInput from './TextInput'; export default function DerivationPathField(props) { @@ -58,12 +58,24 @@ export default function DerivationPathField(props) { {showAdvancedField && { - const derivationPath = keyExtract(text); - onChange({ - derivationPassword: derivationPath.password || '', - derivationPath: derivationPath.derivePath || '' - }); - setIsValidPath(!!derivationPath.password || !!derivationPath.derivePath); + try { + const derivationPath = parseDerivationPath(text); + + onChange({ + derivationPassword: derivationPath.password || '', + derivationPath: derivationPath.derivePath || '', + isDerivationPathValid: true + }); + setIsValidPath(true); + } catch (e) { + // wrong derivationPath + onChange({ + derivationPassword: '', + derivationPath: '', + isDerivationPathValid: false + }); + setIsValidPath(false); + } }} placeholder="optional derivation path" style={isValidPath ? ownStyles.validInput: ownStyles.invalidInput} diff --git a/src/screens/AccountNew.js b/src/screens/AccountNew.js index 5ea15064e4..a77513d3f7 100644 --- a/src/screens/AccountNew.js +++ b/src/screens/AccountNew.js @@ -17,7 +17,7 @@ 'use strict'; import React from 'react'; -import { StyleSheet, Text, TouchableOpacity, View } from 'react-native'; +import { StyleSheet, Text, View } from 'react-native'; import { Subscribe } from 'unstated'; import colors from '../colors'; @@ -32,6 +32,7 @@ import { NETWORK_LIST, NetworkProtocols } from '../constants'; import fonts from '../fonts'; import AccountsStore from '../stores/AccountsStore'; import { empty, validateSeed } from '../util/account'; +import {constructSURI} from '../util/suri'; export default class AccountNew extends React.Component { static navigationOptions = { @@ -55,6 +56,7 @@ class AccountNewView extends React.Component { this.state = { derivationPassword: '', derivationPath: '', + isDerivationPathValid: true, selectedAccount: undefined, selectedNetwork: undefined, }; @@ -79,7 +81,7 @@ class AccountNewView extends React.Component { render() { const { accounts, navigation } = this.props; - const { derivationPassword, derivationPath, selectedAccount, selectedNetwork } = this.state; + const { derivationPassword, derivationPath, isDerivationPathValid, selectedAccount, selectedNetwork } = this.state; const {address, name, seed, validBip39Seed} = selectedAccount; const isSubstrate = selectedNetwork.protocol === NetworkProtocols.SUBSTRATE; @@ -100,22 +102,38 @@ class AccountNewView extends React.Component { derivationPassword={derivationPassword} derivationPath={derivationPath} onSelect={({ newAddress, isBip39, newSeed }) => { - if (isSubstrate) { - accounts.updateNew({ - address: newAddress, - derivationPassword, - derivationPath, - seed: `${newSeed}${derivationPath}///${derivationPassword}`, - seedPhrase: newSeed, - validBip39Seed: isBip39 - }); + if (newAddress && isBip39 && newSeed){ + if (isSubstrate) { + try { + const suri = constructSURI({ + derivePath: derivationPath, + password: derivationPassword, + phrase: newSeed + }); + + accounts.updateNew({ + address: newAddress, + derivationPassword, + derivationPath, + seed: suri, + seedPhrase: newSeed, + validBip39Seed: isBip39 + }); + } catch (e) { + console.error(e); + } + } else { + // Ethereum account + accounts.updateNew({ + address: newAddress, + seed: newSeed, + validBip39Seed: isBip39 + }); + } } else { - accounts.updateNew({ - address: newAddress, - seed: newSeed, - validBip39Seed: isBip39 - }); - }}} + accounts.updateNew({ address: '', seed: '', validBip39Seed: false}) + } + }} network={selectedNetwork} value={address && address} /> @@ -126,8 +144,8 @@ class AccountNewView extends React.Component { placeholder="Enter a new account name" /> {isSubstrate && { - this.setState({ derivationPath, derivationPassword }); + onChange = { ({derivationPassword, derivationPath, isDerivationPathValid}) => { + this.setState({ derivationPath, derivationPassword, isDerivationPathValid }); }} styles={styles} />} @@ -139,7 +157,7 @@ class AccountNewView extends React.Component {