View Source DiodeClient.EIP712 (Diode Client v1.3.1)

EIP-712 implementation

Examples

# Example 1: Using separate arguments
iex> domain_data = %{
...>   "name" => "Ether Mail",
...>   "version" => "1",
...>   "chainId" => 1,
...>   "verifyingContract" => "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
...>   "salt" => "decafbeef"
...> }
iex> message_types = %{
...>   "Person" => [
...>     %{"name" => "name", "type" => "string"},
...>     %{"name" => "wallet", "type" => "address"}
...>   ],
...>   "Mail" => [
...>     %{"name" => "from", "type" => "Person"},
...>     %{"name" => "to", "type" => "Person"},
...>     %{"name" => "contents", "type" => "string"}
...>   ]
...> }
iex> message_data = %{
...>   "from" => %{
...>     "name" => "Cow",
...>     "wallet" => "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
...>   },
...>   "to" => %{
...>     "name" => "Bob",
...>     "wallet" => "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
...>   },
...>   "contents" => "Hello, Bob!"
...> }
iex> hashed_message = DiodeClient.EIP712.hash_typed_data(%{
...>   "types" => message_types,
...>   "primaryType" => "Mail",
...>   "domain" => domain_data,
...>   "message" => message_data
...> })
iex> DiodeClient.Base16.encode(hashed_message)
"0xc5bb16ccc59ae9a3ad1cb8343d4e3351f057c994a97656e1aff8c134e56f7530"

# Example 2: Using a single full message
iex> full_message = %{
...>   "types" => %{
...>     "EIP712Domain" => [
...>       %{"name" => "name", "type" => "string"},
...>       %{"name" => "version", "type" => "string"},
...>       %{"name" => "chainId", "type" => "uint256"},
...>       %{"name" => "verifyingContract", "type" => "address"},
...>       %{"name" => "salt", "type" => "bytes32"}
...>     ],
...>     "Person" => [
...>       %{"name" => "name", "type" => "string"},
...>       %{"name" => "wallet", "type" => "address"}
...>     ],
...>     "Mail" => [
...>       %{"name" => "from", "type" => "Person"},
...>       %{"name" => "to", "type" => "Person"},
...>       %{"name" => "contents", "type" => "string"}
...>     ]
...>   },
...>   "primaryType" => "Mail",
...>   "domain" => %{
...>     "name" => "Ether Mail",
...>     "version" => "1",
...>     "chainId" => 1,
...>     "verifyingContract" => "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
...>     "salt" => "decafbeef"
...>   },
...>   "message" => %{
...>     "from" => %{
...>       "name" => "Cow",
...>       "wallet" => "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
...>     },
...>     "to" => %{
...>       "name" => "Bob",
...>       "wallet" => "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
...>     },
...>     "contents" => "Hello, Bob!"
...>   }
...> }
iex> hashed_message = DiodeClient.EIP712.hash_typed_data(full_message)
iex> DiodeClient.Base16.encode(hashed_message)
"0xc5bb16ccc59ae9a3ad1cb8343d4e3351f057c994a97656e1aff8c134e56f7530"

.. _EIP-712: https://eips.ethereum.org/EIPS/eip-712

Summary

Functions

encode(domain_separator, primary_type, message)

encode(domain_separator, primary_type, type_data, message, opts \\ [])

encode_single_type(type, fields)

encode_type(primary_type, type_data)

hash_domain_separator(values)

hash_domain_separator(name, version, chain_id)

hash_domain_separator(name, version, chain_id, verifying_contract)

hash_struct(primary_type, type_data)

@spec hash_struct(binary(), [{String.t(), String.t(), any()}]) :: binary()

hash_struct(primary_type, type_data, message)

hash_typed_data(map, opts \\ [])

sign_typed_data(account_wallet, map)

type_hash(primary_type, type_data)