diff --git a/src/status_im/contexts/shell/qr_reader/view.cljs b/src/status_im/contexts/shell/qr_reader/view.cljs index df2fe9d7d78..322f6951cd2 100644 --- a/src/status_im/contexts/shell/qr_reader/view.cljs +++ b/src/status_im/contexts/shell/qr_reader/view.cljs @@ -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])) @@ -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) @@ -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)) diff --git a/src/status_im/contexts/wallet/common/scan_account/view.cljs b/src/status_im/contexts/wallet/common/scan_account/view.cljs index 92d988b0f34..29965153f1c 100644 --- a/src/status_im/contexts/wallet/common/scan_account/view.cljs +++ b/src/status_im/contexts/wallet/common/scan_account/view.cljs @@ -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)) diff --git a/src/utils/address.cljs b/src/utils/address.cljs index a51a627f6a5..14d7a0957c0 100644 --- a/src/utils/address.cljs +++ b/src/utils/address.cljs @@ -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") @@ -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)] @@ -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) diff --git a/src/utils/ethereum/eip/eip681.cljs b/src/utils/ethereum/eip/eip681.cljs index bdc8b2bcbb7..14acb588a00 100644 --- a/src/utils/ethereum/eip/eip681.cljs +++ b/src/utils/ethereum/eip/eip681.cljs @@ -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])) @@ -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 @@ -63,16 +67,16 @@ 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)] @@ -80,7 +84,7 @@ (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 @@ -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