Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1312 from helium/andymck/verify-poc-secret
Browse files Browse the repository at this point in the history
verify poc key pair
  • Loading branch information
evanmcc committed Apr 25, 2022
2 parents b325960 + 7ca46ed commit 5e123b0
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/transactions/v2/blockchain_txn_poc_receipts_v2.erl
Expand Up @@ -187,7 +187,6 @@ check_is_valid_poc(POCVersion, Txn, Chain) ->
Ledger = blockchain:ledger(Chain),
Challenger = ?MODULE:challenger(Txn),
POCOnionKeyHash = ?MODULE:onion_key_hash(Txn),
BlockHash = ?MODULE:block_hash(Txn),
POCID = ?MODULE:poc_id(Txn),
StartPre = maybe_start_duration(),
case blockchain_ledger_v1:find_public_poc(POCOnionKeyHash, Ledger) of
Expand All @@ -198,10 +197,12 @@ check_is_valid_poc(POCVersion, Txn, Chain) ->
Error;
{ok, PoC} ->
Secret = ?MODULE:secret(Txn),
case blockchain_ledger_poc_v3:verify(PoC, Challenger, BlockHash) of
false ->
{error, invalid_poc};
true ->
Keys = libp2p_crypto:keys_from_bin(Secret),
case verify_poc_details(Txn, PoC, Keys) of
{error, _Reason} = Error ->
lager:debug("invalid poc ~p. Reason ~p", [POCOnionKeyHash, _Reason]),
Error;
ok ->
PrePocBlockHeight = blockchain_ledger_poc_v3:start_height(PoC),
case blockchain:get_block_info(PrePocBlockHeight, Chain) of
{error, Reason}=Error ->
Expand All @@ -216,7 +217,6 @@ check_is_valid_poc(POCVersion, Txn, Chain) ->
{ok, OldLedger} = blockchain:ledger_at(BlockHeight, Chain),
StartFT = maybe_log_duration(ledger_at, StartLA),
Vars = vars(OldLedger),
Keys = libp2p_crypto:keys_from_bin(Secret),
Entropy = <<POCOnionKeyHash/binary, PrePoCBlockHash/binary>>,
{Path, StartP} = get_path(POCVersion, Challenger, BlockTime, Entropy, Keys, Vars, OldLedger, Ledger, StartFT),
N = erlang:length(Path),
Expand Down Expand Up @@ -304,6 +304,7 @@ connections(Txn) ->
end, blockchain_poc_path_element_v1:witnesses(PathElement)) ++ Acc
end, [], TaggedPaths).


-spec poc_particpants(Txn :: txn_poc_receipts(),
Chain :: blockchain:blockchain()) -> [libp2p_crypto:pubkey_bin()].
poc_particpants(Txn, Chain) ->
Expand Down Expand Up @@ -1173,6 +1174,38 @@ poc_version(Ledger) ->
{ok, V} -> V
end.

-spec verify_poc_details(
Txn :: txn_poc_receipts(),
PoC :: blockchain_ledger_poc_v3:poc(),
Keys :: map()
) -> ok | {error, atom()}.
verify_poc_details(Txn, PoC, Keys) ->
%% verify the secret (pub and priv keys) submitted by the challenger
%% are a valid key pair
%% to do this sign a msg with the priv key and verify its sig with
%% the pub key
%% we also verify the hash of the pub key matches the onion key hash
POCOnionKeyHash = ?MODULE:onion_key_hash(Txn),
BlockHash = ?MODULE:block_hash(Txn),
Challenger = ?MODULE:challenger(Txn),
#{public := PubKey, secret := PrivKey} = Keys,
OnionHash = crypto:hash(sha256, libp2p_crypto:pubkey_to_bin(PubKey)),
SigFun = libp2p_crypto:mk_sig_fun(PrivKey),
SignedPayload = SigFun(OnionHash),
case blockchain_ledger_poc_v3:verify(PoC, Challenger, BlockHash) of
false -> {error, mismatched_poc};
true ->
case POCOnionKeyHash == OnionHash of
false -> {error, mismatched_onion_key_hash};
true ->
case libp2p_crypto:verify(OnionHash, SignedPayload, PubKey) of
false -> {error, invalid_secret};
true -> ok
end

end
end.

%% ------------------------------------------------------------------
%% EUNIT Tests
%% ------------------------------------------------------------------
Expand Down

0 comments on commit 5e123b0

Please sign in to comment.