diff --git a/src/status_im/chat/commands/core.cljs b/src/status_im/chat/commands/core.cljs index 7f9417e480..fb92bfc2a4 100644 --- a/src/status_im/chat/commands/core.cljs +++ b/src/status_im/chat/commands/core.cljs @@ -35,23 +35,22 @@ [type] (keyword (str (protocol/id type) "-button"))) -(defn- contact->address [all-contacts pub-key] - (get-in all-contacts [pub-key :address])) +(defn- contact->address [contact] + (str "0x" (db/public-key->address contact))) (defn add-chat-contacts "Enrich command-message by adding contact list of the current private or group chat" - [all-contacts contacts {:keys [public? group-chat] :as command-message}] + [contacts {:keys [public? group-chat] :as command-message}] (cond public? command-message - group-chat (assoc command-message :contacts (map #(contact->address all-contacts %) contacts)) - :else (assoc command-message :contact (contact->address all-contacts (first contacts))))) + group-chat (assoc command-message :contacts (map contact->address contacts)) + :else (assoc command-message :contact (contact->address (first contacts))))) (defn enrich-command-message-for-events "adds new pairs to command-message to be consumed by extension events" [db {:keys [chat-id] :as command-message}] - (let [{:keys [contacts public? group-chat]} (get-in db [:chats chat-id]) - all-contacts (get db :contacts/contacts)] - (add-chat-contacts all-contacts contacts (assoc command-message :public? public? :group-chat group-chat)))) + (let [{:keys [contacts public? group-chat]} (get-in db [:chats chat-id])] + (add-chat-contacts contacts (assoc command-message :public? public? :group-chat group-chat)))) (defn generate-short-preview "Returns short preview for command" diff --git a/src/status_im/contact/core.cljs b/src/status_im/contact/core.cljs index a0992fe23d..beeb3dbd76 100644 --- a/src/status_im/contact/core.cljs +++ b/src/status_im/contact/core.cljs @@ -28,8 +28,10 @@ (defn build-contact [{{:keys [chats] :account/keys [account] :contacts/keys [contacts]} :db} public-key] - (cond-> (or (get contacts public-key) - (contact.db/public-key->new-contact public-key)) + (cond-> (assoc (or (get contacts public-key) + (contact.db/public-key->new-contact public-key)) + :address (contact.db/public-key->address public-key)) + (= public-key (:public-key account)) (assoc :name (:name account)))) (defn- own-info [db] @@ -124,7 +126,9 @@ {:public-key public-key :photo-path profile-image :name name - :address (or address (:address contact)) + :address (or address + (:address contact) + (contact.db/public-key->address public-key)) :last-updated timestamp-ms ;;NOTE (yenda) in case of concurrent contact request :pending? (get contact :pending? true)} diff --git a/src/status_im/contact/db.cljs b/src/status_im/contact/db.cljs index 01a74989d7..fe2373e53e 100644 --- a/src/status_im/contact/db.cljs +++ b/src/status_im/contact/db.cljs @@ -87,6 +87,16 @@ :photo-path (identicon/identicon public-key) :public-key public-key}) +(defn public-key->address [public-key] + (let [length (count public-key) + normalized-key (case length + 132 (subs public-key 4) + 130 (subs public-key 2) + 128 public-key + nil)] + (when normalized-key + (subs (.sha3 js-dependencies/Web3.prototype normalized-key #js {:encoding "hex"}) 26)))) + (defn- contact-by-address [[_ contact] address] (when (ethereum/address= (:address contact) address) contact)) diff --git a/src/status_im/contact/subs.cljs b/src/status_im/contact/subs.cljs index f8d48be25b..81cd957e14 100644 --- a/src/status_im/contact/subs.cljs +++ b/src/status_im/contact/subs.cljs @@ -1,6 +1,5 @@ (ns status-im.contact.subs (:require [re-frame.core :as re-frame] - [clojure.string :as str] [status-im.utils.identicon :as identicon] [status-im.contact.db :as contact.db])) @@ -48,12 +47,6 @@ (fn [contacts] (remove :dapp? contacts))) -(re-frame/reg-sub - :contacts/all-added-people-contacts-with-address - :<- [:contacts/all-added-people-contacts] - (fn [contacts] - (remove #(-> % :address (str/blank?)) contacts))) - (re-frame/reg-sub :contacts/all-dapps :<- [::dapps] diff --git a/src/status_im/ui/screens/chat/message/message.cljs b/src/status_im/ui/screens/chat/message/message.cljs index 7db28a5e5a..1c98bf5eb9 100644 --- a/src/status_im/ui/screens/chat/message/message.cljs +++ b/src/status_im/ui/screens/chat/message/message.cljs @@ -33,7 +33,6 @@ (defview message-content-command [command-message] (letsubs [id->command [:chats/id->command] - all-contacts [:contacts/contacts] {:keys [contacts]} [:chats/current-chat]] (let [{:keys [type] :as command} (commands-receiving/lookup-command-by-ref command-message id->command) extension-id (get-in command-message [:content :params :extension-id])] @@ -45,7 +44,7 @@ ;; or installed extension has differen extension id [install-extension-message extension-id (:outgoing command-message)] (if command - (commands/generate-preview command (commands/add-chat-contacts all-contacts contacts command-message)) + (commands/generate-preview command (commands/add-chat-contacts contacts command-message)) [react/text (str "Unhandled command: " (-> command-message :content :command-path first))]))))) (defview message-timestamp [t justify-timestamp? outgoing command? content] diff --git a/src/status_im/ui/screens/home/views/inner_item.cljs b/src/status_im/ui/screens/home/views/inner_item.cljs index b7fcacbcb2..534260acb4 100644 --- a/src/status_im/ui/screens/home/views/inner_item.cljs +++ b/src/status_im/ui/screens/home/views/inner_item.cljs @@ -23,10 +23,9 @@ (defview command-short-preview [message] (letsubs [id->command [:chats/id->command] - all-contacts [:contacts/contacts] {:keys [contacts]} [:chats/current-chat]] (when-let [command (commands-receiving/lookup-command-by-ref message id->command)] - (commands/generate-short-preview command (commands/add-chat-contacts all-contacts contacts message))))) + (commands/generate-short-preview command (commands/add-chat-contacts contacts message))))) (defn message-content-text [{:keys [content content-type] :as message}] [react/view styles/last-message-container diff --git a/src/status_im/ui/screens/profile/contact/views.cljs b/src/status_im/ui/screens/profile/contact/views.cljs index d9a1c60d68..966c81d36a 100644 --- a/src/status_im/ui/screens/profile/contact/views.cljs +++ b/src/status_im/ui/screens/profile/contact/views.cljs @@ -1,6 +1,5 @@ (ns status-im.ui.screens.profile.contact.views (:require [re-frame.core :as re-frame] - [clojure.string :as str] [status-im.contact.db :as contact.db] [status-im.i18n :as i18n] [status-im.ui.components.list.views :as list] @@ -17,7 +16,7 @@ toolbar/default-nav-back [toolbar/content-title ""]]) -(defn actions [{:keys [pending? public-key dapp? address]}] +(defn actions [{:keys [pending? public-key dapp?]}] (concat (if (or (nil? pending?) pending?) [{:label (i18n/label :t/add-to-contacts) :icon :icons/add-contact @@ -35,7 +34,6 @@ [{:label (i18n/label :t/send-transaction) :icon :icons/arrow-right :action #(re-frame/dispatch [:profile/send-transaction public-key]) - :disabled? (str/blank? address) :accessibility-label :send-transaction-button}]) [{:label (i18n/label :t/share-profile-link) :icon :icons/share diff --git a/src/status_im/ui/screens/wallet/components/views.cljs b/src/status_im/ui/screens/wallet/components/views.cljs index 61ae70230e..5febb416f4 100644 --- a/src/status_im/ui/screens/wallet/components/views.cljs +++ b/src/status_im/ui/screens/wallet/components/views.cljs @@ -211,7 +211,7 @@ (ethereum/normalized-address (:address contact))]]]]) (views/defview recent-recipients [] - (views/letsubs [contacts [:contacts/all-added-people-contacts-with-address]] + (views/letsubs [contacts [:contacts/all-added-people-contacts]] [simple-screen [toolbar (i18n/label :t/recipient)] [react/view styles/recent-recipients diff --git a/test/cljs/status_im/test/chat/commands/core.cljs b/test/cljs/status_im/test/chat/commands/core.cljs index 29f00dfc99..043206a0bf 100644 --- a/test/cljs/status_im/test/chat/commands/core.cljs +++ b/test/cljs/status_im/test/chat/commands/core.cljs @@ -115,11 +115,6 @@ "0x21631d18d9681d4ffdd460fc45fa52159fcd95c8" "0x5541e3be81b76d76cdbf968516caa5a5b773763b")) -(def contacts-list - (let [pairs (zipmap contacts contacts_addresses)] - (reduce (fn [acc [pub addr]] - (assoc acc pub {:address addr})) {} pairs))) - (deftest enrich-command-message-for-events-test-public (let [db {:chats {"1" {:contacts nil :public? true :group-chat false}}} msg {:chat-id "1"} @@ -129,8 +124,7 @@ (assoc msg :public? true :group-chat false)))))) (deftest enrich-command-message-for-events-test-groupchat - (let [db {:contacts/contacts contacts-list - :chats {"1" {:contacts contacts :public? false :group-chat true}}} + (let [db {:chats {"1" {:contacts contacts :public? false :group-chat true}}} msg {:chat-id "1"} enriched-msg (core/enrich-command-message-for-events db msg)] (testing "command-message correctly enriched - group chat" @@ -138,8 +132,7 @@ (assoc msg :public? false :group-chat true :contacts contacts_addresses)))))) (deftest enrich-command-message-for-events-test-1on1-chat - (let [db {:contacts/contacts contacts-list - :chats {"1" {:contacts contacts :public? false :group-chat false}}} + (let [db {:chats {"1" {:contacts contacts :public? false :group-chat false}}} msg {:chat-id "1"} enriched-msg (core/enrich-command-message-for-events db msg)] (testing "command-message correctly enriched - 1on1 chat" diff --git a/test/cljs/status_im/test/models/contact.cljs b/test/cljs/status_im/test/models/contact.cljs index 46ec75c6ac..4166304d13 100644 --- a/test/cljs/status_im/test/models/contact.cljs +++ b/test/cljs/status_im/test/models/contact.cljs @@ -118,8 +118,7 @@ :photo-path "old-image" :name "old-name" :last-updated 0 - :pending? false - :address address}}}}) + :pending? false}}}}) contact (get-in actual [:db :contacts/contacts public-key])] (testing "it stores the contact in the database" (is (:data-store/tx actual)))