support for all .eth username resolution

- resolve all eth usernames
- resolve stateofus.eth without domain

Signed-off-by: yenda <eric@status.im>
This commit is contained in:
yenda 2018-10-12 10:02:29 +02:00
parent 364b08e3b2
commit 211e39bb5f
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
4 changed files with 45 additions and 43 deletions

View File

@ -3,26 +3,29 @@
[status-im.ui.screens.add-new.new-chat.db :as db] [status-im.ui.screens.add-new.new-chat.db :as db]
[status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.core :as ethereum]
[status-im.utils.ethereum.ens :as ens] [status-im.utils.ethereum.ens :as ens]
[status-im.utils.ethereum.stateofus :as stateofus] [status-im.utils.ethereum.resolver :as resolver]
[status-im.utils.handlers :as handlers])) [status-im.utils.handlers :as handlers]
[clojure.string :as string]))
(re-frame/reg-fx (re-frame/reg-fx
:resolve-whisper-identity :resolve-whisper-identity
(fn [{:keys [web3 registry ens-name cb]}] (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 (handlers/register-handler-fx
:new-chat/set-new-identity :new-chat/set-new-identity
(fn [{{:keys [web3 network network-status] :as db} :db} [_ new-identity]] (fn [{{:keys [web3 network network-status] :as db} :db} [_ new-identity]]
(let [new-identity-error (db/validate-pub-key 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]) (let [network (get-in db [:account/account :networks network])
chain (ethereum/network->chain-keyword network)] chain (ethereum/network->chain-keyword network)]
{:resolve-whisper-identity {:web3 web3 {:resolve-whisper-identity {:web3 web3
:registry (get ens/ens-registries :registry (get ens/ens-registries chain)
chain) :ens-name (if (ens/is-valid-eth-name? new-identity)
:ens-name new-identity new-identity
:cb #(re-frame/dispatch [:new-chat/set-new-identity %])}}) (str new-identity ".stateofus.eth"))
{:db (assoc db :cb #(re-frame/dispatch [:new-chat/set-new-identity %])}})))))
:contacts/new-identity new-identity
:contacts/new-identity-error new-identity-error)}))))

View File

@ -15,6 +15,8 @@
:rinkeby "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"}) :rinkeby "0xe7410170f87102DF0055eB195163A03B7F2Bff4A"})
(def default-namehash "0000000000000000000000000000000000000000000000000000000000000000") (def default-namehash "0000000000000000000000000000000000000000000000000000000000000000")
(def default-address "0x0000000000000000000000000000000000000000")
(def default-key "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
(defn namehash [s] (defn namehash [s]
(ethereum/normalized-address (if (string/blank? s) (ethereum/normalized-address (if (string/blank? s)
@ -33,7 +35,9 @@
(ethereum/call-params registry (ethereum/call-params registry
"resolver(bytes32)" "resolver(bytes32)"
(namehash ens-name)) (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] (defn owner [web3 registry ens-name cb]
(ethereum/call web3 (ethereum/call web3
@ -79,19 +83,22 @@
(def pubkey-hash "0xc8690233") (def pubkey-hash "0xc8690233")
(defn add-uncompressed-public-key-prefix [key] (defn add-uncompressed-public-key-prefix [key]
(when key (when (and key
(not= "0x" key)
(not= default-key key))
(str "0x04" (subs key 2)))) (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] (defn pubkey [web3 resolver ens-name cb]
(ethereum/call web3 (ethereum/call web3
(ethereum/call-params resolver (ethereum/call-params resolver
"pubkey(bytes32)" "pubkey(bytes32)"
(namehash ens-name)) (namehash ens-name))
(fn [_ key] (cb (add-uncompressed-public-key-prefix key))))) (fn [_ key] (when-let [public-key (add-uncompressed-public-key-prefix key)]
(cb public-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] (defn get-addr [web3 registry ens-name cb]
{:pre [(is-valid-eth-name? ens-name)]} {:pre [(is-valid-eth-name? ens-name)]}

View File

@ -1,14 +1,25 @@
(ns status-im.utils.ethereum.resolver (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") (def default-hash "0x0000000000000000000000000000000000000000000000000000000000000000")
(defn content [web3 registry ens-name cb] (defn content [web3 registry ens-name cb]
(ens/resolver web3 (ens/resolver web3
registry registry
ens-name ens-name
(fn [address] #(ens/content web3 % ens-name cb)))
(if (and address (not= address default-address))
(ens/content web3 address ens-name cb) (defn name [web3 registry ens-name cb]
(cb nil))))) (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)))

View File

@ -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)