transaction

package
v0.25.2 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: LGPL-3.0 Imports: 19 Imported by: 7

Documentation

Overview

Package transaction provides transaction signing and submission for TRON.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilTransaction      = errors.New("transaction is nil")
	ErrNoContracts         = errors.New("transaction has no contracts")
	ErrNilParameter        = errors.New("contract parameter is nil")
	ErrUnsupportedContract = errors.New("unsupported contract type")
	ErrUnmarshalContract   = errors.New("failed to unmarshal contract parameter")
)
View Source
var (
	// ErrEmptyRawData is returned when a raw data hex string is empty.
	ErrEmptyRawData = errors.New("raw data hex string is empty")
	// ErrInvalidHex is returned when a hex string cannot be decoded.
	ErrInvalidHex = errors.New("invalid hex string")
	// ErrInvalidTxJSON is returned when transaction JSON cannot be parsed.
	ErrInvalidTxJSON = errors.New("invalid transaction JSON")
	// ErrTxIDMismatch is returned when the txID does not match the hash of raw_data.
	ErrTxIDMismatch = errors.New("txID does not match hash of raw_data")
	// ErrNilRawData is returned when a transaction has nil raw_data.
	ErrNilRawData = errors.New("transaction raw_data is nil")
)
View Source
var (
	// ErrBadTransactionParam is returned when invalid params are given to the
	// controller upon execution of a transaction.
	ErrBadTransactionParam = errors.New("transaction has bad parameters")
)

Functions

func FromJSON added in v0.24.3

func FromJSON(jsonData []byte) (*core.Transaction, error)

FromJSON reconstructs a Transaction from a JSON representation as returned by TRON HTTP API endpoints (e.g., /wallet/createtransaction).

Deserialization uses the raw_data_hex field exclusively for reliable protobuf reconstruction. The raw_data JSON object is ignored because it has encoding differences (hex vs base64 bytes) that make direct JSON-to-proto mapping unreliable.

If a txID is present in the JSON, it is validated against the SHA256 hash of the deserialized raw_data. Signatures are decoded from hex strings and attached to the transaction.

Example:

resp, _ := http.Post("https://api.trongrid.io/wallet/createtransaction", ...)
body, _ := io.ReadAll(resp.Body)
tx, err := transaction.FromJSON(body)

func FromRawDataHex added in v0.24.3

func FromRawDataHex(rawDataHex string, signatures ...[]byte) (*core.Transaction, error)

FromRawDataHex reconstructs a Transaction from a hex-encoded raw_data protobuf. This is the format commonly returned by TRON HTTP APIs in the raw_data_hex field.

The hex string may optionally have a "0x" prefix, which is stripped before decoding. Any provided signatures are attached to the resulting transaction, preserving their order.

Example:

tx, err := transaction.FromRawDataHex(apiResponse.RawDataHex)
if err != nil { ... }
tx, err = transaction.SignTransaction(tx, privateKey)

func SignTransaction added in v0.24.1

func SignTransaction(tx *core.Transaction, signer *btcec.PrivateKey) (*core.Transaction, error)

SignTransaction signs a transaction using a btcec private key.

func SignTransactionECDSA added in v0.24.1

func SignTransactionECDSA(tx *core.Transaction, signer *ecdsa.PrivateKey) (*core.Transaction, error)

SignTransactionECDSA signs a transaction using an ECDSA private key.

func ToJSON added in v0.24.3

func ToJSON(tx *core.Transaction) ([]byte, error)

ToJSON serializes a Transaction to a JSON envelope containing txID, raw_data_hex, raw_data, and signature fields.

The txID is computed as the SHA256 hash of the protobuf-encoded raw_data. The raw_data_hex field contains the canonical protobuf serialization and is suitable for use with FromJSON or FromRawDataHex. Signatures are encoded as lowercase hex strings.

Note: the raw_data JSON object uses protojson encoding, which differs from the TRON HTTP API format (protojson uses base64 for bytes and @type for Any fields, while TRON uses hex strings and type_url/value). The raw_data field is included for human readability; use raw_data_hex for programmatic deserialization.

func ToRawDataHex added in v0.24.3

func ToRawDataHex(tx *core.Transaction) (string, error)

ToRawDataHex returns the hex-encoded protobuf serialization of the transaction's raw_data. The output is lowercase hex without a "0x" prefix, matching the format used by TRON HTTP APIs.

func WithPermissionID added in v0.24.3

func WithPermissionID(id int32) func(*Controller)

WithPermissionID sets the permission ID for multi-signature transactions. PermissionID = 0 is the owner permission (default), PermissionID = 2 is commonly used for active permissions in multi-sig setups.

Types

type ContractData added in v0.25.2

type ContractData struct {
	Type   string         // e.g. "TransferContract", "TriggerSmartContract"
	Fields map[string]any // decoded fields with base58 addresses and converted amounts
}

ContractData holds decoded contract parameters with human-readable values.

func DecodeContractData added in v0.25.2

func DecodeContractData(tx *core.Transaction) (*ContractData, error)

DecodeContractData decodes the first contract parameter from a transaction into a ContractData struct with base58 addresses and human-readable amounts.

type Controller

type Controller struct {
	Behavior behavior
	Result   *api.Return
	Receipt  *core.TransactionInfo
	// contains filtered or unexported fields
}

Controller drives the transaction signing process

func NewController

func NewController(
	client *client.GrpcClient,
	senderKs *keystore.KeyStore,
	senderAcct *keystore.Account,
	tx *core.Transaction,
	options ...func(*Controller),
) *Controller

NewController initializes a Controller, caller can control behavior via options

func (*Controller) ExecuteTransaction

func (C *Controller) ExecuteTransaction() error

ExecuteTransaction is the single entrypoint to execute a plain transaction. Each step in transaction creation, execution probably includes a mutation Each becomes a no-op if executionError occurred in any previous step

func (*Controller) GetRawData

func (C *Controller) GetRawData() ([]byte, error)

GetRawData Byes from Transaction

func (*Controller) GetResultError

func (C *Controller) GetResultError() error

GetResultError return result error

func (*Controller) TransactionHash

func (C *Controller) TransactionHash() (string, error)

TransactionHash extract hash from TX

type SignerImpl

type SignerImpl int

SignerImpl identifies the signing backend used for transactions.

const (
	// Software signs transactions using an in-process private key.
	Software SignerImpl = iota
	// Ledger signs transactions using a connected Ledger hardware wallet.
	Ledger
)

Jump to

Keyboard shortcuts

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