From e2c6eda2f266ab2af00f0cfe591f892801cb49ef Mon Sep 17 00:00:00 2001 From: Antoine Estienne Date: Mon, 16 Nov 2020 13:16:34 +0100 Subject: [PATCH] Feature/add verify (#4006) * support ethereum addresses * corrections * add standalone url and ethereum identicons * add moonbeam config * added changes from the old pr * add new config * added dummy rpc method declarations for eth methods * wip fix address add modal * support ethereum addresses * corrections * add standalone url and ethereum identicons * add moonbeam config * added changes from the old pr * add new config * added dummy rpc method declarations for eth methods * wip fix address add modal * wip repair app with new keyring * use isDevelopement false for ethereum * lint * more formatng * eth addresses by default for account creation * added many more types to support moonbeam * added color * add h160 to types and components * add rpc methods to app-config * start modifying verify * solbe merge conflicts * wip * finish and cleanup * simplify verifysignature and reformat for eslint * Update packages/page-signing/src/Verify.tsx Co-authored-by: Jaco Greeff Co-authored-by: Jaco Greeff --- packages/page-signing/src/Verify.tsx | 42 ++++++++-------------------- 1 file changed, 12 insertions(+), 30 deletions(-) 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 (