View Source Signet.Hex (Signet v1.3.8)

Helper module for parsing and encoding hex values.

If you use Signet.Hex, then you can use the ~h sigil for compile-time hex-to-binary compilation.

Summary

Functions

Parses an Ethereum 20-bytes hex string.

Parses a hex string, but returns :error instead of raising if hex is invalid.

Parses a hex string and raises if invalid.

Parses hex value as a big-endian integer.

Parses hex value as a big-endian integer. Raises if invalid.

Parses hex is value is not nil, otherwise returns nil.

Parses an Ethereum x-bytes hex string.

Parses an Ethereum 32-bytes hex string.

Encodes a binary as a checksummed Ethereum address.

Encodes hex, in CAPITALS.

Encodes a given value as a lowercase hex string, starting with 0x.

If input is a tuple {:ok, x} then returns a tuple {:ok, hex} where hex = encode(x). Otherwise, returns its input unchanged.

Encodes hex, striping any leading zeros.

Alias for decode_hex.

Alias for decode_hex!.

Similar non-sigil compile-time hex parser.

If input is non-nil, returns input encoded as a hex string. Otherwise, returns nil.

Handles the sigil ~h for list of words.

Alias for encode_address.

Alias for encode_hex.

Types

Functions

@spec decode_address!(String.t()) :: t() | no_return()

Parses an Ethereum 20-bytes hex string.

Identical to decode_hex!/1 except fails if string is not exactly 20-bytes.

Examples

iex> Signet.Hex.decode_address!("0x0000000000000000000000000000000000000001") <<1::160>>

iex> Signet.Hex.decode_address!("0xaabb") ** (Signet.Hex.HexError) invalid hex address: "0xaabb"

@spec decode_hex(String.t()) :: {:ok, t()} | :error

Parses a hex string, but returns :error instead of raising if hex is invalid.

Examples

iex> Signet.Hex.decode_hex("0xaabb")

iex> Signet.Hex.decode_hex("aabb")

iex> Signet.Hex.decode_hex("0xgggg") :invalid_hex

@spec decode_hex!(String.t()) :: t()

Parses a hex string and raises if invalid.

Examples

iex> Signet.Hex.decode_hex!("aabb") <<170, 187>>

iex> Signet.Hex.decode_hex!("0xggaabb") ** (Signet.Hex.HexError) invalid hex: "0xggaabb"

@spec decode_hex_number(String.t()) :: {:ok, integer()} | :error

Parses hex value as a big-endian integer.

Examples

iex> Signet.Hex.decode_hex_number("0xaabb")

iex> Signet.Hex.decode_hex_number("0xgggg") :invalid_hex

@spec decode_hex_number!(String.t()) :: integer() | no_return()

Parses hex value as a big-endian integer. Raises if invalid.

Examples

iex> Signet.Hex.decode_hex_number!("0xaabb") 0xaabb

iex> Signet.Hex.decode_hex_number!("0xgggg") ** (Signet.Hex.HexError) invalid hex number: "0xgggg"

@spec decode_maybe_hex!(String.t() | nil) :: t() | nil

Parses hex is value is not nil, otherwise returns nil.

Examples

iex> Signet.Hex.decode_maybe_hex!("0xaabb") <<170, 187>>

iex> Signet.Hex.decode_maybe_hex!(nil) nil

Link to this function

decode_sized!(hex, sz, msg \\ nil)

View Source
@spec decode_sized!(String.t(), integer(), String.t() | nil) :: t() | no_return()

Parses an Ethereum x-bytes hex string.

Identical to decode_hex!/1 except fails if string is not exactly x-bytes.

Examples

iex> Signet.Hex.decode_sized!("0x001122", 3) <<0x00, 0x11, 0x22>>

iex> Signet.Hex.decode_sized!("0xaabb", 3) ** (Signet.Hex.HexError) invalid 3-byte sized hex: "0xaabb"

@spec decode_word!(String.t()) :: t() | no_return()

Parses an Ethereum 32-bytes hex string.

Identical to decode_hex!/1 except fails if string is not exactly 32-bytes.

Examples

iex> Signet.Hex.decode_word!("0x0000000000000000000000000000000000000000000000000000000000000001") <<1::256>>

iex> Signet.Hex.decode_word!("0xaabb") ** (Signet.Hex.HexError) invalid hex word: "0xaabb"

@spec encode_address(t()) :: String.t()

Encodes a binary as a checksummed Ethereum address.

Examples

iex> Signet.Hex.encode_address(<<0xaa, 0xbb, 0xcc, 0::136>>) "0xaABbcC0000000000000000000000000000000000"

iex> Signet.Hex.encode_address(<<55>>) ** (Signet.Hex.HexError) Expected 20-byte address for in Signet.Hex.encode_address/1

@spec encode_big_hex(binary()) :: String.t()

Encodes hex, in CAPITALS.

Examples

iex> Signet.Hex.encode_big_hex(<<0xcc, 0xdd>>) "0xCCDD"

@spec encode_hex(t()) :: String.t()

Encodes a given value as a lowercase hex string, starting with 0x.

Examples

iex> Signet.Hex.encode_hex(<<0xaa, 0xbb>>) "0xaabb"

@spec encode_hex_result({:ok, t()} | term()) :: {:ok, String.t()} | term()

If input is a tuple {:ok, x} then returns a tuple {:ok, hex} where hex = encode(x). Otherwise, returns its input unchanged.

Examples

iex> Signet.Hex.encode_hex_result({:ok, <<0xaa, 0xbb>>})

iex> Signet.Hex.encode_hex_result({:error, 55})

@spec encode_short_hex(binary() | integer()) :: String.t()

Encodes hex, striping any leading zeros.

Examples

iex> Signet.Hex.encode_short_hex(<<0xc>>) "0xC"

iex> Signet.Hex.encode_short_hex(12) "0xC"

iex> Signet.Hex.encode_short_hex(<<0x0>>) "0x0"

@spec from_hex(t()) :: String.t()

Alias for decode_hex.

Examples

iex> Signet.Hex.from_hex("0xaabb")

@spec from_hex!(t()) :: String.t()

Alias for decode_hex!.

Examples

iex> Signet.Hex.from_hex!("0xaabb") <<0xaa, 0xbb>>

Similar non-sigil compile-time hex parser.

Examples

iex> use Signet.Hex
iex> hex!("0x22")
<<0x22>>

iex> use Signet.Hex
iex> hex!("0x2244")
<<0x22, 0x44>>
@spec maybe_encode_hex(t() | nil) :: String.t() | nil

If input is non-nil, returns input encoded as a hex string. Otherwise, returns nil.

Examples

iex> Signet.Hex.maybe_encode_hex(<<0xaa, 0xbb>>) "0xaabb"

iex> Signet.Hex.maybe_encode_hex(nil) nil

Link to this macro

sigil_h(term, modifiers)

View Source (macro)

Handles the sigil ~h for list of words.

Parses a hex string at compile-time.

Examples

iex> use Signet.Hex
iex> ~h[0x22]
<<0x22>>

iex> use Signet.Hex
iex> ~h[0x2244]
<<0x22, 0x44>>
@spec to_address(t()) :: String.t()

Alias for encode_address.

Examples

iex> Signet.Hex.to_address(<<0xaa, 0xbb, 0xcc, 0::136>>) "0xaABbcC0000000000000000000000000000000000"

@spec to_hex(t()) :: String.t()

Alias for encode_hex.

Examples

iex> Signet.Hex.to_hex(<<0xaa, 0xbb>>) "0xaabb"