Skip to content

Commit

Permalink
feat: new keypair: mnemonic, address, KP name screen (#18790)
Browse files Browse the repository at this point in the history
feat: new keypair: mnemonic, address, KP name screen (#18790)
  • Loading branch information
OmarBasem authored Feb 21, 2024
1 parent cfaed80 commit 28f43ac
Show file tree
Hide file tree
Showing 15 changed files with 337 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,14 @@ public void deleteMultiaccount(final String keyUID, final Callback callback) thr
final String keyStoreDir = this.utils.getKeyStorePath(keyUID);
this.utils.executeRunnableStatusGoMethod(() -> Statusgo.deleteMultiaccount(keyUID, keyStoreDir), callback);
}

@ReactMethod
public void getRandomMnemonic(final Callback callback) throws JSONException {
this.utils.executeRunnableStatusGoMethod(() -> Statusgo.getRandomMnemonic(), callback);
}

@ReactMethod
public void createAccountFromMnemonicAndDeriveAccountsForPaths(final String mnemonic, final Callback callback) throws JSONException {
this.utils.executeRunnableStatusGoMethod(() -> Statusgo.createAccountFromMnemonicAndDeriveAccountsForPaths(mnemonic), callback);
}
}
16 changes: 16 additions & 0 deletions modules/react-native-status/ios/RCTStatus/AccountManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,20 @@ -(NSString *) prepareDirAndUpdateConfig:(NSString *)config
NSLog(@"%@", result);
}

RCT_EXPORT_METHOD(getRandomMnemonic:(RCTResponseSenderBlock)callback) {
#if DEBUG
NSLog(@"GetRandomMnemonic() method called");
#endif
NSString *result = StatusgoGetRandomMnemonic();
callback(@[result]);
}

RCT_EXPORT_METHOD(createAccountFromMnemonicAndDeriveAccountsForPaths:(NSString *)mnemonic callback:(RCTResponseSenderBlock)callback) {
#if DEBUG
NSLog(@"createAccountFromMnemonicAndDeriveAccountsForPaths() method called");
#endif
NSString *result = StatusgoCreateAccountFromMnemonicAndDeriveAccountsForPaths(mnemonic);
callback(@[result]);
}

@end
10 changes: 10 additions & 0 deletions src/native_module/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,13 @@
(defn init-status-go-logging
[{:keys [enable? mobile-system? log-level callback]}]
(.initLogging ^js (log-manager) enable? mobile-system? log-level callback))

(defn get-random-mnemonic
[callback]
(.getRandomMnemonic ^js (account-manager) #(callback (types/json->clj %))))

(defn create-account-from-mnemonic
[mnemonic callback]
(.createAccountFromMnemonicAndDeriveAccountsForPaths ^js (account-manager)
(types/clj->json mnemonic)
#(callback (types/json->clj %))))
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
(ns status-im.contexts.wallet.create-account.new-keypair.backup-recovery-phrase.view
(:require
[clojure.string :as string]
[native-module.core :as native-module]
[quo.core :as quo]
[quo.theme :as quo.theme]
[react-native.blur :as blur]
[react-native.core :as rn]
[reagent.core :as reagent]
[status-im.contexts.wallet.common.temp :as temp]
[status-im.contexts.wallet.create-account.new-keypair.backup-recovery-phrase.style :as style]
[utils.i18n :as i18n]
[utils.re-frame :as rf]))
Expand All @@ -17,7 +18,7 @@
[quo/text {:style {:margin-left 4}} item]])

(defn- words-column
[words first-half?]
[{:keys [words first-half?]}]
[rn/flat-list
{:style {:padding-vertical 8}
:data (if first-half? (subvec words 0 6) (subvec words 6))
Expand All @@ -34,18 +35,24 @@
:on-change #(swap! checked? assoc (keyword (str index)) %)}]
[quo/text {:style {:margin-left 12}} (i18n/label item)]])

(defn- view-internal
(defn- f-view
[{:keys [theme]}]
(let [step-labels [:t/backup-step-1 :t/backup-step-2 :t/backup-step-3
:t/backup-step-4]
checked? (reagent/atom
{:0 false
:1 false
:2 false
:3 false})
revealed? (reagent/atom false)
{:keys [customization-color]} (rf/sub [:profile/profile])]
(let [step-labels [:t/backup-step-1 :t/backup-step-2 :t/backup-step-3
:t/backup-step-4]
checked? (reagent/atom
{:0 false
:1 false
:2 false
:3 false})
revealed? (reagent/atom false)
customization-color (rf/sub [:profile/customization-color])
secret-phrase (reagent/atom [])
random-phrase (reagent/atom [])]
(fn []
(rn/use-effect
(fn []
(native-module/get-random-mnemonic #(reset! secret-phrase (string/split % #"\s")))
(native-module/get-random-mnemonic #(reset! random-phrase (string/split % #"\s")))))
[rn/view {:style {:flex 1}}
[quo/page-nav
{:icon-name :i/close
Expand All @@ -56,9 +63,15 @@
:description :text
:description-text (i18n/label :t/backup-recovery-phrase-description)}]
[rn/view {:style (style/seed-phrase-container theme)}
[words-column temp/secret-phrase true]
[rn/view {:style (style/separator theme)}]
[words-column temp/secret-phrase false]
(when (pos? (count @secret-phrase))
[:<>
[words-column
{:words @secret-phrase
:first-half? true}]
[rn/view {:style (style/separator theme)}]
[words-column
{:words @secret-phrase
:first-half? false}]])
(when-not @revealed?
[rn/view {:style style/blur-container}
[blur/view (style/blur theme)]])]
Expand All @@ -82,8 +95,9 @@
:button-one-label (i18n/label :t/i-have-written)
:button-one-props {:disabled? (some false? (vals @checked?))
:customization-color customization-color
:on-press #(rf/dispatch [:navigate-to
:wallet-check-your-backup])}}]
:on-press #(rf/dispatch [:wallet/store-secret-phrase
{:secret-phrase @secret-phrase
:random-phrase @random-phrase}])}}]
[quo/text
{:size :paragraph-2
:style (style/description-text theme)}
Expand All @@ -96,4 +110,8 @@
:on-press #(reset! revealed? true)}
:container-style style/slide-button}])])))

