Packages

Ethereum library for Gleam - JSON-RPC client, transaction signing, ABI encoding, and wallet management on the BEAM

Current section

Files

Jump to
gleeth src gleeth@utils@validation.erl
Raw

src/gleeth@utils@validation.erl

-module(gleeth@utils@validation).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleeth/utils/validation.gleam").
-export([validate_address/1, validate_addresses/1, validate_hash/1, validate_address_from_line/1, validate_hex_bytes/3, validate_private_key/1, validate_public_key/1, validate_signature/1, validate_transaction_hash/1, validate_block_hash/1, validate_hex_amount/1, validate_call_data/1, validate_chain_id/1, validate_non_empty/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/gleeth/utils/validation.gleam", 10).
-spec validate_address(binary()) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
validate_address(Address) ->
case string:length(Address) of
42 ->
case gleam_stdlib:string_starts_with(Address, <<"0x"/utf8>>) of
true ->
case gleeth@utils@hex:is_valid_hex_chars(Address) of
true ->
{ok, Address};
false ->
{error,
{invalid_address,
<<"Address contains invalid hex characters"/utf8>>}}
end;
false ->
{error,
{invalid_address,
<<"42 character address must start with 0x"/utf8>>}}
end;
40 ->
case gleeth@utils@hex:is_valid_hex_chars(Address) of
true ->
{ok, <<"0x"/utf8, Address/binary>>};
false ->
{error,
{invalid_address,
<<"Address contains invalid hex characters"/utf8>>}}
end;
_ ->
{error,
{invalid_address,
<<"Address must be 40 hex characters, optionally prefixed with 0x"/utf8>>}}
end.
-file("src/gleeth/utils/validation.gleam", 48).
-spec validate_addresses(list(binary())) -> {ok, list(binary())} |
{error, gleeth@rpc@types:gleeth_error()}.
validate_addresses(Addresses) ->
gleam@list:try_map(Addresses, fun validate_address/1).
-file("src/gleeth/utils/validation.gleam", 56).
-spec validate_hash(binary()) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
validate_hash(Hash) ->
case string:length(Hash) of
66 ->
case gleam_stdlib:string_starts_with(Hash, <<"0x"/utf8>>) of
true ->
{ok, Hash};
false ->
{error,
{invalid_hash,
<<"66 character hash must start with 0x"/utf8>>}}
end;
64 ->
{ok, <<"0x"/utf8, Hash/binary>>};
_ ->
{error,
{invalid_hash,
<<"Hash must be 64 hex characters, optionally prefixed with 0x"/utf8>>}}
end.
-file("src/gleeth/utils/validation.gleam", 77).
-spec validate_address_from_line(binary()) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
validate_address_from_line(Line) ->
Trimmed = gleam@string:trim(Line),
case Trimmed of
<<""/utf8>> ->
{error, {config_error, <<"Empty line"/utf8>>}};
_ ->
case gleam_stdlib:string_starts_with(Trimmed, <<"#"/utf8>>) of
true ->
{error, {config_error, <<"Comment line"/utf8>>}};
false ->
validate_address(Trimmed)
end
end.
-file("src/gleeth/utils/validation.gleam", 99).
?DOC(" Validate hex string with specific byte length requirement\n").
-spec validate_hex_bytes(binary(), integer(), binary()) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
validate_hex_bytes(Hex_string, Expected_bytes, Description) ->
case gleeth@utils@hex:is_valid_hex_chars(Hex_string) of
false ->
{error,
{parse_error,
<<Description/binary,
" contains invalid hex characters"/utf8>>}};
true ->
case gleeth@utils@hex:validate_length(Hex_string, Expected_bytes) of
{ok, _} ->
{ok, gleeth@utils@hex:normalize(Hex_string)};
{error, Msg} ->
{error,
{parse_error,
<<<<Description/binary, ": "/utf8>>/binary,
Msg/binary>>}}
end
end.
-file("src/gleeth/utils/validation.gleam", 121).
?DOC(" Validate private key format (32 bytes)\n").
-spec validate_private_key(binary()) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
validate_private_key(Private_key) ->
validate_hex_bytes(Private_key, 32, <<"Private key"/utf8>>).
-file("src/gleeth/utils/validation.gleam", 128).
?DOC(" Validate public key format (33 or 65 bytes)\n").
-spec validate_public_key(binary()) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
validate_public_key(Public_key) ->
Clean = gleeth@utils@hex:strip_prefix(Public_key),
case string:length(Clean) of
66 ->
validate_hex_bytes(Public_key, 33, <<"Compressed public key"/utf8>>);
130 ->
validate_hex_bytes(
Public_key,
65,
<<"Uncompressed public key"/utf8>>
);
_ ->
{error,
{parse_error,
<<"Public key must be 33 bytes (compressed) or 65 bytes (uncompressed)"/utf8>>}}
end.
-file("src/gleeth/utils/validation.gleam", 143).
?DOC(" Validate signature format (65 bytes: r + s + v)\n").
-spec validate_signature(binary()) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
validate_signature(Signature) ->
validate_hex_bytes(Signature, 65, <<"Signature"/utf8>>).
-file("src/gleeth/utils/validation.gleam", 150).
?DOC(" Validate Ethereum transaction hash\n").
-spec validate_transaction_hash(binary()) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
validate_transaction_hash(Hash) ->
validate_hex_bytes(Hash, 32, <<"Transaction hash"/utf8>>).
-file("src/gleeth/utils/validation.gleam", 157).
?DOC(" Validate block hash\n").
-spec validate_block_hash(binary()) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
validate_block_hash(Hash) ->
validate_hex_bytes(Hash, 32, <<"Block hash"/utf8>>).
-file("src/gleeth/utils/validation.gleam", 164).
?DOC(" Validate amount/value field (can be any valid hex number)\n").
-spec validate_hex_amount(binary()) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
validate_hex_amount(Amount) ->
case Amount of
<<""/utf8>> ->
{error, {parse_error, <<"Amount cannot be empty"/utf8>>}};
_ ->
case gleeth@utils@hex:is_valid_hex_chars(Amount) of
true ->
{ok, gleeth@utils@hex:normalize(Amount)};
false ->
{error,
{parse_error,
<<"Amount contains invalid hex characters"/utf8>>}}
end
end.
-file("src/gleeth/utils/validation.gleam", 179).
?DOC(" Validate contract call data\n").
-spec validate_call_data(binary()) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
validate_call_data(Data) ->
case Data of
<<"0x"/utf8>> ->
{ok, <<"0x"/utf8>>};
_ ->
case gleeth@utils@hex:is_valid_hex_chars(Data) of
true ->
Clean = gleeth@utils@hex:strip_prefix(Data),
case string:length(Clean) rem 2 of
0 ->
{ok, gleeth@utils@hex:normalize(Data)};
_ ->
{error,
{parse_error,
<<"Call data must have even number of hex characters"/utf8>>}}
end;
false ->
{error,
{parse_error,
<<"Call data contains invalid hex characters"/utf8>>}}
end
end.
-file("src/gleeth/utils/validation.gleam", 206).
?DOC(" Validate chain ID (must be positive integer)\n").
-spec validate_chain_id(integer()) -> {ok, integer()} |
{error, gleeth@rpc@types:gleeth_error()}.
validate_chain_id(Chain_id) ->
case Chain_id > 0 of
true ->
{ok, Chain_id};
false ->
{error,
{config_error, <<"Chain ID must be a positive integer"/utf8>>}}
end.
-file("src/gleeth/utils/validation.gleam", 214).
?DOC(" Generic validator for non-empty strings\n").
-spec validate_non_empty(binary(), binary()) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
validate_non_empty(Value, Field_name) ->
case gleam@string:trim(Value) of
<<""/utf8>> ->
{error,
{config_error, <<Field_name/binary, " cannot be empty"/utf8>>}};
Trimmed ->
{ok, Trimmed}
end.