Move common functionality to the Hakuzaru library

This commit is contained in:
2025-03-05 22:35:18 +09:00
parent 970eb1680f
commit 4fd4ab3f54
2 changed files with 45 additions and 106 deletions
+32 -78
View File
@@ -14,7 +14,7 @@
selected/1,
password/2,
refresh/0,
nonce/1, spend/2, chain/1, grids/1, sign_mess/1, sign_tx/1, sign_call/3, dry_run/2,
nonce/1, spend/1, chain/1, grids/1, sign_mess/1, sign_tx/1, sign_call/3, dry_run/2,
deploy/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]).
@@ -135,12 +135,11 @@ nonce(ID) ->
gen_server:call(?MODULE, {nonce, ID}).
-spec spend(KeyID, TX) -> ok
when KeyID :: gajudesk:id(),
TX :: #spend_tx{}.
-spec spend(TX) -> ok
when TX :: #spend_tx{}.
spend(KeyID, TX) ->
gen_server:cast(?MODULE, {spend, KeyID, TX}).
spend(TX) ->
gen_server:cast(?MODULE, {spend, TX}).
-spec chain(ID) -> ok
@@ -400,8 +399,8 @@ handle_cast({password, Old, New}, State) ->
handle_cast(refresh, State) ->
NewState = do_refresh(State),
{noreply, NewState};
handle_cast({spend, KeyID, TX}, State) ->
ok = do_spend(KeyID, TX, State),
handle_cast({spend, TX}, State) ->
ok = do_spend(TX, State),
{noreply, State};
handle_cast({chain, ID}, State) ->
NewState = do_chain(ID, State),
@@ -589,7 +588,6 @@ ensure_hz_set(Node = #node{ip = IP, external = Port}) ->
[{IP, Port}] ->
case hz:status() of
{ok, #{"network_id" := ChainID}} ->
ok = hz:network_id(ChainID),
ok = gd_gui:chain(ChainID, Node),
{ok, list_to_binary(ChainID)};
{error, no_nodes} ->
@@ -714,7 +712,7 @@ do_sign_tx(Request = #{"public_id" := ID, "payload" := CallData, "network_id" :=
case lists:keyfind(ID, #key.id, Keys) of
#key{pair = #{secret := SecKey}} ->
BinaryTX = list_to_binary(CallData),
SignedTX = sign_tx_hash(BinaryTX, SecKey, BinNID),
SignedTX = hz:sign_tx(BinaryTX, SecKey, BinNID),
do_sign_tx2(Request#{"signed" => true, "payload" := SignedTX});
false ->
gd_gui:trouble({bad_key, ID})
@@ -736,29 +734,13 @@ do_sign_tx2(Request = #{"url" := URL}) ->
Error -> gd_gui:trouble(Error)
end.
sign_tx_hash(Unsigned, SecKey, NetworkID) ->
{ok, TX_Data} = gmser_api_encoder:safe_decode(transaction, Unsigned),
{ok, Hash} = eblake2:blake2b(32, TX_Data),
NetworkHash = <<NetworkID/binary, Hash/binary>>,
Signature = ecu_eddsa:sign_detached(NetworkHash, SecKey),
SigTxType = signed_tx,
SigTxVsn = 1,
SigTemplate =
[{signatures, [binary]},
{transaction, binary}],
TX =
[{signatures, [Signature]},
{transaction, TX_Data}],
SignedTX = gmser_chain_objects:serialize(SigTxType, SigTxVsn, SigTemplate, TX),
gmser_api_encoder:encode(transaction, SignedTX).
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 = sign_tx_hash(TX, SecKey, ChainID),
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]),
@@ -791,60 +773,32 @@ do_dry_run(ConID, TX) ->
end.
do_spend(KeyID, TX, State = #s{wallet = #wallet{keys = Keys}}) ->
case lists:keyfind(KeyID, #key.id, Keys) of
do_spend(#spend_tx{sender_id = SenderID,
recipient_id = RecipientID,
amount = Amount,
gas_price = GasPrice,
gas = Gas,
ttl = TTL,
nonce = Nonce,
payload = Payload},
#s{wallet = #wallet{keys = Keys, chain_id = NetworkID}}) ->
case lists:keyfind(SenderID, #key.id, Keys) of
#key{pair = #{secret := SecKey}} ->
do_spend2(SecKey, TX, State);
Outcome = hz:spend(SenderID,
SecKey,
RecipientID,
Amount,
GasPrice,
Gas,
TTL,
Nonce,
Payload,
NetworkID),
tell(info, "Outcome: ~p", [Outcome]);
false ->
log(warning, "Tried do_spend with a bad key: ~p", [KeyID])
log(warning, "Tried do_spend with a bad key: ~p", [SenderID])
end.
do_spend2(SecKey,
#spend_tx{sender_id = SenderID,
recipient_id = RecipientID,
amount = Amount,
gas_price = GasPrice,
gas = Gas,
ttl = TTL,
nonce = Nonce,
payload = Payload},
#s{wallet = #wallet{chain_id = ChainID}}) ->
Type = spend_tx,
Vsn = 1,
Fields =
[{sender_id, SenderID},
{recipient_id, RecipientID},
{amount, Amount},
{gas_price, GasPrice},
{gas, Gas},
{ttl, TTL},
{nonce, Nonce},
{payload, Payload}],
Template =
[{sender_id, id},
{recipient_id, id},
{amount, int},
{gas_price, int},
{gas, int},
{ttl, int},
{nonce, int},
{payload, binary}],
BinaryTX = gmser_chain_objects:serialize(Type, Vsn, Template, Fields),
NetworkTX = <<ChainID/binary, BinaryTX/binary>>,
Signature = ecu_eddsa:sign_detached(NetworkTX, SecKey),
SigTxType = signed_tx,
SigTxVsn = 1,
SigTemplate =
[{signatures, [binary]},
{transaction, binary}],
TX_Data =
[{signatures, [Signature]},
{transaction, BinaryTX}],
SignedTX = gmser_chain_objects:serialize(SigTxType, SigTxVsn, SigTemplate, TX_Data),
Encoded = gmser_api_encoder:encode(transaction, SignedTX),
Outcome = hz:post_tx(Encoded),
tell("Outcome: ~p", [Outcome]).
do_list_keys(#s{selected = Selected, wallet = #wallet{poas = POAs}}) ->
{ok, Selected, [ID || #poa{id = ID} <- POAs]};
@@ -996,7 +950,7 @@ do_deploy(Build,
end.
do_deploy2(SecKey, CreateTX, ChainID) ->
SignedTX = sign_tx_hash(CreateTX, SecKey, ChainID),
SignedTX = hz:sign_tx(CreateTX, SecKey, ChainID),
tell(info, "SignedTX: ~p", [SignedTX]),
case hz:post_tx(SignedTX) of
{ok, Data = #{"tx_hash" := TXHash}} ->