Current section
Files
Jump to
Current section
Files
src/gleeth@crypto@keccak.erl
-module(gleeth@crypto@keccak).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleeth/crypto/keccak.gleam").
-export([function_selector/1, keccak256_hex/1, event_topic/1, keccak256_hex_no_prefix/1, verify_function_selector/2, keccak256_binary/1, keccak256_string/1, hash_binary_to_hex/1]).
-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/crypto/keccak.gleam", 57).
?DOC(" Generate Ethereum function selector (first 4 bytes of keccak256 hash)\n").
-spec function_selector(binary()) -> {ok, binary()} | {error, binary()}.
function_selector(Signature) ->
Input_bytes = gleam_stdlib:identity(Signature),
Hash_bytes = 'Elixir.ExKeccak':hash_256(Input_bytes),
First_4_bytes = gleam_stdlib:bit_array_slice(Hash_bytes, 0, 4),
case First_4_bytes of
{ok, Bytes} ->
{ok,
<<"0x"/utf8,
(string:lowercase(gleam_stdlib:base16_encode(Bytes)))/binary>>};
{error, _} ->
{error,
<<"Failed to compute function selector for: "/utf8,
Signature/binary>>}
end.
-file("src/gleeth/crypto/keccak.gleam", 68).
?DOC(" Generate full keccak256 hash as hex string\n").
-spec keccak256_hex(binary()) -> binary().
keccak256_hex(Input) ->
Input_bytes = gleam_stdlib:identity(Input),
Hash_bytes = 'Elixir.ExKeccak':hash_256(Input_bytes),
<<"0x"/utf8,
(string:lowercase(gleam_stdlib:base16_encode(Hash_bytes)))/binary>>.
-file("src/gleeth/crypto/keccak.gleam", 75).
?DOC(" Generate event topic hash using keccak256\n").
-spec event_topic(binary()) -> binary().
event_topic(Signature) ->
keccak256_hex(Signature).
-file("src/gleeth/crypto/keccak.gleam", 80).
?DOC(" Utility functions for hex output without prefix\n").
-spec keccak256_hex_no_prefix(binary()) -> binary().
keccak256_hex_no_prefix(Input) ->
Hash = keccak256_hex(Input),
case gleam_stdlib:string_starts_with(Hash, <<"0x"/utf8>>) of
true ->
gleam@string:drop_start(Hash, 2);
false ->
Hash
end.
-file("src/gleeth/crypto/keccak.gleam", 89).
?DOC(" Verify function selector matches expected value\n").
-spec verify_function_selector(binary(), binary()) -> boolean().
verify_function_selector(Signature, Expected) ->
case function_selector(Signature) of
{ok, Computed} ->
Normalized_expected = case gleam_stdlib:string_starts_with(
Expected,
<<"0x"/utf8>>
) of
true ->
Expected;
false ->
<<"0x"/utf8, Expected/binary>>
end,
string:lowercase(Computed) =:= string:lowercase(Normalized_expected);
{error, _} ->
false
end.
-file("src/gleeth/crypto/keccak.gleam", 103).
?DOC(" Hash binary data using keccak256\n").
-spec keccak256_binary(bitstring()) -> bitstring().
keccak256_binary(Data) ->
'Elixir.ExKeccak':hash_256(Data).
-file("src/gleeth/crypto/keccak.gleam", 108).
?DOC(" Hash string data to binary using keccak256\n").
-spec keccak256_string(binary()) -> bitstring().
keccak256_string(Data) ->
Input_bytes = gleam_stdlib:identity(Data),
'Elixir.ExKeccak':hash_256(Input_bytes).
-file("src/gleeth/crypto/keccak.gleam", 114).
?DOC(" Hash binary data to hex string\n").
-spec hash_binary_to_hex(bitstring()) -> binary().
hash_binary_to_hex(Data) ->
Hash_result = keccak256_binary(Data),
<<"0x"/utf8,
(string:lowercase(gleam_stdlib:base16_encode(Hash_result)))/binary>>.