Compare commits

..

3 Commits

Author SHA1 Message Date
skkw 81d0daa9d5 add support for encoding/decoding of subname id 2019-08-27 10:39:36 +02:00
skkw 7994428e56 changed encoding of subnames to base64 2019-08-26 12:01:42 +02:00
skkw 91b83cfb89 add subname id 2019-08-26 12:01:42 +02:00
4 changed files with 28 additions and 15 deletions
+10 -10
View File
@@ -32,14 +32,14 @@
| account_pubkey
| signature
| name
| subname
| commitment
| peer_pubkey
| state
| poi
| state_trees
| call_state_tree
| bytearray
| fate_code.
| bytearray.
-type extended_type() :: known_type() | block_hash | {id_hash, [known_type()]}.
@@ -196,12 +196,12 @@ type2enc(signature) -> ?BASE58;
type2enc(commitment) -> ?BASE58;
type2enc(peer_pubkey) -> ?BASE58;
type2enc(name) -> ?BASE58;
type2enc(subname) -> ?BASE64;
type2enc(state) -> ?BASE64;
type2enc(poi) -> ?BASE64;
type2enc(state_trees) -> ?BASE64;
type2enc(call_state_tree) -> ?BASE64;
type2enc(bytearray) -> ?BASE64;
type2enc(fate_code) -> ?BASE64.
type2enc(bytearray) -> ?BASE64.
type2pfx(key_block_hash) -> <<"kh">>;
@@ -225,12 +225,12 @@ type2pfx(signature) -> <<"sg">>;
type2pfx(commitment) -> <<"cm">>;
type2pfx(peer_pubkey) -> <<"pp">>;
type2pfx(name) -> <<"nm">>;
type2pfx(subname) -> <<"sn">>;
type2pfx(state) -> <<"st">>;
type2pfx(poi) -> <<"pi">>;
type2pfx(state_trees) -> <<"ss">>;
type2pfx(call_state_tree) -> <<"cs">>;
type2pfx(bytearray) -> <<"ba">>;
type2pfx(fate_code) -> <<"fc">>.
type2pfx(bytearray) -> <<"ba">>.
pfx2type(<<"kh">>) -> key_block_hash;
pfx2type(<<"mh">>) -> micro_block_hash;
@@ -253,12 +253,12 @@ pfx2type(<<"sg">>) -> signature;
pfx2type(<<"cm">>) -> commitment;
pfx2type(<<"pp">>) -> peer_pubkey;
pfx2type(<<"nm">>) -> name;
pfx2type(<<"sn">>) -> subname;
pfx2type(<<"st">>) -> state;
pfx2type(<<"pi">>) -> poi;
pfx2type(<<"ss">>) -> state_trees;
pfx2type(<<"cs">>) -> call_state_tree;
pfx2type(<<"ba">>) -> bytearray;
pfx2type(<<"fc">>) -> fate_code.
pfx2type(<<"ba">>) -> bytearray.
-spec byte_size_for_type(known_type()) -> non_neg_integer() | not_applicable.
@@ -281,14 +281,14 @@ byte_size_for_type(oracle_response) -> not_applicable;
byte_size_for_type(account_pubkey) -> 32;
byte_size_for_type(signature) -> 64;
byte_size_for_type(name) -> not_applicable;
byte_size_for_type(subname) -> not_applicable;
byte_size_for_type(commitment) -> 32;
byte_size_for_type(peer_pubkey) -> 32;
byte_size_for_type(state) -> 32;
byte_size_for_type(poi) -> not_applicable;
byte_size_for_type(state_trees) -> not_applicable;
byte_size_for_type(call_state_tree) -> not_applicable;
byte_size_for_type(bytearray) -> not_applicable;
byte_size_for_type(fate_code) -> not_applicable.
byte_size_for_type(bytearray) -> not_applicable.
%% TODO: Fix the base58 module so that it consistently uses binaries instead
+4 -2
View File
@@ -52,7 +52,8 @@ tag(name_preclaim_tx) -> 33;
tag(name_update_tx) -> 34;
tag(name_revoke_tx) -> 35;
tag(name_transfer_tx) -> 36;
tag(name_auction) -> 37;
tag(subname) -> 38;
tag(subname_tx) -> 39;
tag(contract) -> 40;
tag(contract_call) -> 41;
tag(contract_create_tx) -> 42;
@@ -110,7 +111,8 @@ rev_tag(33) -> name_preclaim_tx;
rev_tag(34) -> name_update_tx;
rev_tag(35) -> name_revoke_tx;
rev_tag(36) -> name_transfer_tx;
rev_tag(37) -> name_auction;
rev_tag(38) -> subname;
rev_tag(39) -> subname_tx;
rev_tag(40) -> contract;
rev_tag(41) -> contract_call;
rev_tag(42) -> contract_create_tx;
+14 -2
View File
@@ -24,7 +24,7 @@
, val
}).
-type tag() :: 'account' | 'oracle' | 'name'
-type tag() :: 'account' | 'oracle' | 'name' | 'subname'
| 'commitment' | 'contract' | 'channel'.
-type val() :: <<_:256>>.
-opaque(id() :: #id{}).
@@ -37,10 +37,12 @@
-define(PUB_SIZE, 32).
-define(TAG_SIZE, 1).
-define(SERIALIZED_SIZE, 33). %% ?TAG_SIZE + ?PUB_SIZE
-define(SUBNAME_SERIALIZED_SIZE, 65). %% ?TAG_SIZE + ?PUB_SIZE + ?PUB_SIZE
-define(IS_TAG(___TAG___), ___TAG___ =:= account;
___TAG___ =:= oracle;
___TAG___ =:= name;
___TAG___ =:= subname;
___TAG___ =:= commitment;
___TAG___ =:= contract;
___TAG___ =:= channel
@@ -82,24 +84,33 @@ is_id(_) -> false.
-spec encode(id()) -> binary().
encode(#id{tag = Tag, val = Val}) ->
Res = <<(encode_tag(Tag)):?TAG_SIZE/unit:8, Val/binary>>,
true = ?SERIALIZED_SIZE =:= byte_size(Res),
true = serialized_size(Tag) =:= byte_size(Res),
Res.
-spec decode(binary()) -> id().
decode(<<Tag:?TAG_SIZE/unit:8, Val:?PUB_SIZE/binary>>) ->
#id{ tag = decode_tag(Tag)
, val = Val};
decode(<<SubnameTag:?TAG_SIZE/unit:8, Val:(?PUB_SIZE+?PUB_SIZE)/binary>>) ->
#id{ tag = subname = decode_tag(SubnameTag)
, val = Val}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
serialized_size(subname) -> ?SUBNAME_SERIALIZED_SIZE;
serialized_size(_) -> ?SERIALIZED_SIZE.
encode_tag(account) -> 1;
encode_tag(name) -> 2;
encode_tag(commitment) -> 3;
encode_tag(oracle) -> 4;
encode_tag(contract) -> 5;
encode_tag(channel) -> 6;
encode_tag(subname) -> 7;
encode_tag(Other) -> error({illegal_id_tag_name, Other}).
decode_tag(1) -> account;
@@ -108,4 +119,5 @@ decode_tag(3) -> commitment;
decode_tag(4) -> oracle;
decode_tag(5) -> contract;
decode_tag(6) -> channel;
decode_tag(7) -> subname;
decode_tag(X) -> error({illegal_id_tag, X}).
-1
View File
@@ -23,7 +23,6 @@
, {commitment , 32}
, {peer_pubkey , 32}
, {state , 32}
, {fate_code , not_applicable}
, {poi , not_applicable}]).
encode_decode_test_() ->