diff --git a/src/status_im/browser/core.cljs b/src/status_im/browser/core.cljs index 8b986d0225..e64480bcb2 100644 --- a/src/status_im/browser/core.cljs +++ b/src/status_im/browser/core.cljs @@ -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))}))) diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index 0728bf60d1..1a4644c807 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -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 diff --git a/src/status_im/utils/contenthash.cljs b/src/status_im/utils/contenthash.cljs index 72bf58d4b5..a573afea72 100644 --- a/src/status_im/utils/contenthash.cljs +++ b/src/status_im/utils/contenthash.cljs @@ -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]}] diff --git a/src/status_im/utils/ethereum/ens.cljs b/src/status_im/utils/ethereum/ens.cljs index 863e8a116e..3edc6c1db7 100644 --- a/src/status_im/utils/ethereum/ens.cljs +++ b/src/status_im/utils/ethereum/ens.cljs @@ -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] diff --git a/test/cljs/status_im/test/utils/contenthash.cljs b/test/cljs/status_im/test/utils/contenthash.cljs index 38468ef9b8..a3df463c54 100644 --- a/test/cljs/status_im/test/utils/contenthash.cljs +++ b/test/cljs/status_im/test/utils/contenthash.cljs @@ -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"