Do not checksum validate address by default
* make checksum validation optional in valid-address? * update test
This commit is contained in:
parent
6bc3ca8937
commit
0b69459638
|
@ -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))))
|
||||||
|
|
|
@ -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))))))
|
||||||
|
|
Loading…
Reference in New Issue