View Source Signet.Typed.Type (Signet v0.1.7)

Link to this section 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.

Link to this section Types

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

Link to this section Functions

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

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

examples

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}]}
Link to this function

deserialize_type(custom_type)

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

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

examples

Examples

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

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"
Link to this function

deserialize_value!(value, arg2)

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

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

examples

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"
Link to this function

encode_data_value(value, arg2)

View Source
@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

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>>
@spec serialize(t()) :: [%{name: String.t(), type: String.t()}]

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

examples

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",
}]
Link to this function

serialize_type(custom_type)

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

Serializes a primitive or custom type.

examples

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("Person")
"Person"
Link to this function

serialize_value(value, arg2)

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

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

examples

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"