Skip to content

Commit

Permalink
QA fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferossgp committed Aug 7, 2020
1 parent 497de1b commit 04a7513
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 62 deletions.
14 changes: 10 additions & 4 deletions src/status_im/qr_scanner/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
:content (i18n/label :t/can-not-add-yourself)}}))

(fx/defn handle-public-chat [cofx {:keys [topic]}]
(chat/start-public-chat cofx topic {}))
(when (seq topic)
(chat/start-public-chat cofx topic {})))

(fx/defn handle-view-profile
[{:keys [db] :as cofx} {:keys [public-key]}]
Expand All @@ -58,13 +59,18 @@
public-key
(navigation/navigate-to-cofx (assoc-in cofx [:db :contacts/identity] public-key)
:tabs
{:screen :profile-stack
:params {:screen :profile}})))
{:screen :chat-stack
:params {:screen :profile}})

:else
{:utils/show-popup {:title (i18n/label :t/unable-to-read-this-code)
:content (i18n/label :t/ens-name-not-found)
:on-dismiss #(re-frame/dispatch [:navigate-to :home])}}))

(fx/defn handle-eip681 [cofx data]
(fx/merge cofx
{:dispatch [:wallet/parse-eip681-uri-and-resolve-ens data]}
(navigation/navigate-to-cofx :tabs {:screen :wallet} nil)))
(navigation/navigate-to-cofx :tabs {:screen :wallet})))

