Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 79b43b0665 | |||
| a84dcc880d | |||
| a368c64f7e | |||
| dbfc013c8a |
@@ -117,6 +117,10 @@ tag(entry_transfer_tx) -> 142;
|
||||
tag(entry_destroy_tx) -> 143;
|
||||
tag(account_key_store) -> 144;
|
||||
tag(account_create_tx) -> 145;
|
||||
tag(account_sig_store) -> 146;
|
||||
tag(auth_tx) -> 147;
|
||||
tag(proposal_gossip_tx) -> 148;
|
||||
tag(account_auth_update_tx) -> 149;
|
||||
tag(pof) -> 200.
|
||||
|
||||
rev_tag(10) -> account;
|
||||
@@ -198,4 +202,8 @@ rev_tag(142) -> entry_transfer_tx;
|
||||
rev_tag(143) -> entry_destroy_tx;
|
||||
rev_tag(144) -> account_key_store;
|
||||
rev_tag(145) -> account_create_tx;
|
||||
rev_tag(146) -> account_sig_store;
|
||||
rev_tag(147) -> auth_tx;
|
||||
rev_tag(148) -> proposal_gossip_tx;
|
||||
rev_tag(149) -> account_auth_update_tx;
|
||||
rev_tag(200) -> pof.
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
, specialize/1
|
||||
, specialize/2
|
||||
, specialize_type/1
|
||||
, is_account/1
|
||||
, account_pubkey/1
|
||||
, to_map/1
|
||||
, is_id/1
|
||||
]).
|
||||
@@ -104,6 +106,20 @@ specialize_type(#id{tag = {Tag, _}}) when ?IS_TAG(Tag) ->
|
||||
specialize_type(#id{tag = Tag}) when ?IS_TAG(Tag) ->
|
||||
Tag.
|
||||
|
||||
-spec is_account(id() | term()) -> boolean().
|
||||
is_account(#id{tag = account}) ->
|
||||
true;
|
||||
is_account(#id{tag = {account, _}}) ->
|
||||
true;
|
||||
is_account(_) ->
|
||||
false.
|
||||
|
||||
-spec account_pubkey(id()) -> val().
|
||||
account_pubkey(#id{tag = account, val = Val}) when ?IS_VAL(Val) ->
|
||||
Val;
|
||||
account_pubkey(#id{tag = {account, _}, val = Val}) when ?IS_VAL(Val) ->
|
||||
Val.
|
||||
|
||||
-spec to_map(id()) -> id_map().
|
||||
to_map(#id{tag = {Tag, SubType}, val = Val}) when ?IS_TAG(Tag) ->
|
||||
#{ type => Tag
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
-module(gmser_id_tests).
|
||||
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
-define(PUBKEY, <<12345:32/unit:8>>).
|
||||
|
||||
is_account_test() ->
|
||||
{"is_account recognizes standard and extended account ids",
|
||||
fun() ->
|
||||
?assert(gmser_id:is_account(gmser_id:create(account, ?PUBKEY))),
|
||||
?assert(gmser_id:is_account(gmser_id:create({account, 0}, ?PUBKEY))),
|
||||
?assert(gmser_id:is_account(gmser_id:create({account, 5}, ?PUBKEY))),
|
||||
?assertNot(gmser_id:is_account(gmser_id:create(contract, ?PUBKEY))),
|
||||
?assertNot(gmser_id:is_account(not_an_id))
|
||||
end}.
|
||||
|
||||
account_pubkey_test() ->
|
||||
{"account_pubkey returns the 32-byte account hash",
|
||||
fun() ->
|
||||
?assertEqual(?PUBKEY,
|
||||
gmser_id:account_pubkey(gmser_id:create(account, ?PUBKEY))),
|
||||
?assertEqual(?PUBKEY,
|
||||
gmser_id:account_pubkey(gmser_id:create({account, 3}, ?PUBKEY))),
|
||||
?assertEqual(?PUBKEY,
|
||||
gmser_id:account_pubkey(gmser_id:create({account, 6}, ?PUBKEY)))
|
||||
end}.
|
||||
|
||||
account_pubkey_matches_specialize_test() ->
|
||||
{"account_pubkey agrees with specialize/2 for account ids",
|
||||
fun() ->
|
||||
Id = gmser_id:create({account, 2}, ?PUBKEY),
|
||||
?assertEqual(gmser_id:specialize(Id, account),
|
||||
gmser_id:account_pubkey(Id))
|
||||
end}.
|
||||
|
||||
extended_account_roundtrip_test() ->
|
||||
{"extended account ids round-trip through encode/decode",
|
||||
fun() ->
|
||||
Id = gmser_id:create({account, 4}, ?PUBKEY),
|
||||
?assert(gmser_id:is_account(Id)),
|
||||
Id1 = gmser_id:decode(gmser_id:encode(Id)),
|
||||
?assertEqual(?PUBKEY, gmser_id:account_pubkey(Id1)),
|
||||
?assertEqual(account, gmser_id:specialize_type(Id1))
|
||||
end}.
|
||||
Reference in New Issue
Block a user