Skip to content

Commit

Permalink
Merge pull request #72 from ArslanKibria98/feature/node-infra
Browse files Browse the repository at this point in the history
validation function added for evm and non-evm
  • Loading branch information
zikriya authored Sep 13, 2023
2 parents 93dd2d6 + 818dd5a commit c67c996
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
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;
};

0 comments on commit c67c996

Please sign in to comment.