Packages

Ethereum blockchain client for Gleam

Current section

Files

Jump to
glethers src glethers@primitives.erl
Raw

src/glethers@primitives.erl

-module(glethers@primitives).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([from_string/1, from_uint256/1, from_bytes32/1, from_address/1, from/1, eip712_encode/1]).
-export_type([primitive/0]).
-type primitive() :: {string, binary()} |
{uint256, glethers@primitives@integer:uint256()} |
{bytes32, glethers@primitives@bytes:bytes32()} |
{address, glethers@address:address()}.
-spec from_string(binary()) -> primitive().
from_string(Value) ->
{string, Value}.
-spec from_uint256(glethers@primitives@integer:uint256()) -> primitive().
from_uint256(Value) ->
{uint256, Value}.
-spec from_bytes32(glethers@primitives@bytes:bytes32()) -> primitive().
from_bytes32(Value) ->
{bytes32, Value}.
-spec from_address(glethers@address:address()) -> primitive().
from_address(Value) ->
{address, Value}.
-spec from(any()) -> {ok, primitive()} |
{error, list(gleam@dynamic:decode_error())}.
from(Value) ->
A = gleam@dynamic:from(Value),
Decoder = gleam@dynamic:any(
[gleam@dynamic:decode1(
fun(Field@0) -> {string, Field@0} end,
fun gleam@dynamic:string/1
),
gleam@dynamic:decode1(
fun(Field@0) -> {uint256, Field@0} end,
fun glethers@primitives@integer:decoder/1
),
gleam@dynamic:decode1(
fun(Field@0) -> {bytes32, Field@0} end,
fun glethers@primitives@bytes:decoder/1
),
gleam@dynamic:decode1(
fun(Field@0) -> {address, Field@0} end,
fun glethers@address:decoder/1
)]
),
Decoder(A).
-spec eip712_encode(primitive()) -> bitstring().
eip712_encode(Value) ->
case Value of
{string, Value@1} ->
'Elixir.ExKeccak':hash_256(
begin
_pipe = Value@1,
gleam_stdlib:identity(_pipe)
end
);
{uint256, Value@2} ->
_pipe@1 = Value@2,
glethers@primitives@integer:to_bit_array(_pipe@1);
{bytes32, Value@3} ->
_pipe@2 = Value@3,
glethers@primitives@bytes:to_bit_array(_pipe@2);
{address, Value@4} ->
_pipe@3 = Value@4,
glethers@address:to_bit_array(_pipe@3)
end.