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 <jacogr@gmail.com> * iterate on suggestions * Apply suggestions from code review Co-authored-by: Jaco Greeff <jacogr@gmail.com> * update * remove address generation in dropdown * lint Co-authored-by: Jaco Greeff <jacogr@gmail.com>
This commit is contained in:
parent
00dbb0ae6c
commit
934319cc63
@ -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<Props> {
|
||||
function Create ({ className = '', onClose, onStatusChange, seed: propsSeed, type: propsType }: CreateProps): React.ReactElement<CreateProps> {
|
||||
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<string>('Advanced creation options')}
|
||||
>
|
||||
<Modal.Columns hint={t<string>('If you are moving accounts between applications, ensure that you use the correct type.')}>
|
||||
<Dropdown
|
||||
defaultValue={pairType}
|
||||
help={t<string>('Determines what cryptography will be used to create this account. Note that to validate on Polkadot, the session account must use "ed25519".')}
|
||||
label={t<string>('keypair crypto type')}
|
||||
onChange={_onChangePairType}
|
||||
options={
|
||||
isEthereum
|
||||
? settings.availableCryptosEth
|
||||
: isLedgerEnabled
|
||||
? settings.availableCryptosLedger
|
||||
: settings.availableCryptos
|
||||
}
|
||||
tabIndex={-1}
|
||||
/>
|
||||
</Modal.Columns>
|
||||
{
|
||||
pairType !== 'ethereum' && <Modal.Columns hint={t<string>('If you are moving accounts between applications, ensure that you use the correct type.')}>
|
||||
<Dropdown
|
||||
defaultValue={pairType}
|
||||
help={t<string>('Determines what cryptography will be used to create this account. Note that to validate on Polkadot, the session account must use "ed25519".')}
|
||||
label={t<string>('keypair crypto type')}
|
||||
onChange={_onChangePairType}
|
||||
options={
|
||||
isEthereum
|
||||
? settings.availableCryptosEth
|
||||
: isLedgerEnabled
|
||||
? settings.availableCryptosLedger
|
||||
: settings.availableCryptos
|
||||
}
|
||||
tabIndex={-1}
|
||||
/>
|
||||
</Modal.Columns>}
|
||||
{pairType === 'ed25519-ledger'
|
||||
? (
|
||||
<CreateSuriLedger
|
||||
@ -377,42 +344,44 @@ function Create ({ className = '', onClose, onStatusChange, seed: propsSeed, typ
|
||||
seedType={seedType}
|
||||
/>
|
||||
)
|
||||
: (
|
||||
<Modal.Columns hint={
|
||||
pairType === 'ethereum' && seedType === 'raw'
|
||||
? t<string>('The derivation path is only relevant when deriving keys from a mnemonic.')
|
||||
: t<string>('The derivation path allows you to create different accounts from the same base mnemonic.')
|
||||
}>
|
||||
{(pairType !== 'ethereum' || seedType !== 'raw') && (
|
||||
: pairType === 'ethereum'
|
||||
? (
|
||||
<CreateEthDerivationPath
|
||||
derivePath={derivePath}
|
||||
deriveValidation={deriveValidation}
|
||||
onChange={_onChangePath}
|
||||
seed={seed}
|
||||
seedType={seedType}
|
||||
/>
|
||||
)
|
||||
: (
|
||||
<Modal.Columns hint={t<string>('The derivation path allows you to create different accounts from the same base mnemonic.')}>
|
||||
<Input
|
||||
help={(pairType === 'ethereum' ? t<string>('You can set a custom derivation path for this account using the following syntax "m/<purpose>/<coin_type>/<account>/<change>/<address_index>') : t<string>('You can set a custom derivation path for this account using the following syntax "/<soft-key>//<hard-key>". The "/<soft-key>" and "//<hard-key>" may be repeated and mixed`. An optional "///<password>" can be used with a mnemonic seed, and may only be specified once.'))}
|
||||
isDisabled={pairType === 'ethereum' && seedType === 'raw'}
|
||||
help={(t<string>('You can set a custom derivation path for this account using the following syntax "/<soft-key>//<hard-key>". The "/<soft-key>" and "//<hard-key>" may be repeated and mixed`. An optional "///<password>" can be used with a mnemonic seed, and may only be specified once.'))}
|
||||
isDisabled={seedType === 'raw'}
|
||||
isError={!!deriveValidation?.error}
|
||||
label={t<string>('secret derivation path')}
|
||||
onChange={_onChangePath}
|
||||
placeholder={
|
||||
pairType === 'ethereum'
|
||||
? ETH_DEFAULT_PATH
|
||||
: seedType === 'raw'
|
||||
? pairType === 'sr25519'
|
||||
? t<string>('//hard/soft')
|
||||
: t<string>('//hard')
|
||||
: pairType === 'sr25519'
|
||||
? t<string>('//hard/soft///password')
|
||||
: t<string>('//hard///password')
|
||||
seedType === 'raw'
|
||||
? pairType === 'sr25519'
|
||||
? t<string>('//hard/soft')
|
||||
: t<string>('//hard')
|
||||
: pairType === 'sr25519'
|
||||
? t<string>('//hard/soft///password')
|
||||
: t<string>('//hard///password')
|
||||
}
|
||||
tabIndex={-1}
|
||||
value={derivePath}
|
||||
/>
|
||||
)}
|
||||
{deriveValidation?.error && (
|
||||
<MarkError content={errorIndex.current[deriveValidation.error] || deriveValidation.error} />
|
||||
)}
|
||||
{deriveValidation?.warning && (
|
||||
<MarkWarning content={errorIndex.current[deriveValidation.warning]} />
|
||||
)}
|
||||
</Modal.Columns>
|
||||
)}
|
||||
{deriveValidation?.error && (
|
||||
<MarkError content={errorIndex.current[deriveValidation.error] || deriveValidation.error} />
|
||||
)}
|
||||
{deriveValidation?.warning && (
|
||||
<MarkWarning content={errorIndex.current[deriveValidation.warning]} />
|
||||
)}
|
||||
</Modal.Columns>
|
||||
)}
|
||||
</Expander>
|
||||
<Modal.Columns>
|
||||
<ExternalWarning />
|
||||
|
||||
127
packages/page-accounts/src/modals/CreateEthDerivationPath.tsx
Normal file
127
packages/page-accounts/src/modals/CreateEthDerivationPath.tsx
Normal file
@ -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<Props> {
|
||||
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<Record<string, string>>({
|
||||
INVALID_DERIVATION_PATH: t<string>('This is an invalid derivation path.'),
|
||||
PASSWORD_IGNORED: t<string>('Password are ignored for hex seed'),
|
||||
SOFT_NOT_ALLOWED: t<string>('Soft derivation paths are not allowed on ed25519'),
|
||||
WARNING_SLASH_PASSWORD: t<string>('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 (
|
||||
<Modal.Columns
|
||||
className={className}
|
||||
hint={
|
||||
seedType === 'raw'
|
||||
? t<string>('The derivation path is only relevant when deriving keys from a mnemonic.')
|
||||
: t<string>('The derivation path allows you to create different accounts from the same base mnemonic.')
|
||||
}
|
||||
>
|
||||
{seedType === 'bip'
|
||||
? (
|
||||
<>
|
||||
<div className='saveToggle'>
|
||||
<Checkbox
|
||||
label={<>{t<string>('Use custom address index')}</>}
|
||||
onChange={toggleCustomIndex}
|
||||
value={useCustomIndex}
|
||||
/>
|
||||
</div>
|
||||
{useCustomIndex
|
||||
? (
|
||||
<InputNumber
|
||||
help={t<string>('You can set a custom derivation index for this account')}
|
||||
isDecimal={false}
|
||||
label={t<string>('Custom index')}
|
||||
onChange={setCustomIndex}
|
||||
value={customIndex}
|
||||
/>
|
||||
)
|
||||
: (
|
||||
<Dropdown
|
||||
help={t('The address index (derivation on account) to use')}
|
||||
label={t('address index')}
|
||||
onChange={setAddIndex}
|
||||
options={addressList}
|
||||
value={addIndex}
|
||||
/>
|
||||
)}
|
||||
<div className='saveToggle'>
|
||||
<Checkbox
|
||||
label={<>{t<string>('Use custom derivation path')}</>}
|
||||
onChange={toggleCustomPath}
|
||||
value={useCustomPath}
|
||||
/>
|
||||
</div>
|
||||
{useCustomPath
|
||||
? (
|
||||
<Input
|
||||
help={t<string>(
|
||||
'You can set a custom derivation path for this account using the following syntax "m/<purpose>/<coin_type>/<account>/<change>/<address_index>'
|
||||
)}
|
||||
isError={!!deriveValidation?.error}
|
||||
label={t<string>('secret derivation path')}
|
||||
onChange={onChange}
|
||||
placeholder={ETH_DEFAULT_PATH}
|
||||
tabIndex={-1}
|
||||
value={derivePath}
|
||||
/>
|
||||
)
|
||||
: null}
|
||||
</>
|
||||
)
|
||||
: (
|
||||
<MarkWarning content={t<string>('The derivation path is only relevant when deriving keys from a mnemonic.')} />
|
||||
)}
|
||||
|
||||
{deriveValidation?.error && (
|
||||
<MarkError content={errorIndex.current[deriveValidation.error] || deriveValidation.error} />
|
||||
)}
|
||||
{deriveValidation?.warning && <MarkWarning content={errorIndex.current[deriveValidation.warning]} />}
|
||||
</Modal.Columns>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(CreateEthDerivationPath);
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user