Skip to content

Commit

Permalink
dropped String.ToBytes String.MustToBytes FromBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Oct 5, 2024
1 parent b09b2f1 commit 2722ac1
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 69 deletions.
4 changes: 2 additions & 2 deletions mod/primitives/pkg/bytes/b.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Bytes []byte

// MarshalText implements encoding.TextMarshaler.
func (b Bytes) MarshalText() ([]byte, error) {
return hex.EncodeBytes(b), nil
return []byte(hex.EncodeBytes(b)), nil
}

// UnmarshalJSON implements json.Unmarshaler.
Expand All @@ -51,5 +51,5 @@ func (b *Bytes) UnmarshalText(input []byte) error {

// String returns the hex encoding of b.
func (b Bytes) String() string {
return hex.FromBytes(b).Unwrap()
return hex.EncodeBytes(b)
}
2 changes: 1 addition & 1 deletion mod/primitives/pkg/bytes/b20.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (h *B20) UnmarshalText(text []byte) error {

// String returns the hex string representation of B20.
func (h *B20) String() string {
return hex.FromBytes(h[:]).Unwrap()
return hex.EncodeBytes(h[:])
}

/* -------------------------------------------------------------------------- */
Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/bytes/b256.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (h *B256) UnmarshalText(text []byte) error {

// String returns the hex string representation of B256.
func (h *B256) String() string {
return hex.FromBytes(h[:]).Unwrap()
return hex.EncodeBytes(h[:])
}

/* -------------------------------------------------------------------------- */
Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/bytes/b32.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (h *B32) UnmarshalText(text []byte) error {

// String returns the hex string representation of B32.
func (h B32) String() string {
return hex.FromBytes(h[:]).Unwrap()
return hex.EncodeBytes(h[:])
}

/* -------------------------------------------------------------------------- */
Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/bytes/b4.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (h *B4) UnmarshalText(text []byte) error {

// String returns the hex string representation of B4.
func (h B4) String() string {
return hex.FromBytes(h[:]).Unwrap()
return hex.EncodeBytes(h[:])
}

/* -------------------------------------------------------------------------- */
Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/bytes/b48.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (h *B48) UnmarshalText(text []byte) error {

// String returns the hex string representation of B48.
func (h B48) String() string {
return hex.FromBytes(h[:]).Unwrap()
return hex.EncodeBytes(h[:])
}

/* -------------------------------------------------------------------------- */
Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/bytes/b8.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (h *B8) UnmarshalText(text []byte) error {

// String returns the hex string representation of B8.
func (h B8) String() string {
return hex.FromBytes(h[:]).Unwrap()
return hex.EncodeBytes(h[:])
}

/* -------------------------------------------------------------------------- */
Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/bytes/b96.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (h *B96) UnmarshalText(text []byte) error {

// String returns the hex string representation of B96.
func (h *B96) String() string {
return hex.FromBytes(h[:]).Unwrap()
return hex.EncodeBytes(h[:])
}

/* -------------------------------------------------------------------------- */
Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/common/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewRootFromBytes(input []byte) Root {
}

// Hex converts a root to a hex string.
func (r Root) Hex() string { return hex.FromBytes(r[:]).Unwrap() }
func (r Root) Hex() string { return hex.EncodeBytes(r[:]) }

// String implements the stringer interface and is used also by the logger when
// doing full logging into a file.
Expand Down
6 changes: 3 additions & 3 deletions mod/primitives/pkg/common/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewExecutionHashFromHex(input string) ExecutionHash {
}

// Hex converts a hash to a hex string.
func (h ExecutionHash) Hex() string { return hex.FromBytes(h[:]).Unwrap() }
func (h ExecutionHash) Hex() string { return hex.EncodeBytes(h[:]) }

// String implements the stringer interface and is used also by the logger when
// doing full logging into a file.
Expand All @@ -65,7 +65,7 @@ func (h ExecutionHash) String() string {

// MarshalText returns the hex representation of h.
func (h ExecutionHash) MarshalText() ([]byte, error) {
return hex.EncodeBytes(h[:]), nil
return []byte(hex.EncodeBytes(h[:])), nil
}

// UnmarshalText parses a hash in hex syntax.
Expand Down Expand Up @@ -128,7 +128,7 @@ func (a *ExecutionAddress) UnmarshalJSON(input []byte) error {

// checksumHex returns the checksummed hex representation of a.
func (a *ExecutionAddress) checksumHex() []byte {
buf := hex.EncodeBytes(a[:])
buf := []byte(hex.EncodeBytes(a[:]))

// compute checksum
sha := sha3.NewLegacyKeccak256()
Expand Down
30 changes: 15 additions & 15 deletions mod/primitives/pkg/encoding/hex/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,21 @@ import (

var ErrInvalidHexStringLength = errors.New("invalid hex string length")

func EncodeBytes[B ~[]byte](b B) []byte {
result := make([]byte, len(b)*2+prefixLen)
copy(result, prefix)
hex.Encode(result[prefixLen:], b)
return result
func EncodeBytes[B ~[]byte](b B) string {
hexStr := make([]byte, len(b)*2+prefixLen)
copy(hexStr, prefix)
hex.Encode(hexStr[prefixLen:], b)
return string(hexStr)
}

// ToBytes returns the bytes represented by the given hex string.
// An error is returned if the input is not a valid hex string.
func ToBytes(hexStr string) ([]byte, error) {
strippedInput, err := IsValidHex(hexStr)
if err != nil {
return nil, err
}
return hex.DecodeString(strippedInput)
}

func UnmarshalByteText(input []byte) ([]byte, error) {
Expand Down Expand Up @@ -93,13 +103,3 @@ func MustToBytes(input string) []byte {
}
return bz
}

// ToBytes returns the bytes represented by the given hex string.
// An error is returned if the input is not a valid hex string.
func ToBytes(input string) ([]byte, error) {
strippedInput, err := IsValidHex(input)
if err != nil {
return nil, err
}
return hex.DecodeString(strippedInput)
}
10 changes: 5 additions & 5 deletions mod/primitives/pkg/encoding/hex/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,29 @@ func TestEncodeBytes(t *testing.T) {
tests := []struct {
name string
input []byte
expected []byte
expected string
}{
{
name: "typical byte slice",
input: []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f},
expected: []byte("0x48656c6c6f"),
expected: "0x48656c6c6f",
},
{
name: "empty byte slice",
input: []byte{},
expected: []byte("0x"),
expected: "0x",
},
{
name: "single byte",
input: []byte{0x01},
expected: []byte("0x01"),
expected: "0x01",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := hex.EncodeBytes(tt.input)
require.Equal(t, tt.expected, result, "Test case : %s", tt.name)
require.Equal(t, tt.expected, result)
})
}
}
Expand Down
13 changes: 5 additions & 8 deletions mod/primitives/pkg/encoding/hex/hex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,10 @@ func TestFromBytes(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := hex.FromBytes(tt.input)
require.Equal(t, tt.expected, result.Unwrap())
result := hex.EncodeBytes(tt.input)
require.Equal(t, tt.expected, result)

_, err := hex.IsValidHex(result)
require.NoError(t, err)

decoded, err := result.ToBytes()
decoded, err := hex.ToBytes(result)
require.NoError(t, err)
require.Equal(t, tt.input, decoded)
})
Expand Down Expand Up @@ -307,7 +304,7 @@ func TestUnmarshalJSONText(t *testing.T) {
func TestString_MustToBytes(t *testing.T) {
tests := []struct {
name string
input hex.String
input string
expected []byte
panics bool
}{
Expand All @@ -321,7 +318,7 @@ func TestString_MustToBytes(t *testing.T) {
var (
res []byte
f = func() {
res = tt.input.MustToBytes()
res = hex.MustToBytes(tt.input)
}
)
if tt.panics {
Expand Down
22 changes: 0 additions & 22 deletions mod/primitives/pkg/encoding/hex/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package hex

import (
"bytes"
"encoding/hex"
"math/big"
"strconv"
"strings"
Expand Down Expand Up @@ -77,12 +76,6 @@ func NewString[T []byte | string](s T) String {
return String(str)
}

// FromBytes creates a hex string with 0x prefix.
func FromBytes[B ~[]byte](input B) String {
b := EncodeBytes(input)
return NewString(b)
}

// FromUint64 encodes i as a hex string with 0x prefix.
func FromUint64[U ~uint64](i U) String {
enc := make([]byte, prefixLen, initialCapacity)
Expand All @@ -107,21 +100,6 @@ func FromJSONString[B ~[]byte](b B) String {
return NewString(bytes.Trim(b, "\""))
}

// ToBytes decodes a hex string with 0x prefix.
func (s String) ToBytes() ([]byte, error) {
return hex.DecodeString(string(s[prefixLen:]))
}

// MustToBytes decodes a hex string with 0x prefix.
// It panics for invalid input.
func (s String) MustToBytes() []byte {
b, err := s.ToBytes()
if err != nil {
panic(err)
}
return b
}

// ToUint64 decodes a hex string with 0x prefix.
func (s String) ToUint64() (uint64, error) {
raw, err := formatAndValidateNumber(s.Unwrap())
Expand Down
6 changes: 3 additions & 3 deletions mod/primitives/pkg/net/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewRandom() (*Secret, error) {
if err != nil {
return nil, err
}
return NewFromHex(hex.FromBytes(secret).Unwrap())
return NewFromHex(hex.EncodeBytes(secret))
}

// BuildSignedToken creates a signed JWT token from the secret.
Expand All @@ -90,13 +90,13 @@ func (s *Secret) BuildSignedToken() (string, error) {
// String returns the JWT secret as a string with the first 8 characters
// visible and the rest masked out for security.
func (s *Secret) String() string {
secret := hex.FromBytes(s[:]).Unwrap()
secret := hex.EncodeBytes(s.Bytes())
return secret[:8] + strings.Repeat("*", len(secret[8:]))
}

// Hex returns the JWT secret as a hexadecimal string.
func (s *Secret) Hex() string {
return hex.FromBytes(s[:]).Unwrap()
return hex.EncodeBytes(s.Bytes())
}

// Bytes returns the JWT secret as a byte array.
Expand Down
4 changes: 2 additions & 2 deletions mod/primitives/pkg/net/jwt/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ func TestSecretRoundTripEncoding(t *testing.T) {
require.NoError(t, err, "NewRandom() error")

// Encode the original secret to hex string
encodedSecret := hex.FromBytes(originalSecret.Bytes())
encodedSecret := hex.EncodeBytes((originalSecret.Bytes()))

// Decode the hex string back to secret
decodedSecret, err := jwt.NewFromHex(encodedSecret.Unwrap())
decodedSecret, err := jwt.NewFromHex(encodedSecret)
require.NoError(t, err, "NewFromHex() error")

// Compare the original and decoded secrets
Expand Down
2 changes: 1 addition & 1 deletion mod/storage/pkg/filedb/range_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (db *RangeDB) Prune(start, end uint64) error {

// prefix prefixes the given key with the index and a slash.
func (db *RangeDB) prefix(index uint64, key []byte) []byte {
return []byte(fmt.Sprintf("%d/%s", index, hex.FromBytes(key).Unwrap()))
return []byte(fmt.Sprintf("%d/%s", index, hex.EncodeBytes(key)))
}

// ExtractIndex extracts the index from a prefixed key.
Expand Down

0 comments on commit 2722ac1

Please sign in to comment.