View Source Signet.Typed (Signet v1.0.0-beta7)

Module to build EIP-712 typed data, which can then be signed or recovered from.

Summary

Functions

Deserializes a Typed value from JSON or a map into a struct.

Builds a domain struct for a given type, per the EIP-712 spec.

Encodes a given typed value such that it can be signed or recovered.

Encodes the struct type per EIP-712. For this, we basically build an ABI-style value like Mail(Person from,Person to,string contents), but then to that we need to append any other types we've seen, like

Hashes a struct value, per the EIP-712 spec.

Serializes a Typed value, such that it can be passed to JSON or JavaScript.

Types

@type t() :: %Signet.Typed{domain: Domain.t(), types: type_map(), value: value_map()}
@type type_map() :: %{required(String.t()) => Type.t()}
@type value_map() :: %{required(String.t()) => term()}

Functions

@spec deserialize(%{}) :: t()

Deserializes a Typed value from JSON or a map into a struct.

Examples

iex> %{
...>   "domain" => %{
...>     "name" => "Ether Mail",
...>     "version" => "1",
...>     "chainId" => 1,
...>     "verifyingContract" => "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
...>   },
...>   "types" => %{
...>     "Person" => [
...>       %{
...>         "name" => "name",
...>         "type" => "string"
...>       },
...>       %{
...>         "name" => "wallet",
...>         "type" => "address"
...>       },
...>     ],
...>     "Mail" => [
...>       %{
...>         "name" => "from",
...>         "type" => "Person"
...>       },
...>       %{
...>         "name" => "to",
...>         "type" => "Person"
...>       },
...>       %{
...>         "name" => "contents",
...>         "type" => "string"
...>       },
...>     ]
...>   },
...>   "value" => %{
...>     "from" => %{ "name" => "Cow", "wallet" => "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" },
...>     "to" => %{ "name" => "Bob", "wallet" => "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB" },
...>     "contents" => "Hello, Bob!"
...>   }
...> }
...> |> Signet.Typed.deserialize()
%Signet.Typed{
  domain: %Signet.Typed.Domain{
    chain_id: 1,
    name: "Ether Mail",
    verifying_contract: Base.decode16!("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"),
    version: "1"
  },
  types: %{
    "Mail" => %Signet.Typed.Type{fields: [{"from", "Person"}, {"to", "Person"}, {"contents", :string}]},
    "Person" => %Signet.Typed.Type{fields: [{"name", :string}, {"wallet", :address}]}
  },
  value: %{
    "contents" => "Hello, Bob!",
    "from" => %{
      "name" => "Cow",
      "wallet" => Base.decode16!("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826")
    },
    "to" => %{
      "name" => "Bob",
      "wallet" => Base.decode16!("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB")
    }
  }
}
@spec domain_seperator(t()) :: binary()

Builds a domain struct for a given type, per the EIP-712 spec.

Examples

iex> %Signet.Typed{
...>   domain: %Signet.Typed.Domain{
...>     chain_id: 1,
...>     name: "Ether Mail",
...>     verifying_contract: Signet.Util.decode_hex!("0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"),
...>     version: "1"
...>   },
...>   types: %{
...>     "Mail" => %Signet.Typed.Type{fields: [{"from", "Person"}, {"to", "Person"}, {"contents", :string}]},
...>     "Person" => %Signet.Typed.Type{fields: [{"name", :string}, {"wallet", :address}]}
...>   },
...>   value: %{
...>     "contents" => "Hello, Bob!",
...>     "from" => %{
...>       "name" => "Cow",
...>       "wallet" => Signet.Util.decode_hex!("0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826")
...>     },
...>     "to" => %{
...>       "name" => "Bob",
...>       "wallet" => Signet.Util.decode_hex!("0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB")
...>     }
...>   }
...> }
...> |> Signet.Typed.domain_seperator()
...> |> Signet.Util.encode_hex()
"0xF2CEE375FA42B42143804025FC449DEAFD50CC031CA257E0B194A650A912090F"
@spec encode(t()) :: binary()

Encodes a given typed value such that it can be signed or recovered.

Examples

iex> %Signet.Typed{
...>   domain: %Signet.Typed.Domain{
...>     chain_id: 1,
...>     name: "Ether Mail",
...>     verifying_contract: Signet.Util.decode_hex!("0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"),
...>     version: "1"
...>   },
...>   types: %{
...>     "Mail" => %Signet.Typed.Type{fields: [{"from", "Person"}, {"to", "Person"}, {"contents", :string}]},
...>     "Person" => %Signet.Typed.Type{fields: [{"name", :string}, {"wallet", :address}]}
...>   },
...>   value: %{
...>     "contents" => "Hello, Bob!",
...>     "from" => %{
...>       "name" => "Cow",
...>       "wallet" => Signet.Util.decode_hex!("0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826")
...>     },
...>     "to" => %{
...>       "name" => "Bob",
...>       "wallet" => Signet.Util.decode_hex!("0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB")
...>     }
...>   }
...> }
...> |> Signet.Typed.encode()
...> |> Signet.Util.encode_hex()
"0x1901F2CEE375FA42B42143804025FC449DEAFD50CC031CA257E0B194A650A912090FC52C0EE5D84264471806290A3F2C4CECFC5490626BF912D01F240D7A274B371E"

