# `Cartouche.Typed.Domain`
[🔗](https://github.com/zenhive/cartouche/blob/main/lib/cartouche/typed.ex#L307)

EIP-712 domain separator — the standard `EIP712Domain` struct
(`name`, `version`, `chainId`, `verifyingContract`, `salt`). Each field is
optional; only the populated fields contribute to the encoded domain type.

# `t`

```elixir
@type t() :: %Cartouche.Typed.Domain{
  chain_id: nil | number(),
  name: nil | String.t(),
  salt: nil | &lt;&lt;_::256&gt;&gt;,
  verifying_contract: nil | &lt;&lt;_::160&gt;&gt;,
  version: nil | String.t()
}
```

# `deserialize`

```elixir
@spec deserialize(map()) :: t()
```

Deserializes a domain from JSON or JavaScript encoding to a struct.

## Examples

    iex> %{
    ...>   "name" => "Ether Mail",
    ...>   "version" => "1",
    ...>   "chainId" => 1,
    ...>   "verifyingContract" => "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
    ...> }
    ...> |> Cartouche.Typed.Domain.deserialize()
    %Cartouche.Typed.Domain{
      name: "Ether Mail",
      version: "1",
      chain_id: 1,
      verifying_contract: ~h[CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC]
    }

    iex> %{
    ...>   "name" => "Ether Mail",
    ...>   "version" => "1"
    ...> }
    ...> |> Cartouche.Typed.Domain.deserialize()
    %Cartouche.Typed.Domain{
      name: "Ether Mail",
      version: "1"
    }

# `domain_type`

```elixir
@spec domain_type(t()) :: %{required(String.t()) =&gt; Cartouche.Typed.Type.t()}
```

Builds the EIP-712 domain type based on a given domain.

## Examples

    iex> %Cartouche.Typed.Domain{
    ...>   name: "Ether Mail",
    ...>   version: "1",
    ...>   chain_id: 1,
    ...>   verifying_contract: ~h[CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC]
    ...> }
    ...> |> Cartouche.Typed.Domain.domain_type()
    %{"EIP712Domain" => %Cartouche.Typed.Type{fields: [{"name", :string}, {"version", :string}, {"chainId", {:uint, 256}}, {"verifyingContract", :address}]}}

    iex> %Cartouche.Typed.Domain{
    ...>   name: "Ether Mail",
    ...>   version: "1",
    ...> }
    ...> |> Cartouche.Typed.Domain.domain_type()
    %{"EIP712Domain" => %Cartouche.Typed.Type{fields: [{"name", :string}, {"version", :string}]}}

# `serialize`

```elixir
@spec serialize(t()) :: %{required(String.t()) =&gt; term()}
```

Serializes a domain, such that it can be JSON-encoded or passed to JavaScript.

## Examples

    iex> %Cartouche.Typed.Domain{
    ...>   name: "Ether Mail",
    ...>   version: "1",
    ...>   chain_id: 1,
    ...>   verifying_contract: ~h[CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC]
    ...> }
    ...> |> Cartouche.Typed.Domain.serialize()
    %{
      "name" => "Ether Mail",
      "version" => "1",
      "chainId" => 1,
      "verifyingContract" => "0xcccccccccccccccccccccccccccccccccccccccc",
    }

    iex> %Cartouche.Typed.Domain{
    ...>   name: "Ether Mail",
    ...>   version: "1"
    ...> }
    ...> |> Cartouche.Typed.Domain.serialize()
    %{
      "name" => "Ether Mail",
      "version" => "1"
    }

# `serialize_keys`

```elixir
@spec serialize_keys(t()) :: %{required(String.t()) =&gt; term()}
```

Serializes a domain's keys to be JSON-compatible. This is so that it can be used
as a value for `hashStruct`, per the EIP-712 spec to build a domain specifier.

## Examples

    iex> use Cartouche.Hex
    iex> %Cartouche.Typed.Domain{
    ...>   name: "Ether Mail",
    ...>   version: "1",
    ...>   chain_id: 1,
    ...>   verifying_contract: ~h[CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC]
    ...> }
    ...> |> Cartouche.Typed.Domain.serialize_keys()
    %{
      "name" => "Ether Mail",
      "version" => "1",
      "chainId" => 1,
      "verifyingContract" => ~h[CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC]
    }

    iex> use Cartouche.Hex
    iex> %Cartouche.Typed.Domain{
    ...>   name: "Ether Mail",
    ...>   version: "1",
    ...> }
    ...> |> Cartouche.Typed.Domain.serialize_keys()
    %{
      "name" => "Ether Mail",
      "version" => "1"
    }

---

*Consult [api-reference.md](api-reference.md) for complete listing*
