Skip to content

Commit

Permalink
[#7911 #7894] delete account and logout after keycard reset
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Novotochinov <dmitry.novot@gmail.com>
  • Loading branch information
dmitryn committed May 8, 2019
1 parent 5926c2b commit 99e0089
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 64 deletions.
2 changes: 1 addition & 1 deletion mobile_files/package.json.orig
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"react-native-safe-area-view": "0.9.0",
"react-native-securerandom": "git+https://github.com/status-im/react-native-securerandom.git#0.1.1-2",
"react-native-splash-screen": "3.1.1",
"react-native-status-keycard": "git+https://github.com/status-im/react-native-status-keycard.git#v2.5.0",
"react-native-status-keycard": "git+https://github.com/status-im/react-native-status-keycard.git#v2.5.3",
"react-native-svg": "^9.2.4",
"react-native-svg-transformer": "^0.12.1",
"react-native-tcp": "git+https://github.com/status-im/react-native-tcp.git#v3.3.0-1-status",
Expand Down
6 changes: 3 additions & 3 deletions mobile_files/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5582,9 +5582,9 @@ react-native-splash-screen@3.1.1:
resolved "https://registry.yarnpkg.com/react-native-splash-screen/-/react-native-splash-screen-3.1.1.tgz#1a4e46c9fdce53ff52af2a2cb4181788c4e30b30"
integrity sha512-PU2YocOSGbLjL9Vgcq/cwMNuHHKNjjuPpa1IPMuWo+6EB/fSZ5VOmxSa7+eucQe3631s3NhGuk3eHKahU03a4Q==

"react-native-status-keycard@git+https://github.com/status-im/react-native-status-keycard.git#v2.5.0":
version "2.5.0"
resolved "git+https://github.com/status-im/react-native-status-keycard.git#d9ccdd6a7db0ae6c3d52b8c998529ac5a21ca029"
"react-native-status-keycard@git+https://github.com/status-im/react-native-status-keycard.git#v2.5.3":
version "2.5.3"
resolved "git+https://github.com/status-im/react-native-status-keycard.git#4ba5e8a771e764c449c19180e5eaa8553c789b8d"

react-native-svg-transformer@^0.12.1:
version "0.12.1"
Expand Down
9 changes: 3 additions & 6 deletions src/status_im/accounts/login/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,9 @@

(fx/defn open-keycard-login
[{:keys [db] :as cofx}]
(let [navigation-stack (:navigation-stack db)]
(fx/merge cofx
{:db (assoc-in db [:hardwallet :pin :enter-step] :login)}
(if (empty? navigation-stack)
(navigation/navigate-to-cofx :accounts nil)
(navigation/navigate-to-cofx :enter-pin nil)))))
(fx/merge cofx
{:db (assoc-in db [:hardwallet :pin :enter-step] :login)}
(navigation/navigate-to-cofx :enter-pin nil)))

(fx/defn get-user-password
[_ address]
Expand Down
8 changes: 8 additions & 0 deletions src/status_im/data_store/accounts.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@
(fn [realm]
(core/create realm :account (serialize-account account) true)))

(defn delete-account-tx
"Returns tx function for deleting account"
[address]
(fn [realm]
(let [account (core/single (core/get-by-field realm :account :address address))]
(when account
(core/delete realm account)))))