iex> %Signet.Typed{
...>   domain: %Signet.Typed.Domain{
...>     chain_id: 1,
...>     name: "Complex Array",
...>     verifying_contract: Signet.Util.decode_hex!("0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"),
...>     version: "1"
...>   },
...>   types: %{
...>     "Array" => %Signet.Typed.Type{fields: [{"a", {:uint, 256}}, {"b", {:uint, 256}}, {"c", :string}, {"d", {:array, :bytes}}]}
...>   },
...>   value: %{
...>     "a" => 55,
...>     "b" => 66,
...>     "c" => "Hello",
...>     "d" => [<<0x11, 0x22>>, <<0x33, 0x44>>]
...>   }
...> }
...> |> Signet.Typed.encode()
...> |> Signet.Util.encode_hex()
"0x190103BD1627B4C5F7540C63D7EE347524DCEF247EED29C833DD3B1455B8DEC4009FCC95538BFC3F979CA59D9EF7DE5ED402A4E403857B3DE87D1FC8ED4A2A7CDDD9"
Link to this function

encode_type(name, types)

View Source
@spec encode_type(String.t(), type_map()) :: String.t()

Encodes the struct type per EIP-712. For this, we basically build an ABI-style value like Mail(Person from,Person to,string contents), but then to that we need to append any other types we've seen, like:

Mail(Person from,Person to,string contents)Person(string name,address wallet).

This is a tail-call optimized implementation to build the types then track and append types that need to be added.

Examples

iex> Signet.Typed.encode_type("Mail", %{
...>   "Mail" => %Signet.Typed.Type{fields: [{"from", "Person"}, {"to", "Person"}, {"contents", :string}]},
...>   "Person" => %Signet.Typed.Type{fields: [{"name", :string}, {"wallet", :address}]}
...> })
"Mail(Person from,Person to,string contents)Person(string name,address wallet)"
Link to this function

hash_struct(name, value, types)

View Source
@spec hash_struct(String.t(), value_map(), type_map()) :: binary()

Hashes a struct value, per the EIP-712 spec.

Examples

iex> types = %{
...>   "Mail" => %Signet.Typed.Type{fields: [{"from", "Person"}, {"to", "Person"}, {"contents", :string}]},
...>   "Person" => %Signet.Typed.Type{fields: [{"name", :string}, {"wallet", :address}]}
...> }
...> value = %{
...>   "contents" => "Hello, Bob!",
...>   "from" => %{
...>     "name" => "Cow",
...>     "wallet" => Signet.Util.decode_hex!("0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826")
...>   },
...>   "to" => %{
...>     "name" => "Bob",
...>     "wallet" => Signet.Util.decode_hex!("0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB")
...>   }
...> }
...> Signet.Typed.hash_struct("Mail", value, types)
...> |> Signet.Util.encode_hex()
"0xC52C0EE5D84264471806290A3F2C4CECFC5490626BF912D01F240D7A274B371E"
@spec serialize(t()) :: %{}

Serializes a Typed value, such that it can be passed to JSON or JavaScript.

Examples

iex> %Signet.Typed{
...>   domain: %Signet.Typed.Domain{
...>     chain_id: 1,
...>     name: "Ether Mail",
...>     verifying_contract: Base.decode16!("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"),
...>     version: "1"
...>   },
...>   types: %{
...>     "Mail" => %Signet.Typed.Type{fields: [{"from", "Person"}, {"to", "Person"}, {"contents", :string}]},
...>     "Person" => %Signet.Typed.Type{fields: [{"name", :string}, {"wallet", :address}]}
...>   },
...>   value: %{
...>     "contents" => "Hello, Bob!",
...>     "from" => %{
...>       "name" => "Cow",
...>       "wallet" => Base.decode16!("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826")
...>     },
...>     "to" => %{
...>       "name" => "Bob",
...>       "wallet" => Base.decode16!("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB")
...>     }
...>   }
...> }
...> |> Signet.Typed.serialize()
%{
  "domain" => %{
    "name" => "Ether Mail",
    "version" => "1",
    "chainId" => 1,
    "verifyingContract" => "0xCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
  },
  "types" => %{
    "Person" => [
      %{
        "name" => "name",
        "type" => "string"
      },
      %{
        "name" => "wallet",
        "type" => "address"
      },
    ],
    "Mail" => [
      %{
        "name" => "from",
        "type" => "Person"
      },
      %{
        "name" => "to",
        "type" => "Person"
      },
      %{
        "name" => "contents",
        "type" => "string"
      },
    ]
  },
  "value" => %{
    "from" => %{ "name" => "Cow", "wallet" => "0xCD2A3D9F938E13CD947EC05ABC7FE734DF8DD826" },
    "to" => %{ "name" => "Bob", "wallet" => "0xBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" },
    "contents" => "Hello, Bob!"
  }
}