IsValidPublicKey() and IsValid32ByteHex() replacing IsValidPublicKeyHex()

This commit is contained in:
fiatjaf
2024-01-18 16:27:56 -03:00
parent 1a7b8991a3
commit 70f719ea31
2 changed files with 19 additions and 0 deletions

View File

@@ -39,6 +39,7 @@ func GetPublicKey(sk string) (string, error) {
return hex.EncodeToString(schnorr.SerializePubKey(pk)), nil return hex.EncodeToString(schnorr.SerializePubKey(pk)), nil
} }
// Deprecated: use IsValid32ByteHex instead -- functionality unchanged.
func IsValidPublicKeyHex(pk string) bool { func IsValidPublicKeyHex(pk string) bool {
if strings.ToLower(pk) != pk { if strings.ToLower(pk) != pk {
return false return false
@@ -46,3 +47,9 @@ func IsValidPublicKeyHex(pk string) bool {
dec, _ := hex.DecodeString(pk) dec, _ := hex.DecodeString(pk)
return len(dec) == 32 return len(dec) == 32
} }
func IsValidPublicKey(pk string) bool {
v, _ := hex.DecodeString(pk)
_, err := btcec.ParsePubKey(v)
return len(v) == 32 && err == nil
}

View File

@@ -1,6 +1,7 @@
package nostr package nostr
import ( import (
"encoding/hex"
"net/url" "net/url"
"strings" "strings"
) )
@@ -18,3 +19,14 @@ func IsValidRelayURL(u string) bool {
} }
return true return true
} }
func IsValid32ByteHex(thing string) bool {
if strings.ToLower(thing) != thing {
return false
}
if len(thing) != 64 {
return false
}
_, err := hex.DecodeString(thing)
return err == nil
}