Add function_name_from_function_hash to aeb_fate_abi

This commit is contained in:
Hans Svensson 2019-06-18 10:08:57 +02:00
parent 506f9ca72e
commit a0c3a990ed
2 changed files with 29 additions and 1 deletions

View File

@ -9,7 +9,11 @@
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(aeb_fate_abi). -module(aeb_fate_abi).
-export([ create_calldata/2 ]). -export([ create_calldata/2
, function_name_from_function_hash/2
, get_function_hash_from_calldata/1 ]).
-include("../include/aeb_fate_data.hrl").
%%%=================================================================== %%%===================================================================
%%% API %%% API
@ -21,3 +25,24 @@ create_calldata(FunName, Args) ->
{ok, aeb_fate_encoding:serialize( {ok, aeb_fate_encoding:serialize(
aeb_fate_data:make_tuple({FunctionId, aeb_fate_data:make_tuple({FunctionId,
aeb_fate_data:make_tuple(list_to_tuple(Args))}))}. aeb_fate_data:make_tuple(list_to_tuple(Args))}))}.
-spec function_name_from_function_hash(any(), aeb_fate_code:fcode()) ->
{ok, term()} | {error, term()}.
function_name_from_function_hash(<<SymbolHash:4/binary, _:28/binary>>, FateCode) ->
function_name_from_function_hash(SymbolHash, FateCode);
function_name_from_function_hash(SymbolHash = <<_:4/binary>>, FateCode) ->
Symbols = aeb_fate_code:symbols(FateCode),
case maps:get(SymbolHash, Symbols, undefined) of
undefined -> {error, no_function_matching_function_hash};
Function -> {ok, Function}
end.
-spec get_function_hash_from_calldata(binary()) ->
{ok, binary()} | {error, term()}.
get_function_hash_from_calldata(CallData) ->
try ?FATE_TUPLE_ELEMENTS(aeb_fate_encoding:deserialize(CallData)) of
[FunHash, _Args] -> {ok, FunHash};
_ -> {error, bad_calldata}
catch _:_ ->
{error, bad_calldata}
end.

View File

@ -39,6 +39,9 @@
-define(HASH_BYTES, 32). -define(HASH_BYTES, 32).
-type fcode() :: #fcode{}.
-export_type([fcode/0]).
%%%=================================================================== %%%===================================================================
%%% API %%% API
%%%=================================================================== %%%===================================================================