mirror of
https://github.com/status-im/status-mobile.git
synced 2025-01-14 02:35:54 +00:00
[#6169] Display .eth or other domain rather than IPFS redirect
Signed-off-by: Andrey Shovkoplyas <motor4ik@gmail.com>
This commit is contained in:
parent
7264ae2a14
commit
e9bf1d7864
@ -7,7 +7,6 @@
|
||||
[status-im.i18n :as i18n]
|
||||
[status-im.js-dependencies :as dependencies]
|
||||
[status-im.native-module.core :as status]
|
||||
[status-im.qr-scanner.core :as qr-scanner]
|
||||
[status-im.ui.components.list-selection :as list-selection]
|
||||
[status-im.ui.screens.browser.default-dapps :as default-dapps]
|
||||
[status-im.ui.screens.navigation :as navigation]
|
||||
@ -33,14 +32,35 @@
|
||||
(let [dapp-permissions (into {} (map #(vector (:dapp %) %) all-dapp-permissions))]
|
||||
{:db (assoc db :dapps/permissions dapp-permissions)}))
|
||||
|
||||
(fx/defn navigate-to-browser
|
||||
[{{:keys [view-id]} :db :as cofx}]
|
||||
(if (= view-id :dapp-description)
|
||||
(navigation/navigate-reset cofx
|
||||
{:index 1
|
||||
:actions [{:routeName :home}
|
||||
{:routeName :browser}]})
|
||||
(navigation/navigate-to-cofx cofx :browser nil)))
|
||||
|
||||
(fx/defn update-browser-option
|
||||
[{:keys [db]} option-key option-value]
|
||||
{:db (assoc-in db [:browser/options option-key] option-value)})
|
||||
|
||||
(fx/defn update-browser-options
|
||||
[{:keys [db]} options]
|
||||
{:db (update db :browser/options merge options)})
|
||||
|
||||
(defn get-current-browser [db]
|
||||
(get-in db [:browser/browsers (get-in db [:browser/options :browser-id])]))
|
||||
|
||||
(defn get-current-url [{:keys [history history-index]}]
|
||||
(when (and history-index history)
|
||||
(nth history history-index)))
|
||||
|
||||
(defn secure? [{:keys [error? dapp?] :as browser}]
|
||||
(defn secure? [{:keys [error? dapp?]} {:keys [url]}]
|
||||
(or dapp?
|
||||
(and (not error?)
|
||||
(string/starts-with? (get-current-url browser) "https://"))))
|
||||
(when url
|
||||
(string/starts-with? url "https://")))))
|
||||
|
||||
(fx/defn remove-browser
|
||||
[{:keys [db]} browser-id]
|
||||
@ -61,9 +81,31 @@
|
||||
(let [history-host (http/url-host (try (nth history history-index) (catch js/Error _)))]
|
||||
(assoc browser :unsafe? (dependencies/phishing-detect history-host))))
|
||||
|
||||
(defn resolve-ens-multihash-callback [hex]
|
||||
(let [hash (when hex (multihash/base58 (multihash/create :sha2-256 (subs hex 2))))]
|
||||
(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-error]))))
|
||||
|
||||
(fx/defn resolve-url
|
||||
[{{:keys [web3 network] :as db} :db} {:keys [loading? error? resolved-url]}]
|
||||
(when (and (not loading?) (not error?))
|
||||
(let [current-url (get-current-url (get-current-browser db))
|
||||
host (http/url-host current-url)]
|
||||
(if (and (not resolved-url) (ens/is-valid-eth-name? host))
|
||||
(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-multihash {:web3 web3
|
||||
:registry (get ens/ens-registries
|
||||
chain)
|
||||
:ens-name host
|
||||
:cb resolve-ens-multihash-callback}})
|
||||
{:db (update db :browser/options assoc :url (or resolved-url current-url) :resolving? false)}))))
|
||||
|
||||
(fx/defn update-browser
|
||||
[{:keys [db now]}
|
||||
{:keys [browser-id history history-index error? dapp?] :as browser}]
|
||||
{:keys [browser-id] :as browser}]
|
||||
(let [updated-browser (-> (assoc browser :timestamp now)
|
||||
(check-if-dapp-in-list)
|
||||
(check-if-phishing-url))]
|
||||
@ -72,9 +114,6 @@
|
||||
merge updated-browser)
|
||||
:data-store/tx [(browser-store/save-browser-tx updated-browser)]}))
|
||||
|
||||
(defn get-current-browser [db]
|
||||
(get-in db [:browser/browsers (get-in db [:browser/options :browser-id])]))
|
||||
|
||||
(defn can-go-back? [{:keys [history-index]}]
|
||||
(pos? history-index))
|
||||
|
||||
@ -82,7 +121,9 @@
|
||||
[cofx]
|
||||
(let [{:keys [history-index] :as browser} (get-current-browser (:db cofx))]
|
||||
(when (can-go-back? browser)
|
||||
(update-browser cofx (assoc browser :history-index (dec history-index))))))
|
||||
(fx/merge cofx
|
||||
(update-browser (assoc browser :history-index (dec history-index)))
|
||||
(resolve-url nil)))))
|
||||
|
||||
(defn can-go-forward? [{:keys [history-index history]}]
|
||||
(< history-index (dec (count history))))
|
||||
@ -91,17 +132,19 @@
|
||||
[cofx]
|
||||
(let [{:keys [history-index] :as browser} (get-current-browser (:db cofx))]
|
||||
(when (can-go-forward? browser)
|
||||
(update-browser cofx (assoc browser :history-index (inc history-index))))))
|
||||
(fx/merge cofx
|
||||
(update-browser (assoc browser :history-index (inc history-index)))
|
||||
(resolve-url nil)))))
|
||||
|
||||
(fx/defn update-browser-history
|
||||
;; TODO: not clear how this works
|
||||
[cofx browser url loading?]
|
||||
[{db :db :as cofx} browser url loading?]
|
||||
(when-not loading?
|
||||
(let [history-index (:history-index browser)
|
||||
history (:history browser)
|
||||
history-url (get-current-url browser)]
|
||||
(when (not= history-url url)
|
||||
(let [slash? (= url (str history-url "/"))
|
||||
current-url (get-in db [:browser/options :url])]
|
||||
(when (and (not= current-url url) (not= (str current-url "/") url))
|
||||
(let [slash? (= url (str current-url "/"))
|
||||
new-history (if slash?
|
||||
(assoc history history-index url)
|
||||
(conj (subvec history 0 (inc history-index)) url))
|
||||
@ -113,44 +156,24 @@
|
||||
:history new-history
|
||||
:history-index new-index)))))))
|
||||
|
||||
(defn ens? [host]
|
||||
(and (string? host)
|
||||
(string/ends-with? host ".eth")))
|
||||
|
||||
(defn resolve-ens-multihash-callback [hex]
|
||||
(let [hash (when hex (multihash/base58 (multihash/create :sha2-256 (subs hex 2))))]
|
||||
(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-error]))))
|
||||
|
||||
(fx/defn resolve-ens-multihash-success
|
||||
[{:keys [db] :as cofx} hash]
|
||||
(let [options (:browser/options db)
|
||||
browsers (:browser/browsers db)
|
||||
browser (get browsers (:browser-id options))
|
||||
history-index (:history-index browser)]
|
||||
(let [current-url (get-current-url (get-current-browser db))
|
||||
host (http/url-host current-url)
|
||||
infura-host "ipfs.infura.io/ipfs/"]
|
||||
(fx/merge cofx
|
||||
{:db (assoc-in db [:browser/options :resolving?] false)}
|
||||
(update-browser (assoc-in browser [:history history-index]
|
||||
(str "https://ipfs.infura.io/ipfs/" hash))))))
|
||||
{:db (-> (update db :browser/options
|
||||
assoc
|
||||
:url (str "https://" infura-host hash
|
||||
(subs current-url (+ (.indexOf current-url host) (count host))))
|
||||
:resolving? false)
|
||||
(assoc-in [:browser/options :resolved-ens host] (str infura-host hash)))})))
|
||||
|
||||
(fx/defn resolve-ens-multihash
|
||||
[{{:keys [web3 network] :as db} :db} host loading? error?]
|
||||
(when (and (not loading?)
|
||||
(not error?)
|
||||
(ens? host))
|
||||
(let [network (get-in db [:account/account :networks network])
|
||||
chain (ethereum/network->chain-keyword network)]
|
||||
{:db (assoc-in db [:browser/options :resolving?] true)
|
||||
:browser/resolve-ens-multihash {:web3 web3
|
||||
:registry (get ens/ens-registries
|
||||
chain)
|
||||
:ens-name host
|
||||
:cb resolve-ens-multihash-callback}})))
|
||||
|
||||
(fx/defn update-browser-option
|
||||
[{:keys [db]} option-key option-value]
|
||||
{:db (assoc-in db [:browser/options option-key] option-value)})
|
||||
(fx/defn resolve-ens-multihash-error
|
||||
[{:keys [db] :as cofx}]
|
||||
(update-browser-options cofx {:url (get-current-url (get-current-browser db))
|
||||
:resolving? false
|
||||
:error? true}))
|
||||
|
||||
(fx/defn handle-browser-error
|
||||
[cofx]
|
||||
@ -160,11 +183,14 @@
|
||||
|
||||
(fx/defn update-browser-on-nav-change
|
||||
[cofx browser url loading? error?]
|
||||
(when (not= "about:blank" url)
|
||||
(let [host (http/url-host url)]
|
||||
(fx/merge cofx
|
||||
(resolve-ens-multihash host loading? error?)
|
||||
(update-browser-history browser url loading?)))))
|
||||
(let [options (get-in cofx [:db :browser/options])
|
||||
current-url (:url options)]
|
||||
(when (and (not= "about:blank" url) (not= current-url url) (not= (str current-url "/") url))
|
||||
(let [resolved-ens (first (filter #(not= (.indexOf url (second %)) -1) (:resolved-ens options)))
|
||||
resolved-url (if resolved-ens (string/replace url (second resolved-ens) (first resolved-ens)) url)]
|
||||
(fx/merge cofx
|
||||
(update-browser-history browser resolved-url loading?)
|
||||
(resolve-url {:loading? loading? :error? error? :resolved-url (when resolved-ens url)}))))))
|
||||
|
||||
(fx/defn navigation-state-changed
|
||||
[cofx event error?]
|
||||
@ -182,21 +208,11 @@
|
||||
;; TODO(yenda) is that desirable ?
|
||||
[cofx url]
|
||||
(let [browser (get-current-browser (:db cofx))
|
||||
normalized-url (http/normalize-and-decode-url url)
|
||||
host (http/url-host normalized-url)]
|
||||
normalized-url (http/normalize-and-decode-url url)]
|
||||
(fx/merge cofx
|
||||
(update-browser-option :url-editing? false)
|
||||
(resolve-ens-multihash host false false)
|
||||
(update-browser-history browser normalized-url false))))
|
||||
|
||||
(fx/defn navigate-to-browser
|
||||
[{{:keys [view-id]} :db :as cofx}]
|
||||
(if (= view-id :dapp-description)
|
||||
(navigation/navigate-reset cofx
|
||||
{:index 1
|
||||
:actions [{:routeName :home}
|
||||
{:routeName :browser}]})
|
||||
(navigation/navigate-to-cofx cofx :browser nil)))
|
||||
(update-browser-history browser normalized-url false)
|
||||
(resolve-url nil))))
|
||||
|
||||
(fx/defn open-url
|
||||
"Opens a url in the browser. If a host can be extracted from the url and
|
||||
@ -213,7 +229,7 @@
|
||||
{:browser-id (:browser-id browser)})}
|
||||
(navigate-to-browser)
|
||||
(update-browser browser)
|
||||
(resolve-ens-multihash host false false))))
|
||||
(resolve-url nil))))
|
||||
|
||||
(fx/defn open-existing-browser
|
||||
"Opens an existing browser with it's history"
|
||||
@ -223,7 +239,8 @@
|
||||
{:db (assoc db :browser/options
|
||||
{:browser-id browser-id})}
|
||||
(update-browser browser)
|
||||
(navigation/navigate-to-cofx :browser nil))))
|
||||
(navigation/navigate-to-cofx :browser nil)
|
||||
(resolve-url nil))))
|
||||
|
||||
(fx/defn web3-send-async
|
||||
[{:keys [db]} {:keys [method] :as payload} message-id]
|
||||
@ -282,7 +299,9 @@
|
||||
(and (= type constants/history-state-changed)
|
||||
platform/ios?
|
||||
(not= "about:blank" url))
|
||||
(update-browser-history cofx browser url false)
|
||||
(fx/merge cofx
|
||||
(update-browser-history browser url false)
|
||||
(resolve-url nil))
|
||||
|
||||
(= type constants/web3-send-async)
|
||||
(web3-send-async cofx payload messageId)
|
||||
|
@ -5,6 +5,7 @@
|
||||
(spec/def :browser/browser-id (spec/nilable string?))
|
||||
(spec/def :browser/timestamp (spec/nilable int?))
|
||||
(spec/def :browser/name (spec/nilable string?))
|
||||
(spec/def :browser/url (spec/nilable string?))
|
||||
(spec/def :browser/dapp? (spec/nilable boolean?))
|
||||
(spec/def :browser/error? (spec/nilable boolean?))
|
||||
(spec/def :browser/history (spec/nilable vector?))
|
||||
@ -15,6 +16,7 @@
|
||||
(spec/def :browser/url-editing? (spec/nilable boolean?))
|
||||
(spec/def :browser/show-tooltip (spec/nilable keyword?))
|
||||
(spec/def :browser/show-permission (spec/nilable map?))
|
||||
(spec/def :browser/resolved-ens (spec/nilable map?))
|
||||
(spec/def :browser/pending-permissions (spec/nilable list?))
|
||||
(spec/def :browser/yielding-control? (spec/nilable boolean?))
|
||||
|
||||
@ -22,6 +24,7 @@
|
||||
(spec/nilable
|
||||
(allowed-keys
|
||||
:opt-un [:browser/browser-id
|
||||
:browser/url
|
||||
:browser/loading?
|
||||
:browser/resolving?
|
||||
:browser/url-editing?
|
||||
@ -29,6 +32,7 @@
|
||||
:browser/show-permission
|
||||
:browser/pending-permissions
|
||||
:browser/yielding-control?
|
||||
:browser/resolved-ens
|
||||
:browser/error?])))
|
||||
|
||||
(spec/def :browser/browser
|
||||
|
@ -925,7 +925,7 @@
|
||||
(handlers/register-handler-fx
|
||||
:browser.callback/resolve-ens-multihash-error
|
||||
(fn [cofx _]
|
||||
(browser/update-browser-option cofx :resolving? false)))
|
||||
(browser/resolve-ens-multihash-error cofx)))
|
||||
|
||||
(handlers/register-handler-fx
|
||||
:browser.callback/call-rpc
|
||||
|
@ -14,8 +14,7 @@
|
||||
(fn [[browsers dapps]]
|
||||
(reduce (fn [acc [k {:keys [dapp? name] :as browser}]]
|
||||
(cond-> (update acc k assoc
|
||||
:url (browser/get-current-url browser)
|
||||
:secure? (browser/secure? browser))
|
||||
:url (browser/get-current-url browser))
|
||||
dapp? (assoc-in [k :dapp] (get dapps name))))
|
||||
browsers
|
||||
browsers)))
|
||||
@ -25,4 +24,5 @@
|
||||
:<- [:get :browser/options]
|
||||
:<- [:browser/browsers]
|
||||
(fn [[options browsers]]
|
||||
(get browsers (:browser-id options))))
|
||||
(let [browser (get browsers (:browser-id options))]
|
||||
(assoc browser :secure? (browser/secure? browser options)))))
|
||||
|
@ -28,7 +28,7 @@
|
||||
(def browser-config
|
||||
(edn/read-string (slurp "./src/status_im/utils/browser_config.edn")))
|
||||
|
||||
(defn toolbar-content [url {:keys [secure?] :as browser} url-editing?]
|
||||
(defn toolbar-content [url url-original {:keys [secure?]} url-editing?]
|
||||
(let [url-text (atom url)]
|
||||
[react/view
|
||||
[react/view (styles/toolbar-content false)
|
||||
@ -48,20 +48,20 @@
|
||||
:ellipsize :end
|
||||
:style styles/url-input}]
|
||||
[react/touchable-highlight {:style {:flex 1} :on-press #(re-frame/dispatch [:browser.ui/url-input-pressed])}
|
||||
[react/text {:style styles/url-text} (http/url-host url)]])]]))
|
||||
[react/text {:style styles/url-text} (http/url-host url-original)]])]]))
|
||||
|
||||
(defn- on-options [name browser-id]
|
||||
(list-selection/show {:title name
|
||||
:options (wallet.actions/actions browser-id)}))
|
||||
|
||||
(defn toolbar [error? url browser browser-id url-editing?]
|
||||
(defn toolbar [error? url url-original browser browser-id url-editing?]
|
||||
[toolbar.view/toolbar {}
|
||||
[toolbar.view/nav-button-with-count
|
||||
(actions/close (fn []
|
||||
(re-frame/dispatch [:navigate-to :home])
|
||||
(when error?
|
||||
(re-frame/dispatch [:browser.ui/remove-browser-pressed browser-id]))))]
|
||||
[toolbar-content url browser url-editing?]
|
||||
[toolbar-content url url-original browser url-editing?]
|
||||
[toolbar.view/actions [{:icon :icons/options
|
||||
:icon-opts {:color :black
|
||||
:accessibility-label :chat-menu-button}
|
||||
@ -107,14 +107,14 @@
|
||||
;; that's why it can't be used in `browser`, because `url` comes from subs
|
||||
(views/defview browser-component
|
||||
[{:keys [webview error? url browser browser-id unsafe? can-go-back?
|
||||
can-go-forward? url-editing? resolving? network-id address
|
||||
can-go-forward? url-editing? resolving? network-id address url-original
|
||||
show-permission show-tooltip opt-in? loading? dapp? rpc-url name]}]
|
||||
{:should-component-update (fn [_ _ args]
|
||||
(let [[_ props] args]
|
||||
(not (nil? (:url props)))))}
|
||||
[react/view styles/browser
|
||||
[status-bar/status-bar]
|
||||
[toolbar error? url browser browser-id url-editing?]
|
||||
[toolbar error? url url-original browser browser-id url-editing?]
|
||||
[react/view components.styles/flex
|
||||
(if unsafe?
|
||||
[site-blocked.views/view {:can-go-back? can-go-back?
|
||||
@ -125,7 +125,7 @@
|
||||
:ref #(do
|
||||
(reset! webview %)
|
||||
(re-frame/dispatch [:set :webview-bridge %]))
|
||||
:source (when-not resolving? {:uri url})
|
||||
:source (when (and url (not resolving?)) {:uri url})
|
||||
:java-script-enabled true
|
||||
:bounces false
|
||||
:local-storage-enabled true
|
||||
@ -159,17 +159,18 @@
|
||||
(views/letsubs [webview (atom nil)
|
||||
{:keys [address settings]} [:get-current-account]
|
||||
{:keys [browser-id dapp? name unsafe?] :as browser} [:get-current-browser]
|
||||
{:keys [error? loading? url-editing? show-tooltip show-permission resolving?]} [:get :browser/options]
|
||||
{:keys [url error? loading? url-editing? show-tooltip show-permission resolving?]} [:get :browser/options]
|
||||
rpc-url [:get :rpc-url]
|
||||
network-id [:get-network-id]]
|
||||
(let [can-go-back? (browser/can-go-back? browser)
|
||||
can-go-forward? (browser/can-go-forward? browser)
|
||||
url (browser/get-current-url browser)
|
||||
url-original (browser/get-current-url browser)
|
||||
opt-in? (:web3-opt-in? settings)]
|
||||
[browser-component {:webview webview
|
||||
:dapp? dapp?
|
||||
:error? error?
|
||||
:url url
|
||||
:url-original url-original
|
||||
:browser browser
|
||||
:browser-id browser-id
|
||||
:unsafe? unsafe?
|
||||
|
@ -36,8 +36,7 @@
|
||||
"resolver(bytes32)"
|
||||
(namehash ens-name))
|
||||
(fn [_ address] (let [address (ethereum/hex->address address)]
|
||||
(when (and address (not= address default-address))
|
||||
(cb address))))))
|
||||
(cb (if (and address (not= address default-address)) address ""))))))
|
||||
|
||||
(defn owner [web3 registry ens-name cb]
|
||||
(ethereum/call web3
|
||||
@ -90,6 +89,7 @@
|
||||
|
||||
(defn is-valid-eth-name? [ens-name]
|
||||
(and ens-name
|
||||
(string? ens-name)
|
||||
(string/ends-with? ens-name ".eth")))
|
||||
|
||||
(defn pubkey [web3 resolver ens-name cb]
|
||||
|
Loading…
x
Reference in New Issue
Block a user