首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过事务哈希获取公钥?

如何通过事务哈希获取公钥?
EN

Ethereum用户
提问于 2023-03-20 18:36:17
回答 3查看 227关注 0票数 1

例如,这是goerli: 0x60605c7e3fddd2be1fa63f4fa8ef12bfc5e5c69062c6a4788d1277e007e7de02.中的事务散列。

我们知道,公钥可以由ECDSA导出。我们可以从事务散列中得到v,r,S。因此,事务哈希=> vrs => ECDSA =>公钥。如何通过传输散列获取公钥?我想要一些密码。谢谢!

EN

回答 3

Ethereum用户

发布于 2023-03-22 15:33:48

下面是一个使用Node.js版本5.4.x的ethers.js脚本:

代码语言:javascript
复制
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
EN

Ethereum用户

发布于 2023-03-23 08:50:47

要获取与事务哈希关联的公钥,需要使用块资源管理器。块资源管理器是一种搜索引擎,允许用户轻松地查找区块链上的信息。打开块资源管理器后,在搜索栏中输入事务哈希,并显示与事务关联的公钥。

票数 0
EN

Ethereum用户

发布于 2023-05-22 10:08:05

如果您想在golang:https://github.com/ashish10677/public-key-extractor中找到解决这个问题的方法,您可以检查这个问题。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/147692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档