Skip to content

Commit

Permalink
[#21135] fix: handle unsupported wallet address (#21820)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen-ghafouri authored Dec 16, 2024
1 parent 93b5f8d commit 4f6e189
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
14 changes: 3 additions & 11 deletions src/status_im/contexts/shell/qr_reader/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
[status-im.feature-flags :as ff]
[utils.address :as utils-address]
[utils.debounce :as debounce]
[utils.ethereum.eip.eip681 :as eip681]
[utils.i18n :as i18n]
[utils.url :as url]))

Expand All @@ -28,13 +27,6 @@
(let [index (string/index-of scanned-text "#")]
(subs scanned-text (inc index))))

(defn eip681-address?
[scanned-text]
(-> scanned-text
eip681/parse-uri
:address
boolean))

(defn pairing-qr-code?
[_]
false)
Expand Down Expand Up @@ -88,10 +80,10 @@
(debounce/debounce-and-dispatch [:generic-scanner/scan-success address] 300)
(debounce/debounce-and-dispatch [:shell/change-tab :wallet-stack] 300))

(eip681-address? scanned-text)
(utils-address/eip-681-address? scanned-text)
(do
(debounce/debounce-and-dispatch [:wallet-legacy/request-uri-parsed
(eip681/parse-uri scanned-text)]
(debounce/debounce-and-dispatch [:generic-scanner/scan-success
(utils-address/eip-681-address->eth-address scanned-text)]
300)
(debounce/debounce-and-dispatch [:shell/change-tab :wallet-stack] 300))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{:title (i18n/label :t/scan-qr)
:subtitle (i18n/label :t/scan-an-address-qr-code)
:error-message (i18n/label :t/oops-this-qr-does-not-contain-an-address)
:validate-fn #(utils-address/supported-address? %)
:validate-fn #(utils-address/supported-scan-address? %)
:on-success-scan (fn [result]
(let [address (utils-address/supported-address->eth-address result)]
(when on-result (on-result address))
Expand Down
24 changes: 23 additions & 1 deletion src/utils/address.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require
[clojure.string :as string]
[native-module.core :as native-module]
[utils.ethereum.eip.eip55 :as eip55]))
[utils.ethereum.eip.eip55 :as eip55]
[utils.ethereum.eip.eip681 :as eip681]))


(def hex-prefix "0x")
Expand Down Expand Up @@ -105,11 +106,23 @@
[address]
(re-find regx-eip-3770-address address))

(defn eip-681-address?
[scanned-text]
(-> scanned-text
eip681/parse-uri
:address
boolean))

(defn supported-address?
[s]
(boolean (or (eip-3770-address? s)
(metamask-address? s))))

(defn supported-scan-address?
[s]
(boolean (or (eip-681-address? s)
(supported-address? s))))

(defn metamask-address->eip-3770-address
[metamask-address]
(when-let [[_ address metamask-network-suffix] (split-metamask-address metamask-address)]
Expand All @@ -129,9 +142,18 @@
[eip-3770-address]
(extract-address-without-chains-info eip-3770-address))

(defn eip-681-address->eth-address
[eip-681-address]
(-> eip-681-address
eip681/parse-uri
:address))

(defn supported-address->eth-address
[address]
(cond
(eip-681-address? address)
(eip-681-address->eth-address address)

(eip-3770-address? address)
(eip-3770-address->eth-address address)

Expand Down
16 changes: 10 additions & 6 deletions src/utils/ethereum/eip/eip681.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
e.g. ethereum:0x1234@1/transfer?to=0x5678&value=1e18&gas=5000"
(:require
[clojure.string :as string]
[utils.address :as address]
[native-module.core :as native-module]
[utils.ens.core :as utils.ens]
[utils.ethereum.chain :as chain]))

Expand All @@ -18,6 +18,10 @@
(def parameter-separator "&")
(def key-value-separator "=")

(defn- address?
[address]
(native-module/address? address))

(def uri-pattern
(re-pattern (str scheme scheme-separator "([^" query-separator "]*)(?:\\" query-separator "(.*))?")))
(def authority-path-pattern
Expand Down Expand Up @@ -63,24 +67,24 @@
Invalid URI will be parsed as `nil`."
[s]
(when (string? s)
(if (address/address? s)
(if (address? s)
{:address s}
(let [[_ authority-path query] (re-find uri-pattern s)]
(when authority-path
(let [[_ raw-address chain-id function-name] (re-find authority-path-pattern authority-path)]
(when (or (or (utils.ens/is-valid-eth-name? raw-address) (address/address? raw-address))
(when (or (or (utils.ens/is-valid-eth-name? raw-address) (address? raw-address))
(when (string/starts-with? raw-address "pay-")
(let [pay-address (string/replace-first raw-address "pay-" "")]
(or (utils.ens/is-valid-eth-name? pay-address)
(address/address? pay-address)))))
(address? pay-address)))))
(let [address (if (string/starts-with? raw-address "pay-")
(string/replace-first raw-address "pay-" "")
raw-address)]
(when-let [arguments (parse-arguments function-name query)]
(let [contract-address (get-in arguments [:function-arguments :address])]
(if-not (or (not contract-address)
(or (utils.ens/is-valid-eth-name? contract-address)
(address/address? contract-address)))
(address? contract-address)))
nil
(merge {:address address
:chain-id (if chain-id
Expand All @@ -98,7 +102,7 @@
"Generate a EIP 681 URI based on `address` and a map (keywords / {bignumbers/strings} ) of extra properties.
No validation of address format is performed."
[address {:keys [chain-id function-name function-arguments] :as m}]
(when (address/address? address)
(when (address? address)
(let [parameters (dissoc (into {} (filter second m)) :chain-id)] ;; filter nil values
(str scheme
scheme-separator
Expand Down

0 comments on commit 4f6e189

Please sign in to comment.