Skip to content

Commit

Permalink
Add ENS validation check to address parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Oct 30, 2019
1 parent bada7c4 commit 2290cdf
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/status_im/ethereum/eip681.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
e.g. ethereum:0x1234@1/transfer?to=0x5678&value=1e18&gas=5000"
(:require [clojure.string :as string]
[status-im.ethereum.core :as ethereum]
[status-im.ethereum.ens :as ens]
[status-im.ethereum.tokens :as tokens]
[status-im.utils.money :as money]))

Expand Down Expand Up @@ -41,21 +42,24 @@
{:function-arguments (apply dissoc m valid-native-arguments)}))
arguments)))

;; TODO add ENS support

(defn parse-uri
"Parse a EIP 681 URI as a map (keyword / strings). Parsed map will contain at least the key `address`.
"Parse a EIP 681 URI as a map (keyword / strings). Parsed map will contain at least the key `address` which will be either a valid ENS or Ethereum address.
Note that values are not decoded and you might need to rely on specific methods for some fields (parse-value, parse-number).
Invalid URI will be parsed as `nil`."
[s]
(when (string? s)
(let [[_ authority-path query] (re-find uri-pattern s)]
(when authority-path
(let [[_ address chain-id function-name] (re-find authority-path-pattern authority-path)]
(when (ethereum/address? address)
(when-let [arguments (parse-arguments function-name query)]
(merge {:address address :chain-id (if chain-id (js/parseInt chain-id) (ethereum/chain-keyword->chain-id :mainnet))}
arguments))))))))
(let [[_ raw-address chain-id function-name] (re-find authority-path-pattern authority-path)]
(when (or (ethereum/address? raw-address)
(if (string/starts-with? raw-address "pay-")
(let [pay-address (string/replace-first raw-address "pay-" "")]
(or (ens/is-valid-eth-name? pay-address)
(ethereum/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)]
(merge {:address address :chain-id (if chain-id (js/parseInt chain-id) (ethereum/chain-keyword->chain-id :mainnet))}
arguments)))))))))

(defn parse-eth-value [s]
"Takes a map as returned by `parse-uri` and returns value as BigNumber"
Expand Down

0 comments on commit 2290cdf

Please sign in to comment.