Add support for extended-auth account types
Gajumaru Serialization Tests / tests (push) Successful in 11s
Gajumaru Serialization Tests / tests (push) Successful in 11s
This commit is contained in:
+43
-10
@@ -14,6 +14,7 @@
|
||||
, specialize/1
|
||||
, specialize/2
|
||||
, specialize_type/1
|
||||
, to_map/1
|
||||
, is_id/1
|
||||
]).
|
||||
|
||||
@@ -29,15 +30,20 @@
|
||||
, val
|
||||
}).
|
||||
|
||||
-type tag() :: 'account'
|
||||
| 'associate_chain'
|
||||
| 'channel'
|
||||
| 'commitment'
|
||||
| 'contract'
|
||||
| 'contract_source'
|
||||
| 'name'
|
||||
| 'native_token'
|
||||
| 'entry'.
|
||||
-type subtype() :: 0..6.
|
||||
-type id_map() :: #{ type := simple_tag()
|
||||
, subtype => subtype()
|
||||
, value := binary() }.
|
||||
|
||||
-type tag() :: {'account', subtype()} | simple_tag().
|
||||
-type simple_tag() :: 'account'
|
||||
| 'associate_chain'
|
||||
| 'channel'
|
||||
| 'commitment'
|
||||
| 'contract'
|
||||
| 'name'
|
||||
| 'native_token'
|
||||
| 'entry'.
|
||||
|
||||
-type val() :: <<_:256>>.
|
||||
-type id() :: #id{}.
|
||||
@@ -57,7 +63,8 @@
|
||||
___TAG___ =:= contract;
|
||||
___TAG___ =:= channel;
|
||||
___TAG___ =:= associate_chain;
|
||||
___TAG___ =:= entry
|
||||
___TAG___ =:= entry;
|
||||
___TAG___ =:= native_token
|
||||
).
|
||||
-define(IS_VAL(___VAL___), byte_size(___VAL___) =:= 32).
|
||||
|
||||
@@ -69,6 +76,8 @@
|
||||
create(Tag, Val) when ?IS_TAG(Tag), ?IS_VAL(Val) ->
|
||||
#id{ tag = Tag
|
||||
, val = Val};
|
||||
create({account,I}, Val) when is_binary(Val), I >= 0, I =< 6 ->
|
||||
#id{ tag = {account, I}, val = Val};
|
||||
create(Tag, Val) when ?IS_VAL(Val) ->
|
||||
error({illegal_tag, Tag});
|
||||
create(Tag, Val) when ?IS_TAG(Tag)->
|
||||
@@ -78,28 +87,52 @@ create(Tag, Val) ->
|
||||
|
||||
|
||||
-spec specialize(id()) -> {tag(), val()}.
|
||||
specialize(#id{tag = {Tag,_}, val = Val}) ->
|
||||
{Tag, Val};
|
||||
specialize(#id{tag = Tag, val = Val}) ->
|
||||
{Tag, Val}.
|
||||
|
||||
-spec specialize(id(), tag()) -> val().
|
||||
specialize(#id{tag = {Tag, _}, val = Val}, Tag) when is_binary(Val) ->
|
||||
Val;
|
||||
specialize(#id{tag = Tag, val = Val}, Tag) when ?IS_TAG(Tag), ?IS_VAL(Val) ->
|
||||
Val.
|
||||
|
||||
-spec specialize_type(id()) -> tag().
|
||||
specialize_type(#id{tag = {Tag, _}}) when ?IS_TAG(Tag) ->
|
||||
Tag;
|
||||
specialize_type(#id{tag = Tag}) when ?IS_TAG(Tag) ->
|
||||
Tag.
|
||||
|
||||
-spec to_map(id()) -> id_map().
|
||||
to_map(#id{tag = {Tag, SubType}, val = Val}) when ?IS_TAG(Tag) ->
|
||||
#{ type => Tag
|
||||
, subtype => SubType
|
||||
, value => Val };
|
||||
to_map(#id{tag = Tag, val = Val}) when ?IS_TAG(Tag) ->
|
||||
#{ type => Tag
|
||||
, value => Val }.
|
||||
|
||||
|
||||
-spec is_id(term()) -> boolean().
|
||||
is_id(#id{}) -> true;
|
||||
is_id(_) -> false.
|
||||
|
||||
-spec encode(id()) -> binary().
|
||||
encode(#id{tag = {account, N}, val = Val}) when N =< 2#111_1111 ->
|
||||
Ext = 2#1000_0000 bor N,
|
||||
<<Ext:8, Val/binary>>;
|
||||
encode(#id{tag = Tag, val = Val}) ->
|
||||
Res = <<(encode_tag(Tag)):?TAG_SIZE/unit:8, Val/binary>>,
|
||||
true = ?SERIALIZED_SIZE =:= byte_size(Res),
|
||||
Res.
|
||||
|
||||
-spec decode(binary()) -> id().
|
||||
decode(<<Ext:8, Rest/binary>>) when Ext >= 2#1000_0000 ->
|
||||
%% Extended account id type
|
||||
Type = Ext band 2#0111_1111,
|
||||
#id{ tag = {account, Type}
|
||||
, val = Rest };
|
||||
decode(<<Tag:?TAG_SIZE/unit:8, Val:?PUB_SIZE/binary>>) ->
|
||||
#id{ tag = decode_tag(Tag)
|
||||
, val = Val}.
|
||||
|
||||
Reference in New Issue
Block a user