Ethers.TypedData.Domain (Ethers v0.7.0)

Copy Markdown View Source

The EIP-712 domain separator data (EIP712Domain).

The domain scopes a signature to a particular application, contract and chain so that a signature produced for one domain cannot be replayed in another. All five standard fields are optional and only the fields that are actually present (non-nil) participate in the domain type - this matches the behaviour of ethers.js / MetaMask and is required for interoperability.

Standard fields (in canonical EIP-712 order) and their Solidity types:

FieldJSON nameSolidity type
:name"name"string
:version"version"string
:chain_id"chainId"uint256
:verifying_contract"verifyingContract"address
:salt"salt"bytes32

Example

Ethers.TypedData.Domain.new(
  name: "Ether Mail",
  version: "1",
  chain_id: 1,
  verifying_contract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
)

Summary

Types

t()

An EIP-712 domain. Every field is optional (nil when absent).

Functions

Creates a new Ethers.TypedData.Domain from a keyword list or a map.

Returns the present (non-nil) domain fields in canonical EIP-712 order as a list of Ethers.TypedData.Field structs.

Types

t()

@type t() :: %Ethers.TypedData.Domain{
  chain_id: non_neg_integer() | nil,
  name: String.t() | nil,
  salt: binary() | nil,
  verifying_contract: Ethers.Types.t_address() | nil,
  version: String.t() | nil
}

An EIP-712 domain. Every field is optional (nil when absent).

  • name - human readable name of the signing domain (string).
  • version - current version of the signing domain (string).
  • chain_id - the EIP-155 chain id (uint256).
  • verifying_contract - address of the contract that will verify the signature (address).
  • salt - a disambiguating 32-byte salt (bytes32).

Functions

new(domain)

@spec new(t() | map() | keyword()) :: t()

Creates a new Ethers.TypedData.Domain from a keyword list or a map.

Accepts atom keys (:chain_id) or string keys ("chain_id"). An existing Ethers.TypedData.Domain struct is returned unchanged. Unknown keys are ignored.

Examples

iex> Ethers.TypedData.Domain.new(name: "Ether Mail", version: "1", chain_id: 1)
%Ethers.TypedData.Domain{name: "Ether Mail", version: "1", chain_id: 1}

present_fields(domain)

@spec present_fields(t()) :: [Ethers.TypedData.Field.t()]

Returns the present (non-nil) domain fields in canonical EIP-712 order as a list of Ethers.TypedData.Field structs.

Each returned Field uses the JSON field name (e.g. "chainId") and its Solidity type. Only fields that are set participate in the EIP712Domain type - absent fields are omitted. This is consumed by both the JSON serializer and the hashing engine in later stages.

Examples

iex> Ethers.TypedData.Domain.new(name: "Ether Mail", chain_id: 1)
...> |> Ethers.TypedData.Domain.present_fields()
[
  %Ethers.TypedData.Field{name: "name", type: "string"},
  %Ethers.TypedData.Field{name: "chainId", type: "uint256"}
]