common

package
v0.26.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2026 License: LGPL-3.0 Imports: 10 Imported by: 100

Documentation

Overview

Package common provides shared utilities for encoding, hashing, and data conversion.

Index

Constants

View Source
const (
	// DefaultConfigAccountAliasesDirName is the directory name for account key aliases.
	DefaultConfigAccountAliasesDirName = "account-keys"
	// DefaultPassphrase is the default passphrase used when none is provided.
	DefaultPassphrase = ""
	// Secp256k1PrivateKeyBytesLength is the required byte length of a secp256k1 private key.
	Secp256k1PrivateKeyBytesLength = 32
	// AmountDecimalPoint is the number of decimal places in TRX (1 TRX = 10^6 SUN).
	AmountDecimalPoint = 6
)
View Source
const (
	// HashLength is the expected length of the hash
	HashLength = 32
)

Variables

View Source
var (
	// DefaultConfigDirName is the default directory name for tronctl configuration.
	DefaultConfigDirName = ".tronctl"
	// DebugGRPC enables verbose gRPC request/response logging when true.
	//
	// Deprecated: Use DebugFromEnv() to obtain a DebugConfig instead.
	DebugGRPC = false
	// DebugTransaction enables verbose transaction logging when true.
	//
	// Deprecated: Use DebugFromEnv() to obtain a DebugConfig instead.
	DebugTransaction = false
	// ErrNotAbsPath is returned when a keypath is not an absolute path.
	ErrNotAbsPath = errors.New("keypath is not absolute path")
	// ErrBadKeyLength is returned when a private key has an invalid length.
	ErrBadKeyLength = errors.New("invalid private key (wrong length)")
	// ErrFoundNoPass is returned when no passphrase file is found.
	ErrFoundNoPass = errors.New("found no passphrase file")
)
View Source
var EmptyString = &hexError{"empty hex string"}

EmptyString is the error returned when decoding an empty hex string.

Functions

func Bytes2Hex

func Bytes2Hex(d []byte) string

Bytes2Hex returns the hexadecimal encoding of d.

func BytesToHexString

func BytesToHexString(bytes []byte) string

BytesToHexString encodes bytes as a hex string.

func CopyBytes

func CopyBytes(b []byte) (copiedBytes []byte)

CopyBytes returns an exact copy of the provided bytes.

func Decode

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

Decode returns the bytes represented by the Base58 string input.

func DecodeCheck

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

DecodeCheck decodes a Base58Check string, validates the checksum and TRON prefix (0x41), and returns the decoded bytes (prefix + 20-byte address payload) without the checksum.

func EnableAllVerbose

func EnableAllVerbose()

EnableAllVerbose enables all debug logging flags (gRPC and transaction).

func Encode

func Encode(input []byte) string

Encode returns the Base58 encoding of input using the Bitcoin alphabet.

func EncodeCheck

func EncodeCheck(input []byte) string

EncodeCheck returns the Base58Check encoding of input (Base58 with a 4-byte SHA-256d checksum).

func FromHex

func FromHex(s string) ([]byte, error)

FromHex returns the bytes represented by the hexadecimal string s. s may be prefixed with "0x".

func Has0xPrefix

func Has0xPrefix(str string) bool

Has0xPrefix validates str begins with '0x' or '0X'.

func Hex2Bytes

func Hex2Bytes(str string) ([]byte, error)

Hex2Bytes returns the bytes represented by the hexadecimal string str.

func Hex2BytesFixed

func Hex2BytesFixed(str string, flen int) []byte

Hex2BytesFixed returns bytes of a specified fixed length flen.

func HexStringToBytes

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

HexStringToBytes decodes a hex string (with or without "0x" prefix) into bytes.

func IsHex

func IsHex(str string) bool

IsHex validates whether each byte is valid hexadecimal string.

func JSONPrettyFormat

func JSONPrettyFormat(in string) string

JSONPrettyFormat returns the JSON string indented for readability.

func Keccak256

func Keccak256(msg []byte) []byte

Keccak256 returns the Keccak-256 hash of msg.

func LeftPadBytes

func LeftPadBytes(slice []byte, l int) []byte

LeftPadBytes zero-pads slice to the left up to length l.

func RightPadBytes

func RightPadBytes(slice []byte, l int) []byte

RightPadBytes zero-pads slice to the right up to length l.

func ToHex deprecated

func ToHex(b []byte) string

ToHex returns the hex representation of b, prefixed with '0x'. For empty slices, the return value is "0x0".

Deprecated: use BytesToHexString instead.

func ToHexArray

func ToHexArray(b [][]byte) []string

ToHexArray converts each byte slice in b to a "0x"-prefixed hex string.

func ToJSONUnsafe

func ToJSONUnsafe(payload interface{}, pretty bool) string

ToJSONUnsafe marshals payload to JSON, returning "{}" on failure.

func TrimLeftZeroes

func TrimLeftZeroes(s []byte) []byte

TrimLeftZeroes returns a subslice of s without leading zero bytes.

func ZeroBytes added in v0.24.3

func ZeroBytes(b []byte)

ZeroBytes overwrites every element of b with zero. Use this to wipe sensitive key material from memory.

Types

type DebugConfig added in v0.26.0

type DebugConfig struct {
	GRPC        bool
	Transaction bool
}

DebugConfig holds debug flags for the SDK.

func DebugFromEnv added in v0.26.0

func DebugFromEnv() DebugConfig

DebugFromEnv creates a DebugConfig from environment variables.

type Hash

type Hash [HashLength]byte

Hash represents the 32 byte Keccak256 hash of arbitrary data.

func BigToHash

func BigToHash(b *big.Int) Hash

BigToHash sets byte representation of b to hash. If b is larger than len(h), b will be cropped from the left.

func BytesToHash

func BytesToHash(b []byte) Hash

BytesToHash sets b to hash. If b is larger than len(h), b will be cropped from the left.

func HexToHash

func HexToHash(s string) (Hash, error)

HexToHash sets byte representation of s to hash. If b is larger than len(h), b will be cropped from the left.

func (Hash) Big

func (h Hash) Big() *big.Int

Big converts a hash to a big integer.

func (Hash) Bytes

func (h Hash) Bytes() []byte

Bytes gets the byte representation of the underlying hash.

func (Hash) Hex

func (h Hash) Hex() string

Hex converts a hash to a hex string.

func (*Hash) SetBytes

func (h *Hash) SetBytes(b []byte)

SetBytes sets the hash to the value of b. If b is larger than len(h), b will be cropped from the left.

func (Hash) String

func (h Hash) String() string

String implements the stringer interface and is used also by the logger when doing full logging into a file.

func (Hash) TerminalString

func (h Hash) TerminalString() string

TerminalString implements log.TerminalStringer, formatting a string for console output during logging.

Directories

Path Synopsis
Package decimals provides decimal formatting and conversion utilities.
Package decimals provides decimal formatting and conversion utilities.
Package numeric provides decimal arithmetic utilities.
Package numeric provides decimal arithmetic utilities.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL