github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/hex/hex.go (about)

     1  package hex
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"math/big"
     7  	"strconv"
     8  	"strings"
     9  )
    10  
    11  const (
    12  	// Base represents the hexadecimal base, which is 16
    13  	Base = 16
    14  
    15  	// BitSize64 64 bits
    16  	BitSize64 = 64
    17  )
    18  
    19  // DecError represents an error when decoding a hex value
    20  type DecError struct{ msg string }
    21  
    22  func (err DecError) Error() string { return err.msg }
    23  
    24  // EncodeToHex generates a hex string based on the byte representation, with the '0x' prefix
    25  func EncodeToHex(str []byte) string {
    26  	return "0x" + hex.EncodeToString(str)
    27  }
    28  
    29  // EncodeToString is a wrapper method for hex.EncodeToString
    30  func EncodeToString(str []byte) string {
    31  	return hex.EncodeToString(str)
    32  }
    33  
    34  // DecodeString returns the byte representation of the hexadecimal string
    35  func DecodeString(str string) ([]byte, error) {
    36  	return hex.DecodeString(str)
    37  }
    38  
    39  // DecodeHex converts a hex string to a byte array
    40  func DecodeHex(str string) ([]byte, error) {
    41  	str = strings.TrimPrefix(str, "0x")
    42  
    43  	return hex.DecodeString(str)
    44  }
    45  
    46  // MustDecodeHex type-checks and converts a hex string to a byte array
    47  func MustDecodeHex(str string) []byte {
    48  	buf, err := DecodeHex(str)
    49  	if err != nil {
    50  		panic(fmt.Errorf("could not decode hex: %v", err))
    51  	}
    52  
    53  	return buf
    54  }
    55  
    56  // DecodeUint64 type-checks and converts a hex string to a uint64
    57  func DecodeUint64(str string) uint64 {
    58  	i := DecodeBig(str)
    59  	return i.Uint64()
    60  }
    61  
    62  // EncodeUint64 encodes a number as a hex string with 0x prefix.
    63  func EncodeUint64(i uint64) string {
    64  	enc := make([]byte, 2, 10) //nolint:gomnd
    65  	copy(enc, "0x")
    66  	return string(strconv.AppendUint(enc, i, Base))
    67  }
    68  
    69  // BadNibble is a nibble that is bad
    70  const BadNibble = ^uint64(0)
    71  
    72  // DecodeNibble decodes a byte into a uint64
    73  func DecodeNibble(in byte) uint64 {
    74  	switch {
    75  	case in >= '0' && in <= '9':
    76  		return uint64(in - '0')
    77  	case in >= 'A' && in <= 'F':
    78  		return uint64(in - 'A' + 10) //nolint:gomnd
    79  	case in >= 'a' && in <= 'f':
    80  		return uint64(in - 'a' + 10) //nolint:gomnd
    81  	default:
    82  		return BadNibble
    83  	}
    84  }
    85  
    86  // EncodeBig encodes bigint as a hex string with 0x prefix.
    87  // The sign of the integer is ignored.
    88  func EncodeBig(bigint *big.Int) string {
    89  	numBits := bigint.BitLen()
    90  	if numBits == 0 {
    91  		return "0x0"
    92  	}
    93  
    94  	return fmt.Sprintf("%#x", bigint)
    95  }
    96  
    97  // DecodeBig converts a hex number to a big.Int value
    98  func DecodeBig(hexNum string) *big.Int {
    99  	str := strings.TrimPrefix(hexNum, "0x")
   100  	createdNum := new(big.Int)
   101  	createdNum.SetString(str, Base)
   102  
   103  	return createdNum
   104  }
   105  
   106  // IsValid checks if the provided string is a valid hexadecimal value
   107  func IsValid(s string) bool {
   108  	str := strings.TrimPrefix(s, "0x")
   109  	for _, b := range []byte(str) {
   110  		if !(b >= '0' && b <= '9' || b >= 'a' && b <= 'f' || b >= 'A' && b <= 'F') {
   111  			return false
   112  		}
   113  	}
   114  	return true
   115  }