Compare commits

..

No commits in common. "e0860f72cafd494c39eb63c1e7680531dbed5fca" and "b69ababf0f73325d6ed0fa68aedef0621d06534d" have entirely different histories.

3 changed files with 40 additions and 41 deletions

View File

@ -656,13 +656,13 @@ dry_run_map(Map) ->
%% @doc %% @doc
%% Decode the "cb_XXXX" string that came out of a tx_info or dry_run, to %% Decode the "cb_XXXX" string that came out of a tx_info or dry_run, to
%% the Erlang representation of FATE objects used by gmb_fate_encoding. See %% the Erlang representation of FATE objects used by aeb_fate_encoding. See
%% decode_bytearray/2 for an alternative that provides simpler outputs based on %% decode_bytearray/2 for an alternative that provides simpler outputs based on
%% information provided by an AACI. %% information provided by an AACI.
decode_bytearray_fate(EncodedStr) -> decode_bytearray_fate(EncodedStr) ->
Encoded = unicode:characters_to_binary(EncodedStr), Encoded = unicode:characters_to_binary(EncodedStr),
{contract_bytearray, Binary} = gmser_api_encoder:decode(Encoded), {contract_bytearray, Binary} = aeser_api_encoder:decode(Encoded),
case Binary of case Binary of
<<>> -> {ok, none}; <<>> -> {ok, none};
<<"Out of gas">> -> {error, out_of_gas}; <<"Out of gas">> -> {error, out_of_gas};
@ -670,7 +670,7 @@ decode_bytearray_fate(EncodedStr) ->
% FIXME there may be other errors that are encoded directly into % FIXME there may be other errors that are encoded directly into
% the byte array. We could try and catch to at least return % the byte array. We could try and catch to at least return
% *something* for cases that we don't already detect. % *something* for cases that we don't already detect.
Object = gmb_fate_encoding:deserialize(Binary), Object = aeb_fate_encoding:deserialize(Binary),
{ok, Object} {ok, Object}
end. end.
@ -952,7 +952,7 @@ contract_create(CreatorID, Path, InitArgs) ->
%% </li> %% </li>
%% <li> %% <li>
%% <b>GasPrice:</b> %% <b>GasPrice:</b>
%% This is a factor that is used calculate a value in pucks (the smallest unit of %% This is a factor that is used calculate a value in aettos (the smallest unit of
%% Gajumaru's currency value) for the gas consumed. In times of high contention %% Gajumaru's currency value) for the gas consumed. In times of high contention
%% in the mempool increasing the gas price increases the value of mining a given %% in the mempool increasing the gas price increases the value of mining a given
%% transaction, thus making miners more likely to prioritize the high value ones. %% transaction, thus making miners more likely to prioritize the high value ones.
@ -1058,14 +1058,14 @@ contract_create3(CreatorID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, InitArg
contract_create4(CreatorID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData) -> contract_create4(CreatorID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData) ->
PK = unicode:characters_to_binary(CreatorID), PK = unicode:characters_to_binary(CreatorID),
try try
{account_pubkey, OwnerID} = gmser_api_encoder:decode(PK), {account_pubkey, OwnerID} = aeser_api_encoder:decode(PK),
contract_create5(OwnerID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData) contract_create5(OwnerID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData)
catch catch
Error:Reason -> {Error, Reason} Error:Reason -> {Error, Reason}
end. end.
contract_create5(OwnerID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData) -> contract_create5(OwnerID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData) ->
Code = gmser_contract_code:serialize(Compiled), Code = aeser_contract_code:serialize(Compiled),
Source = maps:get(contract_source, Compiled, <<>>), Source = maps:get(contract_source, Compiled, <<>>),
VM = 1, VM = 1,
ABI = 1, ABI = 1,
@ -1073,7 +1073,7 @@ contract_create5(OwnerID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData)
ContractCreateVersion = 1, ContractCreateVersion = 1,
Type = contract_create_tx, Type = contract_create_tx,
Fields = Fields =
[{owner_id, gmser_id:create(account, OwnerID)}, [{owner_id, aeser_id:create(account, OwnerID)},
{nonce, Nonce}, {nonce, Nonce},
{code, Code}, {code, Code},
{source, Source}, {source, Source},
@ -1096,9 +1096,9 @@ contract_create5(OwnerID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData)
{gas_price, int}, {gas_price, int},
{gas, int}, {gas, int},
{call_data, binary}], {call_data, binary}],
TXB = gmser_chain_objects:serialize(Type, ContractCreateVersion, Template, Fields), TXB = aeser_chain_objects:serialize(Type, ContractCreateVersion, Template, Fields),
try try
{ok, gmser_api_encoder:encode(transaction, TXB)} {ok, aeser_api_encoder:encode(transaction, TXB)}
catch catch
error:Reason -> {error, Reason} error:Reason -> {error, Reason}
end. end.
@ -1271,7 +1271,7 @@ contract_call(CallerID, Gas, AACI, ConID, Fun, Args) ->
%% </li> %% </li>
%% <li> %% <li>
%% <b>GasPrice:</b> %% <b>GasPrice:</b>
%% This is a factor that is used calculate a value in pucks (the smallest unit of %% This is a factor that is used calculate a value in aettos (the smallest unit of
%% Gajumaru's currency value) for the gas consumed. In times of high contention %% Gajumaru's currency value) for the gas consumed. In times of high contention
%% in the mempool increasing the gas price increases the value of mining a given %% in the mempool increasing the gas price increases the value of mining a given
%% transaction, thus making miners more likely to prioritize the high value ones. %% transaction, thus making miners more likely to prioritize the high value ones.
@ -1329,7 +1329,7 @@ contract_call(CallerID, Nonce, Gas, GP, Amount, TTL, AACI, ConID, Fun, Args) ->
contract_call2(CallerID, Nonce, Gas, GasPrice, Amount, TTL, ConID, CallData) -> contract_call2(CallerID, Nonce, Gas, GasPrice, Amount, TTL, ConID, CallData) ->
CallerBin = unicode:characters_to_binary(CallerID), CallerBin = unicode:characters_to_binary(CallerID),
try try
{account_pubkey, PK} = gmser_api_encoder:decode(CallerBin), {account_pubkey, PK} = aeser_api_encoder:decode(CallerBin),
contract_call3(PK, Nonce, Gas, GasPrice, Amount, TTL, ConID, CallData) contract_call3(PK, Nonce, Gas, GasPrice, Amount, TTL, ConID, CallData)
catch catch
Error:Reason -> {Error, Reason} Error:Reason -> {Error, Reason}
@ -1338,7 +1338,7 @@ contract_call2(CallerID, Nonce, Gas, GasPrice, Amount, TTL, ConID, CallData) ->
contract_call3(PK, Nonce, Gas, GasPrice, Amount, TTL, ConID, CallData) -> contract_call3(PK, Nonce, Gas, GasPrice, Amount, TTL, ConID, CallData) ->
ConBin = unicode:characters_to_binary(ConID), ConBin = unicode:characters_to_binary(ConID),
try try
{contract_pubkey, CK} = gmser_api_encoder:decode(ConBin), {contract_pubkey, CK} = aeser_api_encoder:decode(ConBin),
contract_call4(PK, Nonce, Gas, GasPrice, Amount, TTL, CK, CallData) contract_call4(PK, Nonce, Gas, GasPrice, Amount, TTL, CK, CallData)
catch catch
Error:Reason -> {Error, Reason} Error:Reason -> {Error, Reason}
@ -1349,9 +1349,9 @@ contract_call4(PK, Nonce, Gas, GasPrice, Amount, TTL, CK, CallData) ->
CallVersion = 1, CallVersion = 1,
Type = contract_call_tx, Type = contract_call_tx,
Fields = Fields =
[{caller_id, gmser_id:create(account, PK)}, [{caller_id, aeser_id:create(account, PK)},
{nonce, Nonce}, {nonce, Nonce},
{contract_id, gmser_id:create(contract, CK)}, {contract_id, aeser_id:create(contract, CK)},
{abi_version, ABI}, {abi_version, ABI},
{ttl, TTL}, {ttl, TTL},
{amount, Amount}, {amount, Amount},
@ -1368,9 +1368,9 @@ contract_call4(PK, Nonce, Gas, GasPrice, Amount, TTL, CK, CallData) ->
{gas_price, int}, {gas_price, int},
{gas, int}, {gas, int},
{call_data, binary}], {call_data, binary}],
TXB = gmser_chain_objects:serialize(Type, CallVersion, Template, Fields), TXB = aeser_chain_objects:serialize(Type, CallVersion, Template, Fields),
try try
{ok, gmser_api_encoder:encode(transaction, TXB)} {ok, aeser_api_encoder:encode(transaction, TXB)}
catch catch
error:Reason -> {error, Reason} error:Reason -> {error, Reason}
end. end.
@ -1385,7 +1385,7 @@ contract_call4(PK, Nonce, Gas, GasPrice, Amount, TTL, CK, CallData) ->
%% of calldata %% of calldata
prepare_contract(File) -> prepare_contract(File) ->
case gmso_compiler:file(File, [{aci, json}]) of case aeso_compiler:file(File, [{aci, json}]) of
{ok, #{aci := ACI}} -> {ok, prepare_aaci(ACI)}; {ok, #{aci := ACI}} -> {ok, prepare_aaci(ACI)};
Error -> Error Error -> Error
end. end.
@ -1453,7 +1453,7 @@ simplify_args(#{name := NameBin, type := TypeDef}, Types) ->
% Type preparation has two goals. First, we need a data structure that can be % Type preparation has two goals. First, we need a data structure that can be
% traversed quickly, to take sophia-esque erlang expressions and turn them into % traversed quickly, to take sophia-esque erlang expressions and turn them into
% fate-esque erlang expressions that gmbytecode can serialize. Second, we need % fate-esque erlang expressions that aebytecode can serialize. Second, we need
% partially substituted names, so that error messages can be generated for why % partially substituted names, so that error messages can be generated for why
% "foobar" is not valid as the third field of a `bazquux`, because the third % "foobar" is not valid as the third field of a `bazquux`, because the third
% field is supposed to be `option(integer)`, not `string`. % field is supposed to be `option(integer)`, not `string`.
@ -1708,7 +1708,7 @@ coerce({O, N, integer}, S, to_fate) when is_list(S) ->
end; end;
coerce({O, N, address}, S, to_fate) -> coerce({O, N, address}, S, to_fate) ->
try try
case gmser_api_encoder:decode(unicode:characters_to_binary(S)) of case aeser_api_encoder:decode(unicode:characters_to_binary(S)) of
{account_pubkey, Key} -> {ok, {address, Key}}; {account_pubkey, Key} -> {ok, {address, Key}};
_ -> single_error({invalid, O, N, S}) _ -> single_error({invalid, O, N, S})
end end
@ -1716,11 +1716,11 @@ coerce({O, N, address}, S, to_fate) ->
error:_ -> single_error({invalid, O, N, S}) error:_ -> single_error({invalid, O, N, S})
end; end;
coerce({_, _, address}, {address, Bin}, from_fate) -> coerce({_, _, address}, {address, Bin}, from_fate) ->
Address = gmser_api_encoder:encode(account_pubkey, Bin), Address = aeser_api_encoder:encode(account_pubkey, Bin),
{ok, unicode:characters_to_list(Address)}; {ok, unicode:characters_to_list(Address)};
coerce({O, N, contract}, S, to_fate) -> coerce({O, N, contract}, S, to_fate) ->
try try
case gmser_api_encoder:decode(unicode:characters_to_binary(S)) of case aeser_api_encoder:decode(unicode:characters_to_binary(S)) of
{contract_pubkey, Key} -> {ok, {contract, Key}}; {contract_pubkey, Key} -> {ok, {contract, Key}};
_ -> single_error({invalid, O, N, S}) _ -> single_error({invalid, O, N, S})
end end
@ -1728,7 +1728,7 @@ coerce({O, N, contract}, S, to_fate) ->
error:_ -> single_error({invalid, O, N, S}) error:_ -> single_error({invalid, O, N, S})
end; end;
coerce({_, _, contract}, {contract, Bin}, from_fate) -> coerce({_, _, contract}, {contract, Bin}, from_fate) ->
Address = gmser_api_encoder:encode(contract_pubkey, Bin), Address = aeser_api_encoder:encode(contract_pubkey, Bin),
{ok, unicode:characters_to_list(Address)}; {ok, unicode:characters_to_list(Address)};
coerce({_, _, boolean}, true, _) -> coerce({_, _, boolean}, true, _) ->
{ok, true}; {ok, true};
@ -1995,7 +1995,8 @@ aaci_lookup_spec({aaci, _, FunDefs, _}, Fun) ->
%% @doc %% @doc
%% This function always returns 1,000,000,000 in the current version. %% This function always returns 1,000,000,000 in the current version.
%% %%
%% This is the minimum gas price returned by aec_tx_pool:minimum_miner_gas_price() %% This is the minimum gas price returned by aec_tx_pool:minimum_miner_gas_price(),
%% (the default set in aeternity_config_schema.json).
%% %%
%% Surely there can be some more nuance to this, but until a "gas station" type %% Surely there can be some more nuance to this, but until a "gas station" type
%% market/chain survey service exists we will use this naive value as a default %% market/chain survey service exists we will use this naive value as a default
@ -2029,7 +2030,7 @@ encode_call_data({aaci, _ContractName, FunDefs, _TypeDefs}, Fun, Args) ->
encode_call_data2(ArgDef, Fun, Args) -> encode_call_data2(ArgDef, Fun, Args) ->
case coerce_bindings(ArgDef, Args, to_fate) of case coerce_bindings(ArgDef, Args, to_fate) of
{ok, Coerced} -> gmb_fate_abi:create_calldata(Fun, Coerced); {ok, Coerced} -> aeb_fate_abi:create_calldata(Fun, Coerced);
Errors -> Errors Errors -> Errors
end. end.
@ -2049,7 +2050,7 @@ encode_call_data2(ArgDef, Fun, Args) ->
%% check failed before verification was able to pass or fail (bad key encoding or similar). %% check failed before verification was able to pass or fail (bad key encoding or similar).
verify_signature(Sig, Message, PubKey) -> verify_signature(Sig, Message, PubKey) ->
case gmser_api_encoder:decode(PubKey) of case aeser_api_encoder:decode(PubKey) of
{account_pubkey, PK} -> verify_signature2(Sig, Message, PK); {account_pubkey, PK} -> verify_signature2(Sig, Message, PK);
Other -> {error, {bad_key, Other}} Other -> {error, {bad_key, Other}}
end. end.
@ -2058,11 +2059,20 @@ verify_signature2(Sig, Message, PK) ->
% Gajumaru signatures require messages to be salted and hashed, then % Gajumaru signatures require messages to be salted and hashed, then
% the hash is what gets signed in order to protect % the hash is what gets signed in order to protect
% the user from accidentally signing a transaction disguised as a message. % the user from accidentally signing a transaction disguised as a message.
%
% Salt the message then hash with blake2b. See:
% 1. Erlang Blake2 blake2b/2 function:
% https://gitlab.com/ioecs/eblake2/blob/60a079f00d72d1bfcc25de8e6996d28f912db3fd/src/eblake2.erl#L23-L25
% 2. SDK salting step:
% https://gitlab.com/ioecs/aepp-sdk-js/blob/370f1e30064ad0239ba59931908d9aba0a2e86b6/src/utils/crypto.ts#L171-L175
% 3. SDK hashing:
% https://gitlab.com/ioecs/aepp-sdk-js/blob/370f1e30064ad0239ba59931908d9aba0a2e86b6/src/utils/crypto.ts#L83-L85
Prefix = <<"Gajumaru Signed Message:\n">>, Prefix = <<"Gajumaru Signed Message:\n">>,
{ok, PSize} = vencode(byte_size(Prefix)), {ok, PSize} = vencode(byte_size(Prefix)),
{ok, MSize} = vencode(byte_size(Message)), {ok, MSize} = vencode(byte_size(Message)),
Smashed = iolist_to_binary([PSize, Prefix, MSize, Message]), Smashed = iolist_to_binary([PSize, Prefix, MSize, Message]),
{ok, Hashed} = eblake2:blake2b(32, Smashed), {ok, Hashed} = eblake2:blake2b(32, Smashed),
% Signature = <<(binary_to_integer(Sig, 16)):(64 * 8)>>,
Signature = base64:decode(Sig), Signature = base64:decode(Sig),
Result = ecu_eddsa:sign_verify_detached(Signature, Hashed, PK), Result = ecu_eddsa:sign_verify_detached(Signature, Hashed, PK),
{ok, Result}. {ok, Result}.

View File

@ -6,6 +6,8 @@
-export([connect/4, slowly_connect/4]). -export([connect/4, slowly_connect/4]).
-include("$zx_include/zx_logger.hrl").
connect(Node = {Host, Port}, Request, From, Timeout) -> connect(Node = {Host, Port}, Request, From, Timeout) ->
Timer = erlang:send_after(Timeout, self(), timeout), Timer = erlang:send_after(Timeout, self(), timeout),
@ -234,10 +236,3 @@ url({Node, Port}, Path) when is_list(Node) ->
["https://", Node, ":", integer_to_list(Port), Path]; ["https://", Node, ":", integer_to_list(Port), Path];
url({Node, Port}, Path) when is_tuple(Node) -> url({Node, Port}, Path) when is_tuple(Node) ->
["https://", inet:ntoa(Node), ":", integer_to_list(Port), Path]. ["https://", inet:ntoa(Node), ":", integer_to_list(Port), Path].
log(Level, Format, Args) ->
Raw = io_lib:format("~w ~w: " ++ Format, [?MODULE, self() | Args]),
Entry = unicode:characters_to_list(Raw),
logger:log(Level, Entry).

View File

@ -29,6 +29,8 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
code_change/3, terminate/2]). code_change/3, terminate/2]).
%% TODO: Make logging more flexible
-include("$zx_include/zx_logger.hrl").
%%% Type and Record Definitions %%% Type and Record Definitions
@ -195,7 +197,7 @@ handle_info({'DOWN', Mon, process, PID, Info}, State) ->
NewState = handle_down(PID, Mon, Info, State), NewState = handle_down(PID, Mon, Info, State),
{noreply, NewState}; {noreply, NewState};
handle_info(Unexpected, State) -> handle_info(Unexpected, State) ->
ok = log(warning, "Unexpected info: ~tp~n", [Unexpected]), ok = log("Unexpected info: ~tp~n", [Unexpected]),
{noreply, State}. {noreply, State}.
@ -285,11 +287,3 @@ do_request(Request,
do_request(Request, From, State = #s{chain_nodes = {[], Used}}) -> do_request(Request, From, State = #s{chain_nodes = {[], Used}}) ->
Fresh = lists:reverse(Used), Fresh = lists:reverse(Used),
do_request(Request, From, State#s{chain_nodes = {Fresh, []}}). do_request(Request, From, State#s{chain_nodes = {Fresh, []}}).
log(Level, Format, Args) ->
Raw = io_lib:format("~w ~w: " ++ Format, [?MODULE, self() | Args]),
Entry = unicode:characters_to_list(Raw),
logger:log(Level, Entry).