Last active
March 30, 2024 22:05
-
-
Save pldespaigne/33c9525156aabf7b273f34fba3b6aad2 to your computer and use it in GitHub Desktop.
Ethereum Tx Hash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { encode } from '@ethereumjs/rlp'; | |
import { ecrecover } from '@ethereumjs/util'; | |
import { keccak256 } from 'ethereum-cryptography/keccak'; | |
// tx: 0xb00b758da7b97dbeedd1ef62592b2c1427c70ca042dc51a63f3a39d8d31ebbcc | |
const chainId = 0x1n; | |
const nonce = 0x3n; | |
const maxPriorityFeePerGas = 0xf4240n; | |
const maxFeePerGas = 0x73323c225n; | |
const gasLimit = 0xdd9cn; | |
const to = hexToBytes('0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'); | |
const value = 0x0n; | |
const data = hexToBytes('0xa9059cbb000000000000000000000000352f4c972a4f1640079082eea4f658719ffb3c200000000000000000000000000000000000000000000000000000000390b25c30'); | |
const accessList: [ address: Uint8Array, key: Uint8Array ][] = []; | |
// as defined in the YP (Appendix H, equation 308) | |
const rlp = encode([ chainId, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, data, accessList ]); | |
// ⚠️ 0x9c68b22e031082ded83e299e884c5b84c94667db3f9de921fd527e7bc23861a6 | |
const txHash = keccak256(new Uint8Array([ 0x2, ...rlp])); | |
// from the tx data | |
const v = BigInt(0x1); | |
const r = hexToBytes('0x31c19c6b7dfcbba6ef0ced907fb30ff992ffc3b6847bb84b31873e090941cae7'); | |
const s = hexToBytes('0x3c69dd761b7eb3762b3515f1041d6227d63a16190dcb68d343be1d2fc17b293a'); | |
const pubKey = ecrecover(txHash, v, r, s); | |
const pubKeyHash = keccak256(pubKey); | |
// ✅ 0xaa37a0e0dcdda61d3b7e265bfc0441ebdb87fc9b | |
const sender = pubKeyHash.slice(-20); | |
// we add the signature (v,r,s) | |
const fullRlp = encode([ chainId, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, data, accessList, v, r, s ]); | |
// ✅ 0xb00b758da7b97dbeedd1ef62592b2c1427c70ca042dc51a63f3a39d8d31ebbcc | |
const fulTxHash = keccak256(new Uint8Array([0x2, ...fullRlp])); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment