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

ether lib added to generated evm to non-evm signature #37

Merged
merged 1 commit into from
May 11, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"crypto-js": "^4.1.1",
"dotenv": "^16.0.3",
"ethereumjs-util": "^7.1.5",
"ethers": "^6.3.0",
"express": "^4.17.2",
"web3": "^1.8.1",
"web3-utils": "^1.9.0"
Expand Down
127 changes: 127 additions & 0 deletions src/services/cosmWasm.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Web3 from 'web3';
import { TransactionReceipt, Transaction } from '../interfaces';
const { SigningCosmWasmClient } = require('@cosmjs/cosmwasm-stargate');
import { Wallet, ethers } from 'ethers';
// import ethers from 'ethers';
import { CUDOS_CHAIN_ID } from '../constants/constants';

export const getTransactionReceipt = async (
txId: string,
Expand All @@ -14,3 +17,127 @@ export const getTransactionReceipt = async (
}
return transaction;
};

export const signedTransaction = async (
job: any,
decodedData: any,
transaction: any,
): Promise<any> => {
try {
const destinationAmountToMachine = await getDestinationAmount(job.data);
const txData = {
transactionHash: job.returnvalue.transactionHash,
from: transaction.from,
token: decodedData.sourceToken,
amount: decodedData.sourceAmount,
chainId: decodedData.sourceChainId,
targetChainId: decodedData.targetChainId,
targetToken: decodedData.targetToken,
targetAddress: decodedData.targetAddress,
signatures: [],
salt: '',
};

txData.salt = Web3.utils.keccak256(
txData.transactionHash.toLocaleLowerCase(),
);
const payBySig = await createSignedPayment(
txData.targetChainId,
txData.targetAddress,
destinationAmountToMachine,
txData.targetToken,
txData.salt,
job,
);

return {
...txData,
signatures: [payBySig.signatures, payBySig.signatures],
hash: 'payBySig.hash',
};
} catch (error) {
console.error('Error occured while decoding transaction', error);
}
};

const createSignedPayment = async (
chainId: string,
address: string,
amount: string,
token: string,
salt: string,
job: any,
) => {
const payBySig = produceSignatureWithdrawHash(
chainId,
token,
address,
amount,
salt,
);
console.log('hash', payBySig.hash);
const privateKey = process.env.PRIVATE_KEY as string;
let provider = ethers.getDefaultProvider(job.data.sourceRpcURL);
const wallet = new Wallet(privateKey, provider);
let signature = await wallet.signMessage(payBySig.hash);
signature = signature.replace(/^0x/, '');
payBySig.signatures = [signature];
return payBySig;
};

const produceSignatureWithdrawHash = (
chainId: string,
token: string,
payee: string,
amount: string,
salt: string,
): any => {
const hash = `{"chain_id":"${chainId}","payee":"${payee}","token":"${token}","amount":"${amount}","salt":"${salt}"}`;
return {
signatures: [],
hash,
};
};

export const getLogsFromTransactionReceipt = (job: any) => {
try {
let decodedData: any = {};
job.returnvalue.transactionHash = job?.returnvalue?.hash;
job.returnvalue.status = false;
if (job?.returnvalue?.code && job?.returnvalue?.code == 0) {
job.returnvalue.status = true;
}
let rawLogs = job?.returnvalue?.rawLog;
var logs = JSON.parse(rawLogs);
decodedData.sourceToken = filterLogsAndGetValue(logs, 'token');
decodedData.sourceAmount = filterLogsAndGetValue(logs, 'amount');
decodedData.sourceChainId = CUDOS_CHAIN_ID;
decodedData.targetChainId = filterLogsAndGetValue(logs, 'target_chain_id');
decodedData.targetToken = filterLogsAndGetValue(logs, 'target_token');
decodedData.targetAddress = filterLogsAndGetValue(logs, 'target_address');
decodedData.from = filterLogsAndGetValue(logs, 'from');
return decodedData;
} catch (error) {
console.error('Error occured while getting logs from transaction', error);
}
};

export const filterLogsAndGetValue = (logs: any, key: string) => {
if (logs?.length > 0) {
let events = logs[0].events;
for (const event of events) {
if (event?.attributes.length > 0) {
for (const attribute of event?.attributes) {
if (attribute.key === key) {
return attribute.value;
}
}
}
}
}
};

const getDestinationAmount = async (data: any) => {
console.log('data.bridgeAmount', data.bridgeAmount);
return data.bridgeAmount;
};