(re-frame/reg-cofx
:data-store/get-all-accounts
(fn [coeffects _]
Expand Down
25 changes: 25 additions & 0 deletions src/status_im/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,16 @@
(fn [cofx [_ error]]
(hardwallet/on-delete-error cofx error)))

(handlers/register-handler-fx
:hardwallet.callback/on-remove-key-success
(fn [cofx _]
(hardwallet/on-remove-key-success cofx)))

(handlers/register-handler-fx
:hardwallet.callback/on-remove-key-error
(fn [cofx [_ error]]
(hardwallet/on-remove-key-error cofx error)))

(handlers/register-handler-fx
:hardwallet.callback/on-get-keys-success
(fn [cofx [_ data]]
Expand Down Expand Up @@ -1336,6 +1346,11 @@
(fn [cofx _]
(hardwallet/card-ready-next-button-pressed cofx)))

(handlers/register-handler-fx
:hardwallet/proceed-to-generate-mnemonic
(fn [cofx _]
(hardwallet/proceed-to-generate-mnemonic cofx)))

(handlers/register-handler-fx
:hardwallet/generate-mnemonic
(fn [cofx _]
Expand Down Expand Up @@ -1383,6 +1398,11 @@
(fn [cofx _]
(hardwallet/unpair cofx)))

(handlers/register-handler-fx
:hardwallet/verify-pin
(fn [cofx _]
(hardwallet/verify-pin cofx)))

(handlers/register-handler-fx
:keycard-settings.ui/reset-card-pressed
(fn [cofx _]
Expand All @@ -1403,6 +1423,11 @@
(fn [cofx _]
(hardwallet/unpair-and-delete cofx)))

(handlers/register-handler-fx
:hardwallet/remove-key-with-unpair
(fn [cofx _]
(hardwallet/remove-key-with-unpair cofx)))

(handlers/register-handler-fx
:hardwallet/navigate-to-enter-pin-screen
(fn [cofx _]
Expand Down
14 changes: 14 additions & 0 deletions src/status_im/hardwallet/card.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@
(then #(re-frame/dispatch [:hardwallet.callback/on-delete-success %]))
(catch #(re-frame/dispatch [:hardwallet.callback/on-delete-error (error-object->map %)]))))

(defn remove-key
[{:keys [pin pairing]}]
(.. keycard
(removeKey pairing pin)
(then #(re-frame/dispatch [:hardwallet.callback/on-remove-key-success %]))
(catch #(re-frame/dispatch [:hardwallet.callback/on-remove-key-error (error-object->map %)]))))

(defn remove-key-with-unpair
[{:keys [pin pairing]}]
(.. keycard
(removeKeyWithUnpair pairing pin)
(then #(re-frame/dispatch [:hardwallet.callback/on-remove-key-success %]))
(catch #(re-frame/dispatch [:hardwallet.callback/on-remove-key-error (error-object->map %)]))))

(defn unpair-and-delete
[{:keys [pin pairing]}]
(when (and pairing pin)
Expand Down
141 changes: 108 additions & 33 deletions src/status_im/hardwallet/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@
navigate-to-browser? (contains? #{:wallet-sign-message-modal
:wallet-send-transaction-modal
:wallet-send-modal-stack} screen-before)]
(if navigate-to-browser?
(fx/merge cofx
{:db (assoc-in db [:hardwallet :on-card-connected] nil)}
(models.wallet/discard-transaction)
(navigation/navigate-to-cofx :browser nil))
(fx/merge cofx
{:db (assoc-in db [:hardwallet :on-card-connected] nil)}
(navigation/navigate-back)))))
(if (nil? screen-before)
(navigation/navigate-to-cofx cofx :accounts nil)
(if navigate-to-browser?
(fx/merge cofx
{:db (assoc-in db [:hardwallet :on-card-connected] nil)}
(models.wallet/discard-transaction)
(navigation/navigate-to-cofx :browser nil))
(fx/merge cofx
{:db (assoc-in db [:hardwallet :on-card-connected] nil)}
(navigation/navigate-back))))))

(fx/defn remove-pairing-from-account
[{:keys [db]} {:keys [remove-instance-uid?]}]
Expand Down Expand Up @@ -99,6 +101,7 @@
(case state
:not-paired :pair
:no-pairing-slots :no-slots
:init :card-ready
:begin))

(defn- get-card-state
Expand All @@ -114,7 +117,6 @@
:no-pairing-slots

(and (not paired?)
has-master-key?
(pos? free-pairing-slots))
:not-paired

Expand Down Expand Up @@ -144,7 +146,7 @@
(fx/merge cofx
{:db db'}
(set-setup-step card-state)
(if (= :pre-init card-state)
(if (contains? #{:init :pre-init} card-state)
(navigation/navigate-to-cofx :hardwallet-setup nil)
(when-not (= :not-paired card-state)
(navigation/navigate-to-cofx :hardwallet-authentication-method nil)))
Expand Down Expand Up @@ -447,20 +449,70 @@
{:hardwallet/unpair-and-delete {:pin pin
:pairing pairing}}))

(fx/defn remove-key-with-unpair
[{:keys [db] :as cofx}]
(let [pin (vector->string (get-in db [:hardwallet :pin :current]))
pairing (get-pairing db)
card-connected? (get-in db [:hardwallet :card-connected?])]
(if card-connected?
{:hardwallet/remove-key-with-unpair {:pin pin
:pairing pairing}}
(fx/merge cofx
{:db (assoc-in db [:hardwallet :on-card-connected] :hardwallet/remove-key-with-unpair)}
(navigation/navigate-to-cofx :hardwallet-connect nil)))))

(fx/defn on-remove-key-success
[{:keys [db] :as cofx}]
(let [account-address (get-in db [:account/account :address])
pairing (get-in db [:account/account :keycard-pairing])]
(fx/merge cofx
{:db (-> db
(update :accounts/accounts dissoc account-address)
(assoc-in [:hardwallet :whisper-public-key] nil)
(assoc-in [:hardwallet :wallet-address] nil)
(assoc-in [:hardwallet :secrets] {:pairing pairing})
(assoc-in [:hardwallet :application-info] nil)
(assoc-in [:hardwallet :on-card-connected] nil)
(assoc-in [:hardwallet :pin] {:status nil
:error-label nil
:on-verified nil}))
:data-store/base-tx [(accounts-store/delete-account-tx account-address)]
:utils/show-popup {:title ""
:content (i18n/label :t/card-reseted)}}
(accounts.logout/logout))))

(fx/defn on-remove-key-error
[{:keys [db] :as cofx} error]
(log/debug "[hardwallet] remove key error" error)
(let [tag-was-lost? (= "Tag was lost." (:error error))]
(fx/merge cofx
(if tag-was-lost?
(fx/merge cofx
{:db (-> db
(assoc-in [:hardwallet :on-card-connected] :hardwallet/remove-key-with-unpair)
(assoc-in [:hardwallet :pin :status] nil))
:utils/show-popup {:title (i18n/label :t/error)
:content (i18n/label :t/cannot-read-card)}}
(navigation/navigate-to-cofx :hardwallet-connect nil))
(show-wrong-keycard-alert cofx true)))))

(fx/defn on-delete-success
[{:keys [db] :as cofx}]
(fx/merge cofx
{:db (-> db
(assoc-in [:hardwallet :secrets] nil)
(assoc-in [:hardwallet :application-info] nil)
(assoc-in [:hardwallet :on-card-connected] nil)
(assoc-in [:hardwallet :pin] {:status nil
:error-label nil
:on-verified nil}))
:utils/show-popup {:title ""
:content (i18n/label :t/card-reseted)}}
(remove-pairing-from-account {:remove-instance-uid? true})
(navigation/navigate-to-cofx :keycard-settings nil)))
(let [account-address (get-in db [:account/account :address])]
(fx/merge cofx
{:db (-> db
(update :accounts/accounts dissoc account-address)
(assoc-in [:hardwallet :secrets] nil)
(assoc-in [:hardwallet :application-info] nil)
(assoc-in [:hardwallet :on-card-connected] nil)
(assoc-in [:hardwallet :pin] {:status nil
:error-label nil
:on-verified nil}))
:data-store/base-tx [(accounts-store/delete-account-tx account-address)]
:utils/show-popup {:title ""
:content (i18n/label :t/card-reseted)}}

(accounts.logout/logout))))

(fx/defn on-delete-error
[{:keys [db] :as cofx} error]
Expand Down Expand Up @@ -517,7 +569,7 @@
:puk []
:status nil
:error-label nil
:on-verified :hardwallet/unpair-and-delete}))}
:on-verified :hardwallet/remove-key-with-unpair}))}
(if card-connected?
(navigate-to-enter-pin-screen)
(navigation/navigate-to-cofx :hardwallet-connect nil)))))
Expand Down Expand Up @@ -627,7 +679,9 @@
{:db (-> db
(update-in [:hardwallet :pin] merge {:status nil
:error-label nil}))}
(when-not (contains? #{:hardwallet/unpair :hardwallet/unpair-and-delete} on-verified)
(when-not (contains? #{:hardwallet/unpair
:hardwallet/remove-key-with-unpair
:hardwallet/unpair-and-delete} on-verified)
(get-application-info pairing nil))
(when on-verified
(dispatch-on-verified-event on-verified)))))
Expand Down Expand Up @@ -696,12 +750,17 @@
(navigation/navigate-to-cofx :keycard-settings nil)))

