Skip to content
This repository was archived by the owner on Jun 12, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions src/miner_gateway_ecc_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
%%%-------------------------------------------------------------------
-module(miner_gateway_ecc_worker).

-include_lib("public_key/include/public_key.hrl").

-behaviour(gen_server).

-export([
Expand All @@ -29,7 +31,8 @@
connection_monitor :: reference(),
host = "localhost" :: string(),
port = 4468 :: integer(),
transport = tcp :: tcp | ssl
transport = tcp :: tcp | ssl,
key = libp2p_crypto:generate_keys(ecc_compact)
}).

-define(CONNECT_RETRY_WAIT, 100).
Expand Down Expand Up @@ -91,18 +94,31 @@ handle_call(pubkey, _From, State = #state{connection = Connection}) ->
{reply, {ok, Reply}, State};
handle_call({sign, Binary}, _From, State = #state{connection = Connection}) ->
Reply =
case rpc(Connection, #{data => Binary}, sign, ?MAX_RETRIES) of
{ok, #{signature := Signature}} -> Signature;
Error -> Error
case application:get_env(miner, stub_miner_ecc, true) of
true ->
#state{key = #{secret := Secret}} = State,
public_key:sign(Binary, sha256, Secret);
_ ->
case rpc(Connection, #{data => Binary}, sign, ?MAX_RETRIES) of
{ok, #{signature := Signature}} -> Signature;
Error -> Error
end
end,
{reply, {ok, Reply}, State};
handle_call({ecdh, PubKey}, _From, State = #state{connection = Connection}) ->
Reply =
case
rpc(Connection, #{address => libp2p_crypto:pubkey_to_bin(PubKey)}, ecdh, ?MAX_RETRIES)
of
{ok, #{secret := Secret}} -> Secret;
Error -> Error
case application:get_env(miner, stub_miner_ecc, true) of
true ->
#state{key = #{secret := Secret}} = State,
{ecc_compact, {PK, {namedCurve, ?secp256r1}}} = PubKey,
public_key:compute_key(PK, Secret);
_ ->
case
rpc(Connection, #{address => libp2p_crypto:pubkey_to_bin(PubKey)}, ecdh, ?MAX_RETRIES)
of
{ok, #{secret := Secret}} -> Secret;
Error -> Error
end
end,
{reply, {ok, Reply}, State};
handle_call(_Msg, _From, State) ->
Expand Down
2 changes: 1 addition & 1 deletion src/miner_gateway_port.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
-define(CONNECT_RETRY_WAIT, 100).
-define(CONNECT_ATTEMPTS, 5).
-define(HEALTHCHECK_INTERVAL, 600000).
-define(HEALTHCHECK_TIMEOUT, 4000).
-define(HEALTHCHECK_TIMEOUT, 10000).

start_link(Options) when is_list(Options) ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [Options], []).
Expand Down