2020-07-10 10:05:36 +00:00
|
|
|
(ns status-im.keycard.recovery
|
2020-04-29 13:11:24 +00:00
|
|
|
(:require [status-im.navigation :as navigation]
|
2020-02-25 08:52:40 +00:00
|
|
|
[status-im.utils.datetime :as utils.datetime]
|
|
|
|
[status-im.multiaccounts.create.core :as multiaccounts.create]
|
2021-05-17 08:10:08 +00:00
|
|
|
[status-im.multiaccounts.model :as multiaccounts.model]
|
2020-02-25 08:52:40 +00:00
|
|
|
[status-im.utils.fx :as fx]
|
|
|
|
[re-frame.core :as re-frame]
|
2021-02-12 14:58:43 +00:00
|
|
|
[status-im.i18n.i18n :as i18n]
|
2020-02-25 08:52:40 +00:00
|
|
|
[taoensso.timbre :as log]
|
2020-07-10 10:05:36 +00:00
|
|
|
[status-im.keycard.common :as common]
|
|
|
|
status-im.keycard.fx
|
2020-02-25 08:52:40 +00:00
|
|
|
[status-im.constants :as constants]
|
|
|
|
[status-im.ethereum.eip55 :as eip55]
|
|
|
|
[status-im.ethereum.core :as ethereum]
|
2021-02-12 14:58:43 +00:00
|
|
|
[status-im.bottom-sheet.core :as bottom-sheet]
|
2020-07-03 12:24:07 +00:00
|
|
|
[status-im.utils.platform :as platform]))
|
2020-02-25 08:52:40 +00:00
|
|
|
|
|
|
|
(fx/defn pair* [_ password]
|
2020-07-10 10:05:36 +00:00
|
|
|
{:keycard/pair {:password password}})
|
2020-02-25 08:52:40 +00:00
|
|
|
|
|
|
|
(fx/defn pair
|
2020-07-10 10:05:36 +00:00
|
|
|
{:events [:keycard/pair]}
|
2020-03-13 12:42:26 +00:00
|
|
|
[cofx]
|
2020-07-10 10:05:36 +00:00
|
|
|
(let [{:keys [password]} (get-in cofx [:db :keycard :secrets])]
|
2020-03-13 12:42:26 +00:00
|
|
|
(common/show-connection-sheet
|
|
|
|
cofx
|
2020-07-10 10:05:36 +00:00
|
|
|
{:on-card-connected :keycard/pair
|
2020-03-13 12:42:26 +00:00
|
|
|
:handler (pair* password)})))
|
2020-02-25 08:52:40 +00:00
|
|
|
|
|
|
|
(fx/defn pair-code-next-button-pressed
|
|
|
|
{:events [:keycard.onboarding.pair.ui/input-submitted
|
2020-07-10 10:05:36 +00:00
|
|
|
:keycard.ui/pair-code-next-button-pressed
|
2020-02-25 08:52:40 +00:00
|
|
|
:keycard.onboarding.pair.ui/next-pressed]}
|
|
|
|
[{:keys [db] :as cofx}]
|
2020-07-10 10:05:36 +00:00
|
|
|
(let [pairing (get-in db [:keycard :secrets :pairing])
|
|
|
|
paired-on (get-in db [:keycard :secrets :paired-on] (utils.datetime/timestamp))]
|
2020-02-25 08:52:40 +00:00
|
|
|
(fx/merge cofx
|
|
|
|
(if pairing
|
|
|
|
{:db (-> db
|
2020-07-10 10:05:36 +00:00
|
|
|
(assoc-in [:keycard :setup-step] :import-multiaccount)
|
|
|
|
(assoc-in [:keycard :secrets :paired-on] paired-on))}
|
2020-02-25 08:52:40 +00:00
|
|
|
(pair)))))
|
|
|
|
|
|
|
|
(fx/defn load-pair-screen
|
|
|
|
[{:keys [db] :as cofx}]
|
2020-07-10 10:05:36 +00:00
|
|
|
(log/debug "[keycard] load-pair-screen")
|
2020-02-25 08:52:40 +00:00
|
|
|
(fx/merge cofx
|
|
|
|
{:db (-> db
|
2020-07-10 10:05:36 +00:00
|
|
|
(assoc-in [:keycard :setup-step] :pair))
|
2020-05-19 10:40:39 +00:00
|
|
|
:dispatch [:bottom-sheet/hide]}
|
2020-02-25 08:52:40 +00:00
|
|
|
(common/listen-to-hardware-back-button)
|
|
|
|
(navigation/navigate-to-cofx :keycard-recovery-pair nil)))
|
|
|
|
|
|
|
|
(fx/defn keycard-storage-selected-for-recovery
|
|
|
|
{:events [:recovery.ui/keycard-storage-selected]}
|
|
|
|
[{:keys [db] :as cofx}]
|
|
|
|
(fx/merge cofx
|
2020-07-10 10:05:36 +00:00
|
|
|
{:db (assoc-in db [:keycard :flow] :recovery)}
|
2020-02-25 08:52:40 +00:00
|
|
|
(navigation/navigate-to-cofx :keycard-recovery-enter-mnemonic nil)))
|
|
|
|
|
|
|
|
(fx/defn start-import-flow
|
2020-02-25 09:22:33 +00:00
|
|
|
{:events [::recover-with-keycard-pressed]}
|
2020-02-25 08:52:40 +00:00
|
|
|
[{:keys [db] :as cofx}]
|
|
|
|
(fx/merge cofx
|
2021-03-04 09:43:53 +00:00
|
|
|
{:db
|
|
|
|
(-> db
|
|
|
|
(assoc-in [:keycard :flow] :import)
|
|
|
|
(assoc :recovered-account? true))
|
2020-07-10 10:05:36 +00:00
|
|
|
:keycard/check-nfc-enabled nil}
|
2020-02-25 08:52:40 +00:00
|
|
|
(bottom-sheet/hide-bottom-sheet)
|
|
|
|
(navigation/navigate-to-cofx :keycard-recovery-intro nil)))
|
|
|
|
|
|
|
|
(fx/defn access-key-pressed
|
|
|
|
{:events [:multiaccounts.recover.ui/recover-multiaccount-button-pressed]}
|
|
|
|
[_]
|
|
|
|
{:dispatch [:bottom-sheet/show-sheet :recover-sheet]})
|
|
|
|
|
|
|
|
(fx/defn recovery-keycard-selected
|
|
|
|
{:events [:recovery.ui/keycard-option-pressed]}
|
|
|
|
[{:keys [db] :as cofx}]
|
|
|
|
(fx/merge cofx
|
2020-07-10 10:05:36 +00:00
|
|
|
{:db (assoc-in db [:keycard :flow] :recovery)
|
|
|
|
:keycard/check-nfc-enabled nil}
|
2020-02-25 08:52:40 +00:00
|
|
|
(navigation/navigate-to-cofx :keycard-onboarding-intro nil)))
|
|
|
|
|
2020-02-25 09:54:11 +00:00
|
|
|
(fx/defn cancel-confirm
|
|
|
|
{:events [::cancel-confirm]}
|
|
|
|
[{:keys [db] :as cofx}]
|
|
|
|
(fx/merge cofx
|
|
|
|
(common/cancel-sheet-confirm)
|
|
|
|
(navigation/navigate-back)))
|
|
|
|
|
|
|
|
(fx/defn cancel-pressed
|
|
|
|
{:events [::cancel-pressed]}
|
|
|
|
[_]
|
|
|
|
{:ui/show-confirmation {:title (i18n/label :t/keycard-cancel-setup-title)
|
|
|
|
:content (i18n/label :t/keycard-cancel-setup-text)
|
|
|
|
:confirm-button-text (i18n/label :t/yes)
|
|
|
|
:cancel-button-text (i18n/label :t/no)
|
|
|
|
:on-accept #(re-frame/dispatch [::cancel-confirm])
|
|
|
|
:on-cancel #()}})
|
|
|
|
|
2020-02-25 08:52:40 +00:00
|
|
|
(fx/defn begin-setup-pressed
|
|
|
|
{:events [:keycard.recovery.intro.ui/begin-recovery-pressed]}
|
|
|
|
[{:keys [db] :as cofx}]
|
2020-03-13 12:42:26 +00:00
|
|
|
(fx/merge
|
|
|
|
cofx
|
|
|
|
{:db (-> db
|
2020-07-10 10:05:36 +00:00
|
|
|
(update :keycard
|
2020-03-13 12:42:26 +00:00
|
|
|
dissoc :secrets :card-state :multiaccount-wallet-address
|
|
|
|
:multiaccount-whisper-public-key
|
|
|
|
:application-info)
|
2020-07-10 10:05:36 +00:00
|
|
|
(assoc-in [:keycard :setup-step] :begin)
|
|
|
|
(assoc-in [:keycard :pin :on-verified] nil))}
|
2020-03-13 12:42:26 +00:00
|
|
|
(common/show-connection-sheet
|
2020-07-10 10:05:36 +00:00
|
|
|
{:on-card-connected :keycard/get-application-info
|
|
|
|
:on-card-read :keycard/check-card-state
|
2020-03-13 12:42:26 +00:00
|
|
|
:sheet-options {:on-cancel [::cancel-pressed]}
|
2021-03-10 12:42:50 +00:00
|
|
|
:handler (common/get-application-info :keycard/check-card-state)})))
|
2020-02-25 08:52:40 +00:00
|
|
|
|
|
|
|
(fx/defn recovery-success-finish-pressed
|
|
|
|
{:events [:keycard.recovery.success/finish-pressed]}
|
|
|
|
[{:keys [db] :as cofx}]
|
|
|
|
(fx/merge cofx
|
2020-07-10 10:05:36 +00:00
|
|
|
{:db (update db :keycard dissoc
|
2020-02-25 08:52:40 +00:00
|
|
|
:multiaccount-wallet-address
|
|
|
|
:multiaccount-whisper-public-key)}
|
2020-07-03 12:24:07 +00:00
|
|
|
(navigation/navigate-to-cofx (if platform/android?
|
|
|
|
:notifications-settings :welcome) nil)))
|
2020-02-25 08:52:40 +00:00
|
|
|
|
|
|
|
(fx/defn recovery-no-key
|
|
|
|
{:events [:keycard.recovery.no-key.ui/generate-key-pressed]}
|
|
|
|
[{:keys [db] :as cofx}]
|
|
|
|
(fx/merge cofx
|
2020-07-10 10:05:36 +00:00
|
|
|
{:db (assoc-in db [:keycard :flow] :create)
|
|
|
|
:keycard/check-nfc-enabled nil}
|
2020-02-25 08:52:40 +00:00
|
|
|
(multiaccounts.create/intro-wizard)))
|
|
|
|
|
|
|
|
(fx/defn create-keycard-multiaccount
|
|
|
|
[{:keys [db] :as cofx}]
|
2020-07-10 10:05:36 +00:00
|
|
|
(let [{{:keys [multiaccount secrets flow]} :keycard} db
|
2020-02-25 08:52:40 +00:00
|
|
|
{:keys [address
|
|
|
|
name
|
2020-11-30 11:11:06 +00:00
|
|
|
identicon
|
2020-02-25 08:52:40 +00:00
|
|
|
public-key
|
|
|
|
whisper-public-key
|
|
|
|
wallet-public-key
|
|
|
|
wallet-root-public-key
|
|
|
|
whisper-address
|
|
|
|
wallet-address
|
|
|
|
wallet-root-address
|
|
|
|
whisper-private-key
|
|
|
|
encryption-public-key
|
|
|
|
instance-uid
|
2020-10-05 10:11:57 +00:00
|
|
|
key-uid
|
2020-11-30 11:11:06 +00:00
|
|
|
recovered]} multiaccount
|
|
|
|
{:keys [pairing paired-on]} secrets
|
|
|
|
{:keys [name identicon]}
|
2020-02-25 08:52:40 +00:00
|
|
|
(if (nil? name)
|
|
|
|
;; name might have been generated during recovery via passphrase
|
|
|
|
(get-in db [:intro-wizard :derived constants/path-whisper-keyword])
|
|
|
|
{:name name
|
2020-11-30 11:11:06 +00:00
|
|
|
:identicon identicon})]
|
2020-02-25 08:52:40 +00:00
|
|
|
;; if a name is still `nil` we have to generate it before multiaccount's
|
|
|
|
;; creation otherwise spec validation will fail
|
|
|
|
(if (nil? name)
|
2020-07-10 10:05:36 +00:00
|
|
|
{:keycard/generate-name-and-photo
|
2020-02-25 18:36:29 +00:00
|
|
|
{:public-key whisper-public-key
|
2020-02-25 09:22:33 +00:00
|
|
|
:on-success ::on-name-and-photo-generated}}
|
2020-02-25 08:52:40 +00:00
|
|
|
(fx/merge cofx
|
|
|
|
{:db (-> db
|
2020-07-10 10:05:36 +00:00
|
|
|
(assoc-in [:keycard :setup-step] nil)
|
2020-02-25 18:36:29 +00:00
|
|
|
(dissoc :intro-wizard))}
|
2020-02-25 08:52:40 +00:00
|
|
|
(multiaccounts.create/on-multiaccount-created
|
2020-10-05 10:11:57 +00:00
|
|
|
{:recovered (or recovered (get-in db [:intro-wizard :recovering?]))
|
|
|
|
:derived {constants/path-wallet-root-keyword
|
2020-02-25 08:52:40 +00:00
|
|
|
{:public-key wallet-root-public-key
|
|
|
|
:address (eip55/address->checksum wallet-root-address)}
|
|
|
|
constants/path-whisper-keyword
|
|
|
|
{:public-key whisper-public-key
|
|
|
|
:address (eip55/address->checksum whisper-address)
|
|
|
|
:name name
|
2020-11-30 11:11:06 +00:00
|
|
|
:identicon identicon}
|
2020-02-25 08:52:40 +00:00
|
|
|
constants/path-default-wallet-keyword
|
|
|
|
{:public-key wallet-public-key
|
|
|
|
:address (eip55/address->checksum wallet-address)}}
|
|
|
|
:address address
|
|
|
|
:public-key public-key
|
|
|
|
:keycard-instance-uid instance-uid
|
|
|
|
:key-uid (ethereum/normalized-hex key-uid)
|
|
|
|
:keycard-pairing pairing
|
|
|
|
:keycard-paired-on paired-on
|
|
|
|
:chat-key whisper-private-key}
|
|
|
|
encryption-public-key
|
|
|
|
{})
|
|
|
|
(if (= flow :import)
|
2020-04-29 13:11:24 +00:00
|
|
|
(navigation/navigate-replace :keycard-recovery-success nil)
|
2020-07-03 12:24:07 +00:00
|
|
|
(navigation/navigate-to-cofx (if platform/android?
|
|
|
|
:notifications-settings :welcome) nil))))))
|
2020-02-25 08:52:40 +00:00
|
|
|
|
2021-05-17 08:10:08 +00:00
|
|
|
(fx/defn return-to-keycard-login
|
|
|
|
[{:keys [db] :as cofx}]
|
|
|
|
(fx/merge cofx
|
|
|
|
{:db (-> db
|
|
|
|
(update-in [:keycard :pin] assoc :enter-step :login
|
|
|
|
:status nil
|
|
|
|
:login [])
|
|
|
|
(update :keycard dissoc :application-info))}
|
|
|
|
(navigation/navigate-reset {:index 0
|
|
|
|
:routes [{:name :intro-stack
|
|
|
|
:state {:routes [{:name :multiaccounts},
|
|
|
|
{:name :keycard-login-pin}]}}]})))
|
|
|
|
|
2021-03-25 13:39:13 +00:00
|
|
|
(fx/defn on-backup-success
|
|
|
|
[{:keys [db] :as cofx}]
|
|
|
|
(fx/merge cofx
|
|
|
|
{:utils/show-popup {:title (i18n/label :t/keycard-backup-success-title)
|
|
|
|
:content (i18n/label :t/keycard-backup-success-body)}}
|
2021-05-17 08:10:08 +00:00
|
|
|
(if (multiaccounts.model/logged-in? cofx)
|
|
|
|
(navigation/navigate-to-cofx :keycard-settings nil)
|
|
|
|
(return-to-keycard-login))))
|
2021-03-25 13:39:13 +00:00
|
|
|
|
2020-02-25 08:52:40 +00:00
|
|
|
(fx/defn on-generate-and-load-key-success
|
2020-07-10 10:05:36 +00:00
|
|
|
{:events [:keycard.callback/on-generate-and-load-key-success]
|
2020-02-25 08:52:40 +00:00
|
|
|
:interceptors [(re-frame/inject-cofx :random-guid-generator)
|
|
|
|
(re-frame/inject-cofx ::multiaccounts.create/get-signing-phrase)]}
|
|
|
|
[{:keys [db random-guid-generator] :as cofx} data]
|
2021-03-25 13:39:13 +00:00
|
|
|
(let [account-data (js->clj data :keywordize-keys true)
|
|
|
|
backup? (get-in db [:keycard :creating-backup?])]
|
2020-02-25 08:52:40 +00:00
|
|
|
(fx/merge cofx
|
|
|
|
{:db (-> db
|
2020-07-10 10:05:36 +00:00
|
|
|
(assoc-in [:keycard :multiaccount]
|
2020-02-25 08:52:40 +00:00
|
|
|
(-> account-data
|
|
|
|
(update :address ethereum/normalized-hex)
|
|
|
|
(update :whisper-address ethereum/normalized-hex)
|
|
|
|
(update :wallet-address ethereum/normalized-hex)
|
|
|
|
(update :wallet-root-address ethereum/normalized-hex)
|
|
|
|
(update :public-key ethereum/normalized-hex)
|
|
|
|
(update :whisper-public-key ethereum/normalized-hex)
|
|
|
|
(update :wallet-public-key ethereum/normalized-hex)
|
|
|
|
(update :wallet-root-public-key ethereum/normalized-hex)
|
2020-07-10 10:05:36 +00:00
|
|
|
(update :instance-uid #(get-in db [:keycard :multiaccount :instance-uid] %))))
|
|
|
|
(assoc-in [:keycard :multiaccount-wallet-address] (:wallet-address account-data))
|
|
|
|
(assoc-in [:keycard :multiaccount-whisper-public-key] (:whisper-public-key account-data))
|
2020-11-25 09:30:00 +00:00
|
|
|
(assoc-in [:keycard :pin :status] nil)
|
2020-07-10 10:05:36 +00:00
|
|
|
(assoc-in [:keycard :application-info :key-uid]
|
2020-04-10 09:06:51 +00:00
|
|
|
(ethereum/normalized-hex (:key-uid account-data)))
|
2020-07-10 10:05:36 +00:00
|
|
|
(update :keycard dissoc :recovery-phrase)
|
2021-03-25 13:39:13 +00:00
|
|
|
(update :keycard dissoc :creating-backup?)
|
2020-07-10 10:05:36 +00:00
|
|
|
(update-in [:keycard :secrets] dissoc :pin :puk :password)
|
2020-02-25 08:52:40 +00:00
|
|
|
(assoc :multiaccounts/new-installation-id (random-guid-generator))
|
2020-07-10 10:05:36 +00:00
|
|
|
(update-in [:keycard :secrets] dissoc :mnemonic))}
|
2020-02-25 08:52:40 +00:00
|
|
|
(common/remove-listener-to-hardware-back-button)
|
2020-03-13 12:42:26 +00:00
|
|
|
(common/hide-connection-sheet)
|
2021-03-25 13:39:13 +00:00
|
|
|
(if backup? (on-backup-success) (create-keycard-multiaccount)))))
|
2020-02-25 08:52:40 +00:00
|
|
|
|
|
|
|
(fx/defn on-generate-and-load-key-error
|
2020-07-10 10:05:36 +00:00
|
|
|
{:events [:keycard.callback/on-generate-and-load-key-error]}
|
2020-02-25 08:52:40 +00:00
|
|
|
[{:keys [db] :as cofx} {:keys [error code]}]
|
2020-07-10 10:05:36 +00:00
|
|
|
(log/debug "[keycard] generate and load key error: " error)
|
2020-02-25 09:54:11 +00:00
|
|
|
(when-not (common/tag-lost? error)
|
|
|
|
(fx/merge cofx
|
2020-07-10 10:05:36 +00:00
|
|
|
{:db (assoc-in db [:keycard :setup-error] error)}
|
|
|
|
(common/set-on-card-connected :keycard/load-loading-keys-screen)
|
2020-02-25 09:54:11 +00:00
|
|
|
(common/process-error code error))))
|
2020-02-25 08:52:40 +00:00
|
|
|
|
|
|
|
(fx/defn import-multiaccount
|
2020-07-10 10:05:36 +00:00
|
|
|
{:events [:keycard/import-multiaccount]}
|
2020-02-25 08:52:40 +00:00
|
|
|
[{:keys [db] :as cofx}]
|
2020-07-10 10:05:36 +00:00
|
|
|
(let [{:keys [pairing]} (get-in db [:keycard :secrets])
|
|
|
|
instance-uid (get-in db [:keycard :application-info :instance-uid])
|
|
|
|
key-uid (get-in db [:keycard :application-info :key-uid])
|
2020-02-25 08:52:40 +00:00
|
|
|
pairing' (or pairing (common/get-pairing db key-uid))
|
2020-07-10 10:05:36 +00:00
|
|
|
pin (common/vector->string (get-in db [:keycard :pin :import-multiaccount]))]
|
2020-02-25 08:52:40 +00:00
|
|
|
(fx/merge cofx
|
|
|
|
{:db (-> db
|
2020-07-10 10:05:36 +00:00
|
|
|
(assoc-in [:keycard :multiaccount :instance-uid] instance-uid)
|
2020-11-25 09:30:00 +00:00
|
|
|
(assoc-in [:keycard :pin :status] :verifying)
|
2020-07-10 10:05:36 +00:00
|
|
|
(assoc-in [:keycard :secrets] {:pairing pairing'
|
|
|
|
:paired-on (utils.datetime/timestamp)}))
|
2021-03-10 12:42:50 +00:00
|
|
|
:keycard/import-keys {:pin pin
|
2021-02-19 10:01:16 +00:00
|
|
|
:on-success :keycard.callback/on-generate-and-load-key-success}})))
|
2020-02-25 08:52:40 +00:00
|
|
|
|
|
|
|
(fx/defn load-recovering-key-screen
|
2020-07-10 10:05:36 +00:00
|
|
|
{:events [:keycard/load-recovering-key-screen]}
|
2020-03-13 12:42:26 +00:00
|
|
|
[cofx]
|
|
|
|
(common/show-connection-sheet
|
|
|
|
cofx
|
2020-07-10 10:05:36 +00:00
|
|
|
{:on-card-connected :keycard/load-recovering-key-screen
|
|
|
|
:handler (common/dispatch-event :keycard/import-multiaccount)}))
|
2020-02-25 08:52:40 +00:00
|
|
|
|
|
|
|
(fx/defn on-name-and-photo-generated
|
2020-02-25 09:22:33 +00:00
|
|
|
{:events [::on-name-and-photo-generated]
|
2020-02-25 08:52:40 +00:00
|
|
|
:interceptors [(re-frame/inject-cofx :random-guid-generator)
|
|
|
|
(re-frame/inject-cofx ::multiaccounts.create/get-signing-phrase)]}
|
2020-11-30 11:11:06 +00:00
|
|
|
[{:keys [db] :as cofx} whisper-name identicon]
|
2020-02-25 08:52:40 +00:00
|
|
|
(fx/merge
|
|
|
|
cofx
|
2020-07-10 10:05:36 +00:00
|
|
|
{:db (update-in db [:keycard :multiaccount]
|
2020-02-25 08:52:40 +00:00
|
|
|
(fn [multiacc]
|
|
|
|
(assoc multiacc
|
2021-03-04 09:43:53 +00:00
|
|
|
:recovered (get db :recovered-account?)
|
2020-02-25 08:52:40 +00:00
|
|
|
:name whisper-name
|
2020-11-30 11:11:06 +00:00
|
|
|
:identicon identicon)))}
|
2020-02-25 08:52:40 +00:00
|
|
|
(create-keycard-multiaccount)))
|