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
field()
@type field() :: {String.t(), field_type()}
field_type()
primitive()
@type t() :: %EIP712.Typed.Type{fields: type_list()}
type_list()
@type type_list() :: [field()]
Functions
deserialize(types)
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}}]}
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"
deserialize_value!(value, arg2)
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"
encode_data_value(value, arg2)
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"
serialize(type)
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[]",
}]
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"
serialize_value(value, arg2)
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"]