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.
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
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
.
@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
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"
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"