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?
"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))))

View File

@ -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))))))