[simulated kk] Add proper signing for send/request transaction messages

This commit is contained in:
Roman Volosovskyi 2020-06-25 14:16:24 +03:00
parent ca75a7a057
commit 7f1e25a515
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
5 changed files with 64 additions and 27 deletions

View File

@ -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

View File

@ -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]}

View File

@ -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]

View File

@ -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

View File

@ -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]}]