(defn- verify-pin
[{:keys [db] :as fx}]
(let [pin (vector->string (get-in fx [:db :hardwallet :pin :current]))
pairing (get-pairing db)]
{:db (assoc-in db [:hardwallet :pin :status] :verifying)
:hardwallet/verify-pin {:pin pin
:pairing pairing}}))
[{:keys [db] :as cofx}]
(let [pin (vector->string (get-in cofx [:db :hardwallet :pin :current]))
pairing (get-pairing db)
card-connected? (get-in db [:hardwallet :card-connected?])]
(if card-connected?
{:db (assoc-in db [:hardwallet :pin :status] :verifying)
:hardwallet/verify-pin {:pin pin
:pairing pairing}}
(fx/merge cofx
{:db (assoc-in db [:hardwallet :on-card-connected] :hardwallet/verify-pin)}
(navigation/navigate-to-cofx :hardwallet-connect nil)))))

(defn- unblock-pin
[{:keys [db] :as cofx}]
Expand Down Expand Up @@ -1055,6 +1114,22 @@
{:db (assoc-in db [:hardwallet :recovery-phrase :confirm-error] (i18n/label :t/wrong-word))})))

(fx/defn card-ready-next-button-pressed
[{:keys [db] :as cofx}]
(let [pin (get-in db [:hardwallet :secrets :pin])
pin-already-set? (boolean pin)]
(if pin-already-set?
(if (= (get-in db [:hardwallet :flow]) :create)
(load-generating-mnemonic-screen cofx)
{:db (assoc-in db [:hardwallet :setup-step] :recovery-phrase)})
(fx/merge cofx
{:db (-> db
(assoc-in [:hardwallet :setup-step] :pin)
(assoc-in [:hardwallet :pin :enter-step] :current)
(assoc-in [:hardwallet :pin :on-verified] :hardwallet/proceed-to-generate-mnemonic)
(assoc-in [:hardwallet :pin :current] [])
(assoc-in [:hardwallet :pin :original] nil))}))))

