Ethereum Tx Decoder


An Ethereum transaction decoder is a tool used to decode the information contained in an Ethereum transaction. Ethereum transactions are encoded in a specific format, and decoding them can be helpful for understanding the details of the transaction and verifying its contents.


<?php 

include_once "../libraries2/vendor/autoload.php";
include_once("html_iframe_header.php");
include_once("eth_utils.php");

use Web3p\RLP\RLP;
use Web3\Utils;
use kornrunner\Ethereum\Transaction;

function convert($input) {
	list($quotient, $residue) = $input;
	$real = $quotient->toString().'.'.str_pad($residue->toString(), 18, '0', STR_PAD_LEFT);
	$real = rtrim($real,'0');
	$real = rtrim($real, '.');
	return $real;
}

function toHumanReadable($txParams, $txType) {
	if ($txType == "LEGACY") {
		//nonce 
		$txParams[0] = $txParams[0] ?: '0';
		$txParams[0] = bchexdec($txParams[0]);
		
		//gas price
		$txParams[1] = bchexdec($txParams[1]);
		$txParams[1] .= " WEI or " . convert(Utils::toEther($txParams[1], 'wei')) . ' ETH';
		
		//gas limit
		$txParams[2] = bchexdec($txParams[2]);
		
		//to
		$txParams[3] = "0x".$txParams[3];
		
		//value 
		$txParams[4] = $txParams[4] ?: '0';
		$txParams[4] = bchexdec($txParams[4]);
		$txParams[4] .= " WEI or " . convert(Utils::toEther($txParams[4], 'wei')) . ' ETH';
		
		//data
		$txParams[5] = $txParams[5] ?: '0';
		$txParams[5] = '0x'.$txParams[5];
		
		//v
		$txParams[6] = bchexdec($txParams[6]);
		
		//r 
		$txParams[7] = '0x'.$txParams[7];
		
		//s
		$txParams[8] = '0x'.$txParams[8];
	} else if ($txType == "EIP1559") {
		
		//Chain Id
		$txParams[0] = $txParams[0] ?: '0';
		$txParams[0] = bchexdec($txParams[0]);
		
		//Nonce
		$txParams[1] = $txParams[1] ?: '0';
		$txParams[1] = bchexdec($txParams[1]);
		
		//Max Priority Fee Per Gas
		$txParams[2] = bchexdec($txParams[2]);
		$txParams[2] .= " WEI or " . convert(Utils::toEther($txParams[2], 'wei')) . ' ETH';
		
		//Max Fee Per Gas
		$txParams[3] = bchexdec($txParams[3]);
		$txParams[3] .= " WEI or " . convert(Utils::toEther($txParams[3], 'wei')) . ' ETH';
		
		//Gas Limit
		$txParams[4] = bchexdec($txParams[4]);
		
		//To
		$txParams[5] = "0x".$txParams[5];
		
		//Value
		$txParams[6] = $txParams[6] ?: '0';
		$txParams[6] = bchexdec($txParams[6]);
		$txParams[6] .= " WEI or " . convert(Utils::toEther($txParams[6], 'wei')) . ' ETH';
		
		//Data
		$txParams[7] = $txParams[7] ?: '0';
		$txParams[7] = '0x'.$txParams[7];
		
		//Access List
		$txParams[8] = json_encode($txParams[8]);
		
		//v
		$txParams[9] = bchexdec($txParams[9]);
		
		//r 
		$txParams[10] = '0x'.$txParams[10];
		
		//s
		$txParams[11] = '0x'.$txParams[11];
	}

	return $txParams;
}

$result = '';
$txType = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	try {
		$rlp = new RLP;
		
		$txHexWoPrefix = (substr($_POST['tx_hex'],0,2) == '0x') ? substr($_POST['tx_hex'],2) : $_POST['tx_hex'];
		
		if (substr($txHexWoPrefix,0,2) == "02") {
			
			$result = $rlp->decode("0x".substr($txHexWoPrefix,2));
			$txType = "EIP1559";
		} else if (substr($txHexWoPrefix,0,2) == "01") {
			$result = $rlp->decode("0x".substr($txHexWoPrefix,2));
			$txType = "EIP2930";
		} else {
			$result = $rlp->decode("0x".$txHexWoPrefix);
			
			if (@count($result) == 9) {
				$txType = "LEGACY";
			} else {
				throw new Exception("Unknown Tx");
			}
		} 
		
	} catch (Exception $e) {
        $errmsg .= "Problem found. " . $e->getMessage();
    }
}

if ($errmsg) {
?>
	<div class="alert alert-danger">
		<strong>Error!</strong> <?php echo $errmsg?>
	</div>
<?php
}

if ($result) {
	
?>
	<div class="alert alert-success"><strong>Success!</strong> Decoded successfully.</div>
	<h6>Raw Response From RLP Decoder</h6>
	<?php
	echo "<pre>";
	var_dump($result);
	echo "</pre>";
	
	if ($txType == "LEGACY") {
		$captions = ["Nonce", "Gas Price", "Gas Limit", "To", "Value", "Data", "V", "R", "S"];
		$formatteds = toHumanReadable($result, $txType);
	} else if ($txType == "EIP1559"){
		
		$captions = ["Chain Id", "Nonce", "Max Priority Fee Per Gas", "Max Fee Per Gas","Gas Limit", "To", "Value", "Data", "Access List", "V", "R", "S"];
		$formatteds = toHumanReadable($result, $txType);
	}
	
	$combineds = array_combine($captions, $formatteds);
	?>
	<h6>Transaction Details</h6>
	<div class="table-responsive">
		<table border=1 class='table'>
			<tr><td>Tx Type</td><td><?php echo $txType?></td></tr>
			<?php
			foreach($combineds as $caption=>$value) {
			?>
				<tr><td><?php echo $caption?></td><td><?php echo $value?></td></tr>
			<?php 
			}
			?>
		</table>
	</div>
	
<?php
}
?>
<form action='' method='post'>
	<div class="form-group">
		<label for="tx_hex">Data To Decode:</label>
		<input class="form-control" type='text' name='tx_hex' id='tx_hex' rows=10 value="<?php echo $_POST['tx_hex']?>">
	</div>

	<input type='submit' class="btn btn-success btn-block"/>
</form>
<?php 
include_once("html_iframe_footer.php");








Tutorials
About Us
Contents have been open source in GITHUB. Please give me a ⭐ if you found this helpful :)
Community
Problem? Raise me a new issue.
Support Us
Buy me a coffee. so i can spend more nights for this :)

BTCSCHOOLS would like to present you with more pratical but little theory throughout our tutorials. Pages' content are constantly keep reviewed to avoid mistakes, but we cannot warrant correctness of all contents. While using this site, you agree to accept our terms of use, cookie & privacy policy. Copyright 2019 by BTCSCHOOLS. All Rights Reserved.