From 96ac6eb83f85724144b491c9e3d0de199d87f3f7 Mon Sep 17 00:00:00 2001 From: Eric Dvorsak Date: Tue, 7 Aug 2018 03:02:38 +0200 Subject: [PATCH] user can add contact by ens username --- src/status_im/translations/en.cljs | 4 +-- .../ui/screens/add_new/new_chat/db.cljs | 2 +- .../ui/screens/add_new/new_chat/events.cljs | 30 +++++++++++++++++++ .../ui/screens/add_new/new_chat/subs.cljs | 12 +++----- .../ui/screens/add_new/new_chat/views.cljs | 4 +-- src/status_im/ui/screens/contacts/db.cljs | 2 +- src/status_im/ui/screens/db.cljs | 2 +- src/status_im/ui/screens/events.cljs | 1 + src/status_im/utils/ethereum/ens.cljs | 2 +- src/status_im/utils/ethereum/stateofus.cljs | 6 ++-- 10 files changed, 46 insertions(+), 19 deletions(-) create mode 100644 src/status_im/ui/screens/add_new/new_chat/events.cljs diff --git a/src/status_im/translations/en.cljs b/src/status_im/translations/en.cljs index 3e2c4caf66..2bc9ac0c26 100644 --- a/src/status_im/translations/en.cljs +++ b/src/status_im/translations/en.cljs @@ -295,7 +295,7 @@ :copy-qr "Copy code" :qr-code-public-key-hint "Share this code to \nstart chatting" :enter-address "Enter address" - :enter-contact-code "Enter contact code" + :enter-contact-code "Enter contact code or username" :more "more" ;;group-settings @@ -362,7 +362,7 @@ :specify-name "Specify a name" :address-explication "Your public key is used to generate your address on Ethereum and is a series of numbers and letters. You can find it easily in your profile" :unable-to-read-this-code "Unable to read this code" - :use-valid-contact-code "Please scan a valid contact code" + :use-valid-contact-code "Please enter or scan a valid contact code or username" :enter-valid-public-key "Please enter a valid public key or scan a QR code" :contact-already-added "The contact has already been added" :can-not-add-yourself "You can't add yourself" diff --git a/src/status_im/ui/screens/add_new/new_chat/db.cljs b/src/status_im/ui/screens/add_new/new_chat/db.cljs index 9fcfe2341e..a10abed8fc 100644 --- a/src/status_im/ui/screens/add_new/new_chat/db.cljs +++ b/src/status_im/ui/screens/add_new/new_chat/db.cljs @@ -4,7 +4,7 @@ [cljs.spec.alpha :as spec] [clojure.string :as string])) -(defn- validate-pub-key [whisper-identity {:keys [address public-key]}] +(defn validate-pub-key [whisper-identity {:keys [address public-key]}] (cond (string/blank? whisper-identity) (i18n/label :t/use-valid-contact-code) 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 new file mode 100644 index 0000000000..20be445076 --- /dev/null +++ b/src/status_im/ui/screens/add_new/new_chat/events.cljs @@ -0,0 +1,30 @@ +(ns status-im.ui.screens.add-new.new-chat.events + (:require [re-frame.core :as re-frame] + [status-im.utils.handlers :as handlers] + [status-im.utils.handlers-macro :as handlers-macro] + [status-im.ui.screens.add-new.new-chat.db :as db] + [status-im.utils.ethereum.stateofus :as stateofus] + [status-im.utils.ethereum.ens :as ens] + [status-im.utils.ethereum.core :as ethereum])) + +(re-frame/reg-fx + :resolve-whisper-identity + (fn [{:keys [web3 registry ens-name cb]}] + (println registry ens-name) + (stateofus/text web3 registry ens-name cb))) + +(handlers/register-handler-fx + :new-chat/set-new-identity + (fn [{{:keys [web3 network network-status] :as db} :db} [_ new-identity]] + (let [new-identity-error (db/validate-pub-key new-identity (:account/account db))] + (if (stateofus/is-stateofus-name? new-identity) + (let [network (get-in db [:account/account :networks network]) + chain (ethereum/network->chain-keyword network)] + {:resolve-whisper-identity {:web3 web3 + :registry (get ens/ens-registries + chain) + :ens-name new-identity + :cb #(re-frame/dispatch [:new-chat/set-new-identity %])}}) + {:db (assoc db + :contacts/new-identity new-identity + :contacts/new-identity-error new-identity-error)})))) diff --git a/src/status_im/ui/screens/add_new/new_chat/subs.cljs b/src/status_im/ui/screens/add_new/new_chat/subs.cljs index e153627650..4681717690 100644 --- a/src/status_im/ui/screens/add_new/new_chat/subs.cljs +++ b/src/status_im/ui/screens/add_new/new_chat/subs.cljs @@ -1,10 +1,6 @@ (ns status-im.ui.screens.add-new.new-chat.subs - (:require [re-frame.core :as re-frame] - [status-im.ui.screens.add-new.new-chat.db :as db])) + (:require [re-frame.core :as re-frame])) -(re-frame/reg-sub - :new-contact-error-message - :<- [:get :contacts/new-identity] - :<- [:get-current-account] - (fn [[new-identity account]] - (db/validate-pub-key new-identity account))) +(re-frame/reg-sub :new-identity-error + (fn [db _] + (get db :contacts/new-identity-error nil))) diff --git a/src/status_im/ui/screens/add_new/new_chat/views.cljs b/src/status_im/ui/screens/add_new/new_chat/views.cljs index 57740a1656..f43ee7efd4 100644 --- a/src/status_im/ui/screens/add_new/new_chat/views.cljs +++ b/src/status_im/ui/screens/add_new/new_chat/views.cljs @@ -20,13 +20,13 @@ (views/defview new-chat [] (views/letsubs [contacts [:all-added-people-contacts] - error-message [:new-contact-error-message]] + error-message [:new-identity-error]] [react/keyboard-avoiding-view open-dapp.styles/main-container [status-bar/status-bar] [toolbar.view/simple-toolbar (i18n/label :t/new-chat)] [react/view add-new.styles/new-chat-container [react/view add-new.styles/new-chat-input-container - [react/text-input {:on-change-text #(re-frame/dispatch [:set :contacts/new-identity %]) + [react/text-input {:on-change-text #(re-frame/dispatch [:new-chat/set-new-identity %]) :on-submit-editing #(when-not error-message (re-frame/dispatch [:add-contact-handler])) :placeholder (i18n/label :t/enter-contact-code) diff --git a/src/status_im/ui/screens/contacts/db.cljs b/src/status_im/ui/screens/contacts/db.cljs index 870c8ad027..2ad9305ccd 100644 --- a/src/status_im/ui/screens/contacts/db.cljs +++ b/src/status_im/ui/screens/contacts/db.cljs @@ -70,7 +70,7 @@ (spec/def :contacts/contacts (spec/nilable (spec/map-of :global/not-empty-string :contact/contact))) ;public key of new contact during adding this new contact (spec/def :contacts/new-identity (spec/nilable string?)) -(spec/def :contacts/new-public-key-error (spec/nilable string?)) +(spec/def :contacts/new-identity-error (spec/nilable string?)) ;on showing this contact's profile (andrey: better to move into profile ns) (spec/def :contacts/identity (spec/nilable :global/not-empty-string)) (spec/def :contacts/list-ui-props (spec/nilable (allowed-keys :opt-un [:contact-list-ui/edit?]))) diff --git a/src/status_im/ui/screens/db.cljs b/src/status_im/ui/screens/db.cljs index a533b97286..d8689fc4f4 100644 --- a/src/status_im/ui/screens/db.cljs +++ b/src/status_im/ui/screens/db.cljs @@ -174,7 +174,7 @@ [:contacts/contacts :contacts/dapps :contacts/new-identity - :contacts/new-public-key-error + :contacts/new-identity-error :contacts/identity :contacts/ui-props :contacts/list-ui-props diff --git a/src/status_im/ui/screens/events.cljs b/src/status_im/ui/screens/events.cljs index 69483030d7..d041822840 100644 --- a/src/status_im/ui/screens/events.cljs +++ b/src/status_im/ui/screens/events.cljs @@ -7,6 +7,7 @@ status-im.ui.screens.accounts.login.events status-im.ui.screens.accounts.recover.events [status-im.ui.screens.contacts.events :as contacts] + status-im.ui.screens.add-new.new-chat.events status-im.ui.screens.group.chat-settings.events status-im.ui.screens.group.events [status-im.ui.screens.navigation :as navigation] diff --git a/src/status_im/utils/ethereum/ens.cljs b/src/status_im/utils/ethereum/ens.cljs index af8289754a..4f45e96929 100644 --- a/src/status_im/utils/ethereum/ens.cljs +++ b/src/status_im/utils/ethereum/ens.cljs @@ -11,7 +11,7 @@ ;; this is the addresses of ens registries for the different networks (def ens-registries {:mainnet "0x314159265dd8dbb310642f98f50c066173c1259b" - :ropsten "0x112234455c3a32fd11230c42e7bccd4a84e02010" + :testnet "0x112234455c3a32fd11230c42e7bccd4a84e02010" :rinkeby "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"}) (def default-namehash "0000000000000000000000000000000000000000000000000000000000000000") diff --git a/src/status_im/utils/ethereum/stateofus.cljs b/src/status_im/utils/ethereum/stateofus.cljs index 9276f3d16b..90d608ead5 100644 --- a/src/status_im/utils/ethereum/stateofus.cljs +++ b/src/status_im/utils/ethereum/stateofus.cljs @@ -28,7 +28,7 @@ :data (str "0x59d1d43c" (subs (ens/namehash ens-name) 2) "0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000d7374617475734163636f756e7400000000000000000000000000000000000000")} - (fn [_ text] (cb (ethereum/hex->string (subs text 130 214))))))) + (fn [_ text] (cb (ethereum/hex->string (subs text 130 394))))))) -#_(addr (:web3 @re-frame.db/app-db) "0x112234455c3a32fd11230c42e7bccd4a84e02010" "test.stateofus.eth" println) -#_(text (:web3 @re-frame.db/app-db) "0x112234455c3a32fd11230c42e7bccd4a84e02010" "test.stateofus.eth" println) +#_(addr (:web3 @re-frame.db/app-db) "0x112234455c3a32fd11230c42e7bccd4a84e02010" "erictest.stateofus.eth" println) +#_(text (:web3 @re-frame.db/app-db) "0x112234455c3a32fd11230c42e7bccd4a84e02010" "erictest.stateofus.eth" println)