use xhex everywhere.

This commit is contained in:
fiatjaf
2025-11-21 21:16:34 -03:00
parent 61b9717c5c
commit 55a43e46b7
46 changed files with 185 additions and 177 deletions
+8 -7
View File
@@ -1,10 +1,11 @@
package nostr
import (
"encoding/hex"
stdjson "encoding/json"
"fmt"
"unsafe"
"github.com/templexxx/xhex"
)
// RelayEvent represents an event received from a specific relay.
@@ -24,11 +25,11 @@ var (
)
func (id ID) String() string { return "id::" + id.Hex() }
func (id ID) Hex() string { return hex.EncodeToString(id[:]) }
func (id ID) Hex() string { return HexEncodeToString(id[:]) }
func (id ID) MarshalJSON() ([]byte, error) {
res := make([]byte, 66)
hex.Encode(res[1:], id[:])
xhex.Encode(res[1:], id[:])
res[0] = '"'
res[65] = '"'
return res, nil
@@ -36,9 +37,9 @@ func (id ID) MarshalJSON() ([]byte, error) {
func (id *ID) UnmarshalJSON(buf []byte) error {
if len(buf) != 66 {
return fmt.Errorf("must be a hex string of 64 characters")
return fmt.Errorf("must be a quoted hex string of 64 characters")
}
_, err := hex.Decode(id[:], buf[1:65])
err := xhex.Decode(id[:], buf[1:65])
return err
}
@@ -48,7 +49,7 @@ func IDFromHex(idh string) (ID, error) {
if len(idh) != 64 {
return id, fmt.Errorf("pubkey should be 64-char hex, got '%s'", idh)
}
if _, err := hex.Decode(id[:], unsafe.Slice(unsafe.StringData(idh), 64)); err != nil {
if err := xhex.Decode(id[:], unsafe.Slice(unsafe.StringData(idh), 64)); err != nil {
return id, fmt.Errorf("'%s' is not valid hex: %w", idh, err)
}
@@ -57,7 +58,7 @@ func IDFromHex(idh string) (ID, error) {
func MustIDFromHex(idh string) ID {
id := ID{}
if _, err := hex.Decode(id[:], unsafe.Slice(unsafe.StringData(idh), 64)); err != nil {
if err := xhex.Decode(id[:], unsafe.Slice(unsafe.StringData(idh), 64)); err != nil {
panic(err)
}
return id