This commit is contained in:
2026-04-06 10:59:45 +09:00
parent 713650f88b
commit 9fa365e83f
8 changed files with 439 additions and 424 deletions
+153 -190
View File
@@ -14,9 +14,9 @@
selected/1, network/0,
password/2,
refresh/0,
nonce/1, spend/1, chain/1, grids/1,
sign_mess/1, sign_binary/1, sign_tx/1, sign_call/3, dry_run/2,
deploy/3, make_call/3,
nonce/1, spend/1, chain_id/0, grids/1,
sign_mess/1, sign_binary/1, sign_tx/1, sign_call/3,
prompt_call/3,
make_key/6, recover_key/1, mnemonic/1, rename_key/2, drop_key/1, list_keys/0,
add_node/1, set_sole_node/1]).
-export([tic/1, update_balance/2]).
@@ -154,11 +154,11 @@ spend(TX) ->
gen_server:cast(?MODULE, {spend, TX}).
-spec chain(ID) -> ok
when ID :: string().
-spec chain_id() -> {ok, ID}
when ID :: binary().
chain(ID) ->
gen_server:cast(?MODULE, {chain, ID}).
chain_id() ->
gen_server:cast(?MODULE, {chain_id, ID}).
-spec grids(string()) -> ok.
@@ -188,21 +188,15 @@ sign_tx(Request) ->
gen_server:cast(?MODULE, {sign_tx, Request}).
-spec sign_call(ConID, PubKey, TX) -> ok
when ConID :: gajudesk:id(),
PubKey :: gajudesk:id(),
TX :: binary().
-spec sign_call(ChainID, PubKey, TX) -> Result
when ChainID :: binary(),
PubKey :: gajudesk:id(),
TX :: binary(),
Result :: {ok, SignedTX :: binary()},
| {error, Reason :: term()}.
sign_call(ConID, PubKey, TX) ->
gen_server:cast(?MODULE, {sign_call, ConID, PubKey, TX}).
-spec dry_run(ConID, TX) -> ok
when ConID :: gajudesk:id(),
TX :: binary().
dry_run(ConID, TX) ->
gen_server:cast(?MODULE, {dry_run, ConID, TX}).
sign_call(ChainID, PubKey, TX) ->
gen_server:call(?MODULE, {sign_call, ChainID, PubKey, TX}).
-spec deploy(Build, Params, InitArgs) -> Result
@@ -222,6 +216,17 @@ deploy(Build, Params, InitArgs) ->
gen_server:cast(?MODULE, {deploy, Build, Params, InitArgs}).
-spec prompt_call(FunDef, ConID, Build) -> ok
when FunDef :: {FunName, FunType},
FunName :: string(),
FunType :: call | dryr | init,
ConID :: none | binary(),
Build :: map(). % Fixme
prompt_call(FunDef, ConID, Build) ->
gen_server:cast(?MODULE, {prompt_call, FunDef, ConID, Build}).
-spec make_key(Type, Size, Name, Seed, Encoding, Transform) -> ok
when Type :: {eddsa, ed25519},
Size :: 256,
@@ -401,6 +406,12 @@ handle_call(list_keys, _, State) ->
handle_call({nonce, ID}, _, State) ->
Response = do_nonce(ID),
{reply, Response, State};
handle_call({chain_id, State) ->
Response = do_chain_id(State),
{reply, Response, NewState};
handle_call({sign_call, ChainID, PubKey, TX}, _, State) ->
Response = do_sign_call(State, ChainID, PubKey, TX),
{reply, Response, State};
handle_call({open_wallet, Path, Phrase}, _, State) ->
{Response, NewState} = do_open_wallet(Path, Phrase, State),
{reply, Response, NewState};
@@ -456,9 +467,6 @@ handle_cast(refresh, State) ->
handle_cast({spend, TX}, State) ->
ok = do_spend(TX, State),
{noreply, State};
handle_cast({chain, ID}, State) ->
NewState = do_chain(ID, State),
{noreply, NewState};
handle_cast({grids, String}, State) ->
ok = do_grids(String),
{noreply, State};
@@ -471,15 +479,12 @@ handle_cast({sign_binary, Request}, State) ->
handle_cast({sign_tx, Request}, State) ->
ok = do_sign_tx(Request, State),
{noreply, State};
handle_cast({sign_call, ConID, PubKey, TX}, State) ->
ok = do_sign_call(State, ConID, PubKey, TX),
{noreply, State};
handle_cast({dry_run, ConID, TX}, State) ->
ok = do_dry_run(ConID, TX),
{noreply, State};
handle_cast({deploy, Build, Params, InitArgs}, State) ->
ok = do_deploy(Build, Params, InitArgs, State),
{noreply, State};
handle_cast({prompt_call, FunDef, ConID, Build}) ->
NewState = do_prompt_call(FunDef, ConID, Build, State),
{noreply, NewState};
handle_cast({make_key, Name, Seed, Encoding, Transform}, State) ->
NewState = do_make_key(Name, Seed, Encoding, Transform, State),
{noreply, NewState};
@@ -588,9 +593,11 @@ task_data(gd_v_devman, #s{}) ->
%%% Network operations
do_chain(_, State) ->
tell("Would be doing chain in do_chain/2 here"),
State.
% NOTE: This is temporary. As GD becomes more chain aware this will move.
do_chain_id(#s{wallet = #wallet{chain_id = ChainID}) ->
{ok, ChainID};
do_chain_id(_) ->
{error, no_chain}.
do_add_node(New, State) ->
@@ -802,41 +809,13 @@ post_grids_response(ResponseKeys, Request = #{"url" := URL}) ->
end.
do_sign_call(#s{wallet = #wallet{keys = Keys, chain_id = ChainID}},
ConID,
PubKey,
TX) ->
#key{pair = #{secret := SecKey}} = lists:keyfind(PubKey, #key.id, Keys),
SignedTX = hz:sign_tx(TX, SecKey, ChainID),
case hz:post_tx(SignedTX) of
{ok, Data = #{"tx_hash" := TXHash}} ->
ok = tell("TX succeded with: ~p", [TXHash]),
do_sign_call2(ConID, Data);
{ok, WTF} ->
gd_v_devman:trouble({error, WTF});
Error ->
gd_v_devman:trouble(Error)
end;
do_sign_call(_, _, _, _) ->
gd_v_devman:trouble({error, no_chain}).
do_sign_call2(ConID, #{"tx_hash" := TXHash}) ->
case hz:tx_info(TXHash) of
{ok, CallInfo = #{"call_info" := #{"return_type" := "ok"}}} ->
gd_v_devman:call_result(ConID, CallInfo);
{error, "Tx not mined"} ->
gd_v_devman:trouble({tx_hash, TXHash});
{ok, Reason = #{"call_info" := #{"return_type" := "revert"}}} ->
gd_v_devman:trouble({error, Reason});
Error ->
gd_v_devman:trouble(Error)
end.
do_dry_run(ConID, TX) ->
case hz:dry_run(TX) of
{ok, Result} -> gd_v_devman:dryrun_result(ConID, Result);
Other -> gd_v_devmam:trouble({error, ConID, Other})
do_sign_call(#s{wallet = #wallet{keys = Keys}}, ChainID, PubKey, TX) ->
case lists:keyfind(PubKey, #key.id, Keys) of
#key{pair = #{secret := SecKey}} ->
SignedTX = hz:sign_tx(TX, SecKey, ChainID),
{ok, SignedTX};
false ->
{error, bad_key}
end.
@@ -883,23 +862,26 @@ do_network(#s{wallet = #wallet{chain_id = ChainID}}) ->
{ok, ChainID}.
%%% Stateless Operations
encrypt(Pass, Binary) ->
Flags = [{encrypt, true}, {padding, pkcs_padding}],
crypto:crypto_one_time(aes_256_ecb, Pass, Binary, Flags).
decrypt(Pass, Binary) ->
Flags = [{encrypt, false}, {padding, pkcs_padding}],
crypto:crypto_one_time(aes_256_ecb, Pass, Binary, Flags).
pass(none) ->
none;
pass(Phrase) ->
crypto:hash(sha3_256, Phrase).
do_prompt_call(FunDef, ConID, Build, State = #s{tasks = Tasks, prefs = Prefs}) ->
Name = {ConID, FunDef},
case do_list_keys(State) of
{ok, Selected, KeyIDs} ->
case lists:keyfind(Name, #ui.name, Tasks) of
#ui{wx = Win} ->
ok = gd_v_call:to_front(Win),
State;
false ->
CallPrefs = maps:get(gd_v_call, Prefs, #{}),
Win = gd_v_call:start_link({CallPrefs, FunDef, ConID, Build, Selected, Keys}),
PID = wx_object:get_pid(Win),
Mon = monitor(process, PID),
UI = #ui{name = Name, pid = PID, wx = Win, mon = Mon},
State#s{tasks = [UI | Tasks]}
end;
error ->
ok = gd_gui:trouble("ERROR: No Wallet Selected"),
State
end.
do_make_key(Name, <<>>, _, Transform, State) ->
@@ -950,33 +932,6 @@ do_make_key2(Name, Bin, Transform,
State#s{wallet = Updated}.
base64_decode(String) ->
try
{ok, base64:decode(String)}
catch
E:R -> {E, R}
end.
transform({sha3, 256}) ->
fun(D) -> crypto:hash(sha3_256, D) end;
transform({sha2, 256}) ->
fun(D) -> crypto:hash(sha256, D) end;
transform({x_or, 256}) ->
fun t_xor/1.
t_xor(Bin) -> t_xor(Bin, <<0:256>>).
t_xor(<<H:32/binary, T/binary>>, A) ->
t_xor(T, crypto:exor(H, A));
t_xor(<<>>, A) ->
A;
t_xor(B, A) ->
H = <<0:(256 - bit_size(B)), B/binary>>,
crypto:exor(H, A).
do_recover_key(Mnemonic, State) ->
case hz_key_master:decode(Mnemonic) of
{ok, Seed} ->
@@ -1005,54 +960,6 @@ do_recover_key2(Seed, State = #s{wallet = Current, wallets = Wallets, pass = Pas
end.
do_mnemonic(ID, #s{wallet = #wallet{keys = Keys}}) ->
case lists:keyfind(ID, #key.id, Keys) of
#key{pair = #{secret := <<K:32/binary, _/binary>>}} ->
Mnemonic = hz_key_master:encode(K),
{ok, Mnemonic};
false ->
{error, bad_key}
end.
do_deploy(Build,
{PubKey, Nonce, TTL, GasPrice, Gas, Amount},
InitArgs,
#s{wallet = #wallet{keys = Keys, chain_id = ChainID}}) ->
#key{pair = #{secret := SecKey}} = lists:keyfind(PubKey, #key.id, Keys),
case hz:contract_create_built(PubKey,
Nonce, Amount, TTL, Gas, GasPrice,
Build, InitArgs) of
{ok, CreateTX} -> do_deploy2(SecKey, CreateTX, ChainID);
Error -> gd_v_devman:trouble(Error)
end.
do_deploy2(SecKey, CreateTX, ChainID) ->
SignedTX = hz:sign_tx(CreateTX, SecKey, ChainID),
tell(info, "SignedTX: ~p", [SignedTX]),
case hz:post_tx(SignedTX) of
{ok, Data = #{"tx_hash" := TXHash}} ->
ok = tell("Contract deploy TX succeded with: ~p", [TXHash]),
do_deploy3(Data);
{ok, WTF} ->
gd_v_devman:trouble({error, WTF});
Error ->
gd_v_devman:trouble(Error)
end.
do_deploy3(#{"tx_hash" := TXHash}) ->
case hz:tx_info(TXHash) of
{ok, #{"call_info" := #{"return_type" := "ok", "contract_id" := ConID}}} ->
gd_v_devman:open_contract(ConID);
{error, "Tx not mined"} ->
gd_v_devman:trouble({tx_hash, TXHash});
{ok, Reason = #{"call_info" := #{"return_type" := "revert"}}} ->
gd_v_devman:trouble({error, Reason});
Error ->
gd_v_devman:trouble(Error)
end.
do_rename_key(ID, NewName, State = #s{wallet = W, wallets = Wallets, pass = Pass}) ->
#wallet{name = Name, poas = POAs, keys = Keys} = W,
RW = lists:keyfind(Name, #wr.name, Wallets),
@@ -1105,26 +1012,6 @@ do_open_wallet(Path, Phrase, State = #s{timer = Timer}) ->
end.
default_wallet(mainnet) ->
Node = #node{ip = "groot.mainnet.gajumaru.io"},
Groot = #chain{id = <<"groot.mainnet">>,
nodes = [Node]},
MainNet = #net{id = <<"mainnet">>, chains = [Groot]},
#wallet{nets = [MainNet], endpoint = Node};
default_wallet(testnet) ->
Node = #node{ip = "groot.testnet.gajumaru.io"},
Groot = #chain{id = <<"groot.testnet">>,
nodes = [Node]},
TestNet = #net{id = <<"testnet">>, chains = [Groot]},
#wallet{nets = [TestNet], endpoint = Node};
default_wallet(devnet) ->
% TODO: This accounts for the nature of devnets by defining *no* chains.
% The GUI/CON will need to manage this properly when encountered and prompt.
DevNet = #net{id = <<"devnet">>,
chains = []},
#wallet{nets = [DevNet]}.
do_password(none, none, State) ->
State;
do_password(none, New, State = #s{pass = none,
@@ -1269,16 +1156,6 @@ maybe_clean(false, _) ->
ok.
get_prefs(K, M, D) ->
P = maps:get(?MODULE, M, #{}),
maps:get(K, P, D).
put_prefs(K, V, M) ->
P = maps:get(?MODULE, M, #{}),
NewP = maps:put(K, V, P),
maps:put(?MODULE, NewP, M).
do_save(Module, Prefs, State = #s{prefs = Cached}) ->
Updated = maps:put(Module, Prefs, Cached),
ok = persist(Updated),
@@ -1396,6 +1273,92 @@ cancel_timer(T) ->
end.
%%% Stateless Operations
encrypt(Pass, Binary) ->
Flags = [{encrypt, true}, {padding, pkcs_padding}],
crypto:crypto_one_time(aes_256_ecb, Pass, Binary, Flags).
decrypt(Pass, Binary) ->
Flags = [{encrypt, false}, {padding, pkcs_padding}],
crypto:crypto_one_time(aes_256_ecb, Pass, Binary, Flags).
pass(none) ->
none;
pass(Phrase) ->
crypto:hash(sha3_256, Phrase).
base64_decode(String) ->
try
{ok, base64:decode(String)}
catch
E:R -> {E, R}
end.
transform({sha3, 256}) ->
fun(D) -> crypto:hash(sha3_256, D) end;
transform({sha2, 256}) ->
fun(D) -> crypto:hash(sha256, D) end;
transform({x_or, 256}) ->
fun t_xor/1.
t_xor(Bin) -> t_xor(Bin, <<0:256>>).
t_xor(<<H:32/binary, T/binary>>, A) ->
t_xor(T, crypto:exor(H, A));
t_xor(<<>>, A) ->
A;
t_xor(B, A) ->
H = <<0:(256 - bit_size(B)), B/binary>>,
crypto:exor(H, A).
do_mnemonic(ID, #s{wallet = #wallet{keys = Keys}}) ->
case lists:keyfind(ID, #key.id, Keys) of
#key{pair = #{secret := <<K:32/binary, _/binary>>}} ->
Mnemonic = hz_key_master:encode(K),
{ok, Mnemonic};
false ->
{error, bad_key}
end.
default_wallet(mainnet) ->
Node = #node{ip = "groot.mainnet.gajumaru.io"},
Groot = #chain{id = <<"groot.mainnet">>,
nodes = [Node]},
MainNet = #net{id = <<"mainnet">>, chains = [Groot]},
#wallet{nets = [MainNet], endpoint = Node};
default_wallet(testnet) ->
Node = #node{ip = "groot.testnet.gajumaru.io"},
Groot = #chain{id = <<"groot.testnet">>,
nodes = [Node]},
TestNet = #net{id = <<"testnet">>, chains = [Groot]},
#wallet{nets = [TestNet], endpoint = Node};
default_wallet(devnet) ->
% TODO: This accounts for the nature of devnets by defining *no* chains.
% The GUI/CON will need to manage this properly when encountered and prompt.
DevNet = #net{id = <<"devnet">>,
chains = []},
#wallet{nets = [DevNet]}.
get_prefs(K, M, D) ->
P = maps:get(?MODULE, M, #{}),
maps:get(K, P, D).
put_prefs(K, V, M) ->
P = maps:get(?MODULE, M, #{}),
NewP = maps:put(K, V, P),
maps:put(?MODULE, NewP, M).
persist(Prefs) ->
Path = prefs_path(),
ok = filelib:ensure_dir(Path),