1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import { ethers } from "ethers";
const connection = ""; const provider = new ethers.providers.JsonRpcProvider(connection);
const txHash = "";
const transaction = await provider.getTransaction(txHash);
const txData = { gasLimit: transaction.gasLimit, value: transaction.value, nonce: transaction.nonce, data: transaction.data, to: transaction.to, chainId: transaction.chainId, type: transaction.type, maxFeePerGas: transaction.maxFeePerGas, maxPriorityFeePerGas: transaction.maxPriorityFeePerGas, }
const signingData = ethers.utils.serializeTransaction(txData);
const msgHash = ethers.utils.keccak256(signingData);
const signature = {r: transaction.r, s: transaction.s, v: transaction.v};
let rawPublicKey = ethers.utils.recoverPublicKey(msgHash, signature); console.log(`未压缩公钥:\n ${rawPublicKey}`);
rawPublicKey = `0x${rawPublicKey.slice(4)}`; console.log(`经压缩公钥:\n ${rawPublicKey}`);
let address = ethers.utils.keccak256(rawPublicKey); console.log(`Address:\n 0x${address.slice(address.length - 40)}`);
|