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 <jacogr@gmail.com> Co-authored-by: Jaco Greeff <jacogr@gmail.com>
This commit is contained in:
parent
07bc02066c
commit
e2c6eda2f2
@ -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 (
|
||||
|
||||
Loading…
Reference in New Issue
Block a user