go-ethereum: github.com/axiomzen/go-ethereum/common/hexutil Index | Examples | Files

package hexutil

import "github.com/axiomzen/go-ethereum/common/hexutil"

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

Examples

Package Files

hexutil.go json.go

Variables

var (
    ErrEmptyString   = &decError{"empty hex string"}
    ErrSyntax        = &decError{"invalid hex string"}
    ErrMissingPrefix = &decError{"hex string without 0x prefix"}
    ErrOddLength     = &decError{"hex string of odd length"}
    ErrEmptyNumber   = &decError{"hex string \"0x\""}
    ErrLeadingZero   = &decError{"hex number with leading zero digits"}
    ErrUint64Range   = &decError{"hex number > 64 bits"}
    ErrUintRange     = &decError{fmt.Sprintf("hex number > %d bits", uintBits)}
    ErrBig256Range   = &decError{"hex number > 256 bits"}
)

Errors

func Decode Uses

func Decode(input string) ([]byte, error)

Decode decodes a hex string with 0x prefix.

func DecodeBig Uses

func DecodeBig(input string) (*big.Int, error)

DecodeBig decodes a hex string with 0x prefix as a quantity. Numbers larger than 256 bits are not accepted.

func DecodeUint64 Uses

func DecodeUint64(input string) (uint64, error)

DecodeUint64 decodes a hex string with 0x prefix as a quantity.

func Encode Uses

func Encode(b []byte) string

Encode encodes b as a hex string with 0x prefix.

func EncodeBig Uses

func EncodeBig(bigint *big.Int) string

EncodeBig encodes bigint as a hex string with 0x prefix. The sign of the integer is ignored.

func EncodeUint64 Uses

func EncodeUint64(i uint64) string

EncodeUint64 encodes i as a hex string with 0x prefix.

func MustDecode Uses

func MustDecode(input string) []byte

MustDecode decodes a hex string with 0x prefix. It panics for invalid input.

func MustDecodeBig Uses

func MustDecodeBig(input string) *big.Int

MustDecodeBig decodes a hex string with 0x prefix as a quantity. It panics for invalid input.

func MustDecodeUint64 Uses

func MustDecodeUint64(input string) uint64

MustDecodeUint64 decodes a hex string with 0x prefix as a quantity. It panics for invalid input.

func UnmarshalFixedJSON Uses

func UnmarshalFixedJSON(typ reflect.Type, input, out []byte) error

UnmarshalFixedJSON decodes the input as a 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.

func UnmarshalFixedText Uses

func UnmarshalFixedText(typname string, input, out []byte) error

UnmarshalFixedText decodes the input as a string with 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalText method for fixed-size types.

Code:

package main

import (
    "encoding/json"
    "fmt"

    "github.com/ethereum/go-ethereum/common/hexutil"
)

type MyType [5]byte

func (v *MyType) UnmarshalText(input []byte) error {
    return hexutil.UnmarshalFixedText("MyType", input, v[:])
}

func (v MyType) String() string {
    return hexutil.Bytes(v[:]).String()
}

func main() {
    var v1, v2 MyType
    fmt.Println("v1 error:", json.Unmarshal([]byte(`"0x01"`), &v1))
    fmt.Println("v2 error:", json.Unmarshal([]byte(`"0x0101010101"`), &v2))
    fmt.Println("v2:", v2)
}

func UnmarshalFixedUnprefixedText Uses

func UnmarshalFixedUnprefixedText(typname string, input, out []byte) error

UnmarshalFixedUnprefixedText decodes the input as a string with optional 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalText method for fixed-size types.

type Big Uses

type Big big.Int

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. Values larger than 256bits are rejected by Unmarshal but will be marshaled without error.

func (Big) ImplementsGraphQLType Uses

func (b Big) ImplementsGraphQLType(name string) bool

ImplementsGraphQLType returns true if Big implements the provided GraphQL type.

func (Big) MarshalText Uses

func (b Big) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler

func (*Big) String Uses

func (b *Big) String() string

String returns the hex encoding of b.

func (*Big) ToInt Uses

func (b *Big) ToInt() *big.Int

ToInt converts b to a big.Int.

func (*Big) UnmarshalGraphQL Uses

func (b *Big) UnmarshalGraphQL(input interface{}) error

UnmarshalGraphQL unmarshals the provided GraphQL query data.

func (*Big) UnmarshalJSON Uses

func (b *Big) UnmarshalJSON(input []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Big) UnmarshalText Uses

func (b *Big) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler

type Bytes Uses

type Bytes []byte

Bytes marshals/unmarshals as a JSON string with 0x prefix. The empty slice marshals as "0x".

func (Bytes) ImplementsGraphQLType Uses

func (b Bytes) ImplementsGraphQLType(name string) bool

ImplementsGraphQLType returns true if Bytes implements the specified GraphQL type.

func (Bytes) MarshalText Uses

func (b Bytes) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler

func (Bytes) String Uses

func (b Bytes) String() string

String returns the hex encoding of b.

func (*Bytes) UnmarshalGraphQL Uses

func (b *Bytes) UnmarshalGraphQL(input interface{}) error

UnmarshalGraphQL unmarshals the provided GraphQL query data.

func (*Bytes) UnmarshalJSON Uses

func (b *Bytes) UnmarshalJSON(input []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Bytes) UnmarshalText Uses

func (b *Bytes) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Uint Uses

type Uint uint

Uint marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".

func (Uint) MarshalText Uses

func (b Uint) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Uint) String Uses

func (b Uint) String() string

String returns the hex encoding of b.

func (*Uint) UnmarshalJSON Uses

func (b *Uint) UnmarshalJSON(input []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Uint) UnmarshalText Uses

func (b *Uint) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Uint64 Uses

type Uint64 uint64

Uint64 marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".

func (Uint64) ImplementsGraphQLType Uses

func (b Uint64) ImplementsGraphQLType(name string) bool

ImplementsGraphQLType returns true if Uint64 implements the provided GraphQL type.

func (Uint64) MarshalText Uses

func (b Uint64) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Uint64) String Uses

func (b Uint64) String() string

String returns the hex encoding of b.

func (*Uint64) UnmarshalGraphQL Uses

func (b *Uint64) UnmarshalGraphQL(input interface{}) error

UnmarshalGraphQL unmarshals the provided GraphQL query data.

func (*Uint64) UnmarshalJSON Uses

func (b *Uint64) UnmarshalJSON(input []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Uint64) UnmarshalText Uses

func (b *Uint64) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler

Package hexutil imports 6 packages (graph). Updated 2019-07-06. Refresh now. Tools for package owners.