簡體   English   中英

以太坊交易哈希:如何提前獲得?

[英]Ethereum transaction hash: how to get in advance?

請告訴我如何才能獲得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!

在挖掘之前是否有可能提前計算CORRECT事務哈希? 請告訴我應該使用哪些功能。

非常感謝!

這只是ganache一個問題。 queueRawTransaction使用FakeTransaction ,它忽略了事務上的實際簽名,而是偽造它。 我不完全確定為什么它以這種方式工作,但我相信你的代碼會為提交給實際以太坊網絡的交易做正確的事情。

如果你想獲得相同的事務哈希ganache是計算,這可以解決問題,但它不是你從以太坊本身得到的事務哈希:

const EthereumTx = require('ethereumjs-tx');
const FakeTx = require('ethereumjs-tx/fake.js');

var txOpts = {
  "to":"0x345cA3e014Aaf5dcA488057592ee47305D9B3e10",
  "nonce":"0x8",
  "gasPrice":1,
  "gas":250000,
  "value":"0xde0b6b3a7640000",
  "data":"0xd0e30db0"
}

var tx = new EthereumTx(txOpts);
tx.sign(Buffer.from("c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3", "hex"));

const raw = tx.serialize().toString('hex');
const fake = new FakeTx(raw);
fake.from = tx.getSenderAddress();

console.log(fake.hash(true).toString('hex'));

// Output:
// 71ef26c4c1c1b01a5f87525e8e9b3ca7ffe5c9ae30ee1e70b353bf9b14db96be

我們從ganache得到的實際tx哈希是:

0x73d419e3a9a63aab7ee4f3be43c0df0175f4395615945d395c827bffb3bbfecc

FakeTx和ethereumjs-tx都為簽名事務返回相同的哈希值:

71ef26c4c1c1b01a5f87525e8e9b3ca7ffe5c9ae30ee1e70b353bf9b14db96be

但是如果thransaction沒有簽名“tx.hash()。toString('hex')”返回與ganache cli完全相同的值。 即使沒有將'from'添加到txOpts。

事先無法獲得事務哈希,只能在執行事務后獲取事務哈希。

但是,無論何時交易都沒有被任何礦工選擇,這個交易哈希將是暫時的,如果您在交易中收取的費用很少,那么任何礦工都可能不會選擇您的交易。

我挖了一點才得到答案。

你需要調用tx.hash(true).toString('hex') ,而不是只調用tx.hash().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

我希望這將有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM