2017-11-08 18:16:43 +00:00
|
|
|
import { isKeystorePassRequired } from 'libs/wallet';
|
2017-07-04 03:21:19 +00:00
|
|
|
import React, { Component } from 'react';
|
2017-08-28 18:05:38 +00:00
|
|
|
import translate, { translateRaw } from 'translations';
|
2017-05-31 23:54:47 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export interface KeystoreValue {
|
|
|
|
file: string;
|
|
|
|
password: string;
|
|
|
|
valid: boolean;
|
|
|
|
}
|
2017-06-08 02:30:25 +00:00
|
|
|
|
2017-08-20 20:28:47 +00:00
|
|
|
function isPassRequired(file: string): boolean {
|
|
|
|
let passReq = false;
|
|
|
|
try {
|
|
|
|
passReq = isKeystorePassRequired(file);
|
|
|
|
} catch (e) {
|
2017-09-25 02:06:28 +00:00
|
|
|
// TODO: communicate invalid file to user
|
2017-08-20 20:28:47 +00:00
|
|
|
}
|
|
|
|
return passReq;
|
|
|
|
}
|
2017-06-08 02:30:25 +00:00
|
|
|
|
2017-08-20 20:28:47 +00:00
|
|
|
export default class KeystoreDecrypt extends Component {
|
2017-09-25 02:06:28 +00:00
|
|
|
public props: {
|
|
|
|
value: KeystoreValue;
|
|
|
|
onChange(value: KeystoreValue): void;
|
|
|
|
onUnlock(): void;
|
2017-07-04 03:21:19 +00:00
|
|
|
};
|
2017-06-08 02:30:25 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public render() {
|
2017-08-20 20:28:47 +00:00
|
|
|
const { file, password } = this.props.value;
|
2017-09-25 02:06:28 +00:00
|
|
|
const passReq = isPassRequired(file);
|
2017-08-20 20:28:47 +00:00
|
|
|
|
2017-07-04 03:21:19 +00:00
|
|
|
return (
|
|
|
|
<section className="col-md-4 col-sm-6">
|
|
|
|
<div id="selectedUploadKey">
|
2017-11-08 18:16:43 +00:00
|
|
|
<h4>{translate('ADD_Radio_2_alt')}</h4>
|
2017-05-31 23:54:47 +00:00
|
|
|
|
2017-07-04 03:21:19 +00:00
|
|
|
<div className="form-group">
|
|
|
|
<input
|
2017-08-20 20:28:47 +00:00
|
|
|
className={'hidden'}
|
2017-07-04 03:21:19 +00:00
|
|
|
type="file"
|
|
|
|
id="fselector"
|
|
|
|
onChange={this.handleFileSelection}
|
|
|
|
/>
|
2017-08-20 20:28:47 +00:00
|
|
|
<label htmlFor="fselector" style={{ width: '100%' }}>
|
2017-07-04 03:21:19 +00:00
|
|
|
<a
|
2017-08-20 20:28:47 +00:00
|
|
|
className="btn btn-default btn-block"
|
2017-07-04 03:21:19 +00:00
|
|
|
id="aria1"
|
2017-09-25 02:06:28 +00:00
|
|
|
tabIndex={0}
|
2017-07-04 03:21:19 +00:00
|
|
|
role="button"
|
|
|
|
>
|
|
|
|
{translate('ADD_Radio_2_short')}
|
|
|
|
</a>
|
|
|
|
</label>
|
2017-08-20 20:28:47 +00:00
|
|
|
<div className={file.length && passReq ? '' : 'hidden'}>
|
2017-11-08 18:16:43 +00:00
|
|
|
<p>{translate('ADD_Label_3')}</p>
|
2017-08-20 20:28:47 +00:00
|
|
|
<input
|
2017-11-12 19:45:52 +00:00
|
|
|
className={`form-control ${
|
|
|
|
password.length > 0 ? 'is-valid' : 'is-invalid'
|
|
|
|
}`}
|
2017-08-20 20:28:47 +00:00
|
|
|
value={password}
|
|
|
|
onChange={this.onPasswordChange}
|
|
|
|
onKeyDown={this.onKeyDown}
|
2017-08-28 18:05:38 +00:00
|
|
|
placeholder={translateRaw('x_Password')}
|
2017-08-20 20:28:47 +00:00
|
|
|
type="password"
|
|
|
|
/>
|
|
|
|
</div>
|
2017-07-04 03:21:19 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
2017-08-20 20:28:47 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public onKeyDown = (e: any) => {
|
2017-08-20 20:28:47 +00:00
|
|
|
if (e.keyCode === 13) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.props.onUnlock();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public onPasswordChange = (e: any) => {
|
2017-08-20 20:28:47 +00:00
|
|
|
const valid = this.props.value.file.length && e.target.value.length;
|
|
|
|
this.props.onChange({
|
|
|
|
...this.props.value,
|
|
|
|
password: e.target.value,
|
|
|
|
valid
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public handleFileSelection = (e: any) => {
|
2017-08-20 20:28:47 +00:00
|
|
|
const fileReader = new FileReader();
|
2017-09-25 02:06:28 +00:00
|
|
|
const target = e.target;
|
|
|
|
const inputFile = target.files[0];
|
2017-08-20 20:28:47 +00:00
|
|
|
|
|
|
|
fileReader.onload = () => {
|
|
|
|
const keystore = fileReader.result;
|
2017-09-25 02:06:28 +00:00
|
|
|
const passReq = isPassRequired(keystore);
|
2017-08-20 20:28:47 +00:00
|
|
|
|
|
|
|
this.props.onChange({
|
|
|
|
...this.props.value,
|
|
|
|
file: keystore,
|
|
|
|
valid: keystore.length && !passReq
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
fileReader.readAsText(inputFile, 'utf-8');
|
|
|
|
};
|
2017-07-04 03:21:19 +00:00
|
|
|
}
|