diff --git a/packages/page-signing/src/Verify.tsx b/packages/page-signing/src/Verify.tsx index cafbbcb632..0d1c98935d 100644 --- a/packages/page-signing/src/Verify.tsx +++ b/packages/page-signing/src/Verify.tsx @@ -6,10 +6,11 @@ import { KeypairType } from '@polkadot/util-crypto/types'; import React, { useCallback, useEffect, useState } from 'react'; import styled from 'styled-components'; import { Badge, Dropdown, Input, InputAddress, Static } from '@polkadot/react-components'; +import { useApi } from '@polkadot/react-hooks'; import keyring from '@polkadot/ui-keyring'; import uiSettings from '@polkadot/ui-settings'; import { isHex } from '@polkadot/util'; -import { naclVerify, schnorrkelVerify } from '@polkadot/util-crypto'; +import { signatureVerify } from '@polkadot/util-crypto'; import { useTranslation } from './translate'; @@ -21,6 +22,7 @@ interface Props { function Verify ({ className = '' }: Props): React.ReactElement { const { t } = useTranslation(); + const { isEthereum } = useApi(); const [{ cryptoType, isValid }, setValidity] = useState<{ cryptoType: CryptoTypes; isValid: boolean }>({ cryptoType: 'unknown', isValid: false }); const [{ data, isHexData }, setData] = useState<{ data: string; isHexData: boolean }>({ data: '', isHexData: false }); const [{ isValidPk, publicKey }, setPublicKey] = useState<{ isValidPk: boolean; publicKey: Uint8Array | null }>({ isValidPk: false, publicKey: null }); @@ -31,33 +33,13 @@ function Verify ({ className = '' }: Props): React.ReactElement { let cryptoType: CryptoTypes = 'unknown'; let isValid = isValidPk && isValidSignature; - // We cannot just use the keyring verify since it may be an address. So here we first check - // for ed25519, if not valid, we try against sr25519 - if neither are valid, well, we have - // not been able to validate the signature + // We use signatureVerify to detect validity and crypto type if (isValid && publicKey) { - let isValidSr = false; - let isValidEd = false; + const verification = signatureVerify(data, signature, publicKey); - try { - isValidEd = naclVerify(data, signature, publicKey); - } catch (error) { - // do nothing, already set to false - } - - if (isValidEd) { - cryptoType = 'ed25519'; - } else { - try { - isValidSr = schnorrkelVerify(data, signature, publicKey); - } catch (error) { - // do nothing, already set to false - } - - if (isValidSr) { - cryptoType = 'sr25519'; - } else { - isValid = false; - } + if (verification.crypto !== 'none') { + isValid = verification.isValid; + cryptoType = verification.crypto; } } @@ -74,9 +56,9 @@ function Verify ({ className = '' }: Props): React.ReactElement { console.error(err); } - setPublicKey({ isValidPk: !!publicKey && publicKey.length === 32, publicKey }); + setPublicKey({ isValidPk: !!publicKey && (publicKey.length === 32 || (isEthereum && publicKey.length === 20)), publicKey }); }, - [] + [isEthereum] ); const _onChangeData = useCallback( @@ -85,8 +67,8 @@ function Verify ({ className = '' }: Props): React.ReactElement { ); const _onChangeSignature = useCallback( - (signature: string) => setSignature({ isValidSignature: isHex(signature) && signature.length === 130, signature }), - [] + (signature: string) => setSignature({ isValidSignature: isHex(signature) && (signature.length === 130 || (isEthereum && signature.length === 132)), signature }), + [isEthereum] ); return (