MyCrypto/common/components/WalletDecrypt/index.jsx

181 lines
4.4 KiB
React
Raw Normal View History

2017-06-29 23:03:11 +00:00
// @flow
import React, { Component } from 'react';
import translate from 'translations';
import KeystoreDecrypt from './Keystore';
import PrivateKeyDecrypt from './PrivateKey';
import type { PrivateKeyValue } from './PrivateKey';
import MnemonicDecrypt from './Mnemonic';
import LedgerNanoSDecrypt from './LedgerNano';
import TrezorDecrypt from './Trezor';
import ViewOnlyDecrypt from './ViewOnly';
import map from 'lodash/map';
import { unlockPrivateKey, unlockKeystore, setWallet } from 'actions/wallet';
2017-06-29 23:03:11 +00:00
import { connect } from 'react-redux';
import isEmpty from 'lodash/isEmpty';
2017-06-29 23:03:11 +00:00
const WALLETS = {
2017-07-04 03:21:19 +00:00
'keystore-file': {
lid: 'x_Keystore2',
component: KeystoreDecrypt,
initialParams: {
file: '',
password: ''
},
unlock: unlockKeystore
2017-07-04 03:21:19 +00:00
},
'private-key': {
lid: 'x_PrivKey2',
component: PrivateKeyDecrypt,
initialParams: {
key: '',
password: ''
2017-06-29 23:03:11 +00:00
},
2017-07-04 03:21:19 +00:00
unlock: unlockPrivateKey
},
'mnemonic-phrase': {
lid: 'x_Mnemonic',
component: MnemonicDecrypt,
disabled: true
2017-07-04 03:21:19 +00:00
},
'ledger-nano-s': {
lid: 'x_Ledger',
component: LedgerNanoSDecrypt,
disabled: true
2017-07-04 03:21:19 +00:00
},
trezor: {
lid: 'x_Trezor',
component: TrezorDecrypt,
initialParams: {},
unlock: setWallet
2017-07-04 03:21:19 +00:00
},
'view-only': {
lid: 'View with Address Only',
component: ViewOnlyDecrypt,
disabled: true
2017-07-04 03:21:19 +00:00
}
2017-06-29 23:03:11 +00:00
};
type UnlockParams = {} | PrivateKeyValue;
type State = {
2017-07-04 03:21:19 +00:00
selectedWalletKey: string,
value: UnlockParams
2017-06-29 23:03:11 +00:00
};
export class WalletDecrypt extends Component {
2017-07-04 03:21:19 +00:00
props: {
// FIXME
dispatch: (action: any) => void
};
state: State = {
selectedWalletKey: 'keystore-file',
value: WALLETS['keystore-file'].initialParams
};
getDecryptionComponent() {
const { selectedWalletKey, value } = this.state;
const selectedWallet = WALLETS[selectedWalletKey];
if (!selectedWallet) {
return null;
2017-06-29 23:03:11 +00:00
}
2017-07-04 03:21:19 +00:00
return (
<selectedWallet.component
value={value}
onChange={this.onChange}
onUnlock={this.onUnlock}
/>
);
}
buildWalletOptions() {
return map(WALLETS, (wallet, key) => {
const isSelected = this.state.selectedWalletKey === key;
return (
<label className="radio" key={key}>
<input
aria-flowto={`aria-${key}`}
aria-labelledby={`${key}-label`}
type="radio"
name="decryption-choice-radio-group"
value={key}
checked={isSelected}
onChange={this.handleDecryptionChoiceChange}
disabled={wallet.disabled}
2017-07-04 03:21:19 +00:00
/>
<span id={`${key}-label`}>
{translate(wallet.lid)}
</span>
</label>
);
});
}
handleDecryptionChoiceChange = (event: SyntheticInputEvent) => {
const wallet = WALLETS[event.target.value];
if (!wallet) {
return;
2017-06-29 23:03:11 +00:00
}
2017-07-04 03:21:19 +00:00
this.setState({
selectedWalletKey: event.target.value,
value: wallet.initialParams
});
};
render() {
const decryptionComponent = this.getDecryptionComponent();
return (
<article className="Tab-content-pane row">
2017-07-04 03:21:19 +00:00
<section className="col-md-4 col-sm-6">
<h4>
{translate('decrypt_Access')}
</h4>
{this.buildWalletOptions()}
</section>
{decryptionComponent}
{!!this.state.value.valid &&
<section className="col-md-4 col-sm-6">
<h4 id="uploadbtntxt-wallet">
{translate('ADD_Label_6')}
</h4>
<div className="form-group">
<a
tabIndex="0"
role="button"
className="btn btn-primary btn-block"
onClick={this.onUnlock}
>
{translate('ADD_Label_6_short')}
</a>
</div>
</section>}
</article>
);
}
onChange = (value: UnlockParams) => {
this.setState({ value });
};
onUnlock = (payload: any) => {
// some components (TrezorDecrypt) don't take an onChange prop, and thus this.state.value will remain unpopulated.
// in this case, we can expect the payload to contain the unlocked wallet info.
const unlockValue =
this.state.value && !isEmpty(this.state.value)
? this.state.value
: payload;
2017-07-04 03:21:19 +00:00
this.props.dispatch(
WALLETS[this.state.selectedWalletKey].unlock(unlockValue)
2017-07-04 03:21:19 +00:00
);
};
2017-06-29 23:03:11 +00:00
}
export default connect()(WalletDecrypt);