Skip to content

Commit

Permalink
Merge pull request #2713 from aeternity/PT-168132312-new-name-hash
Browse files Browse the repository at this point in the history
Pt 168132312 new name hash
  • Loading branch information
ThomasArts committed Sep 2, 2019
2 parents 3d5959f + b2337ba commit e73c1b0
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 16 deletions.
6 changes: 5 additions & 1 deletion apps/aecore/src/aec_governance.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
name_protection_period/0,
name_claim_preclaim_delta/0,
name_registrars/1,
non_test_registrars/0,
possible_name_registrars/0,
micro_block_cycle/0,
accepted_future_block_time_shift/0,
Expand Down Expand Up @@ -246,10 +247,13 @@ name_claim_preclaim_delta() ->

-spec name_registrars(aec_hard_forks:protocol_vsn()) -> list(binary()).
name_registrars(Protocol) when Protocol >= ?LIMA_PROTOCOL_VSN ->
[<<"aet">>];
non_test_registrars();
name_registrars(_Protocol) ->
[<<"test">>].

non_test_registrars() ->
[<<"aet">>].

%% union of all name_registrars above disregarding the height
possible_name_registrars() ->
[<<"aet">>, <<"test">>].
Expand Down
5 changes: 1 addition & 4 deletions apps/aens/src/aens.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
%%% Types
%%%===================================================================

-define(LABEL_SEPARATOR, <<".">>).

%%%===================================================================
%%% API
%%%===================================================================
Expand Down Expand Up @@ -77,7 +75,7 @@ get_name_hash(Name) when is_binary(Name) ->
%%%===================================================================

is_name(Bin) ->
length(binary:split(Bin, ?LABEL_SEPARATOR)) > 1.
length(aens_utils:name_parts(Bin)) > 1.

name_to_name_hash(Name) ->
case aens_utils:to_ascii(Name) of
Expand Down Expand Up @@ -111,4 +109,3 @@ find_pointer_id(Key, [Pointer | Rest]) ->
end;
find_pointer_id(_Key, []) ->
{error, pointer_id_not_found}.

45 changes: 42 additions & 3 deletions apps/aens/src/aens_hash.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
%%% @copyright (C) 2018, Aeternity Anstalt
%%% @doc
%%% Naming System hashing functions
%%%
%%% We hash names in .test domain in a legacy format, whereas names
%%% in the .aet domain are hashed differently.
%%% If additional domains are added, we need to make their hash function
%%% explicit in this code.
%%%
%%% @end
%%%=============================================================================

Expand All @@ -28,14 +34,43 @@
%%% API
%%%===================================================================


-spec commitment_hash(binary(), integer()) -> commitment_hash().
commitment_hash(NameAscii, Salt) ->
NameHash = name_hash(NameAscii),
SaltBin = int_to_bin(Salt),
hash(<<NameHash/binary, SaltBin/binary>>).
case aens_utils:name_domain(NameAscii) of
{ok, Domain} ->
case lists:member(Domain, aec_governance:non_test_registrars()) of
true ->
assert_salt_positive(Salt),
SaltBin = int_to_bin(Salt),
hash(<<NameAscii/binary, SaltBin/binary>>);
false ->
pre_lima_commitment_hash(NameAscii, Salt)
end;
_ ->
%% This could be .test or any other wrong name backward compatible
pre_lima_commitment_hash(NameAscii, Salt)
end.

-spec name_hash(binary()) -> name_hash().
name_hash(NameAscii) ->
case aens_utils:name_domain(NameAscii) of
{ok, Domain} when Domain =:= <<"aet">> ->
hash(NameAscii);
_ ->
%% This could be .test or any other wrong name backward compatible
pre_lima_name_hash(NameAscii)
end.


-spec pre_lima_commitment_hash(binary(), integer()) -> commitment_hash().
pre_lima_commitment_hash(NameAscii, Salt) ->
NameHash = pre_lima_name_hash(NameAscii),
SaltBin = int_to_bin(Salt),
hash(<<NameHash/binary, SaltBin/binary>>).

