View Source Web3.Type.Hash behaviour (web3ex v0.1.0)

A KECCAK-256 hash.

Copy from Blockscout

Link to this section Summary

Types

A full KECCAK-256 hash is 32, but it can also be truncated to fewer bytes.

t()

A module that implements this behaviour's callbacks

Functions

Number of bits in a byte

Casts term to t/0 using byte_count/0 in module

Dumps the t bytes to :binary (bytea) format used in database.

How many hexadecimal digits are used to represent a byte

Loads the binary hash from the database into t/0 if it has byte_count/0 bytes from callback_module.

Converts the t/0 to the integer version of the hash

Converts the t/0 to iodata representation shown to users.

Converts the t/0 to string representation shown to users.

Link to this section Types

@type byte_count() :: 1..32

A full KECCAK-256 hash is 32, but it can also be truncated to fewer bytes.

@type t() :: %Web3.Type.Hash{byte_count: byte_count(), bytes: <<_::_*8>>}

A module that implements this behaviour's callbacks

Link to this section Callbacks

@callback byte_count() :: byte_count()

Link to this section Functions

Number of bits in a byte

Link to this function

cast(callback_module, term)

View Source
@spec cast(module(), term()) :: {:ok, t()} | :error

Casts term to t/0 using byte_count/0 in module

Link to this function

dump(callback_module, term)

View Source
@spec dump(module(), term()) :: {:ok, binary()} | :error

Dumps the t bytes to :binary (bytea) format used in database.

Link to this function

hexadecimal_digits_per_byte()

View Source

How many hexadecimal digits are used to represent a byte

Link to this function

load(callback_module, term)

View Source
@spec load(module(), term()) :: {:ok, t()} | :error

Loads the binary hash from the database into t/0 if it has byte_count/0 bytes from callback_module.

@spec to_integer(t()) :: pos_integer()

Converts the t/0 to the integer version of the hash

iex> Web3.Type.Hash.to_integer(
...>   %Web3.Type.Hash{
...>     byte_count: 32,
...>     bytes: <<0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b ::
...>              big-integer-size(32)-unit(8)>>
...>   }
...> )
0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b
iex> Web3.Type.Hash.to_integer(
...>   %Web3.Type.Hash{
...>     byte_count: 20,
...>     bytes: <<0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed :: big-integer-size(20)-unit(8)>>
...>   }
...> )
0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed
@spec to_iodata(t()) :: iodata()

Converts the t/0 to iodata representation shown to users.

iex> %Web3.Type.Hash{
...>   byte_count: 32,
...>   bytes: <<0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b ::
...>            big-integer-size(32)-unit(8)>>
...> } |>
...> Web3.Type.Hash.to_iodata() |>
...> IO.iodata_to_binary()
"0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b"

Always pads number, so that it is a valid format for casting.

iex> %Web3.Type.Hash{
...>   byte_count: 32,
...>   bytes: <<0x1234567890abcdef :: big-integer-size(32)-unit(8)>>
...> } |>
...> Web3.Type.Hash.to_iodata() |>
...> IO.iodata_to_binary()
"0x0000000000000000000000000000000000000000000000001234567890abcdef"
@spec to_string(t()) :: String.t()

Converts the t/0 to string representation shown to users.

iex> Web3.Type.Hash.to_string(
...>   %Web3.Type.Hash{
...>     byte_count: 32,
...>     bytes: <<0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b ::
...>              big-integer-size(32)-unit(8)>>
...>   }
...> )
"0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b"

Always pads number, so that it is a valid format for casting.

iex> Web3.Type.Hash.to_string(
...>   %Web3.Type.Hash{
...>     byte_count: 32,
...>     bytes: <<0x1234567890abcdef :: big-integer-size(32)-unit(8)>>
...>   }
...> )
"0x0000000000000000000000000000000000000000000000001234567890abcdef"