[#7890] handle wrong keycard when signing

Signed-off-by: Dmitry Novotochinov <dmitry.novot@gmail.com>
This commit is contained in:
Dmitry Novotochinov 2019-04-15 13:06:48 +03:00
parent 9b9eefbf0e
commit 08d7db80af
No known key found for this signature in database
GPG Key ID: 43D1DAF5AD39C927
2 changed files with 60 additions and 16 deletions

View File

@ -1417,6 +1417,16 @@
(fn [cofx _] (fn [cofx _]
(hardwallet/sign cofx))) (hardwallet/sign cofx)))
(handlers/register-handler-fx
:hardwallet/prepare-to-sign
(fn [cofx _]
(hardwallet/prepare-to-sign cofx)))
(handlers/register-handler-fx
:hardwallet/unblock-pin
(fn [cofx _]
(hardwallet/unblock-pin cofx)))
;; browser module ;; browser module
(handlers/register-handler-fx (handlers/register-handler-fx

View File

@ -291,6 +291,12 @@
[_ event] [_ event]
{:dispatch [event]}) {:dispatch [event]})
(fx/defn show-wrong-keycard-alert
[_ card-connected?]
(when card-connected?
{:utils/show-popup {:title (i18n/label :t/wrong-card)
:content (i18n/label :t/wrong-card-text)}}))
(fx/defn on-get-application-info-success (fx/defn on-get-application-info-success
[{:keys [db] :as cofx} info on-success] [{:keys [db] :as cofx} info on-success]
(let [info' (js->clj info :keywordize-keys true) (let [info' (js->clj info :keywordize-keys true)
@ -327,6 +333,7 @@
[{:keys [db] :as cofx} error] [{:keys [db] :as cofx} error]
(log/debug "[hardwallet] application info error " error) (log/debug "[hardwallet] application info error " error)
(let [on-card-read (get-in db [:hardwallet :on-card-read]) (let [on-card-read (get-in db [:hardwallet :on-card-read])
on-card-connected (get-in db [:hardwallet :on-card-connected])
connect-screen? (= (:view-id db) :hardwallet-connect) connect-screen? (= (:view-id db) :hardwallet-connect)
login? (= on-card-read :hardwallet/login-with-keycard)] login? (= on-card-read :hardwallet/login-with-keycard)]
(if login? (if login?
@ -337,6 +344,8 @@
(navigation/navigate-to-cofx :accounts nil)) (navigation/navigate-to-cofx :accounts nil))
(fx/merge cofx (fx/merge cofx
{:db (assoc-in db [:hardwallet :application-info-error] error)} {:db (assoc-in db [:hardwallet :application-info-error] error)}
(when (= on-card-connected :hardwallet/prepare-to-sign)
(show-wrong-keycard-alert true))
(when-not connect-screen? (when-not connect-screen?
(clear-on-card-read)) (clear-on-card-read))
(when on-card-read (when on-card-read
@ -579,7 +588,7 @@
:db (-> db :db (-> db
(update-in [:hardwallet :pin] merge {:status nil (update-in [:hardwallet :pin] merge {:status nil
:enter-step :original :enter-step :original
:current (vec (string/split default-pin #"")) :current [0 0 0 0 0 0]
:puk [] :puk []
:error-label nil}))} :error-label nil}))}
(navigation/navigate-to-cofx :enter-pin nil)))) (navigation/navigate-to-cofx :enter-pin nil))))
@ -687,14 +696,19 @@
:pairing pairing}})) :pairing pairing}}))
(defn- unblock-pin (defn- unblock-pin
[{:keys [db] :as fx}] [{:keys [db] :as cofx}]
(let [puk (vector->string (get-in fx [:db :hardwallet :pin :puk])) (let [puk (vector->string (get-in cofx [:db :hardwallet :pin :puk]))
instance-uid (get-in db [:hardwallet :application-info :instance-uid]) instance-uid (get-in db [:hardwallet :application-info :instance-uid])
card-connected? (get-in db [:hardwallet :card-connected?])
pairing (get-pairing db instance-uid)] pairing (get-pairing db instance-uid)]
{:db (assoc-in db [:hardwallet :pin :status] :verifying) (if card-connected?
:hardwallet/unblock-pin {:puk puk {:db (assoc-in db [:hardwallet :pin :status] :verifying)
:new-pin default-pin :hardwallet/unblock-pin {:puk puk
:pairing pairing}})) :new-pin default-pin
:pairing pairing}}
(fx/merge cofx
{:db (assoc-in db [:hardwallet :on-card-connected] :hardwallet/unblock-pin)}
(navigation/navigate-to-cofx :hardwallet-connect nil)))))
(def pin-code-length 6) (def pin-code-length 6)
(def puk-code-length 12) (def puk-code-length 12)
@ -739,9 +753,13 @@
[{:keys [db] :as cofx}] [{:keys [db] :as cofx}]
(let [card-connected? (get-in db [:hardwallet :card-connected?]) (let [card-connected? (get-in db [:hardwallet :card-connected?])
pairing (get-pairing db) pairing (get-pairing db)
account-keycard-instance-uid (get-in db [:account/account :keycard-instance-uid])
instance-uid (get-in db [:hardwallet :application-info :instance-uid])
keycard-match? (= account-keycard-instance-uid instance-uid)
hash (get-in db [:hardwallet :hash]) hash (get-in db [:hardwallet :hash])
pin (vector->string (get-in db [:hardwallet :pin :sign]))] pin (vector->string (get-in db [:hardwallet :pin :sign]))]
(if card-connected? (if (and card-connected?
keycard-match?)
{:db (-> db {:db (-> db
(assoc-in [:hardwallet :card-read-in-progress?] true) (assoc-in [:hardwallet :card-read-in-progress?] true)
(assoc-in [:hardwallet :pin :status] :verifying)) (assoc-in [:hardwallet :pin :status] :verifying))
@ -750,6 +768,18 @@
:pin pin}} :pin pin}}
(fx/merge cofx (fx/merge cofx
{:db (assoc-in db [:hardwallet :on-card-connected] :hardwallet/sign)} {:db (assoc-in db [:hardwallet :on-card-connected] :hardwallet/sign)}
(when-not keycard-match?
(show-wrong-keycard-alert card-connected?))
(navigation/navigate-to-cofx :hardwallet-connect nil)))))
(fx/defn prepare-to-sign
[{:keys [db] :as cofx}]
(let [card-connected? (get-in db [:hardwallet :card-connected?])
pairing (get-pairing db)]
(if card-connected?
(get-application-info cofx pairing :hardwallet/sign)
(fx/merge cofx
{:db (assoc-in db [:hardwallet :on-card-connected] :hardwallet/prepare-to-sign)}
(navigation/navigate-to-cofx :hardwallet-connect nil))))) (navigation/navigate-to-cofx :hardwallet-connect nil)))))
; PIN enter steps: ; PIN enter steps:
@ -784,7 +814,7 @@
(and (= enter-step :sign) (and (= enter-step :sign)
(= pin-code-length numbers-entered)) (= pin-code-length numbers-entered))
(sign) (prepare-to-sign)
(and (= enter-step :puk) (and (= enter-step :puk)
(= puk-code-length numbers-entered)) (= puk-code-length numbers-entered))
@ -1165,15 +1195,19 @@
(if transaction (if transaction
(send-transaction-with-signature {:transaction (types/clj->json transaction) (send-transaction-with-signature {:transaction (types/clj->json transaction)
:signature signature :signature signature
:on-completed #(re-frame/dispatch [:status-im.ui.screens.wallet.send.events/transaction-completed (types/json->clj %)])}) :on-completed #(re-frame/dispatch [:wallet.callback/transaction-completed (types/json->clj %)])})
(sign-message-completed signature))))) (sign-message-completed signature)))))
(def pin-mismatch-error #"Unexpected error SW, 0x63C\d+")
(fx/defn on-sign-error (fx/defn on-sign-error
[{:keys [db] :as cofx} error] [{:keys [db] :as cofx} error]
(log/debug "[hardwallet] sign error: " error) (log/debug "[hardwallet] sign error: " error)
(fx/merge cofx (if (re-matches pin-mismatch-error (:error error))
{:db (update-in db [:hardwallet :pin] merge {:status :error (fx/merge cofx
:sign [] {:db (update-in db [:hardwallet :pin] merge {:status :error
:error-label :t/pin-mismatch})} :sign []
(navigation/navigate-to-cofx :enter-pin nil) :error-label :t/pin-mismatch})}
(get-application-info (get-pairing db) nil))) (navigation/navigate-to-cofx :enter-pin nil)
(get-application-info (get-pairing db) nil))
(show-wrong-keycard-alert cofx true)))