Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for appropriate keystore file type on upload #646

Merged
merged 12 commits into from
Dec 29, 2017
Merged
25 changes: 18 additions & 7 deletions common/components/WalletDecrypt/Keystore.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { isKeystorePassRequired } from 'libs/wallet';
import React, { Component } from 'react';
import translate, { translateRaw } from 'translations';
import { TShowNotification } from 'actions/notifications';

export interface KeystoreValue {
file: string;
password: string;
valid: boolean;
}

interface Props {
value: KeystoreValue;
onChange(value: KeystoreValue): void;
onUnlock(): void;
showNotification(level: string, message: string): TShowNotification;
}

function isPassRequired(file: string): boolean {
let passReq = false;
try {
Expand All @@ -18,13 +26,12 @@ function isPassRequired(file: string): boolean {
return passReq;
}

export default class KeystoreDecrypt extends Component {
public props: {
value: KeystoreValue;
onChange(value: KeystoreValue): void;
onUnlock(): void;
};
function isValidFile(rawFile: File): boolean {
const fileType = rawFile.type;
return fileType === '' || fileType === 'application/json';
}

export default class KeystoreDecrypt extends Component<Props> {
public render() {
const { file, password } = this.props.value;
const passReq = isPassRequired(file);
Expand Down Expand Up @@ -96,6 +103,10 @@ export default class KeystoreDecrypt extends Component {
});
};

fileReader.readAsText(inputFile, 'utf-8');
if (isValidFile(inputFile)) {
fileReader.readAsText(inputFile, 'utf-8');
} else {
this.props.showNotification('danger', translateRaw('ERROR_3'));
}
};
}
12 changes: 10 additions & 2 deletions common/components/WalletDecrypt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Help from 'components/ui/Help';
import { knowledgeBaseURL } from 'config/data';
import NavigationPrompt from './NavigationPrompt';
import { IWallet } from 'libs/wallet';
import { showNotification, TShowNotification } from 'actions/notifications';

type UnlockParams = {} | PrivateKeyValue;

Expand All @@ -41,6 +42,7 @@ interface Props {
setWallet: TSetWallet;
unlockWeb3: TUnlockWeb3;
resetWallet: TResetWallet;
showNotification: TShowNotification;
wallet: IWallet;
hidden?: boolean;
offline: boolean;
Expand Down Expand Up @@ -125,7 +127,12 @@ export class WalletDecrypt extends Component<Props, State> {
return null;
}
return (
<selectedWallet.component value={value} onChange={this.onChange} onUnlock={this.onUnlock} />
<selectedWallet.component
value={value}
onChange={this.onChange}
onUnlock={this.onUnlock}
showNotification={this.props.showNotification}
/>
);
}

Expand Down Expand Up @@ -246,5 +253,6 @@ export default connect(mapStateToProps, {
unlockWeb3,
setWallet,
resetWallet,
resetTransactionState: reset
resetTransactionState: reset,
showNotification
})(WalletDecrypt);