EIP712.Typed.Type (eip712 v0.1.0)

Summary

Functions

Deserializes a Type from JSON or a map into a struct.

Deserializes a primitive or custom type. We differentiate custom types by not being a primitive type.

Deserializes a value of a given type for being stored in this struct.

Encodes a value for encodeData, as per the EIP-712 spec. Specifically, raw values are expanded to 32-bytes, and dynamic types are hashed.

Serializes a Type, such that it can be used with JSON or JavaScript.

Serializes a primitive or custom type.

Serializes a value of a given type to pass to JSON or JavaScript.

Types

@type field() :: {String.t(), field_type()}
Link to this type

field_type()

@type field_type() :: primitive() | String.t()
@type primitive() ::
  :address
  | {:uint, number()}
  | {:bytes, number()}
  | :string
  | {:array, primitive()}
@type t() :: %EIP712.Typed.Type{fields: type_list()}
@type type_list() :: [field()]

Functions

Link to this function

deserialize(types)

@spec deserialize([%{name: String.t(), type: String.t()}]) :: t()

Deserializes a Type from JSON or a map into a struct.

Examples

iex> [%{
...>   "name" => "from",
...>   "type" => "Person",
...> }, %{
...>   "name" => "to",
...>   "type" => "Person",
...> }, %{
...>   "name" => "contents",
...>   "type" => "string",
...> }]
...> |> EIP712.Typed.Type.deserialize()
%EIP712.Typed.Type{fields: [{"from", "Person"}, {"to", "Person"}, {"contents", :string}]}

iex> [%{
...>   "name" => "items",
...>   "type" => "string[]",
...> }]
...> |> EIP712.Typed.Type.deserialize()
%EIP712.Typed.Type{fields: [{"items", {:array, :string}}]}
Link to this function

deserialize_type(other_type)

@spec deserialize_type(String.t()) :: field_type()

Deserializes a primitive or custom type. We differentiate custom types by not being a primitive type.

Examples

iex> EIP712.Typed.Type.deserialize_type("address")
:address

iex> EIP712.Typed.Type.deserialize_type("uint256")
{:uint, 256}

iex> EIP712.Typed.Type.deserialize_type("bytes32")
{:bytes, 32}

iex> EIP712.Typed.Type.deserialize_type("string[]")
{:array, :string}

iex> EIP712.Typed.Type.deserialize_type("Person")
"Person"
Link to this function

deserialize_value!(value, arg2)

@spec deserialize_value!(term(), primitive()) :: term()

Deserializes a value of a given type for being stored in this struct.

Examples

iex> EIP712.Typed.Type.deserialize_value!("0x0000000000000000000000000000000000000001", :address)
<<1::160>>

iex> EIP712.Typed.Type.deserialize_value!(55, {:uint, 256})
55

iex> EIP712.Typed.Type.deserialize_value!("0x00000000000000000000000000000000000000000000000000000000000000CC", {:bytes, 32})
<<0xCC::256>>

iex> EIP712.Typed.Type.deserialize_value!("0xCC", {:bytes, 32})
<<0xCC::256>>

iex> EIP712.Typed.Type.deserialize_value!("Cow", :string)
"Cow"
Link to this function

encode_data_value(value, arg2)

@spec encode_data_value(term(), primitive()) :: term()

Encodes a value for encodeData, as per the EIP-712 spec. Specifically, raw values are expanded to 32-bytes, and dynamic types are hashed.

Examples

iex> EIP712.Typed.Type.encode_data_value(<<1::160>>, :address)
<<1::256>>

iex> EIP712.Typed.Type.encode_data_value(55, {:uint, 256})
<<0::248, 55>>

iex> EIP712.Typed.Type.encode_data_value(<<0xCC>>, {:bytes, 32})
<<0::248, 0xCC>>

iex> EIP712.Typed.Type.encode_data_value("foo", :string) |> EIP712.Util.encode_hex()
"0x41b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d"

iex> EIP712.Typed.Type.encode_data_value(["foo", "bar"], {:array, :string}) |> EIP712.Util.encode_hex()
"0x744766909640c85c19ca00139e7af3c5d9cb8dbfbc6635812eedc4e3cbf4fce6"
Link to this function

serialize(type)

@spec serialize(t()) :: [%{name: String.t(), type: String.t()}]

Serializes a Type, such that it can be used with JSON or JavaScript.

Examples

iex> %EIP712.Typed.Type{fields: [{"from", "Person"}, {"to", "Person"}, {"contents", :string}]}
...> |> EIP712.Typed.Type.serialize()
[%{
  "name" => "from",
  "type" => "Person",
}, %{
  "name" => "to",
  "type" => "Person",
}, %{
  "name" => "contents",
  "type" => "string",
}]


iex> %EIP712.Typed.Type{fields: [{"items", {:array, :string}}]}
...> |> EIP712.Typed.Type.serialize()
[%{
  "name" => "items",
  "type" => "string[]",
}]
Link to this function

serialize_type(custom_type)

@spec serialize_type(field_type()) :: String.t()

Serializes a primitive or custom type.

Examples

iex> EIP712.Typed.Type.serialize_type(:address)
"address"

iex> EIP712.Typed.Type.serialize_type({:uint, 256})
"uint256"

iex> EIP712.Typed.Type.serialize_type({:bytes, 32})
"bytes32"

iex> EIP712.Typed.Type.serialize_type({:array, :string})
"string[]"

iex> EIP712.Typed.Type.serialize_type("Person")
"Person"
Link to this function

serialize_value(value, arg2)

@spec serialize_value(term(), primitive()) :: term()

Serializes a value of a given type to pass to JSON or JavaScript.

Examples

iex> EIP712.Typed.Type.serialize_value(<<1::160>>, :address)
"0x0000000000000000000000000000000000000001"

iex> EIP712.Typed.Type.serialize_value(55, {:uint, 256})
55

iex> EIP712.Typed.Type.serialize_value(<<0xCC::256>>, {:bytes, 32})
"0x00000000000000000000000000000000000000000000000000000000000000cc"

iex> EIP712.Typed.Type.serialize_value(<<0xCC>>, {:bytes, 32})
"0x00000000000000000000000000000000000000000000000000000000000000cc"

iex> EIP712.Typed.Type.serialize_value("Cow", :string)
"Cow"

iex> EIP712.Typed.Type.serialize_value(["foo", "bar"], {:array, :string})
["foo", "bar"]