View Source Web3.Type.Full (web3ex v0.1.0)

A 32-byte KECCAK-256 hash.

Link to this section Summary

Types

t()

A 32-byte hash of the t:Explorer.Chain.Block.t/0 or t:Explorer.Chain.Transaction.t/0.

Functions

Casts term to t/0

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

Callback implementation for Ecto.Type.embed_as/1.

Callback implementation for Ecto.Type.equal?/2.

Loads the binary hash from the database.

The underlying database type: binary. binary is used because no Postgres integer type is 32 bytes long.

Link to this section Types

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

A 32-byte hash of the t:Explorer.Chain.Block.t/0 or t:Explorer.Chain.Transaction.t/0.

Link to this section Functions

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

Casts term to t/0

If the term is already in t/0, then it is returned

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

If the term is an non_neg_integer, then it is converted to t/0

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

If the non_neg_integer is too large, then :error is returned.

iex> Web3.Type.Full.cast(0xffff_9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8)
:error

If the term is a String.t that starts with 0x, then is converted to an integer and then to t/0.

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

While non_neg_integers don't have to be the correct width (because zero padding it difficult with numbers), String.t format must always have 64 hexadecimal digits after the 0x base prefix.

iex> Web3.Type.Full.cast("0x0")
:error
@spec dump(term()) :: {:ok, binary()} | :error

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

If the field from the struct is t/0, then it succeeds.

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

If the field from the struct is an incorrect format such as t:Explorer.Chain.Address.Hash.t/0, :error is returned.

iex> Web3.Type.Full.dump(
...>   %Web3.Type.Hash{
...>     byte_count: 20,
...>     bytes: <<0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed :: big-integer-size(20)-unit(8)>>
...>   }
...> )
:error

Callback implementation for Ecto.Type.embed_as/1.

Callback implementation for Ecto.Type.equal?/2.

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

Loads the binary hash from the database.

If the binary hash is the correct format, it is returned

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

If the binary hash is an incorrect format, such as if an Explorer.Chain.Address.Hash field is loaded, :error is returned

iex> Web3.Type.Full.load(
...>   <<0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed :: big-integer-size(20)-unit(8)>>
...> )
:error
@spec type() :: :binary

The underlying database type: binary. binary is used because no Postgres integer type is 32 bytes long.