This commit is contained in:
Roman Volosovskyi 2017-02-02 14:17:04 +02:00 committed by Roman Volosovskyi
parent 5af0905f2f
commit 0d9f35190f
2 changed files with 17 additions and 8 deletions

View File

@ -1,5 +1,6 @@
(ns status-im.contacts.validations
(:require [cljs.spec :as s]
[clojure.string :as str]
[status-im.data-store.contacts :as contacts]))
(def web3 (js/require "web3"))
@ -12,19 +13,27 @@
(:pending? (contacts/get-by-id identity))
true))
(defn hex-string? [s]
(let [s' (if (str/starts-with? s "0x")
(subs s 2)
s)]
(boolean (re-matches #"(?i)[0-9a-f]+" s'))))
(defn valid-length? [identity]
(let [length (count identity)]
(or
(= 130 length)
(= 132 length)
(is-address? identity))))
(and
(hex-string? identity)
(or
(and (= 128 length) (not (str/includes? identity "0x")))
(and (= 130 length) (str/starts-with? identity "0x"))
(and (= 132 length) (str/starts-with? identity "0x04"))
(is-address? identity)))))
(s/def ::identity-length valid-length?)
(s/def ::contact-can-be-added contact-can-be-added?)
(s/def ::not-empty-string (s/and string? not-empty))
(s/def ::name ::not-empty-string)
(s/def ::whisper-identity (s/and ::not-empty-string
::contact-can-be-added
::identity-length))
(s/def ::contact (s/keys :req-un [::name ::whisper-identity]

View File

@ -63,12 +63,12 @@
(normalize-hex whisper-identity))
(label :t/can-not-add-yourself)
(not (s/valid? ::v/contact-can-be-added whisper-identity))
(label :t/contact-already-added)
(not (s/valid? ::v/whisper-identity whisper-identity))
(label :t/enter-valid-public-key)
(not (s/valid? ::v/contact-can-be-added whisper-identity))
(label :t/contact-already-added)
:else error))
(defn toolbar-actions [new-contact-identity account error]