2017-08-28 17:43:57 +00:00
|
|
|
import {
|
|
|
|
DeterministicWalletData,
|
2017-09-25 02:06:28 +00:00
|
|
|
getDeterministicWallets,
|
2017-08-28 17:43:57 +00:00
|
|
|
GetDeterministicWalletsAction,
|
2017-09-25 02:06:28 +00:00
|
|
|
GetDeterministicWalletsArgs,
|
|
|
|
setDesiredToken,
|
2017-08-28 17:43:57 +00:00
|
|
|
SetDesiredTokenAction
|
|
|
|
} from 'actions/deterministicWallets';
|
2017-09-25 02:06:28 +00:00
|
|
|
import Modal, { IButton } from 'components/ui/Modal';
|
|
|
|
import { NetworkConfig, Token } from 'config/data';
|
|
|
|
import { isValidPath } from 'libs/validators';
|
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { getNetworkConfig } from 'selectors/config';
|
|
|
|
import { getTokens, MergedToken } from 'selectors/wallet';
|
|
|
|
import './DeterministicWalletsModal.scss';
|
2017-08-28 17:43:57 +00:00
|
|
|
|
|
|
|
const WALLETS_PER_PAGE = 5;
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
interface Props {
|
|
|
|
// Passed props
|
|
|
|
isOpen?: boolean;
|
|
|
|
walletType?: string;
|
|
|
|
dPath: string;
|
|
|
|
dPaths: { label: string; value: string }[];
|
|
|
|
publicKey?: string;
|
|
|
|
chainCode?: string;
|
|
|
|
seed?: string;
|
|
|
|
|
2017-08-28 17:43:57 +00:00
|
|
|
// Redux state
|
2017-09-25 02:06:28 +00:00
|
|
|
wallets: DeterministicWalletData[];
|
|
|
|
desiredToken: string;
|
|
|
|
network: NetworkConfig;
|
|
|
|
tokens: MergedToken[];
|
2017-08-28 17:43:57 +00:00
|
|
|
|
|
|
|
// Redux actions
|
2017-09-25 02:06:28 +00:00
|
|
|
getDeterministicWallets(
|
|
|
|
args: GetDeterministicWalletsArgs
|
|
|
|
): GetDeterministicWalletsAction;
|
|
|
|
setDesiredToken(tkn: string | undefined): SetDesiredTokenAction;
|
2017-08-28 17:43:57 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
onCancel(): void;
|
|
|
|
onConfirmAddress(address: string, addressIndex: number): void;
|
|
|
|
onPathChange(path: string): void;
|
|
|
|
}
|
2017-08-28 17:43:57 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
interface State {
|
|
|
|
selectedAddress: string;
|
|
|
|
selectedAddrIndex: number;
|
|
|
|
isCustomPath: boolean;
|
|
|
|
customPath: string;
|
|
|
|
page: number;
|
|
|
|
}
|
2017-08-28 17:43:57 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
class DeterministicWalletsModal extends React.Component<Props, State> {
|
|
|
|
public state = {
|
2017-08-28 17:43:57 +00:00
|
|
|
selectedAddress: '',
|
2017-09-15 19:29:38 +00:00
|
|
|
selectedAddrIndex: 0,
|
2017-08-28 17:43:57 +00:00
|
|
|
isCustomPath: false,
|
|
|
|
customPath: '',
|
|
|
|
page: 0
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public componentDidMount() {
|
|
|
|
this.getAddresses();
|
2017-08-28 17:43:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public componentWillReceiveProps(nextProps) {
|
2017-09-15 19:29:38 +00:00
|
|
|
const { publicKey, chainCode, seed, dPath } = this.props;
|
2017-08-28 17:43:57 +00:00
|
|
|
if (
|
|
|
|
nextProps.publicKey !== publicKey ||
|
2017-09-15 19:29:38 +00:00
|
|
|
nextProps.chainCode !== chainCode ||
|
|
|
|
nextProps.dPath !== dPath ||
|
|
|
|
nextProps.seed !== seed
|
2017-08-28 17:43:57 +00:00
|
|
|
) {
|
2017-09-25 02:06:28 +00:00
|
|
|
this.getAddresses(nextProps);
|
2017-08-28 17:43:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public render() {
|
2017-08-28 17:43:57 +00:00
|
|
|
const {
|
|
|
|
wallets,
|
|
|
|
desiredToken,
|
|
|
|
network,
|
|
|
|
tokens,
|
|
|
|
dPath,
|
|
|
|
dPaths,
|
|
|
|
onCancel,
|
|
|
|
walletType
|
|
|
|
} = this.props;
|
|
|
|
const { selectedAddress, isCustomPath, customPath, page } = this.state;
|
|
|
|
const validPathClass = isValidPath(customPath) ? 'is-valid' : 'is-invalid';
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
const buttons: IButton[] = [
|
2017-08-28 17:43:57 +00:00
|
|
|
{
|
|
|
|
text: 'Unlock this Address',
|
|
|
|
type: 'primary',
|
2017-09-25 02:06:28 +00:00
|
|
|
onClick: this.handleConfirmAddress,
|
2017-08-28 17:43:57 +00:00
|
|
|
disabled: !selectedAddress
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: 'Cancel',
|
|
|
|
type: 'default',
|
|
|
|
onClick: onCancel
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
title={`Unlock your ${walletType || ''} Wallet`}
|
|
|
|
isOpen={this.props.isOpen}
|
|
|
|
buttons={buttons}
|
|
|
|
handleClose={onCancel}
|
|
|
|
>
|
|
|
|
<div className="DWModal">
|
|
|
|
<form
|
|
|
|
className="DWModal-path form-group-sm"
|
2017-09-25 02:06:28 +00:00
|
|
|
onSubmit={this.handleSubmitCustomPath}
|
2017-08-28 17:43:57 +00:00
|
|
|
>
|
|
|
|
<span className="DWModal-path-label">Addresses for</span>
|
|
|
|
<select
|
|
|
|
className="form-control"
|
2017-09-25 02:06:28 +00:00
|
|
|
onChange={this.handleChangePath}
|
2017-08-28 17:43:57 +00:00
|
|
|
value={isCustomPath ? 'custom' : dPath}
|
|
|
|
>
|
|
|
|
{dPaths.map(dp =>
|
|
|
|
<option key={dp.value} value={dp.value}>
|
|
|
|
{dp.label}
|
|
|
|
</option>
|
|
|
|
)}
|
|
|
|
<option value="custom">Custom path...</option>
|
|
|
|
</select>
|
|
|
|
{isCustomPath &&
|
|
|
|
<input
|
|
|
|
className={`form-control ${validPathClass}`}
|
|
|
|
value={customPath}
|
|
|
|
placeholder="m/44'/60'/0'/0"
|
2017-09-25 02:06:28 +00:00
|
|
|
onChange={this.handleChangeCustomPath}
|
2017-08-28 17:43:57 +00:00
|
|
|
/>}
|
|
|
|
</form>
|
|
|
|
|
|
|
|
<div className="DWModal-addresses">
|
|
|
|
<table className="DWModal-addresses-table table table-striped table-hover">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<td>#</td>
|
|
|
|
<td>Address</td>
|
|
|
|
<td>
|
|
|
|
{network.unit}
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<select
|
|
|
|
className="DWModal-addresses-table-token"
|
|
|
|
value={desiredToken}
|
2017-09-25 02:06:28 +00:00
|
|
|
onChange={this.handleChangeToken}
|
2017-08-28 17:43:57 +00:00
|
|
|
>
|
|
|
|
<option value="">-Token-</option>
|
|
|
|
{tokens.map(t =>
|
|
|
|
<option key={t.symbol} value={t.symbol}>
|
|
|
|
{t.symbol}
|
|
|
|
</option>
|
|
|
|
)}
|
|
|
|
</select>
|
|
|
|
</td>
|
|
|
|
<td>More</td>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2017-09-25 02:06:28 +00:00
|
|
|
{wallets.map(wallet => this.renderWalletRow(wallet))}
|
2017-08-28 17:43:57 +00:00
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
<div className="DWModal-addresses-nav">
|
|
|
|
<button
|
|
|
|
className="DWModal-addresses-nav-btn btn btn-sm btn-default"
|
|
|
|
disabled={page === 0}
|
2017-09-25 02:06:28 +00:00
|
|
|
onClick={this.prevPage}
|
2017-08-28 17:43:57 +00:00
|
|
|
>
|
|
|
|
← Back
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
className="DWModal-addresses-nav-btn btn btn-sm btn-default"
|
2017-09-25 02:06:28 +00:00
|
|
|
onClick={this.nextPage}
|
2017-08-28 17:43:57 +00:00
|
|
|
>
|
|
|
|
More →
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
2017-09-25 02:06:28 +00:00
|
|
|
|
|
|
|
private getAddresses(props: Props = this.props) {
|
|
|
|
const { dPath, publicKey, chainCode, seed } = props;
|
|
|
|
|
|
|
|
if (dPath && ((publicKey && chainCode) || seed) && isValidPath(dPath)) {
|
|
|
|
this.props.getDeterministicWallets({
|
|
|
|
seed,
|
|
|
|
dPath,
|
|
|
|
publicKey,
|
|
|
|
chainCode,
|
|
|
|
limit: WALLETS_PER_PAGE,
|
|
|
|
offset: WALLETS_PER_PAGE * this.state.page
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private handleChangePath = (ev: React.SyntheticEvent<HTMLSelectElement>) => {
|
|
|
|
const { value } = ev.target as HTMLSelectElement;
|
|
|
|
|
|
|
|
if (value === 'custom') {
|
|
|
|
this.setState({ isCustomPath: true });
|
|
|
|
} else {
|
|
|
|
this.setState({ isCustomPath: false });
|
|
|
|
if (this.props.dPath !== value) {
|
|
|
|
this.props.onPathChange(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleChangeCustomPath = (
|
|
|
|
ev: React.SyntheticEvent<HTMLInputElement>
|
|
|
|
) => {
|
|
|
|
this.setState({ customPath: (ev.target as HTMLInputElement).value });
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleSubmitCustomPath = (
|
|
|
|
ev: React.SyntheticEvent<HTMLFormElement>
|
|
|
|
) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
if (!isValidPath(this.state.customPath)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.props.onPathChange(this.state.customPath);
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleChangeToken = (ev: React.SyntheticEvent<HTMLSelectElement>) => {
|
|
|
|
this.props.setDesiredToken(
|
|
|
|
(ev.target as HTMLSelectElement).value || undefined
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleConfirmAddress = () => {
|
|
|
|
if (this.state.selectedAddress) {
|
|
|
|
this.props.onConfirmAddress(
|
|
|
|
this.state.selectedAddress,
|
|
|
|
this.state.selectedAddrIndex
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private selectAddress(selectedAddress, selectedAddrIndex) {
|
|
|
|
this.setState({ selectedAddress, selectedAddrIndex });
|
|
|
|
}
|
|
|
|
|
|
|
|
private nextPage = () => {
|
|
|
|
this.setState({ page: this.state.page + 1 }, this.getAddresses);
|
|
|
|
};
|
|
|
|
|
|
|
|
private prevPage = () => {
|
|
|
|
this.setState(
|
|
|
|
{ page: Math.max(this.state.page - 1, 0) },
|
|
|
|
this.getAddresses
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
private renderWalletRow(wallet) {
|
|
|
|
const { desiredToken, network } = this.props;
|
|
|
|
const { selectedAddress } = this.state;
|
|
|
|
|
|
|
|
// Get renderable values, but keep 'em short
|
|
|
|
const value = wallet.value ? wallet.value.toEther().toPrecision(4) : '';
|
|
|
|
const tokenValue = wallet.tokenValues[desiredToken]
|
|
|
|
? wallet.tokenValues[desiredToken].toPrecision(4)
|
|
|
|
: '';
|
|
|
|
|
|
|
|
return (
|
|
|
|
<tr
|
|
|
|
key={wallet.address}
|
|
|
|
onClick={this.selectAddress.bind(this, wallet.address, wallet.index)}
|
|
|
|
>
|
|
|
|
<td>
|
|
|
|
{wallet.index + 1}
|
|
|
|
</td>
|
|
|
|
<td className="DWModal-addresses-table-address">
|
|
|
|
<input
|
|
|
|
type="radio"
|
|
|
|
name="selectedAddress"
|
|
|
|
checked={selectedAddress === wallet.address}
|
|
|
|
value={wallet.address}
|
|
|
|
/>
|
|
|
|
{wallet.address}
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
{value} {network.unit}
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
{tokenValue} {desiredToken}
|
|
|
|
</td>
|
|
|
|
<td>
|
|
|
|
<a
|
|
|
|
target="_blank"
|
|
|
|
href={`https://ethplorer.io/address/${wallet.address}`}
|
|
|
|
>
|
|
|
|
<i className="DWModal-addresses-table-more" />
|
|
|
|
</a>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
}
|
2017-08-28 17:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function mapStateToProps(state) {
|
|
|
|
return {
|
|
|
|
wallets: state.deterministicWallets.wallets,
|
|
|
|
desiredToken: state.deterministicWallets.desiredToken,
|
|
|
|
network: getNetworkConfig(state),
|
|
|
|
tokens: getTokens(state)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, {
|
|
|
|
getDeterministicWallets,
|
|
|
|
setDesiredToken
|
|
|
|
})(DeterministicWalletsModal);
|