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

     1  package encoding
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/0xPolygon/supernets2-node/hex"
    10  	"github.com/ethereum/go-ethereum/common"
    11  )
    12  
    13  const (
    14  	// Base10 decimal base
    15  	Base10 = 10
    16  	// TenToThePowerOf18 represents 1000000000000000000
    17  	TenToThePowerOf18 = 1000000000000000000
    18  	// Gwei represents 1000000000 wei
    19  	Gwei = 1000000000
    20  	// MaxUint256StrNumber represents 2**256 -1 as string
    21  	MaxUint256StrNumber = "115792089237316195423570985008687907853269984665640564039457584007913129639935"
    22  )
    23  
    24  // DecodeUint64orHex decodes a string uint64 or hex string into a uint64
    25  func DecodeUint64orHex(val *string) (uint64, error) {
    26  	if val == nil {
    27  		return 0, nil
    28  	}
    29  
    30  	str := *val
    31  	base := 10
    32  	if strings.HasPrefix(str, "0x") {
    33  		str = str[2:]
    34  		base = 16
    35  	}
    36  	return strconv.ParseUint(str, base, hex.BitSize64)
    37  }
    38  
    39  // DecodeUint256orHex decodes a string uint256 or hex string into a bit.Int
    40  func DecodeUint256orHex(val *string) (*big.Int, error) {
    41  	if val == nil {
    42  		return nil, nil
    43  	}
    44  
    45  	str := *val
    46  	base := 10
    47  	if strings.HasPrefix(str, "0x") {
    48  		str = str[2:]
    49  		base = 16
    50  	}
    51  	b, ok := new(big.Int).SetString(str, base)
    52  	if !ok {
    53  		return nil, fmt.Errorf("could not parse")
    54  	}
    55  	return b, nil
    56  }
    57  
    58  // DecodeInt64orHex decodes a string int64 or hex string into a int64
    59  func DecodeInt64orHex(val *string) (int64, error) {
    60  	i, err := DecodeUint64orHex(val)
    61  	return int64(i), err
    62  }
    63  
    64  // DecodeBytes decodes a hex string into a []byte
    65  func DecodeBytes(val *string) ([]byte, error) {
    66  	if val == nil {
    67  		return []byte{}, nil
    68  	}
    69  
    70  	str := strings.TrimPrefix(*val, "0x")
    71  
    72  	return hex.DecodeString(str)
    73  }
    74  
    75  // EncodeUint64 encodes a uint64 into a hex string
    76  func EncodeUint64(b uint64) *string {
    77  	res := fmt.Sprintf("0x%x", b)
    78  	return &res
    79  }
    80  
    81  // EncodeBytes encodes a []bytes into a hex string
    82  func EncodeBytes(b []byte) *string {
    83  	res := "0x" + hex.EncodeToString(b)
    84  	return &res
    85  }
    86  
    87  // EncodeBigInt encodes a big.Int into a hex string
    88  func EncodeBigInt(b *big.Int) *string {
    89  	res := "0x" + b.Text(hex.Base)
    90  	return &res
    91  }
    92  
    93  // DecodeBigIntHexOrDecimal parses a string that can be decimal or hexa (starts with 0x)
    94  // into a *big.Int
    95  func DecodeBigIntHexOrDecimal(s string) (*big.Int, error) {
    96  	var r *big.Int
    97  	if strings.HasPrefix(s, "0x") { // nolint
    98  		// Value in hex format
    99  		s = s[2:]
   100  		r = new(big.Int).SetBytes(common.Hex2Bytes(s))
   101  	} else {
   102  		// Value in decimal format
   103  		value, ok := new(big.Int).SetString(s, Base10)
   104  		if !ok {
   105  			return nil, fmt.Errorf("Could not set base10 %q to big.Int", s)
   106  		}
   107  		r = value
   108  	}
   109  	return r, nil
   110  }