+
+
-
-
- {address.slice(0, offset)}{address.slice(offset, count + offset)}{address.slice(count + offset)}
-
-
- {hexSeed}
-
+ |
+
+
+ {address.slice(0, offset)}{address.slice(offset, count + offset)}{address.slice(count + offset)}
-
-
-
-
-
-
+ |
+
+ {hexSeed}
+ |
+
+
+
+ |
+
);
}
@@ -75,8 +78,7 @@ export default React.memo(styled(Match)`
}
.vanity--Match-addr {
- font-size: 1.5rem;
- padding: 0 1rem;
+ font-size: 1.25rem;
.no {
color: inherit;
diff --git a/packages/page-accounts/src/Vanity/index.tsx b/packages/page-accounts/src/Vanity/index.tsx
index d6b1146417..b3a8200d6f 100644
--- a/packages/page-accounts/src/Vanity/index.tsx
+++ b/packages/page-accounts/src/Vanity/index.tsx
@@ -2,38 +2,35 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
-import { I18nProps } from '@polkadot/react-components/types';
import { KeypairType } from '@polkadot/util-crypto/types';
import { GeneratorMatches, GeneratorMatch, GeneratorResult } from '@polkadot/vanitygen/types';
-import { ComponentProps } from '../types';
+import { ComponentProps as Props } from '../types';
-import React from 'react';
+import React, { useCallback, useEffect, useRef, useState } from 'react';
import styled from 'styled-components';
-import { Button, Dropdown, Input, TxComponent } from '@polkadot/react-components';
+import { Button, Dropdown, Input, Table } from '@polkadot/react-components';
+import { useIsMountedRef } from '@polkadot/react-hooks';
import uiSettings from '@polkadot/ui-settings';
import generator from '@polkadot/vanitygen/generator';
import matchRegex from '@polkadot/vanitygen/regex';
import generatorSort from '@polkadot/vanitygen/sort';
import CreateModal from '../Accounts/modals/Create';
-import translate from '../translate';
+import { useTranslation } from '../translate';
import Match from './Match';
-interface Props extends ComponentProps, I18nProps {}
-
-interface State {
- createSeed: string | null;
- elapsed: number;
+interface Match {
isMatchValid: boolean;
- isRunning: boolean;
- keyCount: 0;
- keyTime: 0;
match: string;
+}
+
+interface Results {
+ elapsed: number;
+ isRunning: boolean;
+ keyCount: number;
+ keyTime: number;
matches: GeneratorMatches;
startAt: number;
- type: KeypairType;
- withCase: boolean;
- withHex: boolean;
}
const DEFAULT_MATCH = 'Some';
@@ -42,244 +39,227 @@ const BOOL_OPTIONS = [
{ text: 'Yes', value: true }
];
-class VanityApp extends TxComponent
{
- private results: GeneratorResult[] = [];
-
- public state: State = {
- createSeed: null,
+function VanityApp ({ className, onStatusChange }: Props): React.ReactElement {
+ const { t } = useTranslation();
+ const results = useRef([]);
+ const runningRef = useRef(false);
+ const mountedRef = useIsMountedRef();
+ const [createSeed, setCreateSeed] = useState(null);
+ const [{ elapsed, isRunning, keyCount, matches }, setResults] = useState({
elapsed: 0,
- isMatchValid: true,
isRunning: false,
- keyCount: 0,
+ keyCount: -1,
keyTime: 0,
- match: DEFAULT_MATCH,
matches: [],
- startAt: 0,
- type: 'ed25519',
- withCase: true,
- withHex: true
- };
+ startAt: 0
+ });
+ const [{ isMatchValid, match }, setMatch] = useState({ isMatchValid: true, match: DEFAULT_MATCH });
+ const [type, setType] = useState('ed25519');
+ const [withCase, setWithCase] = useState(true);
- private _isActive = false;
+ const _clearSeed = useCallback(
+ () => setCreateSeed(null),
+ []
+ );
- public componentWillUnmount (): void {
- this._isActive = false;
- }
+ const _checkMatches = useCallback(
+ (): void => {
+ const checks = results.current;
- public render (): React.ReactNode {
- const { className, onStatusChange, t } = this.props;
- const { createSeed, elapsed, isMatchValid, isRunning, keyCount, match, matches, type, withCase } = this.state;
+ results.current = [];
- return (
-
-
-
-
-
-
-
-
-
-
-
- {keyCount !== 0 && (
-
- {t('Evaluated {{count}} keys in {{elapsed}}s ({{avg}} keys/s)', {
- replace: {
- avg: (keyCount / (elapsed / 1000)).toFixed(3),
- count: keyCount,
- elapsed: (elapsed / 1000).toFixed(2)
- }
- })}
-
- )}
-
- {matches.map((match): React.ReactNode => (
-
- ))}
-
- {createSeed && (
-
- )}
-
- );
- }
+ if (checks.length === 0 || !mountedRef.current) {
+ return;
+ }
- private checkMatches (): void {
- const results = this.results;
-
- this.results = [];
-
- if (results.length === 0 || !this._isActive) {
- return;
- }
-
- this.setState(
- ({ keyCount, keyTime, matches, startAt }: State): Pick => {
- let newKeyCount = keyCount;
- let newKeyTime = keyTime;
- const newMatches = results
- .reduce((result, { elapsed, found }): GeneratorMatch[] => {
+ setResults(
+ ({ isRunning, keyCount, keyTime, matches, startAt }: Results): Results => {
+ let newKeyCount = keyCount;
+ let newKeyTime = keyTime;
+ const newMatches = checks.reduce((result, { elapsed, found }): GeneratorMatch[] => {
newKeyCount += found.length;
newKeyTime += elapsed;
return result.concat(found);
- }, matches)
- .sort(generatorSort)
- .slice(0, 25);
- const elapsed = Date.now() - startAt;
+ }, matches);
- return {
- elapsed,
- keyCount: newKeyCount,
- keyTime: newKeyTime,
- matches: newMatches
- };
- }
- );
- }
-
- private executeGeneration = (): void => {
- if (!this.state.isRunning) {
- this.checkMatches();
-
- return;
- }
-
- setImmediate((): void => {
- if (this._isActive) {
- if (this.results.length === 25) {
- this.checkMatches();
+ return {
+ elapsed: Date.now() - startAt,
+ isRunning,
+ keyCount: newKeyCount,
+ keyTime: newKeyTime,
+ matches: newMatches.sort(generatorSort).slice(0, 25),
+ startAt
+ };
}
+ );
+ },
+ [mountedRef]
+ );
- const { match, type, withCase, withHex } = this.state;
-
- this.results.push(
- generator({
- match,
- runs: 10,
- type,
- withCase,
- withHex
- })
- );
-
- this.executeGeneration();
+ const _executeGeneration = useCallback(
+ (): void => {
+ if (!runningRef.current) {
+ return _checkMatches();
}
- });
- }
- private onCreateToggle = (createSeed: string): void => {
- this.setState({ createSeed });
- }
+ setImmediate((): void => {
+ if (mountedRef.current) {
+ if (results.current.length === 25) {
+ _checkMatches();
+ }
- private onChangeCase = (withCase: boolean): void => {
- this.setState({ withCase });
- }
+ results.current.push(
+ generator({ match, runs: 10, type, withCase, withHex: true })
+ );
- private onChangeMatch = (match: string): void => {
- this.setState({
+ _executeGeneration();
+ }
+ });
+ },
+ [_checkMatches, match, mountedRef, runningRef, type, withCase]
+ );
+
+ const _onChangeMatch = useCallback(
+ (match: string): void => setMatch({
isMatchValid:
matchRegex.test(match) &&
(match.length !== 0) &&
(match.length < 31),
match
- });
- }
+ }),
+ []
+ );
- private onChangeType = (type: KeypairType): void => {
- this.setState({ type });
- }
-
- private onRemove = (address: string): void => {
- this.setState(
- ({ matches }: State): Pick => ({
- matches: matches.filter((item): boolean =>
- item.address !== address
- )
+ const _onRemove = useCallback(
+ (address: string): void => setResults(
+ (results: Results): Results => ({
+ ...results,
+ matches: results.matches.filter((item) => item.address !== address)
})
- );
- }
+ ),
+ []
+ );
- private toggleStart = (): void => {
- this.setState(
- ({ isRunning, keyCount, keyTime, startAt }: State): Pick => {
- this._isActive = !isRunning;
+ const _toggleStart = useCallback(
+ (): void => setResults(
+ ({ elapsed, isRunning, keyCount, keyTime, matches, startAt }: Results): Results => ({
+ elapsed,
+ isRunning: !isRunning,
+ keyCount: isRunning ? keyCount : 0,
+ keyTime: isRunning ? keyTime : 0,
+ matches,
+ startAt: isRunning ? startAt : Date.now()
+ })
+ ),
+ []
+ );
- return {
- isRunning: this._isActive,
- keyCount: this._isActive ? 0 : keyCount,
- keyTime: this._isActive ? 0 : keyTime,
- startAt: this._isActive ? Date.now() : startAt
- };
- },
- this.executeGeneration
- );
- }
+ useEffect((): void => {
+ runningRef.current = isRunning;
- private closeCreate = (): void => {
- this.setState({ createSeed: null });
- }
+ if (isRunning) {
+ _executeGeneration();
+ }
+ }, [_executeGeneration, isRunning]);
+
+ return (
+
+
+
+
+
+
+
+
+
+
+
+ {matches.length !== 0 && (
+
+ {matches.map((match): React.ReactNode => (
+
+ ))}
+
+ )}
+ {createSeed && (
+
+ )}
+
+ );
}
-export default translate(
- styled(VanityApp)`
- .vanity--App-matches {
- padding: 1em 0;
- }
+export default React.memo(styled(VanityApp)`
+ .vanity--App-matches {
+ padding: 1em 0;
+ }
- .vanity--App-stats {
- padding: 1em 0 0 0;
- opacity: 0.45;
- text-align: center;
- }
- `
-);
+ .vanity--App-stats {
+ padding: 1em 0 0 0;
+ opacity: 0.45;
+ text-align: center;
+ }
+`);