rlpx/rlpx_types.hpp¶
Namespaces¶
| Name |
|---|
| rlpx |
| rlpx::detail |
Classes¶
| Name | |
|---|---|
| struct | rlpx::AesBlock RLPx wire structs — sizes are derived via sizeof() rather than magic numbers. |
| struct | rlpx::MacDigestWire 16-byte frame-level MAC digest (first 16 bytes of the running Keccak state). |
| struct | rlpx::FrameHeaderWire 16-byte RLPx frame header: 3-byte length |
Types¶
| Name | |
|---|---|
| enum class uint8_t | SessionState |
| enum class uint8_t | DisconnectReason |
| using std::array< uint8_t, kPublicKeySize > | PublicKey |
| using std::array< uint8_t, kPrivateKeySize > | PrivateKey |
| using std::array< uint8_t, kNonceSize > | Nonce |
| using std::array< uint8_t, kSharedSecretSize > | SharedSecret |
| using std::array< uint8_t, kAesKeySize > | AesKey |
| using std::array< uint8_t, kMacKeySize > | MacKey |
| using std::array< uint8_t, kMacSize > | MacDigest |
| using std::array< uint8_t, kFrameHeaderSize > | FrameHeader |
| using std::vector< uint8_t > | ByteBuffer |
| using gsl::span< const uint8_t > | ByteView |
| using gsl::span< uint8_t > | MutableByteView |
Functions¶
| Name | |
|---|---|
| rlp::ByteView | to_rlp_view(rlpx::ByteView view) |
| rlpx::ByteView | from_rlp_view(rlp::ByteView view) |
| rlp::Bytes | to_rlp_bytes(const rlpx::ByteBuffer & buffer) |
| rlpx::ByteBuffer | from_rlp_bytes(const rlp::Bytes & bytes) |
Attributes¶
Types Documentation¶
enum SessionState¶
| Enumerator | Value | Description |
|---|---|---|
| kUninitialized | 0 | |
| kConnecting | ||
| kAuthenticating | ||
| kHandshaking | ||
| kActive | ||
| kDisconnecting | ||
| kClosed | ||
| kError |
enum DisconnectReason¶
| Enumerator | Value | Description |
|---|---|---|
| kRequested | 0x00 | |
| kTcpError | 0x01 | |
| kProtocolError | 0x02 | |
| kUselessPeer | 0x03 | |
| kTooManyPeers | 0x04 | |
| kAlreadyConnected | 0x05 | |
| kIncompatibleVersion | 0x06 | |
| kInvalidIdentity | 0x07 | |
| kClientQuitting | 0x08 | |
| kUnexpectedIdentity | 0x09 | |
| kSelfConnection | 0x0A | |
| kTimeout | 0x0B | |
| kSubprotocolError | 0x10 |
using PublicKey¶
using PrivateKey¶
using Nonce¶
using SharedSecret¶
using AesKey¶
using MacKey¶
using MacDigest¶
using FrameHeader¶
using ByteBuffer¶
using ByteView¶
using MutableByteView¶
Functions Documentation¶
function to_rlp_view¶
function from_rlp_view¶
function to_rlp_bytes¶
function from_rlp_bytes¶
Attributes Documentation¶
variable kPublicKeySize¶
variable kPrivateKeySize¶
variable kNonceSize¶
variable kSharedSecretSize¶
variable kAesKeySize¶
variable kMacKeySize¶
variable kUncompressedPubKeyPrefixSize¶
Uncompressed secp256k1 public key: 0x04 prefix byte + 64 bytes = 65 bytes total.
The single 0x04 prefix byte
variable kUncompressedPubKeySize¶
variable kUncompressedPubKeyPrefix¶
variable kHmacSha256Size¶
HMAC-SHA256 full digest output size.
variable kAesBlockSize¶
variable kMacSize¶
variable kFrameHeaderSize¶
variable kEciesMacSize¶
ECIES / EIP-8 wire constants.
variable kEciesOverheadSize¶
variable kEip8LengthPrefixSize¶
variable kEip8AuthPaddingSize¶
variable kMaxEip8HandshakePacketSize¶
variable kFrameLengthSize¶
Number of bytes used to encode the frame length inside the frame header.
variable kFrameHeaderDataOffset¶
variable kFrameHeaderWithMacSize¶
variable kFramePaddingAlignment¶
variable kFrameLengthMsbOffset¶
variable kFrameLengthMiddleOffset¶
variable kFrameLengthLsbOffset¶
variable kFrameLengthMsbShift¶
variable kFrameLengthMiddleShift¶
variable kFrameLengthLsbShift¶
variable kFrameHeaderStaticRlpBytes¶
variable kEcdsaCompactSigSize¶
RLPx auth message wire constants Recoverable ECDSA compact signature: 64 bytes data + 1 byte recovery id.
variable kEcdsaRecoveryIdSize¶
variable kEcdsaSigSize¶
variable kAuthVersionSize¶
Version byte appended to auth/ack messages (RLPx v4 EIP-8).
variable kAuthVersion¶
variable kMaxFrameSizeMiB¶
Maximum RLPx frame payload: 16 MiB.
variable kMaxFrameSize¶
variable kHelloMessageId¶
variable kDisconnectMessageId¶
variable kPingMessageId¶
variable kPongMessageId¶
variable kProtocolVersion¶
variable kTcpConnectionTimeout¶
variable kSendLoopPollInterval¶
Source code¶
// Copyright 2025 GeniusVentures
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <array>
#include <chrono>
#include <cstdint>
#include <string_view>
#include <vector>
#include <optional>
#include <gsl/span>
#include <rlp/common.hpp> // For rlp::Bytes and rlp::ByteView types
namespace rlpx {
// Constant sizes — expressed as sizeof() on wire structs where possible
inline constexpr size_t kPublicKeySize = 64;
inline constexpr size_t kPrivateKeySize = 32;
inline constexpr size_t kNonceSize = 32;
inline constexpr size_t kSharedSecretSize = 32;
inline constexpr size_t kAesKeySize = 32;
inline constexpr size_t kMacKeySize = 32;
inline constexpr size_t kUncompressedPubKeyPrefixSize = 1;
inline constexpr size_t kUncompressedPubKeySize = kPublicKeySize + kUncompressedPubKeyPrefixSize;
inline constexpr uint8_t kUncompressedPubKeyPrefix = 0x04U;
inline constexpr size_t kHmacSha256Size = 32;
struct AesBlock { uint8_t bytes[16]; };
struct MacDigestWire { uint8_t bytes[16]; };
struct FrameHeaderWire { uint8_t bytes[16]; };
inline constexpr size_t kAesBlockSize = sizeof(AesBlock);
inline constexpr size_t kMacSize = sizeof(MacDigestWire);
inline constexpr size_t kFrameHeaderSize = sizeof(FrameHeaderWire);
inline constexpr size_t kEciesMacSize = kHmacSha256Size;
inline constexpr size_t kEciesOverheadSize = kUncompressedPubKeySize + kAesBlockSize + kEciesMacSize;
inline constexpr size_t kEip8LengthPrefixSize = sizeof(uint16_t);
inline constexpr size_t kEip8AuthPaddingSize = 100;
inline constexpr size_t kMaxEip8HandshakePacketSize = 2048U;
inline constexpr size_t kFrameLengthSize = 3;
inline constexpr size_t kFrameHeaderDataOffset = kFrameLengthSize;
inline constexpr size_t kFrameHeaderWithMacSize = kFrameHeaderSize + kMacSize;
inline constexpr size_t kFramePaddingAlignment = kAesBlockSize;
inline constexpr size_t kFrameLengthMsbOffset = 0;
inline constexpr size_t kFrameLengthMiddleOffset = 1;
inline constexpr size_t kFrameLengthLsbOffset = 2;
inline constexpr size_t kFrameLengthMsbShift = 16U;
inline constexpr size_t kFrameLengthMiddleShift = 8U;
inline constexpr size_t kFrameLengthLsbShift = 0U;
inline constexpr std::array<uint8_t, kFrameLengthSize> kFrameHeaderStaticRlpBytes = { 0xC2U, 0x80U, 0x80U };
inline constexpr size_t kEcdsaCompactSigSize = 64;
inline constexpr size_t kEcdsaRecoveryIdSize = 1;
inline constexpr size_t kEcdsaSigSize = kEcdsaCompactSigSize + kEcdsaRecoveryIdSize;
inline constexpr size_t kAuthVersionSize = 1;
inline constexpr uint8_t kAuthVersion = 4U;
inline constexpr size_t kMaxFrameSizeMiB = 16;
inline constexpr size_t kMaxFrameSize = kMaxFrameSizeMiB * 1024 * 1024;
// Type aliases for better semantics
using PublicKey = std::array<uint8_t, kPublicKeySize>;
using PrivateKey = std::array<uint8_t, kPrivateKeySize>;
using Nonce = std::array<uint8_t, kNonceSize>;
using SharedSecret = std::array<uint8_t, kSharedSecretSize>;
using AesKey = std::array<uint8_t, kAesKeySize>;
using MacKey = std::array<uint8_t, kMacKeySize>;
using MacDigest = std::array<uint8_t, kMacSize>;
using FrameHeader = std::array<uint8_t, kFrameHeaderSize>;
// Hide implementation details behind type aliases (Law of Demeter)
using ByteBuffer = std::vector<uint8_t>;
using ByteView = gsl::span<const uint8_t>;
using MutableByteView = gsl::span<uint8_t>;
// Session state enum
enum class SessionState : uint8_t {
kUninitialized = 0,
kConnecting,
kAuthenticating,
kHandshaking,
kActive,
kDisconnecting,
kClosed,
kError
};
// Disconnect reasons (from RLPx spec)
enum class DisconnectReason : uint8_t {
kRequested = 0x00,
kTcpError = 0x01,
kProtocolError = 0x02,
kUselessPeer = 0x03,
kTooManyPeers = 0x04,
kAlreadyConnected = 0x05,
kIncompatibleVersion = 0x06,
kInvalidIdentity = 0x07,
kClientQuitting = 0x08,
kUnexpectedIdentity = 0x09,
kSelfConnection = 0x0A,
kTimeout = 0x0B,
kSubprotocolError = 0x10
};
// Protocol message IDs
inline constexpr uint8_t kHelloMessageId = 0x00;
inline constexpr uint8_t kDisconnectMessageId = 0x01;
inline constexpr uint8_t kPingMessageId = 0x02;
inline constexpr uint8_t kPongMessageId = 0x03;
// Protocol version
inline constexpr uint8_t kProtocolVersion = 5;
// Timing constants
inline constexpr auto kTcpConnectionTimeout = std::chrono::seconds(10);
inline constexpr auto kSendLoopPollInterval = std::chrono::milliseconds(10);
// Conversion functions for interop with rlp library
namespace detail {
// Convert rlpx::ByteView (gsl::span) to rlp::ByteView (std::basic_string_view)
inline rlp::ByteView to_rlp_view(rlpx::ByteView view) noexcept {
return rlp::ByteView(view.data(), view.size());
}
// Convert rlp::ByteView to rlpx::ByteView
inline rlpx::ByteView from_rlp_view(rlp::ByteView view) noexcept {
return rlpx::ByteView(view.data(), view.size());
}
// Convert rlpx::ByteBuffer to rlp::Bytes
inline rlp::Bytes to_rlp_bytes(const rlpx::ByteBuffer& buffer) {
return rlp::Bytes(buffer.begin(), buffer.end());
}
// Convert rlp::Bytes to rlpx::ByteBuffer
inline rlpx::ByteBuffer from_rlp_bytes(const rlp::Bytes& bytes) {
return rlpx::ByteBuffer(bytes.begin(), bytes.end());
}
}
} // namespace rlpx
Updated on 2026-04-13 at 23:22:46 -0700