From 0b6945963825298231faeeaa57285259c7ab888e Mon Sep 17 00:00:00 2001 From: Teemu Patja Date: Wed, 22 Feb 2017 21:51:47 +0200 Subject: [PATCH] Do not checksum validate address by default * make checksum validation optional in valid-address? * update test --- src/clj/commiteth/eth/core.clj | 48 ++++++++++++++++++--------------- test/clj/commiteth/test/eth.clj | 7 +++-- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/clj/commiteth/eth/core.clj b/src/clj/commiteth/eth/core.clj index 949df4d..9e9e2a2 100644 --- a/src/clj/commiteth/eth/core.clj +++ b/src/clj/commiteth/eth/core.clj @@ -124,25 +124,29 @@ (defn valid-address? "Validate given ethereum address. Checksum validation is performed and input is case-sensitive" - [address] - (log/debug "valid-address?" address) - ;; logic based on - ;; https://github.com/cilphex/ethereum-address/blob/master/index.js - (and (boolean (re-matches #"(?i)^(0x)?[0-9a-f]{40}$" address)) - (let [addr (strip-0x address) - hash (pandect/keccak-256 (str/lower-case addr))] - (log/debug "address hash" hash "addr" addr) - (->> - (map-indexed (fn [idx _] - (let [hash-ch-int (-> (nth hash idx) - (hex-ch->num)) - ch (nth addr idx) - ch-lower (lower-ch ch) - ch-upper (upper-ch ch)] - (or (and (> hash-ch-int 7) - (not= ch-upper ch)) - (and (<= hash-ch-int 7) - (not= ch-lower ch))))) - addr) - (filter true?) - (empty?))))) + ([address] + (valid-address? address false)) + ([address checksum-validate?] + (log/debug "valid-address?" address) + ;; logic based on + ;; https://github.com/cilphex/ethereum-address/blob/master/index.js + (and (boolean (re-matches #"(?i)^(0x)?[0-9a-f]{40}$" address)) + (if checksum-validate? + (let [addr (strip-0x address) + hash (pandect/keccak-256 (str/lower-case addr))] + (log/debug "address hash" hash "addr" addr) + (->> + (map-indexed (fn [idx _] + (let [hash-ch-int (-> (nth hash idx) + (hex-ch->num)) + ch (nth addr idx) + ch-lower (lower-ch ch) + ch-upper (upper-ch ch)] + (or (and (> hash-ch-int 7) + (not= ch-upper ch)) + (and (<= hash-ch-int 7) + (not= ch-lower ch))))) + addr) + (filter true?) + (empty?))) + true)))) diff --git a/test/clj/commiteth/test/eth.clj b/test/clj/commiteth/test/eth.clj index e28e08b..b6d9ace 100644 --- a/test/clj/commiteth/test/eth.clj +++ b/test/clj/commiteth/test/eth.clj @@ -9,6 +9,9 @@ (testing "Valid address is valid" (let [addr "0xA1cab91b36bea34487c5670Bbd00a1Aa8196aeD8"] (is (true? (eth/valid-address? addr))))) - (testing "Case sensitivity matters" + (testing "Checksum validation works when used" (let [addr "0xa1cab91b36bea34487c5670bbd00a1aa8196aed8"] - (is (false? (eth/valid-address? addr)))))) + (is (false? (eth/valid-address? addr true))))) + (testing "Checksum validation not used by default" + (let [addr "0xa1cab91b36bea34487c5670bbd00a1aa8196aed8"] + (is (true? (eth/valid-address? addr))))))