(fx/defn match-scan
{:events [::match-scanned-value]}
Expand Down
113 changes: 62 additions & 51 deletions src/status_im/router/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [re-frame.core :as re-frame]
[clojure.string :as string]
[bidi.bidi :as bidi]
[taoensso.timbre :as log]
[status-im.ui.screens.add-new.new-public-chat.db :as public-chat.db]
[status-im.utils.security :as security]
[status-im.ethereum.eip681 :as eip681]
Expand All @@ -22,10 +23,13 @@

(def handled-schemes (set (into uri-schemes web-urls)))

(def browser-extractor {[#"(.*)" :domain ] {"" :browser
"/" :browser}})

(def routes ["" {handled-schemes {["" :chat-id] :public-chat
"chat" {["/public/" :chat-id] :public-chat}
["b/" :domain] :browse
["browse/" :domain] :browse
"b/" browser-extractor
"browser/" browser-extractor
["p/" :chat-id] :private-chat
["u/" :user-id] :user
["user/" :user-id] :user
Expand All @@ -48,7 +52,7 @@
ens-name (ens-name-parse contact-identity)]
(resolver/pubkey registry ens-name cb)))

(defn match-contact
(defn match-contact-async
[chain {:keys [user-id]} callback]
(let [public-key? (and (string? user-id)
(string/starts-with? user-id "0x"))
Expand All @@ -62,38 +66,44 @@
(and (not public-key?) (string? user-id))
(let [registry (get ens/ens-registries chain)
ens-name (ens-name-parse user-id)
on-success #(match-contact chain {:user-id %} callback)]
on-success #(match-contact-async chain {:user-id %} callback)]
(resolver/pubkey registry ens-name on-success))

:else
(callback {:type :contact
:error :not-found}))))

(defn match-public-chat [{:keys [chat-id]} cb]
(defn match-public-chat [{:keys [chat-id]}]
(if (public-chat.db/valid-topic? chat-id)
(cb {:type :public-chat
:topic chat-id})
(cb {:type :public-chat
:error :invalid-topic})))

(defn match-private-chat [chain {:keys [chat-id]} cb]
(match-contact chain {:user-id chat-id}
(fn [{:keys [public-key]}]
(if public-key
(cb {:type :private-chat
:chat-id public-key})
(cb {:type :private-chat
:error :invalid-chat-id})))))

(defn match-browser [{:keys [domain]} cb]
(if (security/safe-link? domain)
(cb {:type :browser
:url domain})
(cb {:type :browser
:error :unsafe-link})))
{:type :public-chat
:topic chat-id}
{:type :public-chat
:error :invalid-topic}))

(defn match-private-chat-async [chain {:keys [chat-id]} cb]
(match-contact-async chain
{:user-id chat-id}
(fn [{:keys [public-key]}]
(if public-key
(cb {:type :private-chat
:chat-id public-key})
(cb {:type :private-chat
:error :invalid-chat-id})))))

(defn match-browser [uri {:keys [domain]}]
;; NOTE: We rebuild domain from original URI and matched domain
(let [domain (->> (string/split uri domain)
second
(str domain))]
(prn domain uri)
(if (security/safe-link? domain)
{:type :browser
:url domain}
{:type :browser
:error :unsafe-link})))

;; NOTE(Ferossgp): Better to handle eip681 also with router instead of regexp.
(defn match-eip681 [uri cb]
(defn match-eip681 [uri]
(if-let [message (eip681/parse-uri uri)]
(let [{:keys [paths ens-names]}
(reduce (fn [acc path]
Expand All @@ -107,45 +117,46 @@
[[:address] [:function-arguments :address]])]
(if (empty? ens-names)
;; if there are no ens-names, we dispatch request-uri-parsed immediately
(cb {:type :eip681
:message message
:uri uri})
(cb {:type :eip681
:uri uri
:message message
:paths paths
:ens-names ens-names})))
(cb {:type :eip681
:uri uri
:error :cannot-parse})))

(defn match-referral [{:keys [referrer]} cb]
(cb {:type :referrals
:referrer referrer}))
{:type :eip681
:message message
:uri uri}
{:type :eip681
:uri uri
:message message
:paths paths
:ens-names ens-names}))
{:type :eip681
:uri uri
:error :cannot-parse}))

(defn match-referral [{:keys [referrer]}]
{:type :referrals
:referrer referrer})

(defn handle-uri [chain uri cb]
(let [{:keys [handler route-params]} (match-uri uri)]
(log/info "[router] uri " uri " matched " handler " with " route-params)
(cond
(= handler :public-chat)
(match-public-chat route-params cb)
(cb (match-public-chat route-params))

(= handler :private-chat)
(match-private-chat chain route-params cb)
(= handler :browser)
(cb (match-browser uri route-params))

(= handler :browse)
(match-browser route-params cb)
(= handler :ethereum)
(cb (match-eip681 uri))

(= handler :user)
(match-contact chain route-params cb)
(match-contact-async chain route-params cb)

(= handler :ethereum)
(match-eip681 uri cb)
(= handler :private-chat)
(match-private-chat-async chain route-params cb)

(spec/valid? :global/public-key uri)
(match-contact chain {:user-id uri} cb)
(match-contact-async chain {:user-id uri} cb)

(= handler :referrals)
(match-referral route-params cb)
(cb (match-referral route-params))

:else
(cb {:type :undefined
Expand Down
7 changes: 5 additions & 2 deletions src/status_im/router/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@

(str "status-im://user/" public-key) [:user {:user-id public-key}]

"status-im://b/www.cryptokitties.co" [:browse {:domain "www.cryptokitties.co"}]
"status-im://b/www.cryptokitties.co" [:browser {:domain "www.cryptokitties.c"}]

"https://join.status.im/status" [:public-chat {:chat-id "status"}]

"https://join.status.im/u/statuse2e" [:user {:user-id "statuse2e"}]

(str "https://join.status.im/user/" public-key) [:user {:user-id public-key}]

"https://join.status.im/b/www.cryptokitties.co" [:browse {:domain "www.cryptokitties.co"}]
;; Last char removed by: https://github.com/juxt/bidi/issues/104
"https://join.status.im/b/www.cryptokitties.co" [:browser {:domain "www.cryptokitties.c"}]

"https://join.status.im/b/https://www.google.com/" [:browser {:domain "https://www.google.co"}]

"ethereum:0x89205a3a3b2a69de6dbf7f01ed13b2108b2c43e7" [:ethereum {:path "0x89205a3a3b2a69de6dbf7f01ed13b2108b2c43e7"}]))
12 changes: 7 additions & 5 deletions src/status_im/utils/universal_links/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@

(fx/defn handle-private-chat [{:keys [db] :as cofx} {:keys [chat-id]}]
(log/info "universal-links: handling private chat" chat-id)
(if-not (new-chat.db/own-public-key? db chat-id)
(chat/start-chat cofx chat-id)
{:utils/show-popup {:title (i18n/label :t/unable-to-read-this-code)
:content (i18n/label :t/can-not-add-yourself)}}))
(when chat-id
(if-not (new-chat.db/own-public-key? db chat-id)
(chat/start-chat cofx chat-id)
{:utils/show-popup {:title (i18n/label :t/unable-to-read-this-code)
:content (i18n/label :t/can-not-add-yourself)}})))

(fx/defn handle-public-chat [cofx {:keys [topic]}]
(log/info "universal-links: handling public chat" topic)
(chat/start-public-chat cofx topic {}))
(when (seq topic)
(chat/start-public-chat cofx topic {})))

(fx/defn handle-view-profile
[{:keys [db] :as cofx} {:keys [public-key]}]
Expand Down
1 change: 1 addition & 0 deletions translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@
"ens-username-connected-with-different-key": "Continuing will require a transaction to connect the username with your current chat key.",
"ens-username-owned-continue": "Continuing will connect this username with your chat key.",
"ens-username-taken": "Username already taken :(",
"ens-name-not-found": "Cannot resolve ENS name",
"enter-12-words": "Enter the 12 words of your seed phrase, separated by single spaces",
"enter-a-private-key": "Enter a private key",
"enter-a-seed-phrase": "Enter a seed phrase",
Expand Down

0 comments on commit 04a7513

Please sign in to comment.