Remove not needed ecrecover_util module

This commit is contained in:
Sean Hinde 2023-01-13 17:55:29 +01:00
parent 0161d92f61
commit 71b9f14bc5
2 changed files with 13 additions and 39 deletions

View File

@ -15,15 +15,17 @@ Execute:
The shared library uses NIF. Use the Erlang file `src/ecrecover.erl` to use this: The shared library uses NIF. Use the Erlang file `src/ecrecover.erl` to use this:
``` ```
c("src/ecrecover"). ./rebar3 shell
c("src/ecrecover_util"). Erlang/OTP 25 [erts-13.1.3] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit] [dtrace]
Decoded = ecrecover_util:hex_to_bin("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b650acf9d3f5f0a2c799776a1254355d5f4061762a237396a99a0e0e3fc2bcd6729514a0dacb2e623ac4abd157cb18163ff942280db4d5caad66ddf941ba12e03"). Eshell V13.1.3 (abort with ^G)
List = binary:bin_to_list(Decoded).
Hash = binary:list_to_bin(lists:sublist(List, 1, 32)). 1> Decoded = binary:decode_hex(<<"47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b650acf9d3f5f0a2c799776a1254355d5f4061762a237396a99a0e0e3fc2bcd6729514a0dacb2e623ac4abd157cb18163ff942280db4d5caad66ddf941ba12e03">>).
Sig = binary:list_to_bin(lists:sublist(List, 64, 65)). 2> List = binary:bin_to_list(Decoded).
Input = <<Hash/binary, 0:(8*31), Sig/binary>>. 3> Hash = binary:list_to_bin(lists:sublist(List, 1, 32)).
binary:bin_to_list(Input) == binary:bin_to_list(Decoded). 4> Sig = binary:list_to_bin(lists:sublist(List, 64, 65)).
Result = ecrecover:recover(Hash, Sig). 5> Input = <<Hash/binary, 0:(8*31), Sig/binary>>.
Expected = ecrecover_util:hex_to_bin("000000000000000000000000c08b5542d177ac6686946920409741463a15dddb"). 6> binary:bin_to_list(Input) == binary:bin_to_list(Decoded).
binary:bin_to_list(Result) == binary:bin_to_list(Expected). %% check result 7> Result = ecrecover:recover(Hash, Sig).
8> Expected = binary:decode_hex(<<"000000000000000000000000c08b5542d177ac6686946920409741463a15dddb">>).
9> Result == Expected. %% check result
``` ```

View File

@ -1,28 +0,0 @@
-module(ecrecover_util).
-export([ recover_from_hex/1
, bin_to_hex/1
, hex_to_bin/1
]).
%%=============================================================================
%% External API
recover_from_hex(Input) ->
<<Hash:32/binary, _:31/binary, Sig:65/binary>> = 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]).