From 934319cc632c37e0dc0fd6f68502027fd6cc7232 Mon Sep 17 00:00:00 2001 From: Antoine Estienne Date: Mon, 3 May 2021 06:52:11 +0000 Subject: [PATCH] Add derivation eth options (#4863) * readd eth to dev mode * added specific eth derivation path component * fix derivation and add address list * add maxindex for dropdown * optimize address list * lint * add custom index instead * cleanup and fix * Apply suggestions from code review Co-authored-by: Jaco Greeff * iterate on suggestions * Apply suggestions from code review Co-authored-by: Jaco Greeff * update * remove address generation in dropdown * lint Co-authored-by: Jaco Greeff --- packages/page-accounts/src/modals/Create.tsx | 129 +++++++----------- .../src/modals/CreateEthDerivationPath.tsx | 127 +++++++++++++++++ packages/page-accounts/src/types.ts | 33 +++++ 3 files changed, 209 insertions(+), 80 deletions(-) create mode 100644 packages/page-accounts/src/modals/CreateEthDerivationPath.tsx diff --git a/packages/page-accounts/src/modals/Create.tsx b/packages/page-accounts/src/modals/Create.tsx index b8b8006d75..e793926188 100644 --- a/packages/page-accounts/src/modals/Create.tsx +++ b/packages/page-accounts/src/modals/Create.tsx @@ -3,7 +3,7 @@ import type { ActionStatus } from '@polkadot/react-components/Status/types'; import type { CreateResult } from '@polkadot/ui-keyring/types'; -import type { ModalProps } from '../types'; +import type { AddressState, CreateOptions, CreateProps, DeriveValidationOutput, PairType, SeedType } from '../types'; import FileSaver from 'file-saver'; import React, { useCallback, useRef, useState } from 'react'; @@ -20,45 +20,11 @@ import { hdLedger, hdValidatePath, keyExtractSuri, mnemonicGenerate, mnemonicVal import { useTranslation } from '../translate'; import CreateConfirmation from './CreateConfirmation'; +import CreateEthDerivationPath, { ETH_DEFAULT_PATH } from './CreateEthDerivationPath'; import CreateSuriLedger from './CreateSuriLedger'; import ExternalWarning from './ExternalWarning'; import PasswordInput from './PasswordInput'; -const ETH_DEFAULT_PATH = "m/44'/60'/0'/0/0"; - -type PairType = 'ecdsa' | 'ed25519' | 'ed25519-ledger' | 'ethereum' | 'sr25519'; - -interface Props extends ModalProps { - className?: string; - onClose: () => void; - onStatusChange: (status: ActionStatus) => void; - seed?: string; - type?: PairType; -} - -type SeedType = 'bip' | 'raw' | 'dev'; - -interface AddressState { - address: string | null; - derivePath: string; - deriveValidation?: DeriveValidationOutput - isSeedValid: boolean; - pairType: PairType; - seed: string; - seedType: SeedType; -} - -interface CreateOptions { - genesisHash?: string; - name: string; - tags?: string[]; -} - -interface DeriveValidationOutput { - error?: string; - warning?: string; -} - const DEFAULT_PAIR_TYPE = 'sr25519'; const STEPS_COUNT = 3; @@ -199,7 +165,7 @@ function createAccount (seed: string, derivePath: string, pairType: PairType, { return status; } -function Create ({ className = '', onClose, onStatusChange, seed: propsSeed, type: propsType }: Props): React.ReactElement { +function Create ({ className = '', onClose, onStatusChange, seed: propsSeed, type: propsType }: CreateProps): React.ReactElement { const { t } = useTranslation(); const { api, isDevelopment, isEthereum } = useApi(); const { isLedgerEnabled } = useLedger(); @@ -354,22 +320,23 @@ function Create ({ className = '', onClose, onStatusChange, seed: propsSeed, typ isPadded summary={t('Advanced creation options')} > - ('If you are moving accounts between applications, ensure that you use the correct type.')}> - ('Determines what cryptography will be used to create this account. Note that to validate on Polkadot, the session account must use "ed25519".')} - label={t('keypair crypto type')} - onChange={_onChangePairType} - options={ - isEthereum - ? settings.availableCryptosEth - : isLedgerEnabled - ? settings.availableCryptosLedger - : settings.availableCryptos - } - tabIndex={-1} - /> - + { + pairType !== 'ethereum' && ('If you are moving accounts between applications, ensure that you use the correct type.')}> + ('Determines what cryptography will be used to create this account. Note that to validate on Polkadot, the session account must use "ed25519".')} + label={t('keypair crypto type')} + onChange={_onChangePairType} + options={ + isEthereum + ? settings.availableCryptosEth + : isLedgerEnabled + ? settings.availableCryptosLedger + : settings.availableCryptos + } + tabIndex={-1} + /> + } {pairType === 'ed25519-ledger' ? ( ) - : ( - ('The derivation path is only relevant when deriving keys from a mnemonic.') - : t('The derivation path allows you to create different accounts from the same base mnemonic.') - }> - {(pairType !== 'ethereum' || seedType !== 'raw') && ( + : pairType === 'ethereum' + ? ( + + ) + : ( + ('The derivation path allows you to create different accounts from the same base mnemonic.')}> ('You can set a custom derivation path for this account using the following syntax "m/////') : t('You can set a custom derivation path for this account using the following syntax "///". The "/" and "//" may be repeated and mixed`. An optional "///" can be used with a mnemonic seed, and may only be specified once.'))} - isDisabled={pairType === 'ethereum' && seedType === 'raw'} + help={(t('You can set a custom derivation path for this account using the following syntax "///". The "/" and "//" may be repeated and mixed`. An optional "///" can be used with a mnemonic seed, and may only be specified once.'))} + isDisabled={seedType === 'raw'} isError={!!deriveValidation?.error} label={t('secret derivation path')} onChange={_onChangePath} placeholder={ - pairType === 'ethereum' - ? ETH_DEFAULT_PATH - : seedType === 'raw' - ? pairType === 'sr25519' - ? t('//hard/soft') - : t('//hard') - : pairType === 'sr25519' - ? t('//hard/soft///password') - : t('//hard///password') + seedType === 'raw' + ? pairType === 'sr25519' + ? t('//hard/soft') + : t('//hard') + : pairType === 'sr25519' + ? t('//hard/soft///password') + : t('//hard///password') } tabIndex={-1} value={derivePath} /> - )} - {deriveValidation?.error && ( - - )} - {deriveValidation?.warning && ( - - )} - - )} + {deriveValidation?.error && ( + + )} + {deriveValidation?.warning && ( + + )} + + )} diff --git a/packages/page-accounts/src/modals/CreateEthDerivationPath.tsx b/packages/page-accounts/src/modals/CreateEthDerivationPath.tsx new file mode 100644 index 0000000000..d455ee71f2 --- /dev/null +++ b/packages/page-accounts/src/modals/CreateEthDerivationPath.tsx @@ -0,0 +1,127 @@ +// Copyright 2017-2021 @polkadot/app-accounts authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import BN from 'bn.js'; +import React, { ReactNode, useEffect, useRef, useState } from 'react'; + +import { Checkbox, Dropdown, Input, InputNumber, MarkError, MarkWarning, Modal } from '@polkadot/react-components'; +import { useToggle } from '@polkadot/react-hooks'; + +import { useTranslation } from '../translate'; +import { DeriveValidationOutput } from '../types'; + +interface Props { + className?: string; + onChange: (string: string) => void; + seedType: string; + derivePath: string; + deriveValidation: DeriveValidationOutput | undefined; + seed: string; +} + +export const ETH_DEFAULT_PATH = "m/44'/60'/0'/0/0"; + +function CreateEthDerivationPath ({ className, + derivePath, + deriveValidation, + onChange, + seedType }: Props): React.ReactElement { + const { t } = useTranslation(); + const [addIndex, setAddIndex] = useState(0); + const [customIndex, setCustomIndex] = useState(new BN(0)); + const [addressList] = useState<{ key: number; text: ReactNode; value: number; }[]>(new Array(10).fill(0).map((_, i) => ({ + key: i, + text: t('Address index {{index}}', { + replace: { index: i } + }), + value: i + }))); + const [useCustomPath, toggleCustomPath] = useToggle(); + const [useCustomIndex, toggleCustomIndex] = useToggle(); + + const errorIndex = useRef>({ + INVALID_DERIVATION_PATH: t('This is an invalid derivation path.'), + PASSWORD_IGNORED: t('Password are ignored for hex seed'), + SOFT_NOT_ALLOWED: t('Soft derivation paths are not allowed on ed25519'), + WARNING_SLASH_PASSWORD: t('Your password contains at least one "/" character. Disregard this warning if it is intended.') + }); + + useEffect((): void => { + onChange(`m/44'/60'/0'/0/${useCustomIndex ? Number(customIndex) : addIndex}`); + }, [customIndex, onChange, useCustomIndex, addIndex]); + + return ( + ('The derivation path is only relevant when deriving keys from a mnemonic.') + : t('The derivation path allows you to create different accounts from the same base mnemonic.') + } + > + {seedType === 'bip' + ? ( + <> +
+ {t('Use custom address index')}} + onChange={toggleCustomIndex} + value={useCustomIndex} + /> +
+ {useCustomIndex + ? ( + ('You can set a custom derivation index for this account')} + isDecimal={false} + label={t('Custom index')} + onChange={setCustomIndex} + value={customIndex} + /> + ) + : ( + + )} +
+ {t('Use custom derivation path')}} + onChange={toggleCustomPath} + value={useCustomPath} + /> +
+ {useCustomPath + ? ( + ( + 'You can set a custom derivation path for this account using the following syntax "m/////' + )} + isError={!!deriveValidation?.error} + label={t('secret derivation path')} + onChange={onChange} + placeholder={ETH_DEFAULT_PATH} + tabIndex={-1} + value={derivePath} + /> + ) + : null} + + ) + : ( + ('The derivation path is only relevant when deriving keys from a mnemonic.')} /> + )} + + {deriveValidation?.error && ( + + )} + {deriveValidation?.warning && } +
+ ); +} + +export default React.memo(CreateEthDerivationPath); diff --git a/packages/page-accounts/src/types.ts b/packages/page-accounts/src/types.ts index b58ce75925..1fd0283050 100644 --- a/packages/page-accounts/src/types.ts +++ b/packages/page-accounts/src/types.ts @@ -32,3 +32,36 @@ export interface SortedAccount { delegation?: Delegation; isFavorite: boolean; } + +export type PairType = 'ecdsa' | 'ed25519' | 'ed25519-ledger' | 'ethereum' | 'sr25519'; + +export interface CreateProps extends ModalProps { + className?: string; + onClose: () => void; + onStatusChange: (status: ActionStatus) => void; + seed?: string; + type?: PairType; +} + +export type SeedType = 'bip' | 'raw' | 'dev'; + +export interface AddressState { + address: string | null; + derivePath: string; + deriveValidation?: DeriveValidationOutput + isSeedValid: boolean; + pairType: PairType; + seed: string; + seedType: SeedType; +} + +export interface CreateOptions { + genesisHash?: string; + name: string; + tags?: string[]; +} + +export interface DeriveValidationOutput { + error?: string; + warning?: string; +}