gpt4 book ai didi

hash - 以太坊交易哈希 : how to get in advance?

转载 作者:行者123 更新时间:2023-12-04 22:47:09 37 4
gpt4 key购买 nike

请告诉我如何提前获得 transactionHash?

// I have these tx opts:
var txOpts = {
"to":"0x345cA3e014Aaf5dcA488057592ee47305D9B3e10",
"nonce":"0x8",
"gasPrice":1,
"gas":250000,
"value":"0xde0b6b3a7640000",
"data":"0xd0e30db0"
}

// I create and sign tx:
var tx = new ethereumjs.Tx(txOpts);
tx.sign(new ethereumjs.Buffer.Buffer("c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3", "hex"));

// I've got raw tx:
var rawTx = tx.serialize().toString('hex');
"f86c08018303d09094345ca3e014aaf5dca488057592ee47305d9b3e10880de0b6b3a764000084d0e30db01ca0625e358100f4aacb9a65e6e054d963138565e3ceafb20eae4c9c8aaa583a29eea01d8f74faba33ab577ec36ac383dd5bd5298216bcf69fe2c09bba2d3003ecd008"

当我将此 tx 发送到 ganache-cli 时,我会收到以下日志:
eth_sendRawTransaction
> {
> "jsonrpc": "2.0",
> "id": 7,
> "method": "eth_sendRawTransaction",
> "params": [
> "0xf86c08018303d09094345ca3e014aaf5dca488057592ee47305d9b3e10880de0b6b3a764000084d0e30db01ca0625e358100f4aacb9a65e6e054d963138565e3ceafb20eae4c9c8aaa583a29eea01d8f74faba33ab577ec36ac383dd5bd5298216bcf69fe2c09bba2d3003ecd008"
> ],
> "external": true
> }

Transaction: 0x73d419e3a9a63aab7ee4f3be43c0df0175f4395615945d395c827bffb3bbfecc
Gas usage: 29634
Block Number: 9
Block Time: Fri Jan 12 2018 23:21:22 GMT-0700

交易哈希为 0x73d419e3a9a63aab7ee4f3be43c0df0175f4395615945d395c827bffb3bbfecc
怎么提前拿到?
// use ethereumjs.hash function (is that rlp-hash?):
var hash = tx.hash().toString('hex')
"**71ef26c4c1c1b01a5f87525e8e9b3ca7ffe5c9ae30ee1e70b353bf9b14db96be**"

这不等于 73d419e3a9a63aab7ee4f3be43c0df0175f4395615945d395c827bffb3bbfecc!

有没有可能在挖矿前提前计算正确的交易哈希?请告诉我应该使用哪些功能。

非常感谢!

最佳答案

我挖了一点以获得答案。

而不是直接调用tx.hash().toString('hex') ,您需要这样调用tx.hash(true).toString('hex') , 其中 true代表 includeSignature在计算哈希时。

如果您好奇,这就是方法(https://github.com/ethereumjs/ethereumjs-tx/blob/9a6324f64f4da1cb550a3eec4eaef95da4ab441b/src/transaction.ts#L177)。

您也可以从 rawTx 获得相同的结果像这样:

import * as ethUtil from 'ethereumjs-util';
const rawTx = `0x${signedTx.serialize().toString('hex')}`; // 0x is important
const res1 = `0x${ethUtil.keccak(rawTx).toString('hex')}`;
const res2 = `0x${signedTx.hash(true).toString('hex')}`;
res1 === res2; // true

我希望这将有所帮助。

关于hash - 以太坊交易哈希 : how to get in advance?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48233720/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com