Documentation ¶
Overview ¶
Package hexutil implements hex encoding with 0x prefix. This encoding is used by the Ethereum RPC API to transport binary data in JSON payloads.
Encoding Rules ¶
All hex data must have prefix "0x".
For byte slices, the hex data must be of even length. An empty byte slice encodes as "0x".
Integers are encoded using the least amount of digits (no leading zero digits). Their encoding may be of uneven length. The number zero encodes as "0x0".
Index ¶
- Variables
- func Decode(input string) ([]byte, error)
- func DecodeBig(input string) (*big.Int, error)
- func DecodeUint64(input string) (uint64, error)
- func Encode(b []byte) string
- func EncodeBig(bigint *big.Int) string
- func EncodeUint64(i uint64) string
- func MustDecode(input string) []byte
- func MustDecodeBig(input string) *big.Int
- func MustDecodeUint64(input string) uint64
- func UnmarshalJSON(typname string, input, out []byte) error
- type Big
- type Bytes
- type Uint
- type Uint64
Constants ¶
This section is empty.
Variables ¶
var ( ErrEmptyString = errors.New("empty hex string") ErrMissingPrefix = errors.New("missing 0x prefix for hex data") ErrSyntax = errors.New("invalid hex") ErrEmptyNumber = errors.New("hex number has no digits after 0x") ErrLeadingZero = errors.New("hex number has leading zero digits after 0x") ErrOddLength = errors.New("hex string has odd length") ErrUint64Range = errors.New("hex number does not fit into 64 bits") ErrUintRange = fmt.Errorf("hex number does not fit into %d bits", uintBits) )
Functions ¶
func DecodeUint64 ¶
DecodeUint64 decodes a hex string with 0x prefix as a quantity.
func EncodeBig ¶
EncodeBig encodes bigint as a hex string with 0x prefix. The sign of the integer is ignored.
func EncodeUint64 ¶
EncodeUint64 encodes i as a hex string with 0x prefix.
func MustDecode ¶
MustDecode decodes a hex string with 0x prefix. It panics for invalid input.
func MustDecodeBig ¶
MustDecodeBig decodes a hex string with 0x prefix as a quantity. It panics for invalid input.
func MustDecodeUint64 ¶
MustDecodeUint64 decodes a hex string with 0x prefix as a quantity. It panics for invalid input.
func UnmarshalJSON ¶ added in v1.5.4
UnmarshalJSON decodes input as a JSON string with 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalJSON method for fixed-size types:
type Foo [8]byte func (f *Foo) UnmarshalJSON(input []byte) error { return hexutil.UnmarshalJSON("Foo", input, f[:]) }
Types ¶
type Big ¶
Big marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0". Negative integers are not supported at this time. Attempting to marshal them will return an error.
func (*Big) MarshalJSON ¶ added in v1.5.4
MarshalJSON implements json.Marshaler.
func (*Big) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type Bytes ¶
type Bytes []byte
Bytes marshals/unmarshals as a JSON string with 0x prefix. The empty slice marshals as "0x".
func (Bytes) MarshalJSON ¶ added in v1.5.4
MarshalJSON implements json.Marshaler.
func (*Bytes) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type Uint ¶
type Uint uint
Uint marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".
func (Uint) MarshalJSON ¶ added in v1.5.4
MarshalJSON implements json.Marshaler.
func (*Uint) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type Uint64 ¶
type Uint64 uint64
Uint64 marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".
func (Uint64) MarshalJSON ¶ added in v1.5.4
MarshalJSON implements json.Marshaler.
func (*Uint64) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.