diff --git a/src/status_im/contacts/validations.cljs b/src/status_im/contacts/validations.cljs index 35b33bca70..485d0bc418 100644 --- a/src/status_im/contacts/validations.cljs +++ b/src/status_im/contacts/validations.cljs @@ -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] diff --git a/src/status_im/contacts/views/new_contact.cljs b/src/status_im/contacts/views/new_contact.cljs index e70bdb31a3..eeb49ae1d9 100644 --- a/src/status_im/contacts/views/new_contact.cljs +++ b/src/status_im/contacts/views/new_contact.cljs @@ -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]