1

For example, this is the transaction hash in goerli: 0x60605c7e3fddd2be1fa63f4fa8ef12bfc5e5c69062c6a4788d1277e007e7de02.

We know that the public key can be derived by ECDSA. And we can get v,r,s from transaction hash. So transaction hash => vrs => ECDSA => public key. How to get the public key by the transaciton hash? I want to get some code. Thx!

3 Answers 3

2

Here's a Node.js script that uses ethers.js version 5.4.x:

const { ethers } = require("ethers");

// Function to extract the address associated with to a transaction
async function getPublicKeyFromTransactionHash(provider, txHash) {
  // Fetch the transaction using the transaction hash and provier
  const tx = await provider.getTransaction(txHash);

  // Extract the all the relevant fields from the transaction (We need all of them)
  const unsignedTx = {
    gasLimit: tx.gasLimit,
    value: tx.value,
    nonce: tx.nonce,
    data: tx.data,
    chainId: tx.chainId,
    to: tx.to,
    type: tx.type,
    maxFeePerGas: tx.maxFeePerGas,
    maxPriorityFeePerGas: tx.maxPriorityFeePerGas,
  };

  // Serializing tx without the signature
  const serializedTx = ethers.utils.serializeTransaction(unsignedTx);

  // Extract the signature (v, r, s) from the transaction
  const { v, r, s } = tx;
  
  // Join splitted signature
  const signature = ethers.utils.joinSignature({ v, r, s });

  // Recover the address or public key with (replace recoverAddress by recoverPublicKey) associated with the transaction
  return ethers.utils.recoverAddress(
    ethers.utils.keccak256(serializedTx),
    signature
  );
}

// Call the function with a provider and a transaction hash
(async () => {
  // Public provider for Goerli
  const provider = new ethers.providers.JsonRpcProvider(
    "https://ethereum-goerli-rpc.allthatnode.com"
  );

  // Transaction hash
  const txHash =
    "0x60605c7e3fddd2be1fa63f4fa8ef12bfc5e5c69062c6a4788d1277e007e7de02";

  // And finally call the function to extract the address associated with the transaction
  const address = await getPublicKeyFromTransactionHash(provider, txHash);

  console.log("Address:", address);
})();
2
  • I want to get the public key, but not the address. So I modify your code ethers.utils.recoverAddress to ethers.utils.recoverPublicKey, and then it returns the result I want. Thx my bro.
    – LEVI_104
    Commented Mar 23, 2023 at 6:17
  • Welcome I edited the comment
    – Adam Boudj
    Commented Mar 23, 2023 at 6:36
0

To get the public key associated with a transaction hash, you will need to use a block explorer. A block explorer is a search engine that allows users to easily look up information on the blockchain. Once you have the block explorer open, enter the transaction hash into the search bar and the public key associated with the transaction will be displayed.

0

You can check this out if you're looking for a solution of this problem in golang: https://github.com/ashish10677/public-key-extractor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.