Address
0x9e872295fa40bd0edb8b7efd7ea02fa1194472d4Current Holdings
$0.00
TXs sent
not counted
First Active
2025-11-13
block 25,010,536
Last Active
310 days ago
block 25,010,536
Funded By
not identified
Net worth historyi
No net-worth snapshots recorded yet
exact matchRLPEncodersolc 0.8.20+commit.a1b79de6runtime exact · creation exact
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/// @title EIP1559RLPEncoder
/// @notice RLP encoding for EIP-1559 (type 2) transactions
/// @dev Returns hex bytes for easy integration with JSON-RPC and signing libraries
contract RLPEncoder {
function encodeUint(uint256 value) internal pure returns (bytes memory) {
if (value == 0) return hex"80";
uint256 temp = value;
uint256 len;
while (temp != 0) {
len++;
temp >>= 8;
}
bytes memory result = new bytes(len + 1);
result[0] = bytes1(uint8(0x80 + len));
for (uint256 i = len; i > 0; i--) {
result[i] = bytes1(uint8(value));
value >>= 8;
}
return result;
}
function encodeAddress(address addr) internal pure returns (bytes memory) {
if (addr == address(0)) return hex"80";
bytes memory result = new bytes(21);
result[0] = 0x94;
uint160 addrInt = uint160(addr);
for (uint256 i = 0; i < 20; i++) {
result[20 - i] = bytes1(uint8(addrInt >> (8 * i)));
}
return result;
}
function encodeBytes(
bytes memory data
) internal pure returns (bytes memory) {
uint256 len = data.length;
if (len == 0) return hex"80";
if (len == 1 && uint8(data[0]) < 0x80) return data;
if (len < 56) {
bytes memory result = new bytes(1 + len);
result[0] = bytes1(uint8(0x80 + len));
for (uint256 i = 0; i < len; i++) result[i + 1] = data[i];
return result;
} else {
uint256 lenLen;
uint256 i = len;
while (i != 0) {
lenLen++;
i >>= 8;
}
bytes memory result = new bytes(1 + lenLen + len);
result[0] = bytes1(uint8(0xb7 + lenLen));
for (i = lenLen; i > 0; i--) {
result[i] = bytes1(uint8(len >> (8 * (lenLen - i))));
}
for (i = 0; i < len; i++) result[1 + lenLen + i] = data[i];
return result;
}
}
function encodeList(
bytes[] memory elements
) internal pure returns (bytes memory) {
uint256 totalLen;
for (uint256 i = 0; i < elements.length; i++)
totalLen += elements[i].length;
bytes memory payload;
if (totalLen < 56) {
payload = new bytes(1 + totalLen);
payload[0] = bytes1(uint8(0xc0 + totalLen));
uint256 k = 1;
for (uint256 i = 0; i < elements.length; i++) {
for (uint256 j = 0; j < elements[i].length; j++) {
payload[k++] = elements[i][j];
}
}
} else {
uint256 lenLen;
uint256 i = totalLen;
while (i != 0) {
lenLen++;
i >>= 8;
}
payload = new bytes(1 + lenLen + totalLen);
payload[0] = bytes1(uint8(0xf7 + lenLen));
for (i = lenLen; i > 0; i--) {
payload[i] = bytes1(uint8(totalLen >> (8 * (lenLen - i))));
}
uint256 k = 1 + lenLen;
for (i = 0; i < elements.length; i++) {
for (uint256 j = 0; j < elements[i].length; j++) {
payload[k++] = elements[i][j];
}
}
}
return payload;
}
// --- PURE ENCODING FUNCTIONS ---
/// @notice RLP encode EIP-1559 tx for signing (9 parameters) - PURE
/// @return Encoded transaction bytes ready for hashing and signing
function _encodeForSigningPure(
uint256 chainId,
uint256 nonce,
uint256 maxPriorityFeePerGas,
uint256 maxFeePerGas,
uint256 gasLimit,
address to,
uint256 value,
bytes memory data,
bytes memory accessList
) internal pure returns (bytes memory) {
bytes[] memory items = new bytes[](9);
items[0] = encodeUint(chainId);
items[1] = encodeUint(nonce);
items[2] = encodeUint(maxPriorityFeePerGas);
items[3] = encodeUint(maxFeePerGas);
items[4] = encodeUint(gasLimit);
items[5] = encodeAddress(to);
items[6] = encodeUint(value);
items[7] = encodeBytes(data);
items[8] = accessList.length == 0
? encodeList(new bytes[](0))
: encodeBytes(accessList);
return abi.encodePacked(hex"02", encodeList(items));
}
/// @notice RLP encode EIP-1559 signed tx (12 parameters) - PURE
/// @return Encoded transaction bytes ready for broadcasting
function _encodeSignedPure(
uint256 chainId,
uint256 nonce,
uint256 maxPriorityFeePerGas,
uint256 maxFeePerGas,
uint256 gasLimit,
address to,
uint256 value,
bytes memory data,
bytes memory accessList,
uint256 v,
uint256 r,
uint256 s
) internal pure returns (bytes memory) {
bytes[] memory items = new bytes[](12);
items[0] = encodeUint(chainId);
items[1] = encodeUint(nonce);
items[2] = encodeUint(maxPriorityFeePerGas);
items[3] = encodeUint(maxFeePerGas);
items[4] = encodeUint(gasLimit);
items[5] = encodeAddress(to);
items[6] = encodeUint(value);
items[7] = encodeBytes(data);
items[8] = accessList.length == 0
? encodeList(new bytes[](0))
: encodeBytes(accessList);
items[9] = encodeUint(v);
items[10] = encodeUint(r);
items[11] = encodeUint(s);
return abi.encodePacked(hex"02", encodeList(items));
}
// --- VIEW WRAPPER FUNCTIONS (CALLABLE FROM UI) ---
/// @notice VIEW wrapper to encode EIP-1559 tx for signing
/// @dev Call this from your UI - it's a view function
/// @return Encoded transaction bytes ready for hashing and signing
function encodeForSigning(
uint256 chainId,
uint256 nonce,
uint256 maxPriorityFeePerGas,
uint256 maxFeePerGas,
uint256 gasLimit,
address to,
uint256 value,
bytes memory data,
bytes memory accessList
) external view returns (bytes memory) {
return
_encodeForSigningPure(
chainId,
nonce,
maxPriorityFeePerGas,
maxFeePerGas,
gasLimit,
to,
value,
data,
accessList
);
}
/// @notice VIEW wrapper to encode EIP-1559 signed tx
/// @dev Call this from your UI - it's a view function
/// @return Encoded transaction bytes ready for broadcasting
function encodeSigned(
uint256 chainId,
uint256 nonce,
uint256 maxPriorityFeePerGas,
uint256 maxFeePerGas,
uint256 gasLimit,
address to,
uint256 value,
bytes memory data,
bytes memory accessList,
uint256 v,
uint256 r,
uint256 s
) external view returns (bytes memory) {
return
_encodeSignedPure(
chainId,
nonce,
maxPriorityFeePerGas,
maxFeePerGas,
gasLimit,
to,
value,
data,
accessList,
v,
r,
s
);
}
}