From 211e39bb5f4a6cfa90917bab6470e5a5bf1770b5 Mon Sep 17 00:00:00 2001 From: yenda Date: Fri, 12 Oct 2018 10:02:29 +0200 Subject: [PATCH] support for all .eth username resolution - resolve all eth usernames - resolve stateofus.eth without domain Signed-off-by: yenda --- .../ui/screens/add_new/new_chat/events.cljs | 25 +++++++++++-------- src/status_im/utils/ethereum/ens.cljs | 21 ++++++++++------ src/status_im/utils/ethereum/resolver.cljs | 23 ++++++++++++----- src/status_im/utils/ethereum/stateofus.cljs | 19 -------------- 4 files changed, 45 insertions(+), 43 deletions(-) delete mode 100644 src/status_im/utils/ethereum/stateofus.cljs 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 e229fc8716..37ab931b3c 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 @@ -3,26 +3,29 @@ [status-im.ui.screens.add-new.new-chat.db :as db] [status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.ens :as ens] - [status-im.utils.ethereum.stateofus :as stateofus] - [status-im.utils.handlers :as handlers])) + [status-im.utils.ethereum.resolver :as resolver] + [status-im.utils.handlers :as handlers] + [clojure.string :as string])) (re-frame/reg-fx :resolve-whisper-identity (fn [{:keys [web3 registry ens-name cb]}] - (stateofus/pubkey web3 registry ens-name cb))) + (resolver/pubkey 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 db new-identity)] - (if (stateofus/is-valid-name? new-identity) + (if (and (string? new-identity) + (string/starts-with? new-identity "0x")) + {:db (assoc db + :contacts/new-identity new-identity + :contacts/new-identity-error new-identity-error)} (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)})))) + :registry (get ens/ens-registries chain) + :ens-name (if (ens/is-valid-eth-name? new-identity) + new-identity + (str new-identity ".stateofus.eth")) + :cb #(re-frame/dispatch [:new-chat/set-new-identity %])}}))))) diff --git a/src/status_im/utils/ethereum/ens.cljs b/src/status_im/utils/ethereum/ens.cljs index 71239604c5..e0ad028f5b 100644 --- a/src/status_im/utils/ethereum/ens.cljs +++ b/src/status_im/utils/ethereum/ens.cljs @@ -15,6 +15,8 @@ :rinkeby "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"}) (def default-namehash "0000000000000000000000000000000000000000000000000000000000000000") +(def default-address "0x0000000000000000000000000000000000000000") +(def default-key "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") (defn namehash [s] (ethereum/normalized-address (if (string/blank? s) @@ -33,7 +35,9 @@ (ethereum/call-params registry "resolver(bytes32)" (namehash ens-name)) - (fn [_ address] (cb (ethereum/hex->address address))))) + (fn [_ address] (let [address (ethereum/hex->address address)] + (when (and address (not= address default-address)) + (cb address)))))) (defn owner [web3 registry ens-name cb] (ethereum/call web3 @@ -79,19 +83,22 @@ (def pubkey-hash "0xc8690233") (defn add-uncompressed-public-key-prefix [key] - (when key + (when (and key + (not= "0x" key) + (not= default-key key)) (str "0x04" (subs key 2)))) +(defn is-valid-eth-name? [ens-name] + (and ens-name + (string/ends-with? ens-name ".eth"))) + (defn pubkey [web3 resolver ens-name cb] (ethereum/call web3 (ethereum/call-params resolver "pubkey(bytes32)" (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"))) + (fn [_ key] (when-let [public-key (add-uncompressed-public-key-prefix key)] + (cb public-key))))) (defn get-addr [web3 registry ens-name cb] {:pre [(is-valid-eth-name? ens-name)]} diff --git a/src/status_im/utils/ethereum/resolver.cljs b/src/status_im/utils/ethereum/resolver.cljs index 6dab8a08aa..32ddf1edc9 100644 --- a/src/status_im/utils/ethereum/resolver.cljs +++ b/src/status_im/utils/ethereum/resolver.cljs @@ -1,14 +1,25 @@ (ns status-im.utils.ethereum.resolver - (:require [status-im.utils.ethereum.ens :as ens])) + (:require [status-im.utils.ethereum.ens :as ens]) + (:refer-clojure :exclude [name])) -(def default-address "0x0000000000000000000000000000000000000000") (def default-hash "0x0000000000000000000000000000000000000000000000000000000000000000") (defn content [web3 registry ens-name cb] (ens/resolver web3 registry ens-name - (fn [address] - (if (and address (not= address default-address)) - (ens/content web3 address ens-name cb) - (cb nil))))) \ No newline at end of file + #(ens/content web3 % ens-name cb))) + +(defn name [web3 registry ens-name cb] + (ens/resolver web3 + registry + ens-name + #(ens/name web3 % ens-name cb))) + +(defn pubkey + [web3 registry ens-name cb] + {:pre [(ens/is-valid-eth-name? ens-name)]} + (ens/resolver web3 + registry + ens-name + #(ens/pubkey web3 % ens-name cb))) diff --git a/src/status_im/utils/ethereum/stateofus.cljs b/src/status_im/utils/ethereum/stateofus.cljs deleted file mode 100644 index a0baa35be1..0000000000 --- a/src/status_im/utils/ethereum/stateofus.cljs +++ /dev/null @@ -1,19 +0,0 @@ -(ns status-im.utils.ethereum.stateofus - (:refer-clojure :exclude [name]) - (:require [clojure.string :as string] - [status-im.utils.ethereum.ens :as ens])) - -(defn is-valid-name? [ens-name] - (and ens-name - (string/ends-with? ens-name ".stateofus.eth"))) - -(defn pubkey - [web3 registry ens-name cb] - {:pre [(is-valid-name? ens-name)]} - (ens/resolver web3 - registry - ens-name - #(ens/pubkey web3 % ens-name cb))) - -#_(addr (:web3 @re-frame.db/app-db) "0x112234455c3a32fd11230c42e7bccd4a84e02010" "qweqwe.stateofus.eth" println) -#_(pubkey (:web3 @re-frame.db/app-db) "0x112234455c3a32fd11230c42e7bccd4a84e02010" "qweqwe.stateofus.eth" println)