From 8e288aea56e1b2c21d79bbfdc2ed92ab01aa51b6 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Wed, 23 Dec 2020 18:08:14 +0200 Subject: [PATCH] [#11379] Add a watch only account from an ENS name --- src/status_im/ethereum/resolver.cljs | 11 ++++++++- src/status_im/router/core.cljs | 12 ++-------- .../ui/screens/add_new/new_chat/events.cljs | 12 ++-------- .../ui/screens/wallet/add_new/views.cljs | 2 +- src/status_im/wallet/accounts/core.cljs | 24 ++++++++++++++++++- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/status_im/ethereum/resolver.cljs b/src/status_im/ethereum/resolver.cljs index c00c75637f..ff6f4ceab9 100644 --- a/src/status_im/ethereum/resolver.cljs +++ b/src/status_im/ethereum/resolver.cljs @@ -1,6 +1,8 @@ (ns status-im.ethereum.resolver (:refer-clojure :exclude [name]) - (:require [status-im.ethereum.ens :as ens])) + (:require [status-im.ethereum.ens :as ens] + [status-im.ethereum.stateofus :as stateofus] + [clojure.string :as string])) (def default-hash "0x0000000000000000000000000000000000000000000000000000000000000000") (defn contenthash [registry ens-name cb] @@ -24,3 +26,10 @@ (ens/resolver registry ens-name #(ens/pubkey % ens-name cb))) + +(defn ens-name-parse [contact-identity] + (when (string? contact-identity) + (string/lower-case + (if (ens/is-valid-eth-name? contact-identity) + contact-identity + (stateofus/subdomain contact-identity))))) diff --git a/src/status_im/router/core.cljs b/src/status_im/router/core.cljs index c942beab3b..717f2b8ab5 100644 --- a/src/status_im/router/core.cljs +++ b/src/status_im/router/core.cljs @@ -8,7 +8,6 @@ [status-im.ethereum.eip681 :as eip681] [status-im.ethereum.ens :as ens] [status-im.ethereum.resolver :as resolver] - [status-im.ethereum.stateofus :as stateofus] [cljs.spec.alpha :as spec] [status-im.ethereum.core :as ethereum] [status-im.utils.db :as utils.db] @@ -58,17 +57,10 @@ (defn match-uri [uri] (assoc (bidi/match-route routes uri) :uri uri :query-params (parse-query-params uri))) -(defn- ens-name-parse [contact-identity] - (when (string? contact-identity) - (string/lower-case - (if (ens/is-valid-eth-name? contact-identity) - contact-identity - (stateofus/subdomain contact-identity))))) - (defn resolve-public-key [{:keys [chain contact-identity cb]}] (let [registry (get ens/ens-registries chain) - ens-name (ens-name-parse contact-identity)] + ens-name (resolver/ens-name-parse contact-identity)] (resolver/pubkey registry ens-name cb))) (defn match-contact-async @@ -84,7 +76,7 @@ (and (not public-key?) (string? user-id)) (let [registry (get ens/ens-registries chain) - ens-name (ens-name-parse user-id) + ens-name (resolver/ens-name-parse user-id) on-success #(match-contact-async chain {:user-id %} callback)] (resolver/pubkey registry ens-name on-success)) diff --git a/src/status_im/ui/screens/add_new/new_chat/events.cljs b/src/status_im/ui/screens/add_new/new_chat/events.cljs index 059b9a5ff9..339d7cf546 100644 --- a/src/status_im/ui/screens/add_new/new_chat/events.cljs +++ b/src/status_im/ui/screens/add_new/new_chat/events.cljs @@ -6,7 +6,6 @@ [status-im.ethereum.resolver :as resolver] [status-im.ui.screens.add-new.new-chat.db :as db] [status-im.utils.handlers :as handlers] - [status-im.ethereum.stateofus :as stateofus] [status-im.utils.random :as random] [status-im.utils.utils :as utils] [status-im.utils.fx :as fx] @@ -16,18 +15,11 @@ [status-im.router.core :as router] [status-im.navigation :as navigation])) -(defn- ens-name-parse [contact-identity] - (when (string? contact-identity) - (string/lower-case - (if (ens/is-valid-eth-name? contact-identity) - contact-identity - (stateofus/subdomain contact-identity))))) - (re-frame/reg-fx :resolve-public-key (fn [{:keys [chain contact-identity cb]}] (let [registry (get ens/ens-registries chain) - ens-name (ens-name-parse contact-identity)] + ens-name (resolver/ens-name-parse contact-identity)] (resolver/pubkey registry ens-name cb)))) ;;NOTE we want to handle only last resolve @@ -56,7 +48,7 @@ :else :valid) :error error - :ens-name (ens-name-parse new-ens-name)})} + :ens-name (resolver/ens-name-parse new-ens-name)})} (when is-ens? (reset! resolve-last-id (random/id)) (let [chain (ethereum/chain-keyword db)] 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 f301ca956e..ce89670755 100644 --- a/src/status_im/ui/screens/wallet/add_new/views.cljs +++ b/src/status_im/ui/screens/wallet/add_new/views.cljs @@ -67,7 +67,7 @@ :monospace true :placeholder (i18n/label :t/enter-address) :accessibility-label :add-account-enter-watch-address - :on-change-text #(re-frame/dispatch [:set-in [:add-account :address] %])}] + :on-change-text #(re-frame/dispatch [:wallet.accounts/set-account-to-watch %])}] [quo/text-input {:label (i18n/label :t/password) :show-cancel false diff --git a/src/status_im/wallet/accounts/core.cljs b/src/status_im/wallet/accounts/core.cljs index 4c34529bfa..82b3c9b448 100644 --- a/src/status_im/wallet/accounts/core.cljs +++ b/src/status_im/wallet/accounts/core.cljs @@ -19,7 +19,11 @@ [status-im.multiaccounts.recover.core :as recover] [status-im.ethereum.mnemonic :as mnemonic] [taoensso.timbre :as log] - [status-im.wallet.prices :as prices])) + [status-im.wallet.prices :as prices] + [status-im.utils.hex :as hex] + [status-im.ethereum.ens :as ens] + [status-im.ens.core :as ens.core] + [status-im.ethereum.resolver :as resolver])) (fx/defn start-adding-new-account {:events [:wallet.accounts/start-adding-new-account]} @@ -231,6 +235,24 @@ ::verify-password {:address (get-in db [:multiaccount :wallet-root-address]) :hashed-password hashed-password}}) +(fx/defn set-account-to-watch + {:events [:wallet.accounts/set-account-to-watch]} + [{:keys [db] :as cofx} account] + (let [name? (and (>= (count account) 3) + (not (hex/valid-hex? account))) + chain (ethereum/chain-keyword db)] + (log/debug "[wallet] set-account-to-watch" account + "name?" name?) + (cond-> {:db (assoc-in db [:add-account :address] account)} + name? + (assoc ::ens.core/resolve-address + (let [registry (get ens/ens-registries chain) + ens-name (resolver/ens-name-parse account)] + [registry + ens-name + #(re-frame/dispatch + [:wallet.accounts/set-account-to-watch %])]))))) + (fx/defn add-new-account {:events [:wallet.accounts/add-new-account]} [{:keys [db] :as cofx} hashed-password]