[#6688] Update ENS resolving implementation to match what is defined in EIP-1577

Signed-off-by: Julien Eluard <julien.eluard@gmail.com>
This commit is contained in:
Andrey Shovkoplyas 2018-12-12 15:48:17 +01:00 committed by Julien Eluard
parent 7e8389318d
commit b80230a95f
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
4 changed files with 55 additions and 14 deletions

View File

@ -82,10 +82,17 @@
(let [history-host (http/url-host (try (nth history history-index) (catch js/Error _)))] (let [history-host (http/url-host (try (nth history history-index) (catch js/Error _)))]
(assoc browser :unsafe? (dependencies/phishing-detect history-host)))) (assoc browser :unsafe? (dependencies/phishing-detect history-host))))
(defn resolve-ens-multihash-callback [hex] (defn resolve-ens-content-callback [hex]
(let [hash (when hex (multihash/base58 (multihash/create :sha2-256 (subs hex 2))))] (let [hash (when hex (multihash/base58 (multihash/create :sha2-256 (subs hex 2))))]
(if (and hash (not= hash resolver/default-hash)) (if (and hash (not= hash resolver/default-hash))
(re-frame/dispatch [:browser.callback/resolve-ens-multihash-success hash]) (re-frame/dispatch [:browser.callback/resolve-ens-multihash-success "01" hash])
(re-frame/dispatch [:browser.callback/resolve-ens-contenthash]))))
(defn resolve-ens-contenthash-callback [hex]
(let [proto-code (subs hex 2 4)
hash (when hex (multihash/base58 (multihash/create :sha2-256 (subs hex 8))))]
(if (and (#{"00" "01"} proto-code) hash (not= hash resolver/default-hash))
(re-frame/dispatch [:browser.callback/resolve-ens-multihash-success proto-code hash])
(re-frame/dispatch [:browser.callback/resolve-ens-multihash-error])))) (re-frame/dispatch [:browser.callback/resolve-ens-multihash-error]))))
(fx/defn resolve-url (fx/defn resolve-url
@ -97,13 +104,26 @@
(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)]
{:db (update db :browser/options assoc :resolving? true) {:db (update db :browser/options assoc :resolving? true)
:browser/resolve-ens-multihash {:web3 web3 :browser/resolve-ens-content {:web3 web3
:registry (get ens/ens-registries :registry (get ens/ens-registries
chain) chain)
:ens-name host :ens-name host
:cb resolve-ens-multihash-callback}}) :cb resolve-ens-content-callback}})
{:db (update db :browser/options assoc :url (or resolved-url current-url) :resolving? false)})))) {:db (update db :browser/options assoc :url (or resolved-url current-url) :resolving? false)}))))
(fx/defn resolve-ens-contenthash
[{{:keys [web3 network] :as db} :db}]
(let [current-url (get-current-url (get-current-browser db))
host (http/url-host current-url)]
(let [network (get-in db [:account/account :networks network])
chain (ethereum/network->chain-keyword network)]
{:db (update db :browser/options assoc :resolving? true)
:browser/resolve-ens-contenthash {:web3 web3
:registry (get ens/ens-registries
chain)
:ens-name host
:cb resolve-ens-contenthash-callback}})))
(fx/defn update-browser (fx/defn update-browser
[{:keys [db now]} [{:keys [db now]}
{:keys [browser-id] :as browser}] {:keys [browser-id] :as browser}]
@ -149,17 +169,17 @@
:history-index new-index))))) :history-index new-index)))))
(fx/defn resolve-ens-multihash-success (fx/defn resolve-ens-multihash-success
[{:keys [db] :as cofx} hash] [{:keys [db] :as cofx} proto-code hash]
(let [current-url (get-current-url (get-current-browser db)) (let [current-url (get-current-url (get-current-browser db))
host (http/url-host current-url) host (http/url-host current-url)
infura-host "ipfs.infura.io/ipfs/"] gateways (if (= "00" proto-code) "swarm-gateways.net/bzz:/" "ipfs.infura.io/ipfs/")]
(fx/merge cofx (fx/merge cofx
{:db (-> (update db :browser/options {:db (-> (update db :browser/options
assoc assoc
:url (str "https://" infura-host hash :url (str "https://" gateways hash
(subs current-url (+ (.indexOf current-url host) (count host)))) (subs current-url (+ (.indexOf current-url host) (count host))))
:resolving? false) :resolving? false)
(assoc-in [:browser/options :resolved-ens host] (str infura-host hash)))}))) (assoc-in [:browser/options :resolved-ens host] (str gateways hash)))})))
(fx/defn resolve-ens-multihash-error (fx/defn resolve-ens-multihash-error
[{:keys [db] :as cofx}] [{:keys [db] :as cofx}]
@ -323,10 +343,15 @@
{:dispatch [:chat.ui/start-public-chat topic {:modal? true :navigation-reset? true}]})) {:dispatch [:chat.ui/start-public-chat topic {:modal? true :navigation-reset? true}]}))
(re-frame/reg-fx (re-frame/reg-fx
:browser/resolve-ens-multihash :browser/resolve-ens-content
(fn [{:keys [web3 registry ens-name cb]}] (fn [{:keys [web3 registry ens-name cb]}]
(resolver/content web3 registry ens-name cb))) (resolver/content web3 registry ens-name cb)))
(re-frame/reg-fx
:browser/resolve-ens-contenthash
(fn [{:keys [web3 registry ens-name cb]}]
(resolver/contenthash web3 registry ens-name cb)))
(re-frame/reg-fx (re-frame/reg-fx
:browser/send-to-bridge :browser/send-to-bridge
(fn [{:keys [message webview]}] (fn [{:keys [message webview]}]

View File

@ -998,14 +998,19 @@
(handlers/register-handler-fx (handlers/register-handler-fx
:browser.callback/resolve-ens-multihash-success :browser.callback/resolve-ens-multihash-success
(fn [cofx [_ hash]] (fn [cofx [_ proto-code hash]]
(browser/resolve-ens-multihash-success cofx hash))) (browser/resolve-ens-multihash-success cofx proto-code hash)))
(handlers/register-handler-fx (handlers/register-handler-fx
:browser.callback/resolve-ens-multihash-error :browser.callback/resolve-ens-multihash-error
(fn [cofx _] (fn [cofx _]
(browser/resolve-ens-multihash-error cofx))) (browser/resolve-ens-multihash-error cofx)))
(handlers/register-handler-fx
:browser.callback/resolve-ens-contenthash
(fn [cofx _]
(browser/resolve-ens-contenthash cofx)))
(handlers/register-handler-fx (handlers/register-handler-fx
:browser.callback/call-rpc :browser.callback/call-rpc
(fn [cofx [_ message]] (fn [cofx [_ message]]

View File

@ -73,6 +73,11 @@
(namehash ens-name)) (namehash ens-name))
(fn [_ address] (cb (ethereum/hex->address address))))) (fn [_ address] (cb (ethereum/hex->address address)))))
(defn contenthash [web3 resolver ens-name cb]
(ethereum/call web3
(ethereum/call-params resolver "contenthash(bytes32)" (namehash ens-name))
(fn [_ hash] (cb hash))))
(defn content [web3 resolver ens-name cb] (defn content [web3 resolver ens-name cb]
(ethereum/call web3 (ethereum/call web3
(ethereum/call-params resolver "content(bytes32)" (namehash ens-name)) (ethereum/call-params resolver "content(bytes32)" (namehash ens-name))

View File

@ -4,6 +4,12 @@
(def default-hash "0x0000000000000000000000000000000000000000000000000000000000000000") (def default-hash "0x0000000000000000000000000000000000000000000000000000000000000000")
(defn contenthash [web3 registry ens-name cb]
(ens/resolver web3
registry
ens-name
#(ens/contenthash web3 % ens-name cb)))
(defn content [web3 registry ens-name cb] (defn content [web3 registry ens-name cb]
(ens/resolver web3 (ens/resolver web3
registry registry