Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active November 8, 2023 14:59
Show Gist options
  • Save miguelmota/9099b705cf433336036065ab748c8404 to your computer and use it in GitHub Desktop.
Save miguelmota/9099b705cf433336036065ab748c8404 to your computer and use it in GitHub Desktop.
JavaScript decode RLP encoded Ethereum transaction (raw transaction) examples
// RLP encoded transaction
const rawTxHex = '0xed8205fc843b9aca00825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d88016345785d8a000080808080'
// using rlp package to decode values
const rlp = require('rlp')
const decoded = rlp.decode(rawTxHex)
console.log(decoded)
/*
[ <Buffer 05 fc>,
<Buffer 3b 9a ca 00>,
<Buffer 52 08>,
<Buffer 45 92 d8 f8 d7 b0 01 e7 2c b2 6a 73 e4 fa 18 06 a5 1a c7 9d>,
<Buffer 01 63 45 78 5d 8a 00 00>,
<Buffer >,
<Buffer >,
<Buffer >,
<Buffer > ]
*/
// using ethers package to decode values
const ethers = require('ethers')
const decoded = ethers.utils.RLP.decode(rawTxHex)
console.log(decoded)
/*
[ '0x05fc',
'0x3b9aca00',
'0x5208',
'0x4592d8f8d7b001e72cb26a73e4fa1806a51ac79d',
'0x016345785d8a0000',
'0x',
'0x',
'0x',
'0x' ]
*/
// using ethereumjs to decode to transaction object
const { Transaction } = require('@ethereumjs/tx')
const tx = Transaction.fromRlpSerializedTx(rawTxHex)
console.log(tx.toJSON())
/*
{ nonce: '0x5fc',
gasPrice: '0x3b9aca00',
gasLimit: '0x5208',
to: '0x4592d8f8d7b001e72cb26a73e4fa1806a51ac79d',
value: '0x16345785d8a0000',
data: '0x',
v: '0x0',
r: '0x0',
s: '0x0' }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment