ethereum.utils.hexadecimal

Utility Functions For Hexadecimal Strings ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. contents:: Table of Contents :backlinks: none :local:

Introduction

Hexadecimal strings specific utility functions used in this specification.

  1"""
  2Utility Functions For Hexadecimal Strings
  3^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  4
  5.. contents:: Table of Contents
  6    :backlinks: none
  7    :local:
  8
  9Introduction
 10------------
 11
 12Hexadecimal strings specific utility functions used in this specification.
 13"""
 14from ethereum.base_types import (
 15    U64,
 16    U256,
 17    Bytes,
 18    Bytes8,
 19    Bytes20,
 20    Bytes32,
 21    Bytes256,
 22    Uint,
 23)
 24from ethereum.crypto.hash import Hash32
 25
 26
 27def has_hex_prefix(hex_string: str) -> bool:
 28    """
 29    Check if a hex string starts with hex prefix (0x).
 30
 31    Parameters
 32    ----------
 33    hex_string :
 34        The hexadecimal string to be checked for presence of prefix.
 35
 36    Returns
 37    -------
 38    has_prefix : `bool`
 39        Boolean indicating whether the hex string has 0x prefix.
 40    """
 41    return hex_string.startswith("0x")
 42
 43
 44def remove_hex_prefix(hex_string: str) -> str:
 45    """
 46    Remove 0x prefix from a hex string if present. This function returns the
 47    passed hex string if it isn't prefixed with 0x.
 48
 49    Parameters
 50    ----------
 51    hex_string :
 52        The hexadecimal string whose prefix is to be removed.
 53
 54    Returns
 55    -------
 56    modified_hex_string : `str`
 57        The hexadecimal string with the 0x prefix removed if present.
 58    """
 59    if has_hex_prefix(hex_string):
 60        return hex_string[len("0x") :]
 61
 62    return hex_string
 63
 64
 65def hex_to_bytes(hex_string: str) -> Bytes:
 66    """
 67    Convert hex string to bytes.
 68
 69    Parameters
 70    ----------
 71    hex_string :
 72        The hexadecimal string to be converted to bytes.
 73
 74    Returns
 75    -------
 76    byte_stream : `bytes`
 77        Byte stream corresponding to the given hexadecimal string.
 78    """
 79    return bytes.fromhex(remove_hex_prefix(hex_string))
 80
 81
 82def hex_to_bytes8(hex_string: str) -> Bytes8:
 83    """
 84    Convert hex string to 8 bytes.
 85
 86    Parameters
 87    ----------
 88    hex_string :
 89        The hexadecimal string to be converted to 8 bytes.
 90
 91    Returns
 92    -------
 93    8_byte_stream : `Bytes8`
 94        8-byte stream corresponding to the given hexadecimal string.
 95    """
 96    return Bytes8(bytes.fromhex(remove_hex_prefix(hex_string).rjust(16, "0")))
 97
 98
 99def hex_to_bytes20(hex_string: str) -> Bytes20:
100    """
101    Convert hex string to 20 bytes.
102
103    Parameters
104    ----------
105    hex_string :
106        The hexadecimal string to be converted to 20 bytes.
107
108    Returns
109    -------
110    20_byte_stream : `Bytes20`
111        20-byte stream corresponding to the given hexadecimal string.
112    """
113    return Bytes20(bytes.fromhex(remove_hex_prefix(hex_string).rjust(20, "0")))
114
115
116def hex_to_bytes32(hex_string: str) -> Bytes32:
117    """
118    Convert hex string to 32 bytes.
119
120    Parameters
121    ----------
122    hex_string :
123        The hexadecimal string to be converted to 32 bytes.
124
125    Returns
126    -------
127    32_byte_stream : `Bytes32`
128        32-byte stream corresponding to the given hexadecimal string.
129    """
130    return Bytes32(bytes.fromhex(remove_hex_prefix(hex_string).rjust(64, "0")))
131
132
133def hex_to_bytes256(hex_string: str) -> Bytes256:
134    """
135    Convert hex string to 256 bytes.
136
137    Parameters
138    ----------
139    hex_string :
140        The hexadecimal string to be converted to 256 bytes.
141
142    Returns
143    -------
144    256_byte_stream : `Bytes256`
145        256-byte stream corresponding to the given hexadecimal string.
146    """
147    return Bytes256(
148        bytes.fromhex(remove_hex_prefix(hex_string).rjust(512, "0"))
149    )
150
151
152def hex_to_hash(hex_string: str) -> Hash32:
153    """
154    Convert hex string to hash32 (32 bytes).
155
156    Parameters
157    ----------
158    hex_string :
159        The hexadecimal string to be converted to hash32.
160
161    Returns
162    -------
163    hash : `Hash32`
164        32-byte stream obtained from the given hexadecimal string.
165    """
166    return Hash32(bytes.fromhex(remove_hex_prefix(hex_string)))
167
168
169def hex_to_uint(hex_string: str) -> Uint:
170    """
171    Convert hex string to Uint.
172
173    Parameters
174    ----------
175    hex_string :
176        The hexadecimal string to be converted to Uint.
177
178    Returns
179    -------
180    converted : `Uint`
181        The unsigned integer obtained from the given hexadecimal string.
182    """
183    return Uint(int(remove_hex_prefix(hex_string), 16))
184
185
186def hex_to_u64(hex_string: str) -> U64:
187    """
188    Convert hex string to U64.
189
190    Parameters
191    ----------
192    hex_string :
193        The hexadecimal string to be converted to U256.
194
195    Returns
196    -------
197    converted : `U64`
198        The U64 integer obtained from the given hexadecimal string.
199    """
200    return U64(int(remove_hex_prefix(hex_string), 16))
201
202
203def hex_to_u256(hex_string: str) -> U256:
204    """
205    Convert hex string to U256.
206
207    Parameters
208    ----------
209    hex_string :
210        The hexadecimal string to be converted to U256.
211
212    Returns
213    -------
214    converted : `U256`
215        The U256 integer obtained from the given hexadecimal string.
216    """
217    return U256(int(remove_hex_prefix(hex_string), 16))
def has_hex_prefix(hex_string: str) -> bool:
28def has_hex_prefix(hex_string: str) -> bool:
29    """
30    Check if a hex string starts with hex prefix (0x).
31
32    Parameters
33    ----------
34    hex_string :
35        The hexadecimal string to be checked for presence of prefix.
36
37    Returns
38    -------
39    has_prefix : `bool`
40        Boolean indicating whether the hex string has 0x prefix.
41    """
42    return hex_string.startswith("0x")

Check if a hex string starts with hex prefix (0x).

Parameters

hex_string : The hexadecimal string to be checked for presence of prefix.

Returns

has_prefix : bool Boolean indicating whether the hex string has 0x prefix.

def remove_hex_prefix(hex_string: str) -> str:
45def remove_hex_prefix(hex_string: str) -> str:
46    """
47    Remove 0x prefix from a hex string if present. This function returns the
48    passed hex string if it isn't prefixed with 0x.
49
50    Parameters
51    ----------
52    hex_string :
53        The hexadecimal string whose prefix is to be removed.
54
55    Returns
56    -------
57    modified_hex_string : `str`
58        The hexadecimal string with the 0x prefix removed if present.
59    """
60    if has_hex_prefix(hex_string):
61        return hex_string[len("0x") :]
62
63    return hex_string

Remove 0x prefix from a hex string if present. This function returns the passed hex string if it isn't prefixed with 0x.

Parameters

hex_string : The hexadecimal string whose prefix is to be removed.

Returns

modified_hex_string : str The hexadecimal string with the 0x prefix removed if present.

def hex_to_bytes(hex_string: str) -> bytes:
66def hex_to_bytes(hex_string: str) -> Bytes:
67    """
68    Convert hex string to bytes.
69
70    Parameters
71    ----------
72    hex_string :
73        The hexadecimal string to be converted to bytes.
74
75    Returns
76    -------
77    byte_stream : `bytes`
78        Byte stream corresponding to the given hexadecimal string.
79    """
80    return bytes.fromhex(remove_hex_prefix(hex_string))

Convert hex string to bytes.

Parameters

hex_string : The hexadecimal string to be converted to bytes.

Returns

byte_stream : bytes Byte stream corresponding to the given hexadecimal string.

def hex_to_bytes8(hex_string: str) -> ethereum.base_types.Bytes8:
83def hex_to_bytes8(hex_string: str) -> Bytes8:
84    """
85    Convert hex string to 8 bytes.
86
87    Parameters
88    ----------
89    hex_string :
90        The hexadecimal string to be converted to 8 bytes.
91
92    Returns
93    -------
94    8_byte_stream : `Bytes8`
95        8-byte stream corresponding to the given hexadecimal string.
96    """
97    return Bytes8(bytes.fromhex(remove_hex_prefix(hex_string).rjust(16, "0")))

Convert hex string to 8 bytes.

Parameters

hex_string : The hexadecimal string to be converted to 8 bytes.

Returns

8_byte_stream : Bytes8 8-byte stream corresponding to the given hexadecimal string.

def hex_to_bytes20(hex_string: str) -> ethereum.base_types.Bytes20:
100def hex_to_bytes20(hex_string: str) -> Bytes20:
101    """
102    Convert hex string to 20 bytes.
103
104    Parameters
105    ----------
106    hex_string :
107        The hexadecimal string to be converted to 20 bytes.
108
109    Returns
110    -------
111    20_byte_stream : `Bytes20`
112        20-byte stream corresponding to the given hexadecimal string.
113    """
114    return Bytes20(bytes.fromhex(remove_hex_prefix(hex_string).rjust(20, "0")))

Convert hex string to 20 bytes.

Parameters

hex_string : The hexadecimal string to be converted to 20 bytes.

Returns

20_byte_stream : Bytes20 20-byte stream corresponding to the given hexadecimal string.

def hex_to_bytes32(hex_string: str) -> ethereum.base_types.Bytes32:
117def hex_to_bytes32(hex_string: str) -> Bytes32:
118    """
119    Convert hex string to 32 bytes.
120
121    Parameters
122    ----------
123    hex_string :
124        The hexadecimal string to be converted to 32 bytes.
125
126    Returns
127    -------
128    32_byte_stream : `Bytes32`
129        32-byte stream corresponding to the given hexadecimal string.
130    """
131    return Bytes32(bytes.fromhex(remove_hex_prefix(hex_string).rjust(64, "0")))

Convert hex string to 32 bytes.

Parameters

hex_string : The hexadecimal string to be converted to 32 bytes.

Returns

32_byte_stream : Bytes32 32-byte stream corresponding to the given hexadecimal string.

def hex_to_bytes256(hex_string: str) -> ethereum.base_types.Bytes256:
134def hex_to_bytes256(hex_string: str) -> Bytes256:
135    """
136    Convert hex string to 256 bytes.
137
138    Parameters
139    ----------
140    hex_string :
141        The hexadecimal string to be converted to 256 bytes.
142
143    Returns
144    -------
145    256_byte_stream : `Bytes256`
146        256-byte stream corresponding to the given hexadecimal string.
147    """
148    return Bytes256(
149        bytes.fromhex(remove_hex_prefix(hex_string).rjust(512, "0"))
150    )

Convert hex string to 256 bytes.

Parameters

hex_string : The hexadecimal string to be converted to 256 bytes.

Returns

256_byte_stream : Bytes256 256-byte stream corresponding to the given hexadecimal string.

def hex_to_hash(hex_string: str) -> ethereum.base_types.Bytes32:
153def hex_to_hash(hex_string: str) -> Hash32:
154    """
155    Convert hex string to hash32 (32 bytes).
156
157    Parameters
158    ----------
159    hex_string :
160        The hexadecimal string to be converted to hash32.
161
162    Returns
163    -------
164    hash : `Hash32`
165        32-byte stream obtained from the given hexadecimal string.
166    """
167    return Hash32(bytes.fromhex(remove_hex_prefix(hex_string)))

Convert hex string to hash32 (32 bytes).

Parameters

hex_string : The hexadecimal string to be converted to hash32.

Returns

hash : Hash32 32-byte stream obtained from the given hexadecimal string.

def hex_to_uint(hex_string: str) -> ethereum.base_types.Uint:
170def hex_to_uint(hex_string: str) -> Uint:
171    """
172    Convert hex string to Uint.
173
174    Parameters
175    ----------
176    hex_string :
177        The hexadecimal string to be converted to Uint.
178
179    Returns
180    -------
181    converted : `Uint`
182        The unsigned integer obtained from the given hexadecimal string.
183    """
184    return Uint(int(remove_hex_prefix(hex_string), 16))

Convert hex string to Uint.

Parameters

hex_string : The hexadecimal string to be converted to Uint.

Returns

converted : Uint The unsigned integer obtained from the given hexadecimal string.

def hex_to_u64(hex_string: str) -> ethereum.base_types.U64:
187def hex_to_u64(hex_string: str) -> U64:
188    """
189    Convert hex string to U64.
190
191    Parameters
192    ----------
193    hex_string :
194        The hexadecimal string to be converted to U256.
195
196    Returns
197    -------
198    converted : `U64`
199        The U64 integer obtained from the given hexadecimal string.
200    """
201    return U64(int(remove_hex_prefix(hex_string), 16))

Convert hex string to U64.

Parameters

hex_string : The hexadecimal string to be converted to U256.

Returns

converted : U64 The U64 integer obtained from the given hexadecimal string.

def hex_to_u256(hex_string: str) -> ethereum.base_types.U256:
204def hex_to_u256(hex_string: str) -> U256:
205    """
206    Convert hex string to U256.
207
208    Parameters
209    ----------
210    hex_string :
211        The hexadecimal string to be converted to U256.
212
213    Returns
214    -------
215    converted : `U256`
216        The U256 integer obtained from the given hexadecimal string.
217    """
218    return U256(int(remove_hex_prefix(hex_string), 16))

Convert hex string to U256.

Parameters

hex_string : The hexadecimal string to be converted to U256.

Returns

converted : U256 The U256 integer obtained from the given hexadecimal string.