-spec pre_lima_name_hash(binary()) -> name_hash().
pre_lima_name_hash(NameAscii) ->
Labels = binary:split(NameAscii, <<".">>, [global]),
hash_labels(lists:reverse(Labels)).

Expand All @@ -58,3 +93,7 @@ hash(Bin) ->

int_to_bin(Int) ->
<<Int:?COMMITMENT_HASH_BYTES/integer-unit:8>>.

assert_salt_positive(Salt) when Salt > 0 -> ok;
assert_salt_positive(_) ->
error(illegal_salt_value).
24 changes: 19 additions & 5 deletions apps/aens/src/aens_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

%% API
-export([check_name_claimed_and_owned/3,
name_parts/1,
name_parts/1, name_join/1,
name_domain/1,
to_ascii/1]).

%%%===================================================================
Expand Down Expand Up @@ -44,6 +45,22 @@ to_ascii(Name) when is_binary(Name)->
E
end.

-spec name_parts(binary()) -> [binary()].
name_parts(Name) ->
binary:split(Name, ?LABEL_SEPARATOR, [global, trim]).

%% inverse of name_parts
-spec name_join([binary()]) -> binary().
name_join(List) when is_list(List) ->
iolist_to_binary(lists:join(?LABEL_SEPARATOR, List)).

-spec name_domain(binary()) -> {ok, binary()} | {error, invalid_name}.
name_domain(Name) ->
case lists:reverse(name_parts(Name)) of
[Domain | _] -> {ok, Domain};
_ -> {error, invalid_name}
end.

%%%===================================================================
%%% Internal functions
%%%===================================================================
Expand All @@ -60,9 +77,6 @@ check_claimed_status(Name) ->
revoked -> {error, name_revoked}
end.

name_parts(Name) ->
binary:split(Name, ?LABEL_SEPARATOR, [global, trim]).

validate_name(Name) ->
case name_parts(Name) of
[_Label, RegistrarNS] ->
Expand All @@ -81,7 +95,7 @@ name_to_ascii(Name) when is_binary(Name) ->
try idna:encode(NameUnicodeList, [{uts46, true}, {std3_rules, true}]) of
NameAscii ->
%% idna:to_ascii(".aet") returns just "aet"
case length(string:split(NameAscii, ".", all)) =:= 1 of
case length(string:split(NameAscii, ?LABEL_SEPARATOR, all)) =:= 1 of
true -> {error, no_label_in_registrar};
false -> {ok, list_to_binary(NameAscii)}
end
Expand Down
2 changes: 1 addition & 1 deletion apps/aens/test/aens_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ groups() ->
-define(NAME, <<"詹姆斯詹姆斯"/utf8>>).
-define(PRE_CLAIM_HEIGHT, 1).

init_per_group(Group, Cfg) ->
init_per_group(_Group, Cfg) ->
%% dependening on how we call 'make' we get different protocols
[{protocol, aec_hard_forks:protocol_effective_at_height(1)} | Cfg].

Expand Down
3 changes: 1 addition & 2 deletions apps/aens/test/aens_test_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fullname_in_protocol(RootName, Protocol) ->
true -> <<"aet">>;
false -> <<"test">>
end,
<<RootName/binary, ".", Reg/binary>>.
aens_utils:name_join([RootName, Reg]).

%%%===================================================================
%%% Names utils
Expand Down Expand Up @@ -232,4 +232,3 @@ revoke_tx_spec(PubKey, NameHash, Spec0, State) ->
revoke_tx_default_spec(PubKey, State) ->
#{nonce => try next_nonce(PubKey, State) catch _:_ -> 0 end,
fee => 50000 * aec_test_utils:min_gas_price()}.

3 changes: 3 additions & 0 deletions docs/release-notes/next-lima/PT-168132312-new-name-hash
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* Name preclaims are no longer allowed to use 0 as Salt value
* New name hash computation

0 comments on commit e73c1b0

Please sign in to comment.