Skip to content

Commit

Permalink
feat: convert evm pubkey to cosmos address
Browse files Browse the repository at this point in the history
  • Loading branch information
fibonacci998 committed Sep 30, 2024
1 parent 39eced2 commit d511a54
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 27 deletions.
107 changes: 91 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@bull-board/api": "^5.1.1",
"@bull-board/express": "^5.1.1",
"@cosmjs/cosmwasm-stargate": "^0.30.0",
"@cosmjs/crypto": "^0.32.4",
"@cosmjs/encoding": "^0.32.3",
"@cosmjs/json-rpc": "^0.29.5",
"@cosmjs/proto-signing": "^0.29.5",
Expand Down
53 changes: 42 additions & 11 deletions src/services/evm/crawl_evm_account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import _, { Dictionary } from 'lodash';
import { Context } from 'moleculer';
import {
bytesToHex,
hexToBytes,
PublicClient,
recoverAddress,
recoverPublicKey,
} from 'viem';
import { ethers } from 'ethers';
import { toBech32 } from '@cosmjs/encoding';
import { pubkeyToRawAddress } from '@cosmjs/tendermint-rpc';
import { Secp256k1 } from '@cosmjs/crypto';
import config from '../../../config.json' assert { type: 'json' };
import '../../../fetch-polyfill.js';
import BullableService, { QueueHandler } from '../../base/bullable.service';
Expand Down Expand Up @@ -176,9 +180,9 @@ export default class CrawlEvmAccountService extends BullableService {
await Account.query()
.transacting(trx)
.insert(batchAccounts)
.onConflict(['address'])
.onConflict(['evm_address'])
.merge(),
'address'
'evm_address'
);
await AccountBalance.query()
.insert(
Expand Down Expand Up @@ -294,28 +298,55 @@ export default class CrawlEvmAccountService extends BullableService {
this.logger.error(`cannot recover address at ${tx.hash}`);
throw Error(`cannot recover address at ${tx.hash}`);
}

const compressPubkey = Secp256k1.compressPubkey(
hexToBytes(recoveredPubKey)
);
const bech32Add = toBech32(
`${config.networkPrefixAddress}`,
pubkeyToRawAddress('secp256k1', compressPubkey)
);

listAccountDict[tx.from] = {
pubkey: JSON.stringify({
pubkeyEVM: recoveredPubKey,
}),
bech32Add,
};
})
);
})
);

const listAccountDB = await Account.query().whereIn(
'evm_address',
Object.keys(listAccountDict)
);
const listAccountDBByAddress = _.keyBy(listAccountDB, 'evm_address');

const listMissingAccount = Object.keys(listAccountDict).filter(
(e) => listAccountDBByAddress[e] === undefined
);
await this.broker.call(SERVICE.V1.CrawlEvmAccount.CrawlNewAccountApi.path, {
addresses: listMissingAccount,
});

const listAccountDBAfterInsert = await Account.query().whereIn(
'evm_address',
Object.keys(listAccountDict)
);
const listAccountAfterInsert = Object.keys(listAccountDict).map((e) => ({
account: e,
pubkey: { type: 'jsonb', value: listAccountDict[e].pubkey },
address: listAccountDict[e].bech32Add,
id: listAccountDBAfterInsert.find((a) => a.evm_address === e)?.id,
}));
await knex.transaction(async (trx) => {
if (Object.keys(listAccountDict).length > 0) {
const listAccountDB = await Account.query().whereIn(
await batchUpdate(trx, 'account', listAccountAfterInsert, [
'pubkey',
'address',
Object.keys(listAccountDict)
);
const listAccount = Object.keys(listAccountDict).map((e) => ({
account: e,
pubkey: { type: 'jsonb', value: listAccountDict[e].pubkey },
id: listAccountDB.find((a) => a.evm_address === e)?.id,
}));
await batchUpdate(trx, 'account', listAccount, ['pubkey']);
]);
}
if (blockCheckpoint) {
blockCheckpoint.height = endBlock;
Expand Down

0 comments on commit d511a54

Please sign in to comment.