Search documentation of Cartouche

# Cartouche.Typed.Domain (Cartouche v0.1.2)

Copy Markdown View Source

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.

## Summary

### Types

t()

### Functions

deserialize(params)

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

domain_type(domain)

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

serialize(domain)

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

serialize_keys(domain)

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.

## Types

# t()

    @type t() :: %Cartouche.Typed.Domain{
      chain_id: nil | number(),
      name: nil | String.t(),
      salt: nil | <<_::256>>,
      verifying_contract: nil | <<_::160>>,
      version: nil | String.t()
    }

## Functions

# deserialize(params)

    @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(domain)

    @spec domain_type(t()) :: %{required(String.t()) => 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(domain)

    @spec serialize(t()) :: %{required(String.t()) => 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(domain)

    @spec serialize_keys(t()) :: %{required(String.t()) => 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"
    }