From d8b8fee2329440def96f6cbb42c16e72fdcff447 Mon Sep 17 00:00:00 2001 From: Craig Everett Date: Sat, 25 Oct 2025 10:41:32 +0900 Subject: [PATCH] WIP: Adding binary signatures --- src/gd_con.erl | 81 +++++++++++++++++++++++++++++++++----------------- src/gd_gui.erl | 42 ++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 27 deletions(-) diff --git a/src/gd_con.erl b/src/gd_con.erl index 6bb2e63..f1d2992 100644 --- a/src/gd_con.erl +++ b/src/gd_con.erl @@ -679,38 +679,63 @@ do_grids_sig(JSON, URL) -> do_grids_sig2(Request = #{"grids" := 1, "type" := "message"}) -> gd_gui:grids_mess_sig(Request); +do_grids_sig2(Request = #{"grids" := 1, "type" := "binary"}) -> + gd_gui:grids_mess_sig(Request); do_grids_sig2(Request = #{"grids" := 1, "type" := "tx"}) -> gd_gui:grids_mess_sig(Request); do_grids_sig2(WTF) -> gd_gui:trouble({trash, WTF}). -do_sign_mess(Request = #{"public_id" := ID, "payload" := Message}, - #s{wallet = #wallet{keys = Keys}}) -> +do_sign_mess(Request = #{"public_id" := ID}, #s{wallet = #wallet{keys = Keys}}) -> case lists:keyfind(ID, #key.id, Keys) of #key{pair = #{secret := SecKey}} -> Sig = base64:encode(hz:sign_message(list_to_binary(Message), SecKey)), - do_sign_mess2(Request#{"signature" => Sig}); + SignedRequest = maps:put("signature", Sig, Request), + ResponseKeys = + ["grids", + "chain", + "network_id", + "type", + "public_id", + "payload", + "signature"], + post_grids_response(ResponseKeys, SignedRequest); false -> gd_gui:trouble({bad_key, ID}) end. -do_sign_mess2(Request = #{"url" := URL}) -> - ResponseKeys = - ["grids", - "chain", - "network_id", - "type", - "public_id", - "payload", - "signature"], - Response = zj:encode(maps:with(ResponseKeys, Request)), - case httpc:request(post, {URL, [], "application/json", Response}, [], []) of - {ok, {{_, 200, _}, _, JSON}} -> log(info, "Signature posted: ~p", [JSON]); - {error, socket_closed_remotely} -> tell("Yep, closed remotely."); - Error -> gd_gui:trouble(Error) + +do_sign_binary(Request = #{"public_id" := ID}, #s{wallet = #wallet{keys = Keys}}) -> + case lists:keyfind(ID, #key.id, Keys) of + #key{pair = #{secret := SecKey}} -> do_sign_binary2(Request, SecKey); + false -> gd_gui:trouble({bad_key, ID}) end. +do_sign_binary2(Request = #{"payload" := Payload}, SecKey) -> + case base64_decode(Payload) of + {ok, Binary} -> + Sig = base64:encode(hz_sign_binary(Binary, SecKey)), + SignedRequest = maps:put("signature", Sig, Request), + ResponseKeys = + ["grids", + "chain", + "network_id", + "type", + "public_id", + "payload", + "signature"], + post_grids_response(ResponseKeys, SignedRequest); + Error -> + gd_gui:trouble(Error) + end. + +hz_sign_binary(Binary, SecKey) -> + Prefix = <<"Gajumaru Signed Binary:">>, + Target = <>, + {ok, Hash} = eblake2:blake2b(32, Target), + ecu_eddsa:sign_detached(Hashed, SecKey). + do_sign_tx(Request = #{"public_id" := ID, "payload" := CallData, "network_id" := NID}, #s{wallet = #wallet{keys = Keys}}) -> @@ -719,20 +744,22 @@ do_sign_tx(Request = #{"public_id" := ID, "payload" := CallData, "network_id" := #key{pair = #{secret := SecKey}} -> BinaryTX = list_to_binary(CallData), SignedTX = hz:sign_tx(BinaryTX, SecKey, BinNID), - do_sign_tx2(Request#{"signed" => true, "payload" := SignedTX}); + SignedRequest = Request#{"signed" => true, "payload" := SignedTX}, + ResponseKeys = + ["grids", + "chain", + "network_id", + "type", + "public_id", + "payload", + "signed"], + post_grids_response(ResponseKeys, SignedRequest); false -> gd_gui:trouble({bad_key, ID}) end. -do_sign_tx2(Request = #{"url" := URL}) -> - ResponseKeys = - ["grids", - "chain", - "network_id", - "type", - "public_id", - "payload", - "signed"], + +post_grids_response(ResponseKeys, Request = #{"url" := URL}) -> Response = zj:encode(maps:with(ResponseKeys, Request)), case httpc:request(post, {URL, [], "application/json", Response}, [], []) of {ok, {{_, 200, _}, _, JSON}} -> log(info, "Signed TX posted: ~p", [JSON]); diff --git a/src/gd_gui.erl b/src/gd_gui.erl index 3b583aa..a5e7b65 100644 --- a/src/gd_gui.erl +++ b/src/gd_gui.erl @@ -1060,6 +1060,48 @@ do_grids_mess_sig2(Request = #{"grids" := 1, ?wxID_CANCEL -> ok end, wxDialog:destroy(Dialog); +do_grids_mess_sig2(Request = #{"grids" := 1, + "type" := "binary", + "url" := URL, + "public_id" := ID, + "payload" := Base64}, + #s{frame = Frame, j = J}) -> + Dialog = wxDialog:new(Frame, ?wxID_ANY, J("Binary Data Signature Request")), + Sizer = wxBoxSizer:new(?wxVERTICAL), + Instruction = + J("The server at the URL below is requesting you sign the following binary data."), + InstTx = wxStaticText:new(Dialog, ?wxID_ANY, Instruction), + AcctSz = wxStaticBoxSizer:new(?wxVERTICAL, Dialog, [{label, J("Signature Account")}]), + AcctTx = wxStaticText:new(Dialog, ?wxID_ANY, ID), + _ = wxStaticBoxSizer:add(AcctSz, AcctTx, zxw:flags(wide)), + URL_Label = J("Originating URL"), + URL_Sz = wxStaticBoxSizer:new(?wxVERTICAL, Dialog, [{label, URL_Label}]), + URL_Tx = wxStaticText:new(Dialog, ?wxID_ANY, URL), + _ = wxStaticBoxSizer:add(URL_Sz, URL_Tx, zxw:flags(wide)), + MessSz = wxStaticBoxSizer:new(?wxVERTICAL, Dialog, [{label, J("Base-64 Data")}]), + MessStyle = ?wxTE_MULTILINE bor ?wxTE_READONLY, + MessTx = wxTextCtrl:new(Dialog, ?wxID_ANY, [{value, Base64}, {style, MessStyle}]), + _ = wxStaticBoxSizer:add(MessSz, MessTx, zxw:flags(wide)), + ButtSz = wxBoxSizer:new(?wxHORIZONTAL), + Affirm = wxButton:new(Dialog, ?wxID_OK), + Cancel = wxButton:new(Dialog, ?wxID_CANCEL), + _ = wxBoxSizer:add(ButtSz, Affirm, zxw:flags(wide)), + _ = wxBoxSizer:add(ButtSz, Cancel, zxw:flags(wide)), + _ = wxBoxSizer:add(Sizer, InstTx, zxw:flags(wide)), + _ = wxBoxSizer:add(Sizer, AcctSz, zxw:flags(wide)), + _ = wxBoxSizer:add(Sizer, URL_Sz, zxw:flags(wide)), + _ = wxBoxSizer:add(Sizer, MessSz, zxw:flags(wide)), + _ = wxBoxSizer:add(Sizer, ButtSz, zxw:flags(base)), + ok = wxDialog:setSizer(Dialog, Sizer), + ok = wxDialog:setSize(Dialog, {500, 500}), + ok = wxBoxSizer:layout(Sizer), + ok = wxFrame:center(Dialog), + ok = + case wxDialog:showModal(Dialog) of + ?wxID_OK -> gd_con:sign_binary(Request); + ?wxID_CANCEL -> ok + end, + wxDialog:destroy(Dialog); do_grids_mess_sig2(Request = #{"grids" := 1, "type" := "tx", "url" := URL,