Do not checksum validate address by default

* make checksum validation optional in valid-address?
* update test
This commit is contained in:
Teemu Patja 2017-02-22 21:51:47 +02:00
parent 6bc3ca8937
commit 0b69459638
No known key found for this signature in database
GPG Key ID: F5B7035E6580FD4C
2 changed files with 31 additions and 24 deletions

View File

@ -124,25 +124,29 @@
(defn valid-address? (defn valid-address?
"Validate given ethereum address. Checksum validation is performed "Validate given ethereum address. Checksum validation is performed
and input is case-sensitive" and input is case-sensitive"
[address] ([address]
(log/debug "valid-address?" address) (valid-address? address false))
;; logic based on ([address checksum-validate?]
;; https://github.com/cilphex/ethereum-address/blob/master/index.js (log/debug "valid-address?" address)
(and (boolean (re-matches #"(?i)^(0x)?[0-9a-f]{40}$" address)) ;; logic based on
(let [addr (strip-0x address) ;; https://github.com/cilphex/ethereum-address/blob/master/index.js
hash (pandect/keccak-256 (str/lower-case addr))] (and (boolean (re-matches #"(?i)^(0x)?[0-9a-f]{40}$" address))
(log/debug "address hash" hash "addr" addr) (if checksum-validate?
(->> (let [addr (strip-0x address)
(map-indexed (fn [idx _] hash (pandect/keccak-256 (str/lower-case addr))]
(let [hash-ch-int (-> (nth hash idx) (log/debug "address hash" hash "addr" addr)
(hex-ch->num)) (->>
ch (nth addr idx) (map-indexed (fn [idx _]
ch-lower (lower-ch ch) (let [hash-ch-int (-> (nth hash idx)
ch-upper (upper-ch ch)] (hex-ch->num))
(or (and (> hash-ch-int 7) ch (nth addr idx)
(not= ch-upper ch)) ch-lower (lower-ch ch)
(and (<= hash-ch-int 7) ch-upper (upper-ch ch)]
(not= ch-lower ch))))) (or (and (> hash-ch-int 7)
addr) (not= ch-upper ch))
(filter true?) (and (<= hash-ch-int 7)
(empty?))))) (not= ch-lower ch)))))
addr)
(filter true?)
(empty?)))
true))))

View File

@ -9,6 +9,9 @@
(testing "Valid address is valid" (testing "Valid address is valid"
(let [addr "0xA1cab91b36bea34487c5670Bbd00a1Aa8196aeD8"] (let [addr "0xA1cab91b36bea34487c5670Bbd00a1Aa8196aeD8"]
(is (true? (eth/valid-address? addr))))) (is (true? (eth/valid-address? addr)))))
(testing "Case sensitivity matters" (testing "Checksum validation works when used"
(let [addr "0xa1cab91b36bea34487c5670bbd00a1aa8196aed8"] (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))))))