Compare commits

...

17 Commits

Author SHA1 Message Date
Hans Svensson 2488ff3978 Add structured delegation signatures (#21)
* Bump enacl dependency

* Add serialization of delegation signature data
2023-12-06 15:23:57 +01:00
Ulf Wiger da8308821c Merge pull request #19 from davidyuk/secret-key
Add serialization of account secret key
2023-11-13 16:13:54 +01:00
Denis Davidyuk 213589ad9f Add serialization of account secret key 2023-09-15 10:08:29 +04:00
Ulf Wiger 270ebecc57 Merge pull request #20 from aeternity/uw-encode-maps
Add #{items := list()} type for encode/decode
2023-08-18 14:40:49 +02:00
Ulf Wiger 893ebb69f6 Update src/aeserialization.erl with duplicates check
From code review

Co-authored-by: Radosław Rowicki <35342116+radrow@users.noreply.github.com>
2023-08-17 20:43:59 +02:00
Ulf Wiger 05c4f87619 Update src/aeserialization.erl with type comment
From code review

Co-authored-by: Radosław Rowicki <35342116+radrow@users.noreply.github.com>
2023-08-17 20:42:48 +02:00
Ulf Wiger 91d6159ebd Add #{items := list()} type for encode/decode 2023-08-17 13:04:06 +02:00
Hans Svensson 177bf604b2 Merge pull request #18 from aeternity/add_ga_meta_tx_auth_data_type
Add ga_meta_tx_auth_data tag
2022-12-20 11:31:35 +01:00
Hans Svensson 95d248aa0c Add ga_meta_tx_auth_data tag 2022-12-20 08:08:51 +01:00
Dincho Todorov 0f9dbe9f52 Switch to OTP23 (#17) 2022-08-29 14:26:09 +03:00
seanhinde eb68fe331b Merge pull request #16 from aeternity/use-latest-enacl-api
Update to use latest enacl with new API
2021-11-02 09:40:44 +01:00
Sean Hinde 27bb4331aa Small tidy up 2021-11-02 09:36:45 +01:00
Sean Hinde 9b36c771c7 Update to use latest enacl with new API 2021-11-01 18:37:12 +01:00
Dimitar Ivanov 58e34ae7b5 Merge pull request #14 from aeternity/GH-2851-paying_for_tx
Add paying_for_tx to aeser_chain_objects
2021-03-10 15:49:31 +02:00
Dimitar Ivanov 87baccba42 Merge pull request #13 from aeternity/channel_set_delegates_tx
Describe channel_set_delegates_tx
2021-03-09 17:46:19 +02:00
Dimitar Ivanov 5e820bed55 Describe channel_set_delegates_tx 2021-03-07 12:28:27 +02:00
Hans Svensson 80ff8a49f4 Add paying_for_tx to aeser_chain_objects 2019-11-08 13:40:35 +01:00
9 changed files with 185 additions and 4 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ version: 2.1
executors:
aebuilder:
docker:
- image: aeternity/builder
- image: aeternity/builder:bionic-otp23
user: builder
working_directory: ~/src
+1 -1
View File
@@ -1,4 +1,4 @@
{erl_opts, [debug_info]}.
{deps, [ {base58, {git, "https://github.com/aeternity/erl-base58.git", {ref, "60a3356"}}}
, {enacl, {git, "https://github.com/aeternity/enacl.git", {ref, "26180f4"}}}
, {enacl, {git, "https://github.com/aeternity/enacl.git", {ref, "67fceef"}}}
]}.
+8
View File
@@ -0,0 +1,8 @@
[{<<"base58">>,
{git,"https://github.com/aeternity/erl-base58.git",
{ref,"60a335668a60328a29f9731b67c4a0e9e3d50ab6"}},
0},
{<<"enacl">>,
{git,"https://github.com/aeternity/enacl.git",
{ref,"67fceef42c0d055570f2e67b571f8d1f8de2f204"}},
0}].
+5
View File
@@ -30,6 +30,7 @@
| oracle_query_id
| oracle_response
| account_pubkey
| account_seckey
| signature
| name
| commitment
@@ -191,6 +192,7 @@ type2enc(oracle_query) -> ?BASE64;
type2enc(oracle_query_id) -> ?BASE58;
type2enc(oracle_response) -> ?BASE64;
type2enc(account_pubkey) -> ?BASE58;
type2enc(account_seckey) -> ?BASE58;
type2enc(signature) -> ?BASE58;
type2enc(commitment) -> ?BASE58;
type2enc(peer_pubkey) -> ?BASE58;
@@ -219,6 +221,7 @@ type2pfx(oracle_query) -> <<"ov">>;
type2pfx(oracle_query_id) -> <<"oq">>;
type2pfx(oracle_response) -> <<"or">>;
type2pfx(account_pubkey) -> <<"ak">>;
type2pfx(account_seckey) -> <<"sk">>;
type2pfx(signature) -> <<"sg">>;
type2pfx(commitment) -> <<"cm">>;
type2pfx(peer_pubkey) -> <<"pp">>;
@@ -246,6 +249,7 @@ pfx2type(<<"ov">>) -> oracle_query;
pfx2type(<<"oq">>) -> oracle_query_id;
pfx2type(<<"or">>) -> oracle_response;
pfx2type(<<"ak">>) -> account_pubkey;
pfx2type(<<"sk">>) -> account_seckey;
pfx2type(<<"sg">>) -> signature;
pfx2type(<<"cm">>) -> commitment;
pfx2type(<<"pp">>) -> peer_pubkey;
@@ -275,6 +279,7 @@ byte_size_for_type(oracle_query) -> not_applicable;
byte_size_for_type(oracle_query_id) -> 32;
byte_size_for_type(oracle_response) -> not_applicable;
byte_size_for_type(account_pubkey) -> 32;
byte_size_for_type(account_seckey) -> 32;
byte_size_for_type(signature) -> 64;
byte_size_for_type(name) -> not_applicable;
byte_size_for_type(commitment) -> 32;
+6
View File
@@ -58,6 +58,7 @@ tag(contract_call) -> 41;
tag(contract_create_tx) -> 42;
tag(contract_call_tx) -> 43;
tag(channel_create_tx) -> 50;
tag(channel_set_delegates_tx) -> 501;
tag(channel_deposit_tx) -> 51;
tag(channel_withdraw_tx) -> 52;
tag(channel_force_progress_tx) -> 521;
@@ -89,6 +90,8 @@ tag(accounts_mtree) -> 626;
tag(compiler_sophia) -> 70;
tag(ga_attach_tx) -> 80;
tag(ga_meta_tx) -> 81;
tag(paying_for_tx) -> 82;
tag(ga_meta_tx_auth_data) -> 810;
tag(key_block) -> 100;
tag(micro_block) -> 101;
tag(light_micro_block) -> 102;
@@ -116,6 +119,7 @@ rev_tag(41) -> contract_call;
rev_tag(42) -> contract_create_tx;
rev_tag(43) -> contract_call_tx;
rev_tag(50) -> channel_create_tx;
rev_tag(501) -> channel_set_delegates_tx;
rev_tag(51) -> channel_deposit_tx;
rev_tag(52) -> channel_withdraw_tx;
rev_tag(521) -> channel_force_progress_tx;
@@ -147,6 +151,8 @@ rev_tag(626) -> accounts_mtree;
rev_tag(70) -> compiler_sophia;
rev_tag(80) -> ga_attach_tx;
rev_tag(81) -> ga_meta_tx;
rev_tag(82) -> paying_for_tx;
rev_tag(810) -> ga_meta_tx_auth_data;
rev_tag(100) -> key_block;
rev_tag(101) -> micro_block;
rev_tag(102) -> light_micro_block;
+1 -2
View File
@@ -23,8 +23,7 @@ serialize(CodeMap = #{ byte_code := ByteCode
SourceHash = case CodeMap of
#{ source_hash := SHash } -> SHash;
#{ contract_source := SrcStr } ->
{ok, SHash} = enacl:generichash(32, list_to_binary(SrcStr)),
SHash
enacl:generichash(32, list_to_binary(SrcStr))
end,
%% Compiler version
+81
View File
@@ -0,0 +1,81 @@
%%%-------------------------------------------------------------------
%%% @copyright (C) 2023, Aeternity Anstalt
%%% @doc
%%% Serialization of delegation signatures
%%% @end
%%%-------------------------------------------------------------------
-module(aeser_delegation).
-export([ aens_preclaim_sig/3
, aens_name_sig/4
, aens_sig/3
, oracle_sig/3
, oracle_response_sig/3
]).
%% Delegation signatures are prefixed with a unique tag, ensuring not to
%% collide with serialized transactions.
-define(DELEGATION_TAG, 16#1a01).
-define(TYPE_AENS, 1).
-define(TYPE_AENS_NAME, 2).
-define(TYPE_AENS_PRECLAIM, 3).
-define(TYPE_ORACLE, 4).
-define(TYPE_ORACLE_RESPONSE, 5).
-define(VSN, 1).
-type sig_data() :: binary().
-spec aens_preclaim_sig(binary(), aeser_id:id(), aeser_id:id()) -> sig_data().
aens_preclaim_sig(NetworkId, Account, Contract) ->
assert_id(account, Account),
assert_id(contract, Contract),
Template = [{account, id}, {contract, id}],
Fields = [{account, Account}, {contract, Contract}],
serialize(?TYPE_AENS_PRECLAIM, NetworkId, Template, Fields).
-spec aens_name_sig(binary(), aeser_id:id(), aeser_id:id(), aeser_id:id()) -> sig_data().
aens_name_sig(NetworkId, Account, Name, Contract) ->
assert_id(account, Account),
assert_id(name, Name),
assert_id(contract, Contract),
Template = [{account, id}, {name, id}, {contract, id}],
Fields = [{account, Account}, {name, Name}, {contract, Contract}],
serialize(?TYPE_AENS_NAME, NetworkId, Template, Fields).
-spec aens_sig(binary(), aeser_id:id(), aeser_id:id()) -> sig_data().
aens_sig(NetworkId, Account, Contract) ->
assert_id(account, Account),
assert_id(contract, Contract),
Template = [{account, id}, {contract, id}],
Fields = [{account, Account}, {contract, Contract}],
serialize(?TYPE_AENS, NetworkId, Template, Fields).
-spec oracle_sig(binary(), aeser_id:id(), aeser_id:id()) -> sig_data().
oracle_sig(NetworkId, Account, Contract) ->
assert_id(account, Account),
assert_id(contract, Contract),
Template = [{account, id}, {contract, id}],
Fields = [{account, Account}, {contract, Contract}],
serialize(?TYPE_ORACLE, NetworkId, Template, Fields).
-spec oracle_response_sig(binary(), aeser_id:id(), aeser_id:id()) -> sig_data().
oracle_response_sig(NetworkId, QueryId, Contract) ->
assert_id(oracle, QueryId),
assert_id(contract, Contract),
Template = [{query, id}, {contract, id}],
Fields = [{query, QueryId}, {contract, Contract}],
serialize(?TYPE_ORACLE_RESPONSE, NetworkId, Template, Fields).
%% ------------------------------------------------------------------------
%% -- Internal functions
%% ------------------------------------------------------------------------
serialize(Type, NetworkId, Template, Fields) ->
Data = aeserialization:serialize(Type, ?VSN, Template, Fields),
<<?DELEGATION_TAG:16, NetworkId/binary, Data/binary>>.
assert_id(Type, AeserId) ->
Type = aeser_id:specialize_type(AeserId).
+15
View File
@@ -28,12 +28,14 @@
| 'binary'
| 'id' %% As defined in aec_id.erl
| [type()] %% Length one in the type. This means a list of any length.
| #{items := [{field_name(), type()}]} %% Record with named fields represented as a map. Encoded as a list in the given order.
| tuple(). %% Any arity, containing type(). This means a static size array.
-type encodable_term() :: non_neg_integer()
| binary()
| boolean()
| [encodable_term()] %% Of any length
| #{atom() => encodable_term()}
| tuple() %% Any arity, containing encodable_term().
| aeser_id:id().
@@ -101,6 +103,12 @@ decode_fields(Template, Values) ->
encode_field([Type], L) when is_list(L) ->
[encode_field(Type, X) || X <- L];
encode_field(#{items := Items}, Map) ->
lists:map(
fun({K, Type}) ->
V = maps:get(K, Map),
encode_field(Type, V)
end, Items);
encode_field(Type, T) when tuple_size(Type) =:= tuple_size(T) ->
Zipped = lists:zip(tuple_to_list(Type), tuple_to_list(T)),
[encode_field(X, Y) || {X, Y} <- Zipped];
@@ -117,6 +125,13 @@ encode_field(Type, Val) -> error({illegal, Type, Val}).
decode_field([Type], List) when is_list(List) ->
[decode_field(Type, X) || X <- List];
decode_field(#{items := Items}, List) when length(List) =:= length(Items) ->
Zipped = lists:zip(Items, List),
lists:foldl(
fun({{K, Type}, V}, Map) ->
maps:is_key(K, Map) andalso error(badarg, duplicate_field),
Map#{ K => decode_field(Type, V) }
end, #{}, Zipped);
decode_field(Type, List) when length(List) =:= tuple_size(Type) ->
Zipped = lists:zip(tuple_to_list(Type), List),
list_to_tuple([decode_field(X, Y) || {X, Y} <- Zipped]);
+67
View File
@@ -0,0 +1,67 @@
%%%-------------------------------------------------------------------
%%% @copyright (C) 2023, Aeternity Anstalt
%%%-------------------------------------------------------------------
-module(aeser_delegation_tests).
-include_lib("eunit/include/eunit.hrl").
-define(TEST_MODULE, aeser_delegation).
-define(ACCOUNT, aeser_id:create(account, <<1:256>>)).
-define(CONTRACT, aeser_id:create(contract, <<2:256>>)).
-define(NAME, aeser_id:create(name, <<3:256>>)).
-define(ORACLE, aeser_id:create(oracle, <<3:256>>)).
-define(NETWORK_ID, <<"my_fancy_network"/utf8>>).
encode_correct_test_() ->
[{"Encode preclaim sig",
fun() ->
aeser_delegation:aens_preclaim_sig(?NETWORK_ID, ?ACCOUNT, ?CONTRACT)
end},
{"Encode name sig",
fun() ->
aeser_delegation:aens_name_sig(?NETWORK_ID, ?ACCOUNT, ?NAME, ?CONTRACT)
end},
{"Encode aens wildcard sig",
fun() ->
aeser_delegation:aens_sig(?NETWORK_ID, ?ACCOUNT, ?CONTRACT)
end},
{"Encode oracle sig",
fun() ->
aeser_delegation:oracle_sig(?NETWORK_ID, ?ACCOUNT, ?CONTRACT)
end},
{"Encode oracle response sig",
fun() ->
aeser_delegation:oracle_response_sig(?NETWORK_ID, ?ORACLE, ?CONTRACT)
end}
].
encode_fail_test_() ->
[{"Bad encoding preclaim sig",
fun() ->
?assertError(_, aeser_delegation:aens_preclaim_sig(?NETWORK_ID, <<42:256>>, ?CONTRACT)),
?assertError(_, aeser_delegation:aens_preclaim_sig(?NETWORK_ID, ?CONTRACT, ?ACCOUNT))
end},
{"Bad encoding name sig",
fun() ->
?assertError(_, aeser_delegation:aens_name_sig(?NETWORK_ID, ?ACCOUNT, <<42:256>>, ?CONTRACT)),
?assertError(_, aeser_delegation:aens_name_sig(?NETWORK_ID, ?NAME, ?ACCOUNT, ?CONTRACT))
end},
{"Bad encoding aens wildcard sig",
fun() ->
?assertError(_, aeser_delegation:aens_sig(?NETWORK_ID, ?ACCOUNT, <<42:256>>)),
?assertError(_, aeser_delegation:aens_sig(?NETWORK_ID, ?CONTRACT, ?CONTRACT))
end},
{"Bad encoding oracle sig",
fun() ->
?assertError(_, aeser_delegation:oracle_sig(?NETWORK_ID, <<42:256>>, ?CONTRACT)),
?assertError(_, aeser_delegation:oracle_sig(?NETWORK_ID, ?ACCOUNT, ?ACCOUNT))
end},
{"Bad encoding oracle response sig",
fun() ->
?assertError(_, aeser_delegation:oracle_response_sig(?NETWORK_ID, <<42:256>>, ?CONTRACT)),
?assertError(_, aeser_delegation:oracle_response_sig(?NETWORK_ID, ?ORACLE, ?ORACLE))
end}
].