Solidity does not support txHash variable yet, but it has been requested at EIP 901 https://github.com/ethereum/EIPs/issues/901 . Meanwhile how could I calculate the txHash of a transaction using Solidity code in the Smart Contract? I guess txHash is built upon some keccak hashing of the msg.sender and probably the nonce of the request, right? Is there a canonical formula for the Ethereum txHash calculation? Thx!


Short answer

It cannot be done.

Longer answer

The txhash is keccak256(signedTransaction).

This keccak256 function is available as a solidity function http://solidity.readthedocs.io/en/v0.4.21/units-and-global-variables.html

So you would need to construct signedTransaction since this value is not exposed to solidity, cf. https://stackoverflow.com/questions/49803424/how-can-we-access-rlp-encoded-signed-raw-transaction-in-solidity

The constituents of signedTransaction are

  1. nonce
  2. gas price
  3. gas limit
  4. to
  5. value in wei
  6. data
  7. ecdsaV
  8. ecdsaR
  9. ecdsaS

Value 3 is not directly available but you can get the current remaining gas at any point in your code execution and calculate from this, how much was available once the execution started. Values 1, 7, 8, and 9 (the nonce and the signature values) are not available using solidity, nor are they available using assembly code (which can be written inline in solidity source code files). So your problem can unfortunately not be solved.

See also: do the contracts of ethereum have the access to the nonce of the blocks?

| improve this answer | |

According to geth source code (file core/types/transactions) and this medium post the hash of a transaction is given by the keccak 256-bit hashing of the rlp encoding of all fields of a transaction, as showed in this image 1.

Therefore in order to calculate the transaction function, you need to be able to:

  1. Calculate the KEC256 bit hash of an arbitrary number of bytes
  2. Access/Calculate an RLP encoding of a transaction
  3. Access all fields of a transaction:

By looking the EVM specification (Yellow Paper, Appendix H), you can see that the first point is very easy, indeed it corresponds to the SHA3 (0x20) instruction. Neither the RLP encoding of all fields of a transaction is given nor an RLP encode instruction in the EVM instruction set, but it is possible to calculate it (Yellow Paper, Appendix B), provided that the sender has enough gas at its disposal. Finally, there aren't instruction to access all fields. In particular I cannot find the EVM instructions to access:

  • the nonce of the transaction: It corresponds to the number of transactions sent by the sender (I cannot find a function to retrieve neither this number)
  • the signature information v and r
  • the transaction gas limit
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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