Ledger derive compat option (#4529)

* Ledger derive compat option

* Hook in Ledger path

* Derive operational

* Bump util

* Use hdLedger from util-crypto

* Optional approach via fill
This commit is contained in:
Jaco Greeff 2021-02-05 10:18:02 +01:00 committed by GitHub
parent 4fd5b8becb
commit cab7d05f36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 226 additions and 97 deletions

View File

@ -4,33 +4,35 @@
import type { ActionStatus } from '@polkadot/react-components/Status/types';
import type { ThemeProps } from '@polkadot/react-components/types';
import type { CreateResult } from '@polkadot/ui-keyring/types';
import type { KeypairType } from '@polkadot/util-crypto/types';
import type { ModalProps } from '../types';
import FileSaver from 'file-saver';
import React, { useCallback, useMemo, useState } from 'react';
import React, { useCallback, useRef, useState } from 'react';
import styled from 'styled-components';
import { DEV_PHRASE } from '@polkadot/keyring/defaults';
import { getEnvironment } from '@polkadot/react-api/util';
import { AddressRow, Button, Checkbox, CopyButton, Dropdown, Expander, Input, InputAddress, MarkError, MarkWarning, Modal, TextArea } from '@polkadot/react-components';
import { useApi } from '@polkadot/react-hooks';
import { useApi, useLedger } from '@polkadot/react-hooks';
import { keyring } from '@polkadot/ui-keyring';
import { settings } from '@polkadot/ui-settings';
import { isHex, u8aToHex } from '@polkadot/util';
import { keyExtractSuri, mnemonicGenerate, mnemonicValidate, randomAsU8a } from '@polkadot/util-crypto';
import { hdLedger, keyExtractSuri, mnemonicGenerate, mnemonicValidate, randomAsU8a } from '@polkadot/util-crypto';
import { useTranslation } from '../translate';
import CreateConfirmation from './CreateConfirmation';
import CreateSuriLedger from './CreateSuriLedger';
import ExternalWarning from './ExternalWarning';
import PasswordInput from './PasswordInput';
type PairType = 'ecdsa' | 'ed25519' | 'ed25519-ledger' | 'ethereum' | 'sr25519';
interface Props extends ModalProps {
className?: string;
onClose: () => void;
onStatusChange: (status: ActionStatus) => void;
seed?: string;
type?: KeypairType;
type?: PairType;
}
type SeedType = 'bip' | 'raw' | 'dev';
@ -40,7 +42,7 @@ interface AddressState {
derivePath: string;
deriveValidation? : DeriveValidationOutput
isSeedValid: boolean;
pairType: KeypairType;
pairType: PairType;
seed: string;
seedType: SeedType;
}
@ -59,7 +61,13 @@ interface DeriveValidationOutput {
const DEFAULT_PAIR_TYPE = 'sr25519';
const STEPS_COUNT = 3;
function deriveValidate (seed: string, seedType: SeedType, derivePath: string, pairType: KeypairType): DeriveValidationOutput {
function getSuri (seed: string, derivePath: string, pairType: PairType): string {
return pairType === 'ed25519-ledger'
? u8aToHex(hdLedger(seed, derivePath).secretKey.slice(0, 32))
: `${seed}${derivePath}`;
}
function deriveValidate (seed: string, seedType: SeedType, derivePath: string, pairType: PairType): DeriveValidationOutput {
try {
const { password, path } = keyExtractSuri(`${seed}${derivePath}`);
let result: DeriveValidationOutput = {};
@ -93,9 +101,9 @@ function rawValidate (seed: string): boolean {
return ((seed.length > 0) && (seed.length <= 32)) || isHexSeed(seed);
}
function addressFromSeed (phrase: string, derivePath: string, pairType: KeypairType): string {
function addressFromSeed (seed: string, derivePath: string, pairType: PairType): string {
return keyring
.createFromUri(`${phrase.trim()}${derivePath}`, {}, pairType)
.createFromUri(getSuri(seed, derivePath, pairType), {}, pairType === 'ed25519-ledger' ? 'ed25519' : pairType)
.address;
}
@ -110,7 +118,7 @@ function newSeed (seed: string | undefined | null, seedType: SeedType): string {
}
}
function generateSeed (_seed: string | undefined | null, derivePath: string, seedType: SeedType, pairType: KeypairType = DEFAULT_PAIR_TYPE): AddressState {
function generateSeed (_seed: string | undefined | null, derivePath: string, seedType: SeedType, pairType: PairType = DEFAULT_PAIR_TYPE): AddressState {
const seed = newSeed(_seed, seedType);
const address = addressFromSeed(seed, derivePath, pairType);
@ -125,8 +133,9 @@ function generateSeed (_seed: string | undefined | null, derivePath: string, see
};
}
function updateAddress (seed: string, derivePath: string, seedType: SeedType, pairType: KeypairType): AddressState {
function updateAddress (seed: string, derivePath: string, seedType: SeedType, pairType: PairType): AddressState {
const deriveValidation = deriveValidate(seed, seedType, derivePath, pairType);
let isSeedValid = seedType === 'raw'
? rawValidate(seed)
: mnemonicValidate(seed);
@ -136,6 +145,8 @@ function updateAddress (seed: string, derivePath: string, seedType: SeedType, pa
try {
address = addressFromSeed(seed, derivePath, pairType);
} catch (error) {
console.error(error);
isSeedValid = false;
}
}
@ -157,12 +168,12 @@ export function downloadAccount ({ json, pair }: CreateResult): void {
FileSaver.saveAs(blob, `${pair.address}.json`);
}
function createAccount (suri: string, pairType: KeypairType, { genesisHash, name, tags = [] }: CreateOptions, password: string, success: string): ActionStatus {
function createAccount (seed: string, derivePath: string, pairType: PairType, { genesisHash, name, tags = [] }: CreateOptions, password: string, success: string): ActionStatus {
// we will fill in all the details below
const status = { action: 'create' } as ActionStatus;
try {
const result = keyring.addUri(suri, password, { genesisHash, isHardware: false, name, tags }, pairType);
const result = keyring.addUri(getSuri(seed, derivePath, pairType), password, { genesisHash, isHardware: false, name, tags }, pairType === 'ed25519-ledger' ? 'ed25519' : pairType);
const { address } = result.pair;
status.account = address;
@ -185,6 +196,7 @@ function createAccount (suri: string, pairType: KeypairType, { genesisHash, name
function Create ({ className = '', onClose, onStatusChange, seed: propsSeed, type: propsType }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api, isDevelopment, isEthereum } = useApi();
const { isLedgerEnabled } = useLedger();
const [{ address, derivePath, deriveValidation, isSeedValid, pairType, seed, seedType }, setAddress] = useState<AddressState>(() => generateSeed(propsSeed, '', propsSeed ? 'raw' : 'bip', isEthereum ? 'ethereum' : propsType));
const [isMnemonicSaved, setIsMnemonicSaved] = useState<boolean>(false);
const [step, setStep] = useState(1);
@ -194,34 +206,43 @@ function Create ({ className = '', onClose, onStatusChange, seed: propsSeed, typ
const isFirstStepValid = !!address && isMnemonicSaved && !deriveValidation?.error && isSeedValid;
const isSecondStepValid = isNameValid && isPasswordValid;
const isValid = isFirstStepValid && isSecondStepValid;
const errorIndex: Record<string, string> = useMemo(() => ({
const errorIndex = useRef<Record<string, string>>({
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.')
}), [t]);
});
const seedOpt = useMemo(() => (
const seedOpt = useRef((
isDevelopment
? [{ text: t<string>('Development'), value: 'dev' }]
: []
).concat(
{ text: t<string>('Mnemonic'), value: 'bip' },
isEthereum ? { text: t<string>('Private Key'), value: 'raw' } : { text: t<string>('Raw seed'), value: 'raw' }
), [isEthereum, isDevelopment, t]);
isEthereum
? { text: t<string>('Private Key'), value: 'raw' }
: { text: t<string>('Raw seed'), value: 'raw' }
));
const _onChangeDerive = useCallback(
(newDerivePath: string) => setAddress(updateAddress(seed, newDerivePath, seedType, pairType)),
const _onChangePath = useCallback(
(newDerivePath: string) => setAddress(
updateAddress(seed, newDerivePath, seedType, pairType)
),
[pairType, seed, seedType]
);
const _onChangeSeed = useCallback(
(newSeed: string) => setAddress(updateAddress(newSeed, derivePath, seedType, pairType)),
(newSeed: string) => setAddress(
updateAddress(newSeed, derivePath, seedType, pairType)
),
[derivePath, pairType, seedType]
);
const _onChangePairType = useCallback(
(newPairType: KeypairType) => setAddress(updateAddress(seed, derivePath, seedType, newPairType)),
[derivePath, seed, seedType]
(newPairType: PairType) => setAddress(
updateAddress(seed, '', seedType, newPairType)
),
[seed, seedType]
);
const _selectSeedType = useCallback(
@ -264,8 +285,8 @@ function Create ({ className = '', onClose, onStatusChange, seed: propsSeed, typ
setIsBusy(true);
setTimeout((): void => {
const options = { genesisHash: isDevelopment ? undefined : api.genesisHash.toString(), name: name.trim() };
const status = createAccount(`${seed}${derivePath}`, pairType, options, password, t<string>('created account'));
const options = { genesisHash: isDevelopment ? undefined : api.genesisHash.toString(), isHardware: false, name: name.trim() };
const status = createAccount(seed, derivePath, pairType, options, password, t<string>('created account'));
onStatusChange(status);
setIsBusy(false);
@ -320,7 +341,7 @@ function Create ({ className = '', onClose, onStatusChange, seed: propsSeed, typ
defaultValue={seedType}
isButton
onChange={_selectSeedType}
options={seedOpt}
options={seedOpt.current}
/>
<CopyButton
className='copyMoved'
@ -345,7 +366,13 @@ function Create ({ className = '', onClose, onStatusChange, seed: propsSeed, typ
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 : settings.availableCryptos}
options={
isEthereum
? settings.availableCryptosEth
: isLedgerEnabled
? settings.availableCryptosLedger
: settings.availableCryptos
}
tabIndex={-1}
/>
</Modal.Column>
@ -353,37 +380,45 @@ function Create ({ className = '', onClose, onStatusChange, seed: propsSeed, typ
<p>{t<string>('If you are moving accounts between applications, ensure that you use the correct type.')}</p>
</Modal.Column>
</Modal.Columns>
<Modal.Columns>
<Modal.Column>
<Input
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.')}
isError={!!deriveValidation?.error}
label={t<string>('secret derivation path')}
onChange={_onChangeDerive}
onEnter={_onCommit}
placeholder={
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}
{pairType === 'ed25519-ledger'
? (
<CreateSuriLedger
onChange={_onChangePath}
seedType={seedType}
/>
{deriveValidation?.error && (
<MarkError content={errorIndex[deriveValidation.error] || deriveValidation.error} />
)}
{deriveValidation?.warning && (
<MarkWarning content={errorIndex[deriveValidation.warning]} />
)}
</Modal.Column>
<Modal.Column>
<p>{t<string>('The derivation path allows you to create different accounts from the same base mnemonic.')}</p>
</Modal.Column>
</Modal.Columns>
)
: (
<Modal.Columns>
<Modal.Column>
<Input
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.')}
isError={!!deriveValidation?.error}
label={t<string>('secret derivation path')}
onChange={_onChangePath}
placeholder={
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.Column>
<Modal.Column>
<p>{t<string>('The derivation path allows you to create different accounts from the same base mnemonic.')}</p>
</Modal.Column>
</Modal.Columns>
)}
</Expander>
<Modal.Columns>
<Modal.Column>
@ -426,51 +461,58 @@ function Create ({ className = '', onClose, onStatusChange, seed: propsSeed, typ
</Modal.Column>
</Modal.Columns>
</>}
{step === 3 && address && <CreateConfirmation
derivePath={derivePath}
isBusy={isBusy}
pairType={pairType}
seed={seed}
/>
}
{step === 3 && address && (
<CreateConfirmation
derivePath={derivePath}
isBusy={isBusy}
pairType={
pairType === 'ed25519-ledger'
? 'ed25519'
: pairType
}
seed={seed}
/>
)}
</Modal.Content>
<Modal.Actions onCancel={onClose}>
{step === 1 &&
<Button
icon='step-forward'
isDisabled={!isFirstStepValid}
label={t<string>('Next')}
onClick={_nextStep}
/>
}
{step === 2 && (
<>
<Button
icon='step-backward'
label={t<string>('Prev')}
onClick={_previousStep}
/>
<Button
icon='step-forward'
isDisabled={!isFirstStepValid}
isDisabled={!isSecondStepValid}
label={t<string>('Next')}
onClick={_nextStep}
/>
}
{step === 2 &&
<><Button
icon='step-backward'
label={t<string>('Prev')}
onClick={_previousStep}
/>
<Button
icon='step-forward'
isDisabled={!isSecondStepValid}
label={t<string>('Next')}
onClick={_nextStep}
/>
</>}
{step === 3 &&
<>
<Button
icon='step-backward'
label={t<string>('Prev')}
onClick={_previousStep}
/>
<Button
icon='plus'
isBusy={isBusy}
label={t<string>('Save')}
onClick={_onCommit}
/>
</>
}
</>
)}
{step === 3 && (
<>
<Button
icon='step-backward'
label={t<string>('Prev')}
onClick={_previousStep}
/>
<Button
icon='plus'
isBusy={isBusy}
label={t<string>('Save')}
onClick={_onCommit}
/>
</>
)}
</Modal.Actions>
</Modal>
);

View File

@ -0,0 +1,86 @@
// Copyright 2017-2021 @polkadot/app-accounts authors & contributors
// SPDX-License-Identifier: Apache-2.0
import React, { useEffect, useRef, useState } from 'react';
import networks from '@polkadot/networks';
import { Dropdown, MarkError, Modal } from '@polkadot/react-components';
import { useTranslation } from '../translate';
import { AVAIL_INDEXES } from './Ledger';
interface Props {
className?: string;
onChange: (string: string) => void;
seedType: string;
}
const ledgerNets = networks.filter(({ hasLedgerSupport }) => hasLedgerSupport);
function CreateSuriLedger ({ className, onChange, seedType }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const [accIndex, setAccIndex] = useState(0);
const [addIndex, setAddIndex] = useState(0);
const [chainType, setChainType] = useState('polkadot');
const netOpts = useRef(ledgerNets.map(({ displayName, network }) => ({
text: displayName,
value: network
})));
const accOps = useRef(AVAIL_INDEXES.map((value) => ({
text: t('Account type {{index}}', { replace: { index: value } }),
value
})));
const addOps = useRef(AVAIL_INDEXES.map((value) => ({
text: t('Address index {{index}}', { replace: { index: value } }),
value
})));
useEffect((): void => {
const network = ledgerNets.find(({ network }) => network === chainType);
onChange(`m/44'/${network?.slip44 as number}'/${accIndex}'/0'/${addIndex}'`);
}, [accIndex, addIndex, chainType, onChange]);
return (
<Modal.Columns className={className}>
<Modal.Column>
{seedType === 'bip'
? (
<>
<Dropdown
help={t('The network to derive on')}
label={t('Ledger app type (originated from)')}
onChange={setChainType}
options={netOpts.current}
value={chainType}
/>
<Dropdown
help={t('The account type (derivation) to use')}
label={t('account type')}
onChange={setAccIndex}
options={accOps.current}
value={accIndex}
/>
<Dropdown
help={t('The address index (derivation on account) to use')}
label={t('address index')}
onChange={setAddIndex}
options={addOps.current}
value={addIndex}
/>
</>
)
: <MarkError content={t<string>('Derivation for Ledger-type accounts are only available on mnemonic seeds.')} />
}
</Modal.Column>
<Modal.Column>
<p>{t<string>('The derivation will be constructed from the values you specify.')}</p>
</Modal.Column>
</Modal.Columns>
);
}
export default React.memo(CreateSuriLedger);

View File

@ -22,7 +22,8 @@ interface Props {
onClose: () => void;
}
const AVAIL = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19];
// new Array(20).fill(0).map((_, index) => index)
export const AVAIL_INDEXES = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19];
// query the ledger for the address, adding it to the keyring
async function queryLedger (api: ApiPromise, getLedger: () => Ledger, name: string, accountOffset: number, addressOffset: number): Promise<void> {
@ -46,12 +47,12 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement<Props>
const [{ isNameValid, name }, setName] = useState({ isNameValid: false, name: '' });
const [isBusy, setIsBusy] = useState(false);
const accOps = useRef(AVAIL.map((value): Option => ({
const accOps = useRef(AVAIL_INDEXES.map((value): Option => ({
text: t('Account type {{index}}', { replace: { index: value } }),
value
})));
const addOps = useRef(AVAIL.map((value): Option => ({
const addOps = useRef(AVAIL_INDEXES.map((value): Option => ({
text: t('Address index {{index}}', { replace: { index: value } }),
value
})));