From a0c3a990ed65466414214606e03257cc372e188e Mon Sep 17 00:00:00 2001 From: Hans Svensson Date: Tue, 18 Jun 2019 10:08:57 +0200 Subject: [PATCH] Add function_name_from_function_hash to aeb_fate_abi --- src/aeb_fate_abi.erl | 27 ++++++++++++++++++++++++++- src/aeb_fate_code.erl | 3 +++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/aeb_fate_abi.erl b/src/aeb_fate_abi.erl index 91f05b2..fc19b0a 100644 --- a/src/aeb_fate_abi.erl +++ b/src/aeb_fate_abi.erl @@ -9,7 +9,11 @@ %%%------------------------------------------------------------------- -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 @@ -21,3 +25,24 @@ create_calldata(FunName, Args) -> {ok, aeb_fate_encoding:serialize( aeb_fate_data:make_tuple({FunctionId, 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(<>, 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. diff --git a/src/aeb_fate_code.erl b/src/aeb_fate_code.erl index a60858b..d5289d9 100644 --- a/src/aeb_fate_code.erl +++ b/src/aeb_fate_code.erl @@ -39,6 +39,9 @@ -define(HASH_BYTES, 32). +-type fcode() :: #fcode{}. +-export_type([fcode/0]). + %%%=================================================================== %%% API %%%===================================================================