Skip to content

Commit

Permalink
IsValidPublicKey() and IsValid32ByteHex() replacing IsValidPublicKeyH…
Browse files Browse the repository at this point in the history
…ex()
  • Loading branch information
fiatjaf committed Jan 18, 2024
1 parent 1a7b899 commit 70f719e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@ func GetPublicKey(sk string) (string, error) {
return hex.EncodeToString(schnorr.SerializePubKey(pk)), nil
}

// Deprecated: use IsValid32ByteHex instead -- functionality unchanged.
func IsValidPublicKeyHex(pk string) bool {
if strings.ToLower(pk) != pk {
return false
}
dec, _ := hex.DecodeString(pk)
return len(dec) == 32
}

func IsValidPublicKey(pk string) bool {
v, _ := hex.DecodeString(pk)
_, err := btcec.ParsePubKey(v)
return len(v) == 32 && err == nil
}
12 changes: 12 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nostr

import (
"encoding/hex"
"net/url"
"strings"
)
Expand All @@ -18,3 +19,14 @@ func IsValidRelayURL(u string) bool {
}
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
}

0 comments on commit 70f719e

Please sign in to comment.