2017-09-25 02:06:28 +00:00
|
|
|
import { mnemonicToSeed, validateMnemonic } from 'bip39';
|
|
|
|
import DPATHS from 'config/dpaths';
|
2017-07-04 03:21:19 +00:00
|
|
|
import React, { Component } from 'react';
|
2017-09-15 19:29:38 +00:00
|
|
|
import translate, { translateRaw } from 'translations';
|
|
|
|
import DeterministicWalletsModal from './DeterministicWalletsModal';
|
2017-12-15 01:08:24 +00:00
|
|
|
import { formatMnemonic } from 'utils/formatters';
|
2017-09-15 19:29:38 +00:00
|
|
|
|
|
|
|
const DEFAULT_PATH = DPATHS.MNEMONIC[0].value;
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
interface Props {
|
|
|
|
onUnlock(param: any): void;
|
|
|
|
}
|
|
|
|
interface State {
|
|
|
|
phrase: string;
|
2017-12-15 01:08:24 +00:00
|
|
|
formattedPhrase: string;
|
2017-09-25 02:06:28 +00:00
|
|
|
pass: string;
|
|
|
|
seed: string;
|
|
|
|
dPath: string;
|
|
|
|
}
|
2017-06-07 01:44:02 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export default class MnemonicDecrypt extends Component<Props, State> {
|
|
|
|
public state: State = {
|
2017-09-15 19:29:38 +00:00
|
|
|
phrase: '',
|
2017-12-15 01:08:24 +00:00
|
|
|
formattedPhrase: '',
|
2017-09-15 19:29:38 +00:00
|
|
|
pass: '',
|
|
|
|
seed: '',
|
|
|
|
dPath: DEFAULT_PATH
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public render() {
|
2017-12-15 01:08:24 +00:00
|
|
|
const { phrase, formattedPhrase, seed, dPath, pass } = this.state;
|
|
|
|
const isValidMnemonic = validateMnemonic(formattedPhrase);
|
2017-09-15 19:29:38 +00:00
|
|
|
|
2017-07-04 03:21:19 +00:00
|
|
|
return (
|
|
|
|
<section className="col-md-4 col-sm-6">
|
2017-09-15 19:29:38 +00:00
|
|
|
<div id="selectedTypeKey">
|
2017-10-19 02:29:49 +00:00
|
|
|
<h4>{translate('ADD_Radio_5')}</h4>
|
2017-09-15 19:29:38 +00:00
|
|
|
<div className="form-group">
|
|
|
|
<textarea
|
|
|
|
id="aria-private-key"
|
|
|
|
className={`form-control ${isValidMnemonic
|
|
|
|
? 'is-valid'
|
|
|
|
: 'is-invalid'}`}
|
|
|
|
value={phrase}
|
|
|
|
onChange={this.onMnemonicChange}
|
|
|
|
placeholder={translateRaw('x_Mnemonic')}
|
2017-09-25 02:06:28 +00:00
|
|
|
rows={4}
|
2017-09-15 19:29:38 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2017-07-04 03:21:19 +00:00
|
|
|
<div className="form-group">
|
2017-09-15 19:29:38 +00:00
|
|
|
<p>Password (optional):</p>
|
|
|
|
<input
|
|
|
|
className="form-control"
|
|
|
|
value={pass}
|
|
|
|
onChange={this.onPasswordChange}
|
|
|
|
placeholder={translateRaw('x_Password')}
|
|
|
|
type="password"
|
|
|
|
/>
|
2017-07-04 03:21:19 +00:00
|
|
|
</div>
|
2017-10-19 02:29:49 +00:00
|
|
|
{isValidMnemonic && (
|
2017-09-15 19:29:38 +00:00
|
|
|
<div className="form-group">
|
|
|
|
<button
|
|
|
|
style={{ width: '100%' }}
|
|
|
|
onClick={this.onDWModalOpen}
|
|
|
|
className="btn btn-primary btn-lg"
|
|
|
|
>
|
|
|
|
{translate('Choose Address')}
|
|
|
|
</button>
|
2017-10-19 02:29:49 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2017-07-04 03:21:19 +00:00
|
|
|
</div>
|
2017-09-15 19:29:38 +00:00
|
|
|
|
|
|
|
<DeterministicWalletsModal
|
|
|
|
isOpen={!!seed}
|
|
|
|
seed={seed}
|
|
|
|
dPath={dPath}
|
|
|
|
dPaths={DPATHS.MNEMONIC}
|
2017-09-25 02:06:28 +00:00
|
|
|
onCancel={this.handleCancel}
|
|
|
|
onConfirmAddress={this.handleUnlock}
|
|
|
|
onPathChange={this.handlePathChange}
|
2017-09-15 19:29:38 +00:00
|
|
|
walletType={translateRaw('x_Mnemonic')}
|
|
|
|
/>
|
2017-07-04 03:21:19 +00:00
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
2017-09-15 19:29:38 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public onPasswordChange = (e: React.SyntheticEvent<HTMLInputElement>) => {
|
|
|
|
this.setState({ pass: (e.target as HTMLInputElement).value });
|
2017-09-15 19:29:38 +00:00
|
|
|
};
|
|
|
|
|
2017-12-15 01:08:24 +00:00
|
|
|
public onMnemonicChange = (e: React.FormEvent<HTMLTextAreaElement>) => {
|
|
|
|
const phrase = e.currentTarget.value;
|
|
|
|
const formattedPhrase = formatMnemonic(phrase);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
phrase,
|
|
|
|
formattedPhrase
|
|
|
|
});
|
2017-09-15 19:29:38 +00:00
|
|
|
};
|
|
|
|
|
2017-10-19 02:29:49 +00:00
|
|
|
public onDWModalOpen = () => {
|
2017-12-15 01:08:24 +00:00
|
|
|
const { formattedPhrase, pass } = this.state;
|
2017-09-15 19:29:38 +00:00
|
|
|
|
2017-12-15 01:08:24 +00:00
|
|
|
if (!validateMnemonic(formattedPhrase)) {
|
2017-09-25 02:06:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-15 19:29:38 +00:00
|
|
|
|
|
|
|
try {
|
2017-12-15 01:08:24 +00:00
|
|
|
const seed = mnemonicToSeed(formattedPhrase, pass).toString('hex');
|
2017-09-15 19:29:38 +00:00
|
|
|
this.setState({ seed });
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
private handleCancel = () => {
|
2017-09-15 19:29:38 +00:00
|
|
|
this.setState({ seed: '' });
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
private handlePathChange = (dPath: string) => {
|
2017-09-15 19:29:38 +00:00
|
|
|
this.setState({ dPath });
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
private handleUnlock = (address, index) => {
|
2017-12-15 01:08:24 +00:00
|
|
|
const { formattedPhrase, pass, dPath } = this.state;
|
2017-09-15 19:29:38 +00:00
|
|
|
|
|
|
|
this.props.onUnlock({
|
|
|
|
path: `${dPath}/${index}`,
|
|
|
|
pass,
|
2017-12-15 01:08:24 +00:00
|
|
|
phrase: formattedPhrase,
|
2017-09-15 19:29:38 +00:00
|
|
|
address
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
seed: '',
|
|
|
|
pass: '',
|
2017-12-15 01:08:24 +00:00
|
|
|
phrase: '',
|
|
|
|
formattedPhrase: ''
|
2017-09-15 19:29:38 +00:00
|
|
|
});
|
|
|
|
};
|
2017-07-04 03:21:19 +00:00
|
|
|
}
|