Add Ledger account name when adding (#4496)

This commit is contained in:
Jaco Greeff 2021-01-26 14:38:11 +01:00 committed by GitHub
parent c7616ed32f
commit 9388f37ba8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,7 @@ import type { Ledger } from '@polkadot/ui-keyring';
import React, { useCallback, useRef, useState } from 'react';
import { Button, Dropdown, MarkError, Modal } from '@polkadot/react-components';
import { Button, Dropdown, Input, MarkError, Modal } from '@polkadot/react-components';
import { useApi, useLedger } from '@polkadot/react-hooks';
import { keyring } from '@polkadot/ui-keyring';
@ -25,14 +25,14 @@ interface Props {
const AVAIL = [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, accountOffset: number, addressOffset: number): Promise<void> {
async function queryLedger (api: ApiPromise, getLedger: () => Ledger, name: string, accountOffset: number, addressOffset: number): Promise<void> {
const { address } = await getLedger().getAddress(false, accountOffset, addressOffset);
keyring.addHardware(address, 'ledger', {
accountOffset,
addressOffset,
genesisHash: api.genesisHash.toHex(),
name: `ledger ${accountOffset}/${addressOffset}`
name: name || `ledger ${accountOffset}/${addressOffset}`
});
}
@ -43,6 +43,7 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement<Props>
const [accIndex, setAccIndex] = useState(0);
const [addIndex, setAddIndex] = useState(0);
const [error, setError] = useState<Error | null>(null);
const [{ isNameValid, name }, setName] = useState({ isNameValid: false, name: '' });
const [isBusy, setIsBusy] = useState(false);
const accOps = useRef(AVAIL.map((value): Option => ({
@ -55,12 +56,17 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement<Props>
value
})));
const _onChangeName = useCallback(
(name: string) => setName({ isNameValid: !!name.trim(), name }),
[]
);
const _onSave = useCallback(
(): void => {
setError(null);
setIsBusy(true);
queryLedger(api, getLedger, accIndex, addIndex)
queryLedger(api, getLedger, name, accIndex, addIndex)
.then(() => onClose())
.catch((error): void => {
console.error(error);
@ -69,7 +75,7 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement<Props>
setError(error);
});
},
[accIndex, addIndex, api, getLedger, onClose]
[accIndex, addIndex, api, getLedger, name, onClose]
);
return (
@ -79,6 +85,23 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement<Props>
size='large'
>
<Modal.Content>
<Modal.Columns>
<Modal.Column>
<Input
autoFocus
className='full'
help={t<string>('Name given to this account to uniquely identity the account to yourself.')}
isError={!isNameValid}
label={t<string>('name')}
onChange={_onChangeName}
placeholder={t<string>('account name')}
value={name}
/>
</Modal.Column>
<Modal.Column>
{t<string>('The name for this account as it will appear under your accounts.')}
</Modal.Column>
</Modal.Columns>
<Modal.Columns>
<Modal.Column>
<Dropdown
@ -90,7 +113,7 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement<Props>
/>
</Modal.Column>
<Modal.Column>
<p>{t('The account type that you wish to create. This is the top-level derivation.')}</p>
{t('The account type that you wish to create. This is the top-level derivation.')}
</Modal.Column>
</Modal.Columns>
<Modal.Columns>
@ -107,7 +130,7 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement<Props>
)}
</Modal.Column>
<Modal.Column>
<p>{t('The address index on the account that you wish to add. This is the second-level derivation.')}</p>
{t('The address index on the account that you wish to add. This is the second-level derivation.')}
</Modal.Column>
</Modal.Columns>
</Modal.Content>
@ -115,6 +138,7 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement<Props>
<Button
icon='plus'
isBusy={isBusy}
isDisabled={!isNameValid}
label={t<string>('Save')}
onClick={_onSave}
/>