small fixes in hex parsing.

This commit is contained in:
fiatjaf
2025-10-24 00:12:25 -03:00
parent dd097470d2
commit 588e415440
2 changed files with 10 additions and 4 deletions

10
keys.go
View File

@@ -50,7 +50,9 @@ func SecretKeyFromHex(skh string) (SecretKey, error) {
func MustSecretKeyFromHex(idh string) SecretKey {
id := SecretKey{}
hex.Decode(id[:], unsafe.Slice(unsafe.StringData(idh), 64))
if _, err := hex.Decode(id[:], unsafe.Slice(unsafe.StringData(idh), 64)); err != nil {
panic(err)
}
return id
}
@@ -77,11 +79,13 @@ func (pk *PubKey) UnmarshalJSON(buf []byte) error {
if len(buf) != 66 {
return fmt.Errorf("must be a hex string of 64 characters")
}
_, err := hex.Decode(pk[:], buf[1:65])
if _, err := hex.Decode(pk[:], buf[1:65]); err != nil {
return err
}
if _, err := schnorr.ParsePubKey(pk[:]); err != nil {
return fmt.Errorf("pubkey is not valid %w", err)
}
return err
return nil
}
func PubKeyFromHex(pkh string) (PubKey, error) {

View File

@@ -51,7 +51,9 @@ func IDFromHex(idh string) (ID, error) {
func MustIDFromHex(idh string) ID {
id := ID{}
hex.Decode(id[:], unsafe.Slice(unsafe.StringData(idh), 64))
if _, err := hex.Decode(id[:], unsafe.Slice(unsafe.StringData(idh), 64)); err != nil {
panic(err)
}
return id
}