View Source Signet.Typed.Type (Signet v1.0.0-alpha7)
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 t() :: %Signet.Typed.Type{fields: type_list()}
@type type_list() :: [{String.t(), field_type()}]
Functions
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",
...> }]
...> |> Signet.Typed.Type.deserialize()
%Signet.Typed.Type{fields: [{"from", "Person"}, {"to", "Person"}, {"contents", :string}]}
@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> Signet.Typed.Type.deserialize_type("address")
:address
iex> Signet.Typed.Type.deserialize_type("bytes")
:bytes
iex> Signet.Typed.Type.deserialize_type("uint256")
{:uint, 256}
iex> Signet.Typed.Type.deserialize_type("bytes32")
{:bytes, 32}
iex> Signet.Typed.Type.deserialize_type("Person")
"Person"
Deserializes a value of a given type for being stored in this struct.
Examples
iex> Signet.Typed.Type.deserialize_value!("0x0000000000000000000000000000000000000001", :address)
<<1::160>>
iex> Signet.Typed.Type.deserialize_value!(55, {:uint, 256})
55
iex> Signet.Typed.Type.deserialize_value!("0x00000000000000000000000000000000000000000000000000000000000000CC", {:bytes, 32})
<<0xCC::256>>
iex> Signet.Typed.Type.deserialize_value!("0xCC", {:bytes, 32})
<<0xCC::256>>
iex> Signet.Typed.Type.deserialize_value!("Cow", :string)
"Cow"
iex> Signet.Typed.Type.deserialize_value!("0xCCDD", :bytes)
<<0xCC, 0xDD>>
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> Signet.Typed.Type.encode_data_value(<<1::160>>, :address)
<<1::256>>
iex> Signet.Typed.Type.encode_data_value(55, {:uint, 256})
<<0::248, 55>>
iex> Signet.Typed.Type.encode_data_value(<<0xCC>>, {:bytes, 32})
<<0::248, 0xCC>>
iex> Signet.Typed.Type.encode_data_value(<<0xCC, 0xDD>>, :bytes)
<<144, 20, 184, 80, 112, 54, 41, 211, 15, 92, 140, 108, 134, 166, 173, 152, 26, 185, 49, 153, 151, 73, 6, 41, 215, 218, 55, 232, 202, 233, 133, 161>>
iex> Signet.Typed.Type.encode_data_value("Cow", :string)
<<140, 29, 43, 213, 52, 131, 148, 118, 23, 25, 218, 17, 236, 103, 238, 218, 233, 80, 45, 19, 126, 137, 64, 254, 232, 236, 214, 246, 65, 238, 22, 72>>
Serializes a Type, such that it can be used with JSON or JavaScript.
Examples
iex> %Signet.Typed.Type{fields: [{"from", "Person"}, {"to", "Person"}, {"contents", :string}]}
...> |> Signet.Typed.Type.serialize()
[%{
"name" => "from",
"type" => "Person",
}, %{
"name" => "to",
"type" => "Person",
}, %{
"name" => "contents",
"type" => "string",
}]
@spec serialize_type(field_type()) :: String.t()
Serializes a primitive or custom type.
Examples
iex> Signet.Typed.Type.serialize_type(:address)
"address"
iex> Signet.Typed.Type.serialize_type({:uint, 256})
"uint256"
iex> Signet.Typed.Type.serialize_type({:bytes, 32})
"bytes32"
iex> Signet.Typed.Type.serialize_type(:bytes)
"bytes"
iex> Signet.Typed.Type.serialize_type("Person")
"Person"
Serializes a value of a given type to pass to JSON or JavaScript.
Examples
iex> Signet.Typed.Type.serialize_value(<<1::160>>, :address)
"0x0000000000000000000000000000000000000001"
iex> Signet.Typed.Type.serialize_value(55, {:uint, 256})
55
iex> Signet.Typed.Type.serialize_value(<<0xCC::256>>, {:bytes, 32})
"0x00000000000000000000000000000000000000000000000000000000000000CC"
iex> Signet.Typed.Type.serialize_value(<<0xCC>>, {:bytes, 32})
"0x00000000000000000000000000000000000000000000000000000000000000CC"
iex> Signet.Typed.Type.serialize_value("Cow", :string)
"Cow"
iex> Signet.Typed.Type.serialize_value(<<0xCC, 0xDD>>, :bytes)
"0xCCDD"