Skip to content

Commit

Permalink
fix: recover evm tx address sender from cosmos tx
Browse files Browse the repository at this point in the history
  • Loading branch information
fibonacci998 committed Jun 14, 2024
1 parent 1f0cfc4 commit 297762b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
64 changes: 62 additions & 2 deletions src/services/crawl-tx/crawl_tx.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import {
import { HttpBatchClient } from '@cosmjs/tendermint-rpc';
import { createJsonRpcRequest } from '@cosmjs/tendermint-rpc/build/jsonrpc';
import { decodeTxRaw } from '@cosmjs/proto-signing';
import { toBase64, fromBase64 } from '@cosmjs/encoding';
import { toBase64, fromBase64, toHex } from '@cosmjs/encoding';
import { Knex } from 'knex';
import { Queue } from 'bullmq';
import { GetNodeInfoResponseSDKType } from '@aura-nw/aurajs/types/codegen/cosmos/base/tendermint/v1beta1/query';
import _ from 'lodash';
import {
TransactionSerializable,
recoverTransactionAddress,
serializeTransaction,
} from 'viem';
import Utils from '../../common/utils/utils';
import {
BULL_JOB_NAME,
Expand All @@ -31,6 +36,8 @@ import config from '../../../config.json' assert { type: 'json' };
import knex from '../../common/utils/db_connection';
import ChainRegistry from '../../common/utils/chain.registry';
import { getProviderRegistry } from '../../common/utils/provider.registry';
import { MSG_TYPE } from '../evm/constant';
import { convertEthAddressToBech32Address } from '../evm/utils';

@Service({
name: SERVICE.V1.CrawlTransaction.key,
Expand Down Expand Up @@ -416,14 +423,23 @@ export default class CrawlTxService extends BullableService {
block_height: tx.height,
source: Event.SOURCE.TX_EVENT,
})) ?? [];
const msgInsert =
const msgInsert: TransactionMessage[] =
rawLogTx.tx.body.messages.map((message: any, index: any) => ({
tx_id: tx.id,
sender,
index,
type: message['@type'],
content: message,
})) ?? [];
this.updateSenderInEVMTx(
msgInsert.filter((msg) =>
[
MSG_TYPE.MSG_ETHEREUM_TX,
MSG_TYPE.MSG_DYNAMIC_FEE_TX,
MSG_TYPE.MSG_LEGACY_TX,
].includes(msg.type)
)
);
listEventModel.push(...eventInsert);
listMsgModel.push(...msgInsert);
});
Expand Down Expand Up @@ -579,6 +595,50 @@ export default class CrawlTxService extends BullableService {
}
}

public async updateSenderInEVMTx(
listTransactionMessage: TransactionMessage[]
) {
await Promise.all(
listTransactionMessage.map(async (txmsg: TransactionMessage) => {
const { content } = txmsg;
const evmTransaction = {
chainId: parseInt(content.data.chain_id, 10),
maxFeePerGas: BigInt(content.data.gas_fee_cap),
maxPriorityFeePerGas: BigInt(content.data.gas_tip_cap),
to: content.data.to,
value: BigInt(content.data.value),
data: `0x${toHex(fromBase64(content.data.data))}`,
nonce: parseInt(content.data.nonce, 10),
gas: BigInt(content.data.gas),
accessList: content.data.accesses.map((accessElement: any) => ({
address: accessElement.address,
storageKeys: accessElement.storage_keys,
})),
} as const satisfies TransactionSerializable;

const recovedAddress = await recoverTransactionAddress({
serializedTransaction: serializeTransaction(evmTransaction),
signature: {
r: `0x${toHex(fromBase64(content.data.r))}`,
s: `0x${toHex(fromBase64(content.data.s))}`,
v: content.data.v ? BigInt(1) : BigInt(0),
},
});
const bech32RecovedAddress = convertEthAddressToBech32Address(
config.networkPrefixAddress,
recovedAddress
);
if (txmsg.sender !== bech32RecovedAddress) {
this.logger.warn(`Sender not match in evm tx ${content.hash}`);
this.logger.warn(`Currently: ${txmsg.sender}`);
this.logger.warn(`Recover: ${bech32RecovedAddress}`);
// eslint-disable-next-line no-param-reassign
txmsg.sender = bech32RecovedAddress;
}
})
);
}

public async _start() {
const providerRegistry = await getProviderRegistry();
this._registry = new ChainRegistry(this.logger, providerRegistry);
Expand Down
2 changes: 2 additions & 0 deletions src/services/evm/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,6 @@ export const BULL_JOB_NAME = {

export const MSG_TYPE = {
MSG_ETHEREUM_TX: '/ethermint.evm.v1.MsgEthereumTx',
MSG_DYNAMIC_FEE_TX: '/ethermint.evm.v1.DynamicFeeTx',
MSG_LEGACY_TX: '/ethermint.evm.v1.LegacyTx',
};

0 comments on commit 297762b

Please sign in to comment.