(defn view-internal
[params]
[:f> f-view params])

(def view (quo.theme/with-theme view-internal))
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[quo.theme :as quo.theme]
[react-native.core :as rn]
[reagent.core :as reagent]
[status-im.contexts.wallet.common.temp :as temp]
[status-im.contexts.wallet.create-account.new-keypair.check-your-backup.style :as style]
[utils.i18n :as i18n]
[utils.re-frame :as rf]))
Expand All @@ -31,7 +30,7 @@

(defn- cheat-warning
[]
(let [{:keys [customization-color]} (rf/sub [:profile/profile])]
(let [customization-color (rf/sub [:profile/customization-color])]
[:<>
[quo/drawer-top {:title (i18n/label :t/do-not-cheat)}]
[quo/text
Expand Down Expand Up @@ -62,26 +61,32 @@

(defn- view-internal
[]
(let [random-indices (random-selection)
quiz-index (reagent/atom 0)
incorrect-count (reagent/atom 0)
show-error? (reagent/atom false)]
(let [random-indices (random-selection)
quiz-index (reagent/atom 0)
incorrect-count (reagent/atom 0)
show-error? (reagent/atom false)
{:keys [secret-phrase random-phrase]} (rf/sub [:wallet/create-account])]
(fn []
(let [current-word-index (get random-indices (min @quiz-index (dec questions-count)))
current-word (get temp/secret-phrase current-word-index)
[options-r-0 options-r-1] (random-words-with-string temp/random-words current-word)
on-button-press (fn [word]
(if (= word current-word)
(do
(reset! quiz-index (inc @quiz-index))
(reset! incorrect-count 0)
(reset! show-error? false))
(do
(when (> @incorrect-count 0)
(rf/dispatch [:show-bottom-sheet
{:content cheat-warning}]))
(reset! incorrect-count (inc @incorrect-count))
(reset! show-error? true))))]
(let [current-word-index (get random-indices
(min @quiz-index (dec questions-count)))
current-word (get secret-phrase current-word-index)
[options-row-0 options-row-1] (random-words-with-string random-phrase current-word)
on-button-press (fn [word]
(if (= word current-word)
(do
(when (< @quiz-index questions-count)
(reset! quiz-index (inc @quiz-index)))
(reset! incorrect-count 0)
(reset! show-error? false)
(when (= @quiz-index questions-count)
(rf/dispatch [:navigate-to
:wallet-keypair-name])))
(do
(when (> @incorrect-count 0)
(rf/dispatch [:show-bottom-sheet
{:content cheat-warning}]))
(reset! incorrect-count (inc @incorrect-count))
(reset! show-error? true))))]
[rn/view {:style {:flex 1}}
[quo/page-nav
{:icon-name :i/arrow-left
Expand Down Expand Up @@ -109,7 +114,7 @@

:else
:disabled)
:word (get temp/secret-phrase num)
:word (get secret-phrase num)
:number (inc num)
:on-press #(when (= @quiz-index index)
(reset! show-error? false))}])
Expand All @@ -119,9 +124,9 @@
[buttons-row
{:on-press on-button-press
:margin-bottom 12
:options options-r-0}]
:options options-row-0}]
[buttons-row
{:on-press on-button-press
:options options-r-1}]]]))))
:options options-row-1}]]]))))

(def view (quo.theme/with-theme view-internal))
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns status-im.contexts.wallet.create-account.new-keypair.keypair-name.style)

(def header-container
{:margin-horizontal 20
:margin-vertical 12})

(def bottom-action
{:position :absolute
:bottom 12
:left 0
:right 0})
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
(ns status-im.contexts.wallet.create-account.new-keypair.keypair-name.view
(:require
[quo.core :as quo]
[react-native.core :as rn]
[reagent.core :as reagent]
[status-im.contexts.wallet.create-account.new-keypair.keypair-name.style :as style]
[utils.i18n :as i18n]
[utils.re-frame :as rf]))

(def keypair-name-max-length 15)

(defn view
[]
(let [keypair-name (reagent/atom "")]
(fn []
(let [customization-color (rf/sub [:profile/customization-color])]
[rn/view {:style {:flex 1}}
[quo/page-nav
{:icon-name :i/arrow-left
:on-press #(rf/dispatch [:navigate-back])
:accessibility-label :top-bar}]
[quo/text-combinations
{:container-style style/header-container
:title (i18n/label :t/keypair-name)
:description (i18n/label :t/keypair-name-description)}]
[quo/input
{:container-style {:margin-horizontal 20}
:placeholder (i18n/label :t/keypair-name-input-placeholder)
:label (i18n/label :t/keypair-name)
:char-limit keypair-name-max-length
:on-change-text #(reset! keypair-name %)}]
[quo/bottom-actions
{:actions :one-action
:button-one-label (i18n/label :t/continue)
:button-one-props {:disabled? (or (zero? (count @keypair-name))
(> (count @keypair-name) keypair-name-max-length))
:customization-color customization-color
:on-press #(rf/dispatch [:wallet/new-keypair-continue
{:keypair-name @keypair-name}])}
:container-style style/bottom-action}]]))))
Loading

0 comments on commit 28f43ac

Please sign in to comment.