View Source Signet.Typed.Type (Signet v1.3.8)
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("bool")
:bool
iex> Signet.Typed.Type.deserialize_type("bytes32[]")
{:array, {:bytes, 32}}
iex> Signet.Typed.Type.deserialize_type("Person")
"Person"
iex> Signet.Typed.Type.deserialize_type("bag")
** (RuntimeError) unknown type: bag
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!(true, :bool)
true
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>>
iex> Signet.Typed.Type.deserialize_value!(["0xCCDD", "0xEE"], {:array, :bytes})
[<<0xCC, 0xDD>>, <<0xEE>>]
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)
~h[9014B850703629D30F5C8C6C86A6AD981AB9319997490629D7DA37E8CAE985A1]
iex> Signet.Typed.Type.encode_data_value("Cow", :string)
~h[8C1D2BD5348394761719DA11EC67EEDAE9502D137E8940FEE8ECD6F641EE1648]
iex> Signet.Typed.Type.encode_data_value([<<0xCC, 0xDD>>, <<0xEE>>], {:array, :bytes})
~h[134619415A3C9FE841D99F7CFD5C0BCCFC7CF0DAE90743A3D717C748A3961CF5]
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(:bool)
"bool"
iex> Signet.Typed.Type.serialize_type({:array, :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(true, :bool)
true
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"
iex> Signet.Typed.Type.serialize_value([<<0xCC, 0xDD>>, <<0xEE>>], {:array, :bytes})
["0xccdd", "0xee"]