(fx/defn proceed-to-generate-mnemonic
[{:keys [db] :as cofx}]
(if (= (get-in db [:hardwallet :flow]) :create)
(load-generating-mnemonic-screen cofx)
Expand All @@ -1071,12 +1146,12 @@

(fx/defn generate-and-load-key
[{:keys [db] :as cofx}]
(let [{:keys [mnemonic pairing]} (get-in db [:hardwallet :secrets])
pin (vector->string (get-in db [:hardwallet :pin :original]))]
(let [{:keys [mnemonic pairing pin]} (get-in db [:hardwallet :secrets])
pin' (or pin (vector->string (get-in db [:hardwallet :pin :current])))]
(fx/merge cofx
{:hardwallet/generate-and-load-key {:mnemonic mnemonic
:pairing pairing
:pin pin}})))
:pin pin'}})))

(fx/defn create-keycard-account
[{:keys [db] :as cofx}]
Expand Down
8 changes: 8 additions & 0 deletions src/status_im/hardwallet/fx.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@
:hardwallet/delete
card/delete)

(re-frame/reg-fx
:hardwallet/remove-key
card/remove-key)

(re-frame/reg-fx
:hardwallet/remove-key-with-unpair
card/remove-key-with-unpair)

(re-frame/reg-fx
:hardwallet/unpair-and-delete
card/unpair-and-delete)
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/init/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
[cofx]
(let [{{:accounts/keys [accounts] :as db} :db} cofx]
(if (empty? accounts)
(navigation/navigate-to-clean cofx :intro nil)
(navigation/navigate-to-cofx cofx :intro nil)
(let [account-with-notification
(when-not platform/desktop?
(notifications/lookup-contact-pubkey-from-hash
Expand Down
23 changes: 12 additions & 11 deletions src/status_im/ui/components/bottom_bar/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,18 @@
(minimize-bar new-view-id))))
:reagent-render
(fn [keyboard-shown? view-id tab]
(if platform/ios?
[tabs-animation-wrapper-ios
[react/animated-view
{:style (tabs.styles/animated-container visible? keyboard-shown?)}
[tabs tab]]]
[tabs-animation-wrapper-android
keyboard-shown?
view-id
[react/animated-view
{:style (tabs.styles/animated-container visible? keyboard-shown?)}
[tabs tab]]]))}))
(when (not= view-id :enter-pin)
(if platform/ios?
[tabs-animation-wrapper-ios
[react/animated-view
{:style (tabs.styles/animated-container visible? keyboard-shown?)}
[tabs tab]]]
[tabs-animation-wrapper-android
keyboard-shown?
view-id
[react/animated-view
{:style (tabs.styles/animated-container visible? keyboard-shown?)}
[tabs tab]]])))}))

(def disappearance-duration 150)
(def appearance-duration 100)
Expand Down
Loading

0 comments on commit 99e0089

Please sign in to comment.