Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validation function added for evm and non-evm #72

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/services/cosmWasm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
};
54 changes: 53 additions & 1 deletion src/services/web3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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;
};