hashtagBytes32

Fixed-size 32-byte value type

Exactly 32 bytes — used for hashes, AES keys, and similar values.

Overview

Bytes32 is a fixed-size byte type that validates its length at construction time. It's used throughout the SDK for values that must be exactly 32 bytes, such as block hashes, AES-GCM keys, and Keccak-256 hashes.

Definition

class Bytes32(HexBytes):
    """Exactly 32 bytes — used for hashes, AES keys, and similar values."""
    _size = 32

Inheritance

  • Inherits from HexBytes (from the hexbytes package)

  • HexBytes itself inherits from Python's bytes type

  • Fully compatible with any API expecting bytes

Construction

Bytes32 can be constructed from:

  • Hex strings (with or without "0x" prefix)

  • Raw bytes objects

  • Integer values

Parameters

Parameter
Type
Required
Description

val

bytes | str | int

Yes

The value to convert to 32 bytes

Raises

  • ValueError - If the resulting length is not exactly 32 bytes

Examples

From Hex String

From Raw Bytes

From Integer

Length Validation

Methods

to_0x_hex()

Returns the hex string representation with "0x" prefix.

Properties

  • Immutable - Cannot be modified after construction

  • Hashable - Can be used as dictionary keys or in sets

  • Compatible with bytes - Passes isinstance(x, bytes) checks

Notes

  • All Bytes32 instances have len() == 32

  • Type validation happens at construction time

  • Compatible with all web3.py APIs expecting bytes

  • Commonly used for block hashes in SeismicElements

See Also

Last updated