From 89bf1b728d3c46b1940e4bac078a7dcdcd7f011a Mon Sep 17 00:00:00 2001 From: Goran Jovic Date: Tue, 4 Sep 2018 12:48:56 +0200 Subject: [PATCH] feature #4747 - ens usernames in wallet send Signed-off-by: Dmitry Novotochinov --- .../wallet/choose_recipient/events.cljs | 25 ++++++++++++++++++- .../ui/screens/wallet/components/views.cljs | 2 +- .../ui/screens/wallet/request/events.cljs | 5 ---- src/status_im/utils/ethereum/ens.cljs | 11 ++++++++ src/status_im/utils/ethereum/stateofus.cljs | 11 ++------ 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/status_im/ui/screens/wallet/choose_recipient/events.cljs b/src/status_im/ui/screens/wallet/choose_recipient/events.cljs index 5cb73c328b..784992cbf0 100644 --- a/src/status_im/ui/screens/wallet/choose_recipient/events.cljs +++ b/src/status_im/ui/screens/wallet/choose_recipient/events.cljs @@ -4,7 +4,9 @@ [status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.eip681 :as eip681] [status-im.utils.handlers :as handlers] - [status-im.utils.money :as money])) + [status-im.utils.money :as money] + [status-im.utils.ethereum.ens :as ens] + [re-frame.core :as re-frame])) (handlers/register-handler-db :wallet/toggle-flashlight @@ -57,6 +59,27 @@ (assoc-in fx [:db :wallet :send-transaction :gas] ethereum/default-transaction-gas)) +(re-frame/reg-fx + :resolve-address + (fn [{:keys [web3 registry ens-name cb]}] + (ens/get-addr web3 registry ens-name cb))) + +(handlers/register-handler-fx + :wallet.send/set-recipient + (fn [{:keys [db]} [_ recipient]] + (let [{:keys [web3 network]} db + network-info (get-in db [:account/account :networks network]) + chain (ethereum/network->chain-keyword network-info)] + (if (ens/is-valid-eth-name? recipient) + {:resolve-address {:web3 web3 + :registry (get ens/ens-registries chain) + :ens-name recipient + :cb #(re-frame/dispatch [:wallet.send/set-recipient %])}} + (if (ethereum/address? recipient) + {:db (assoc-in db [:wallet :send-transaction :to] recipient) + :dispatch [:navigate-back]} + {:show-error (i18n/label :t/wallet-invalid-address {:data recipient})}))))) + (handlers/register-handler-fx :wallet/fill-request-from-url (fn [{{:keys [network] :as db} :db} [_ data origin]] diff --git a/src/status_im/ui/screens/wallet/components/views.cljs b/src/status_im/ui/screens/wallet/components/views.cljs index 3b9e94b587..2369cf0a36 100644 --- a/src/status_im/ui/screens/wallet/components/views.cljs +++ b/src/status_im/ui/screens/wallet/components/views.cljs @@ -234,7 +234,7 @@ :accessibility-label :recipient-address-input}]] [bottom-buttons/bottom-button [button/button {:disabled? (string/blank? @content) - :on-press #(re-frame/dispatch [:wallet/fill-request-from-url @content :code]) + :on-press #(re-frame/dispatch [:wallet.send/set-recipient @content]) :fit-to-text? false} (i18n/label :t/done)]]]]))) diff --git a/src/status_im/ui/screens/wallet/request/events.cljs b/src/status_im/ui/screens/wallet/request/events.cljs index 2a0495ffb6..97d70b1fa0 100644 --- a/src/status_im/ui/screens/wallet/request/events.cljs +++ b/src/status_im/ui/screens/wallet/request/events.cljs @@ -27,11 +27,6 @@ [::wallet-send-chat-request (name symbol) (str (money/internal->formatted amount symbol decimals))]] [:start-chat whisper-identity]]})) -(handlers/register-handler-fx - :wallet.request/set-recipient - (fn [{:keys [db]} [_ s]] - {:db (assoc-in db [:wallet :request-transaction :to] s)})) - (handlers/register-handler-fx :wallet.request/set-and-validate-amount (fn [{:keys [db]} [_ amount symbol decimals]] diff --git a/src/status_im/utils/ethereum/ens.cljs b/src/status_im/utils/ethereum/ens.cljs index 0d98b650d7..71239604c5 100644 --- a/src/status_im/utils/ethereum/ens.cljs +++ b/src/status_im/utils/ethereum/ens.cljs @@ -89,4 +89,15 @@ (namehash ens-name)) (fn [_ key] (cb (add-uncompressed-public-key-prefix key))))) +(defn is-valid-eth-name? [ens-name] + (and ens-name + (string/ends-with? ens-name ".eth"))) + +(defn get-addr [web3 registry ens-name cb] + {:pre [(is-valid-eth-name? ens-name)]} + (resolver web3 + registry + ens-name + #(addr web3 % ens-name cb))) + ;; TODO ABI, pubkey diff --git a/src/status_im/utils/ethereum/stateofus.cljs b/src/status_im/utils/ethereum/stateofus.cljs index 8b084091ad..a0baa35be1 100644 --- a/src/status_im/utils/ethereum/stateofus.cljs +++ b/src/status_im/utils/ethereum/stateofus.cljs @@ -1,18 +1,11 @@ (ns status-im.utils.ethereum.stateofus (:refer-clojure :exclude [name]) (:require [clojure.string :as string] - [status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.ens :as ens])) (defn is-valid-name? [ens-name] - (string/ends-with? ens-name ".stateofus.eth")) - -(defn addr [web3 registry ens-name cb] - {:pre [(is-valid-name? ens-name)]} - (ens/resolver web3 - registry - ens-name - #(ens/addr web3 % ens-name cb))) + (and ens-name + (string/ends-with? ens-name ".stateofus.eth"))) (defn pubkey [web3 registry ens-name cb]