From 818dd5ad47dc2063ad47d9b68b5b978a51bf6ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CArslanKibria=E2=80=9D?= <“kibriya.mushtaq@ferrum.network”> Date: Wed, 13 Sep 2023 15:51:51 +0500 Subject: [PATCH] validation function added for evm and non-evm --- src/services/cosmWasm.service.ts | 46 +++++++++++++++++++++++++++ src/services/web3.service.ts | 54 +++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/src/services/cosmWasm.service.ts b/src/services/cosmWasm.service.ts index a26749e..8fcf50e 100644 --- a/src/services/cosmWasm.service.ts +++ b/src/services/cosmWasm.service.ts @@ -4,6 +4,7 @@ const { SigningCosmWasmClient } = require('@cosmjs/cosmwasm-stargate'); import { Wallet, ethers } from 'ethers'; // import ethers from 'ethers'; import { CUDOS_CHAIN_ID } from '../constants/constants'; +import { recoverPersonalSignature } from 'eth-sig-util'; export const getTransactionReceipt = async ( txId: string, @@ -144,3 +145,48 @@ const getDestinationAmount = async (data: any) => { console.log('data.bridgeAmount', data.bridgeAmount); return data.bridgeAmount; }; + +export const validateSignature = (job: any) => { + let isValid = true; + try { + let signatures = job?.transaction?.validatorSig?.signatures; + if (signatures?.length > 0) { + for (let index = 0; index < signatures.length; index++) { + let signature = signatures[index]; + let sig = signature?.signature; + let hash = signature?.hash; + let address = signature?.address; + if (isRecoverAddressValid(sig, hash, address) == false) { + isValid = false; + } + } + } else { + isValid = false; + } + } catch (e) { + isValid = false; + } + return isValid; +}; + +export const isRecoverAddressValid = ( + signature: string, + hash: string, + publicAddress: string, +): boolean => { + try { + const bufferText = Buffer.from(hash, 'utf8'); + const data = `0x${bufferText.toString('hex')}`; + const address = recoverPersonalSignature({ + data: data, + sig: '0x' + signature, + }); + console.log('cosm public address is:::', address); + if (address.toLowerCase() == publicAddress.toLowerCase()) { + return true; + } + } catch (e) { + console.log(e); + } + return false; +}; diff --git a/src/services/web3.service.ts b/src/services/web3.service.ts index 452d62d..406a953 100644 --- a/src/services/web3.service.ts +++ b/src/services/web3.service.ts @@ -8,8 +8,17 @@ import { CUDOS_CHAIN_ID, THRESHOLD, } from '../constants/constants'; -import { ecsign, toRpcSig } from 'ethereumjs-util'; import { AbiItem } from 'web3-utils'; +import { + ecsign, + toRpcSig, + keccak, + fromRpcSig, + ecrecover, + toBuffer, + pubToAddress, + bufferToHex, +} from 'ethereumjs-util'; import { amountToHuman, amountToMachine } from '../constants/utils'; export const getTransactionReceipt = async ( @@ -291,3 +300,46 @@ const getDestinationAmount = async (data: any) => { console.log('data.bridgeAmount', data.bridgeAmount); return data.bridgeAmount; }; + +export const validateSignature = (job: any) => { + let isValid = true; + try { + let signatures = job?.transaction?.generatorSig?.signatures; + if (signatures?.length > 0) { + for (let index = 0; index < signatures.length; index++) { + let signature = signatures[index]; + let sig = signature?.signature; + let hash = signature?.hash; + let address = signature?.address; + if (isRecoverAddressValid(sig, hash, address) == false) { + isValid = false; + } + } + } else { + isValid = false; + } + } catch (e) { + isValid = false; + } + return isValid; +}; + +export const isRecoverAddressValid = ( + signature: string, + hash: string, + publicAddress: string, +): boolean => { + try { + const { v, r, s } = fromRpcSig(signature); + const pubKey = ecrecover(toBuffer(hash), v, r, s); + const addrBuf = pubToAddress(pubKey); + const address = bufferToHex(addrBuf); + console.log('public address is:::', address); + if (address.toLowerCase() == publicAddress.toLowerCase()) { + return true; + } + } catch (e) { + console.log(e); + } + return false; +};