From 7f1e25a5156d0ffe40b038fb9a3f5481e8ebbf65 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 25 Jun 2020 14:16:24 +0300 Subject: [PATCH] [simulated kk] Add proper signing for send/request transaction messages --- src/status_im/hardwallet/real_keycard.cljs | 4 +- src/status_im/hardwallet/sign.cljs | 40 ++++++++++--------- .../hardwallet/simulated_keycard.cljs | 26 +++++++++++- src/status_im/signing/core.cljs | 3 +- src/status_im/signing/keycard.cljs | 18 ++++++--- 5 files changed, 64 insertions(+), 27 deletions(-) diff --git a/src/status_im/hardwallet/real_keycard.cljs b/src/status_im/hardwallet/real_keycard.cljs index e27705bac6..9d5ff2d6d2 100644 --- a/src/status_im/hardwallet/real_keycard.cljs +++ b/src/status_im/hardwallet/real_keycard.cljs @@ -4,7 +4,8 @@ [status-im.utils.types :as types] [status-im.native-module.core :as status] [status-im.ethereum.core :as ethereum] - [status-im.hardwallet.keycard :as keycard])) + [status-im.hardwallet.keycard :as keycard] + [taoensso.timbre :as log])) (defonce event-emitter (.-DeviceEventEmitter rn)) (defonce active-listeners (atom [])) @@ -191,6 +192,7 @@ (defn sign [{:keys [pairing pin path hash on-success on-failure]}] + (log/debug "keycard sign" "path" path) (when (and pairing pin hash) (if path (.. status-keycard diff --git a/src/status_im/hardwallet/sign.cljs b/src/status_im/hardwallet/sign.cljs index f8f5ccad90..1c0a22e65c 100644 --- a/src/status_im/hardwallet/sign.cljs +++ b/src/status_im/hardwallet/sign.cljs @@ -1,13 +1,13 @@ (ns status-im.hardwallet.sign - (:require [clojure.string :as string] - [re-frame.core :as re-frame] + (:require [re-frame.core :as re-frame] [status-im.ethereum.core :as ethereum] [status-im.ethereum.json-rpc :as json-rpc] [status-im.utils.fx :as fx] [status-im.utils.money :as money] [status-im.utils.types :as types] [taoensso.timbre :as log] - [status-im.hardwallet.common :as common])) + [status-im.hardwallet.common :as common] + [clojure.string :as clojure.string])) (fx/defn sign {:events [:hardwallet/sign]} @@ -18,6 +18,7 @@ instance-uid (get-in db [:hardwallet :application-info :instance-uid]) keycard-match? (= keycard-instance-uid instance-uid) hash (get-in db [:hardwallet :hash]) + data (get-in db [:hardwallet :data]) pin (common/vector->string (get-in db [:hardwallet :pin :sign])) from (get-in db [:signing/tx :from :address]) path (reduce @@ -32,6 +33,7 @@ (assoc-in [:hardwallet :card-read-in-progress?] true) (assoc-in [:hardwallet :pin :status] :verifying)) :hardwallet/sign {:hash (ethereum/naked-address hash) + :data data :pairing pairing :pin pin :path path}} @@ -59,6 +61,7 @@ (assoc-in [:hardwallet :card-read-in-progress?] true) (assoc-in [:hardwallet :pin :status] :verifying)) :hardwallet/sign {:hash (ethereum/naked-address hash) + :data (:data params) :pairing pairing :pin pin :on-success on-success}} @@ -77,6 +80,7 @@ (if message-id [:sign/send-accept-transaction-message message-id tx-hash signature] [:sign/send-transaction-message chat-id value contract tx-hash signature]) + :signing/show-transaction-result nil :db (-> db (assoc-in [:hardwallet :pin :sign] []) (assoc-in [:hardwallet :pin :status] nil))} @@ -144,14 +148,8 @@ (fx/defn sign-message-completed [_ signature] - (let [signature' (-> signature - ; add 27 to last byte - ; https://github.com/ethereum/go-ethereum/blob/master/internal/ethapi/api.go#L431 - (clojure.string/replace-first #"00$", "1b") - (clojure.string/replace-first #"01$", "1c") - (ethereum/normalized-hex))] - {:dispatch - [:signing/sign-message-completed (types/clj->json {:result signature'})]})) + {:dispatch + [:signing/sign-message-completed signature]}) (fx/defn send-transaction-with-signature [_ data] @@ -161,9 +159,14 @@ {:events [:hardwallet.callback/on-sign-success]} [{:keys [db] :as cofx} signature] (log/debug "[hardwallet] sign success: " signature) - (let [transaction (get-in db [:hardwallet :transaction]) - tx-obj (select-keys transaction [:from :to :value :gas :gasPrice :command? :chat-id :message-id]) - command? (:command? transaction)] + (let [signature-json (types/clj->json + {:result (-> signature + (clojure.string/replace-first #"00$", "1b") + (clojure.string/replace-first #"01$", "1c") + ethereum/normalized-hex)}) + transaction (get-in db [:hardwallet :transaction]) + tx-obj (select-keys transaction [:from :to :value :gas :gasPrice :command? :chat-id :message-id]) + command? (:command? transaction)] (fx/merge cofx {:db (-> db (assoc-in [:hardwallet :hash] nil) @@ -179,10 +182,11 @@ (common/get-application-info (common/get-pairing db) nil) (common/hide-connection-sheet)))) (if transaction - (send-transaction-with-signature {:transaction (types/clj->json transaction) - :signature signature - :on-completed #(re-frame/dispatch [:signing/transaction-completed % tx-obj])}) - (sign-message-completed signature))))) + (send-transaction-with-signature + {:transaction (types/clj->json transaction) + :signature signature + :on-completed #(re-frame/dispatch [:signing/transaction-completed % tx-obj])}) + (sign-message-completed signature-json))))) (fx/defn on-sign-error {:events [:hardwallet.callback/on-sign-error]} diff --git a/src/status_im/hardwallet/simulated_keycard.cljs b/src/status_im/hardwallet/simulated_keycard.cljs index bed5d2de71..bea944809f 100644 --- a/src/status_im/hardwallet/simulated_keycard.cljs +++ b/src/status_im/hardwallet/simulated_keycard.cljs @@ -254,10 +254,32 @@ #js {:code "EUNSPECIFIED" :message "Unexpected error SW, 0x63C2"}))))) -(defn sign [{:keys [pin on-success on-failure]}] +(defn sign [{:keys [pin hash data path on-success on-failure]}] (if (= pin (get @state :pin)) (later - #(on-success "123")) + #(let [address + (if path + (reduce + (fn [_ {:keys [address] :as acc}] + (when (= path (:path acc)) + (reduced address))) + nil + (:multiaccount/accounts @re-frame.db/app-db)) + (-> (:multiaccount/accounts @re-frame.db/app-db) + first + :address)) + params (types/clj->json + {:account address + :password (ethereum/sha3 pin) + :data (or data (str "0x" hash))})] + (status/sign-message + params + (fn [res] + (let [signature (-> res + types/json->clj + :result + ethereum/normalized-hex)] + (on-success signature)))))) (do (swap! state update-in [:application-info :pin-retry-counter] diff --git a/src/status_im/signing/core.cljs b/src/status_im/signing/core.cljs index 0a7dd535ce..4a28395de9 100644 --- a/src/status_im/signing/core.cljs +++ b/src/status_im/signing/core.cljs @@ -271,7 +271,8 @@ :message-id message-id :chat-id chat-id :value value - :contract contract} + :contract contract + :data data} hash]))}) (fn [_] {:signing.fx/sign-message diff --git a/src/status_im/signing/keycard.cljs b/src/status_im/signing/keycard.cljs index c7c8e2c799..44d846354d 100644 --- a/src/status_im/signing/keycard.cljs +++ b/src/status_im/signing/keycard.cljs @@ -43,18 +43,26 @@ {::hash-typed-data {:data data :on-completed - (or on-completed #(re-frame/dispatch [:signing.keycard.callback/hash-message-completed %]))}} + (or on-completed + #(re-frame/dispatch + [:signing.keycard.callback/hash-message-completed + data typed? %]))}} {::hash-message {:message data :on-completed - (or on-completed #(re-frame/dispatch [:signing.keycard.callback/hash-message-completed %]))}})) + (or on-completed + #(re-frame/dispatch + [:signing.keycard.callback/hash-message-completed + data typed? %]))}})) (fx/defn hash-message-completed {:events [:signing.keycard.callback/hash-message-completed]} - [{:keys [db]} result] + [{:keys [db]} data typed? result] (let [{:keys [result error]} (types/json->clj result)] - {:db (-> db - (assoc-in [:hardwallet :hash] result))})) + {:db (update db :hardwallet assoc + :hash result + :type? typed? + :data data)})) (fx/defn hash-transaction [{:keys [db]}]