From 2946ac84f8dd13522b417c41c018a162a9539bf9 Mon Sep 17 00:00:00 2001 From: Sean Hinde Date: Thu, 12 Jan 2023 15:12:13 +0100 Subject: [PATCH] Updated README --- README.md | 17 +++-------------- rebar.lock | 13 ++++++++++++- src/ecrecover_util.erl | 28 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 src/ecrecover_util.erl diff --git a/README.md b/README.md index 4a12097..41f247f 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,14 @@ # ecrecover -FFI (NIF) export of Ethereum's ecrecover for use from Erlang. +FFI (NIF) export of Ethereum's ecrecover for use from Erlang based on +https://github.com/bitcoin-core/secp256k1.git -### Prerequisites -The NIF is written in Rust, therefore the following additional build -dependencies are needed: - -- Rust -- Cargo -- Cmake ### Build Execute: ``` -make +./rebar3 compile ``` -To disable the local build of the NIF library, e.g. to use a prebuilt binary, -use the following command: -``` -ECRECOVER_DISABLE_NIF_BUILD=true make -``` ## Erlang integration diff --git a/rebar.lock b/rebar.lock index 57afcca..2ca880d 100644 --- a/rebar.lock +++ b/rebar.lock @@ -1 +1,12 @@ -[]. +{"1.2.0", +[{<<"hex2bin">>,{pkg,<<"hex2bin">>,<<"1.0.0">>},1}, + {<<"sha3">>, + {git,"https://github.com/aeternity/erlang-sha3", + {ref,"b5f27a29ba1179e5907c50d7ec7aa79b2857e981"}}, + 0}]}. +[ +{pkg_hash,[ + {<<"hex2bin">>, <<"AAC26EAB998AE80EACEE1C7607C629AB503EBF77A62B9242BAE2B94D47DCB71E">>}]}, +{pkg_hash_ext,[ + {<<"hex2bin">>, <<"E7012D1D9AADD26E680F0983D26FB8923707F05FAC9688F19F530FA3795E716F">>}]} +]. diff --git a/src/ecrecover_util.erl b/src/ecrecover_util.erl new file mode 100644 index 0000000..6d154b7 --- /dev/null +++ b/src/ecrecover_util.erl @@ -0,0 +1,28 @@ +-module(ecrecover_util). + +-export([ recover_from_hex/1 + , bin_to_hex/1 + , hex_to_bin/1 + ]). + +%%============================================================================= +%% External API + +recover_from_hex(Input) -> + <> = hex_to_bin(Input), + PubKey = ecrecover:recover(Hash, Sig), + bin_to_hex(PubKey). + +bin_to_hex(Bin) -> + lists:flatten([io_lib:format("~2.16.0B", [X]) || X <- binary_to_list(Bin)]). + +hex_to_bin(S) -> + hex_to_bin(S, []). +hex_to_bin([], Acc) -> + list_to_binary(lists:reverse(Acc)); +hex_to_bin([X,Y|T], Acc) -> + {ok, [V], []} = io_lib:fread("~16u", [X,Y]), + hex_to_bin(T, [V | Acc]); +hex_to_bin([X|T], Acc) -> + {ok, [V], []} = io_lib:fread("~16u", lists:flatten([X,"0"])), + hex_to_bin(T, [V | Acc]).