Ensure hardware accounts are always tied to genesis (#4474)

* Ensure hardware accounts are always tied to genesis

* Lessen checks on phishing accounts
This commit is contained in:
Jaco Greeff 2021-01-22 14:34:58 +01:00 committed by GitHub
parent dbab2c5467
commit 2b6bd4319f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 14 deletions

View File

@ -1,12 +1,13 @@
// Copyright 2017-2021 @polkadot/app-accounts authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ApiPromise } from '@polkadot/api';
import type { Ledger } from '@polkadot/ui-keyring';
import React, { useCallback, useRef, useState } from 'react';
import { Button, Dropdown, MarkError, Modal } from '@polkadot/react-components';
import { useLedger } from '@polkadot/react-hooks';
import { useApi, useLedger } from '@polkadot/react-hooks';
import { keyring } from '@polkadot/ui-keyring';
import { useTranslation } from '../translate';
@ -24,14 +25,20 @@ 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 (getLedger: () => Ledger, accountOffset: number, addressOffset: number): Promise<void> {
async function queryLedger (api: ApiPromise, getLedger: () => Ledger, accountOffset: number, addressOffset: number): Promise<void> {
const { address } = await getLedger().getAddress(false, accountOffset, addressOffset);
keyring.addHardware(address, 'ledger', { accountOffset, addressOffset, name: `ledger ${accountOffset}/${addressOffset}` });
keyring.addHardware(address, 'ledger', {
accountOffset,
addressOffset,
genesisHash: api.genesisHash.toHex(),
name: `ledger ${accountOffset}/${addressOffset}`
});
}
function LedgerModal ({ className, onClose }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const { getLedger } = useLedger();
const [accIndex, setAccIndex] = useState(0);
const [addIndex, setAddIndex] = useState(0);
@ -53,7 +60,7 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement<Props>
setError(null);
setIsBusy(true);
queryLedger(getLedger, accIndex, addIndex)
queryLedger(api, getLedger, accIndex, addIndex)
.then(() => onClose())
.catch((error): void => {
console.error(error);
@ -62,7 +69,7 @@ function LedgerModal ({ className, onClose }: Props): React.ReactElement<Props>
setError(error);
});
},
[accIndex, addIndex, getLedger, onClose]
[accIndex, addIndex, api, getLedger, onClose]
);
return (

View File

@ -27,11 +27,13 @@ function isRefcount (accountInfo: AccountInfoWithProviders | AccountInfoWithRefC
return !!(accountInfo as AccountInfoWithRefCount).refcount;
}
async function checkPhishing (senderId: string | null, recipientId: string | null): Promise<[string | null, string | null]> {
async function checkPhishing (_senderId: string | null, recipientId: string | null): Promise<[string | null, string | null]> {
return [
senderId
? await checkAddress(senderId)
: null,
// not being checked atm
// senderId
// ? await checkAddress(senderId)
// : null,
null,
recipientId
? await checkAddress(recipientId)
: null
@ -48,7 +50,7 @@ function Transfer ({ className = '', onClose, recipientId: propRecipientId, send
const [[maxTransfer, noFees], setMaxTransfer] = useState<[BN | null, boolean]>([null, false]);
const [recipientId, setRecipientId] = useState<string | null>(propRecipientId || null);
const [senderId, setSenderId] = useState<string | null>(propSenderId || null);
const [[senderPhish, recipientPhish], setPhishing] = useState<[string | null, string | null]>([null, null]);
const [[, recipientPhish], setPhishing] = useState<[string | null, string | null]>([null, null]);
const balances = useCall<DeriveBalancesAll>(api.derive.balances.all, [senderId]);
const accountInfo = useCall<AccountInfoWithProviders | AccountInfoWithRefCount>(api.query.system.account, [senderId]);
@ -115,9 +117,6 @@ function Transfer ({ className = '', onClose, recipientId: propRecipientId, send
onChange={setSenderId}
type='account'
/>
{senderPhish && (
<MarkError content={t<string>('The sender is associated with a known phishing site, {{url}}', { replace: { url: senderPhish } })} />
)}
</Modal.Column>
<Modal.Column>
<p>{t<string>('The transferred balance will be subtracted (along with fees) from the sender account.')}</p>
@ -217,7 +216,7 @@ function Transfer ({ className = '', onClose, recipientId: propRecipientId, send
<TxButton
accountId={senderId}
icon='paper-plane'
isDisabled={!hasAvailable || !recipientId || !amount || !!senderPhish || !!recipientPhish}
isDisabled={!hasAvailable || !recipientId || !amount || !!recipientPhish}
label={t<string>('Make Transfer')}
onStart={onClose}
params={