From 45b296a2c591592712c9e5722d5b6a26eb03a20f Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 2 Apr 2020 15:17:32 +0300 Subject: [PATCH] [#10243] Handle wrong pin on keycard account creation --- src/status_im/hardwallet/common.cljs | 20 ++++++ src/status_im/hardwallet/core.cljs | 70 +++++++++---------- src/status_im/hardwallet/wallet.cljs | 58 ++++++++------- .../ui/screens/wallet/add_new/views.cljs | 3 +- 4 files changed, 88 insertions(+), 63 deletions(-) diff --git a/src/status_im/hardwallet/common.cljs b/src/status_im/hardwallet/common.cljs index b2aefcdf1a..5887ad46ef 100644 --- a/src/status_im/hardwallet/common.cljs +++ b/src/status_im/hardwallet/common.cljs @@ -464,3 +464,23 @@ (defn keycard-multiaccount? [db] (boolean (get-in db [:multiaccount :keycard-pairing]))) + +(fx/defn verify-pin + {:events [:hardwallet/verify-pin]} + [{:keys [db] :as cofx} {:keys [pin-step on-card-connected on-failure on-success]}] + (fx/merge + cofx + {:db (update-in db [:hardwallet :pin] assoc + :on-verified on-success + :on-verified-failure on-failure)} + (show-connection-sheet + {:on-card-connected (or on-card-connected :hardwallet/verify-pin) + :handler + (fn [{:keys [db] :as cofx}] + (let [pin (vector->string (get-in db [:hardwallet :pin pin-step])) + pairing (get-pairing db)] + (fx/merge + cofx + {:db (assoc-in db [:hardwallet :pin :status] :verifying) + :hardwallet/verify-pin {:pin pin + :pairing pairing}})))}))) diff --git a/src/status_im/hardwallet/core.cljs b/src/status_im/hardwallet/core.cljs index dd76467da8..33acd9337e 100644 --- a/src/status_im/hardwallet/core.cljs +++ b/src/status_im/hardwallet/core.cljs @@ -156,10 +156,10 @@ :puk []})} (common/hide-connection-sheet))))) -(fx/defn dispatch-on-verified-event - [{:keys [db]} event] - {:dispatch [event] - :db (assoc-in db [:hardwallet :pin :on-verified] nil)}) +(fx/defn clear-on-verify-handlers + [{:keys [db]}] + {:db (update-in db [:hardwallet :pin] + dissoc :on-verified-failure :on-verified)}) (fx/defn on-verify-pin-success {:events [:hardwallet.callback/on-verify-pin-success]} @@ -181,55 +181,51 @@ :hardwallet/generate-and-load-key :hardwallet/remove-key-with-unpair :hardwallet/unpair-and-delete - :hardwallet/generate-mnemonic} on-verified) + :hardwallet/generate-mnemonic + :wallet.accounts/generate-new-keycard-account} on-verified) (common/get-application-info pairing nil)) (when on-verified - (dispatch-on-verified-event on-verified))))) + (common/dispatch-event on-verified)) + (clear-on-verify-handlers)))) (fx/defn on-verify-pin-error {:events [:hardwallet.callback/on-verify-pin-error]} [{:keys [db] :as cofx} error] - (let [tag-was-lost? (common/tag-lost? (:error error)) - setup? (boolean (get-in db [:hardwallet :setup-step])) - exporting? (get-in db [:hardwallet :on-export-success])] + (let [tag-was-lost? (common/tag-lost? (:error error)) + setup? (boolean (get-in db [:hardwallet :setup-step])) + on-verified-failure (get-in db [:hardwallet :pin :on-verified-failure]) + exporting? (get-in db [:hardwallet :on-export-success])] (log/debug "[hardwallet] verify pin error" error) (when-not tag-was-lost? (if (re-matches common/pin-mismatch-error (:error error)) (fx/merge cofx - {:db (update-in db [:hardwallet :pin] merge {:status :error - :enter-step :current - :puk [] - :current [] - :original [] - :confirmation [] - :sign [] - :error-label :t/pin-mismatch})} + {:db (update-in db [:hardwallet :pin] + merge + {:status :error + :enter-step :current + :puk [] + :current [] + :original [] + :confirmation [] + :sign [] + :error-label :t/pin-mismatch})} (common/hide-connection-sheet) - (when-not setup? + (when (and (not setup?) + (not on-verified-failure)) (if exporting? (navigation/navigate-back) (navigation/navigate-to-cofx :enter-pin-settings nil))) - (common/get-application-info (common/get-pairing db) nil)) + (common/get-application-info (common/get-pairing db) nil) + (when on-verified-failure + (fn [_] {:utils/dispatch-later + [{:dispatch [on-verified-failure] + :ms 200}]})) + (clear-on-verify-handlers)) (fx/merge cofx (common/hide-connection-sheet) - (common/show-wrong-keycard-alert true)))))) - -(fx/defn verify-pin - {:events [:hardwallet/verify-pin]} - [cofx] - (common/show-connection-sheet - cofx - {:on-card-connected :hardwallet/verify-pin - :handler - (fn [{:keys [db] :as cofx}] - (let [pin (common/vector->string (get-in db [:hardwallet :pin :current])) - pairing (common/get-pairing db)] - (fx/merge - cofx - {:db (assoc-in db [:hardwallet :pin :status] :verifying) - :hardwallet/verify-pin {:pin pin - :pairing pairing}})))})) + (common/show-wrong-keycard-alert true) + (clear-on-verify-handlers)))))) (fx/defn unblock-pin {:events [:hardwallet/unblock-pin]} @@ -313,7 +309,7 @@ (and (= enter-step :current) (= pin-code-length numbers-entered)) - (verify-pin) + (common/verify-pin :current) (and (= enter-step :export-key) (= pin-code-length numbers-entered)) diff --git a/src/status_im/hardwallet/wallet.cljs b/src/status_im/hardwallet/wallet.cljs index 7e06d5edb5..64a7131639 100644 --- a/src/status_im/hardwallet/wallet.cljs +++ b/src/status_im/hardwallet/wallet.cljs @@ -1,6 +1,7 @@ (ns status-im.hardwallet.wallet (:require [status-im.ethereum.core :as ethereum] [status-im.utils.fx :as fx] + [status-im.ui.screens.wallet.add-new.views :as add-new.views] [status-im.hardwallet.common :as common] [status-im.constants :as constants] [status-im.ethereum.eip55 :as eip55] @@ -8,42 +9,51 @@ (fx/defn show-pin-sheet {:events [:hardwallet/new-account-pin-sheet]} - [{:keys [db] :as cofx} sheet-options] + [{:keys [db] :as cofx}] (fx/merge cofx - {:db (assoc-in db [:hardwallet :pin :enter-step] :export-key)} - (bottom-sheet/show-bottom-sheet sheet-options))) + {:db (-> db + (assoc-in [:hardwallet :pin :enter-step] :export-key) + (update-in [:hardwallet :pin] dissoc :export-key))} + (bottom-sheet/show-bottom-sheet + {:view {:content add-new.views/pin + :height 256}}))) (fx/defn hide-pin-sheet {:events [:hardwallet/hide-new-account-pin-sheet]} - [{:keys [db] :as cofx}] + [cofx] (fx/merge cofx {:utils/dispatch-later ;; We need to give previous sheet some time to be fully hidden [{:ms 200 - :dispatch [:wallet.accounts/generate-new-keycard-account]}]} + :dispatch [:wallet.accounts/verify-pin]}]} (bottom-sheet/hide-bottom-sheet))) (fx/defn generate-new-keycard-account {:events [:wallet.accounts/generate-new-keycard-account]} - [cofx] - (common/show-connection-sheet - cofx - {:on-card-connected :wallet.accounts/generate-new-keycard-account - :handler - (fn [{:keys [db]}] - (let [path-num (inc (get-in db [:multiaccount :latest-derived-path])) - path (str constants/path-wallet-root "/" path-num) - pin (common/vector->string (get-in db [:hardwallet :pin :export-key])) - pairing (common/get-pairing db)] - {:db - (assoc-in - db [:hardwallet :on-export-success] - #(vector :wallet.accounts/account-stored - {;; Strip leading 04 prefix denoting uncompressed key format - :address (eip55/address->checksum (str "0x" (ethereum/public-key->address (subs % 2)))) - :public-key (str "0x" %) - :path path})) + [{:keys [db]}] + (let [path-num (inc (get-in db [:multiaccount :latest-derived-path])) + path (str constants/path-wallet-root "/" path-num) + pin (common/vector->string (get-in db [:hardwallet :pin :export-key])) + pairing (common/get-pairing db)] + {:db + (assoc-in + db [:hardwallet :on-export-success] + #(vector :wallet.accounts/account-stored + {;; Strip leading 04 prefix denoting uncompressed key format + :address (eip55/address->checksum (str "0x" (ethereum/public-key->address (subs % 2)))) + :public-key (str "0x" %) + :path path})) - :hardwallet/export-key {:pin pin :pairing pairing :path path}}))})) + :hardwallet/export-key {:pin pin :pairing pairing :path path}})) + +(fx/defn verify-pin + {:events [:wallet.accounts/verify-pin]} + [cofx] + (common/verify-pin + cofx + {:pin-step :export-key + :on-card-connected :wallet.accounts/generate-new-keycard-account + :on-success :wallet.accounts/generate-new-keycard-account + :on-failure :hardwallet/new-account-pin-sheet})) diff --git a/src/status_im/ui/screens/wallet/add_new/views.cljs b/src/status_im/ui/screens/wallet/add_new/views.cljs index 895c49e23e..5d8cf691b9 100644 --- a/src/status_im/ui/screens/wallet/add_new/views.cljs +++ b/src/status_im/ui/screens/wallet/add_new/views.cljs @@ -17,8 +17,7 @@ [status-im.ethereum.core :as ethereum] [status-im.utils.security :as security] [clojure.string :as string] - [status-im.utils.platform :as platform] - [taoensso.timbre :as log])) + [status-im.utils.platform :as platform])) (defn- request-camera-permissions [] (let [options {:handler :wallet.add-new/qr-scanner-result}]