Fixed broken EIP15777 support

Signed-off-by: Julien Eluard <julien.eluard@gmail.com>
This commit is contained in:
Julien Eluard 2019-04-25 15:02:25 +02:00
parent aaaa625e9a
commit 7b0ebf75e9
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
5 changed files with 43 additions and 29 deletions

View File

@ -12,6 +12,7 @@
[status-im.utils.ethereum.core :as ethereum]
[status-im.utils.ethereum.ens :as ens]
[status-im.utils.ethereum.resolver :as resolver]
[status-im.utils.contenthash :as contenthash]
[status-im.utils.fx :as fx]
[status-im.utils.http :as http]
[status-im.utils.multihash :as multihash]
@ -66,17 +67,21 @@
(let [history-host (http/url-host (try (nth history history-index) (catch js/Error _)))]
(cond-> browser history-host (assoc :unsafe? (dependencies/phishing-detect history-host)))))
(defn- content->hash [hex]
(when (and hex (not= hex "0x"))
;; TODO(julien) Remove once our ENS DApp are migrated
(multihash/base58 (multihash/create :sha2-256 (subs hex 2)))))
(defn resolve-ens-content-callback [hex]
(let [hash (when hex (multihash/base58 (multihash/create :sha2-256 (subs hex 2))))]
(let [hash (content->hash hex)]
(if (and hash (not= hash resolver/default-hash))
(re-frame/dispatch [:browser.callback/resolve-ens-multihash-success constants/ipfs-proto-code hash])
(re-frame/dispatch [:browser.callback/resolve-ens-multihash-success {:namespace :ipfs :hash hash}])
(re-frame/dispatch [:browser.callback/resolve-ens-contenthash]))))
(defn resolve-ens-contenthash-callback [hex]
(let [proto-code (when hex (subs hex 2 4))
hash (when hex (multihash/base58 (multihash/create :sha2-256 (subs hex 12))))]
(if (and proto-code (#{constants/swarm-proto-code constants/ipfs-proto-code} proto-code) hash (not= hash resolver/default-hash))
(re-frame/dispatch [:browser.callback/resolve-ens-multihash-success proto-code hash])
(let [{:keys [hash] :as m} (contenthash/decode hex)]
(if (and hash (not= hash resolver/default-hash))
(re-frame/dispatch [:browser.callback/resolve-ens-multihash-success m])
(re-frame/dispatch [:browser.callback/resolve-ens-multihash-error]))))
(fx/defn resolve-url
@ -150,21 +155,29 @@
:history new-history
:history-index new-index)))))
(defmulti storage-gateway :namespace)
(defmethod storage-gateway :ipfs
[{:keys [hash]}]
(let [base32hash (-> (.encode js-dependencies/hi-base32 (alphabase.base58/decode hash))
(string/replace #"=" "")
(string/lower-case))]
(str "https://" base32hash ".infura.status.im")))
(defmethod storage-gateway :swarm
[{:keys [hash]}]
(str "https://swarm-gateways.net/bzz:/" hash))
(fx/defn resolve-ens-multihash-success
[{:keys [db] :as cofx} proto-code hash]
[{:keys [db] :as cofx} m]
(let [current-url (get-current-url (get-current-browser db))
host (http/url-host current-url)
path (subs current-url (+ (.indexOf current-url host) (count host)))
gateway (if (= constants/ipfs-proto-code proto-code)
(let [base32hash (-> (.encode js-dependencies/hi-base32 (alphabase.base58/decode hash))
(string/replace #"=" "")
(string/lower-case))]
(str base32hash ".infura.status.im"))
(str "swarm-gateways.net/bzz:/" hash))]
host (http/url-host current-url)
path (subs current-url (+ (.indexOf current-url host) (count host)))
gateway (storage-gateway m)]
(fx/merge cofx
{:db (-> (update db :browser/options
assoc
:url (str "https://" gateway path)
:url (str gateway path)
:resolving? false)
(assoc-in [:browser/options :resolved-ens host] gateway))})))

View File

@ -1502,8 +1502,8 @@
(handlers/register-handler-fx
:browser.callback/resolve-ens-multihash-success
(fn [cofx [_ proto-code hash]]
(browser/resolve-ens-multihash-success cofx proto-code hash)))
(fn [cofx [_ m]]
(browser/resolve-ens-multihash-success cofx m)))
(handlers/register-handler-fx
:browser.callback/resolve-ens-multihash-error

View File

@ -15,15 +15,15 @@
(nil? ipld))
(str "0xe301" (hex/encode (b58/decode hash)))))
(defn decode [contenthash]
(when (and contenthash
(string/starts-with? contenthash "0xe3011220")
(= 74 (count contenthash)))
(defn decode [hex]
(when (and hex (not= hex "0x")
(string/starts-with? hex "0xe30101")
(= 78 (count hex)))
{:namespace :ipfs
:hash (-> contenthash
(subs 6)
hex/decode
b58/encode)}))
:hash (-> hex
(subs 10)
hex/decode
b58/encode)}))
(fx/defn cat
[cofx {:keys [contenthash on-success on-failure]}]

View File

@ -6,7 +6,8 @@
"
(:refer-clojure :exclude [name])
(:require [clojure.string :as string]
[status-im.utils.ethereum.core :as ethereum]))
[status-im.utils.ethereum.core :as ethereum]
[status-im.utils.ethereum.abi-spec :as abi-spec]))
;; this is the addresses of ens registries for the different networks
(def ens-registries
@ -86,7 +87,7 @@
"contenthash(bytes32)"
(namehash ens-name))
(fn [hash]
(cb hash))))
(cb (first (abi-spec/decode hash ["bytes"]))))))
(defn content
[resolver ens-name cb]

View File

@ -7,7 +7,7 @@
(deftest contenthash-decode
(testing "decoding a valid ipfs hash"
(is (= (contenthash/decode "0xe301122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f")
(is (= (contenthash/decode "0xe3010170122029f2d17be6139079dc48696d1f582a8530eb9805b561eda517e22a892c7e3f1f")
{:namespace :ipfs
:hash "QmRAQB6YaCyidP37UdDnjFY5vQuiBrcqdyoW1CuDgwxkD4"})))
(testing "decoding an invalid ipfs hash"