ethtypes

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2025 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Overview

The ethtypes library provides a collection of common Ethereum types implemented in Go. These types are essential for building various tools and modules within the Ethereum ecosystem. The library aims to standardize and simplify the development process by offering a consistent set of types that can be reused across different projects.

Usage

To use ethtypes in your project, simply pull it in with:

go get -u github.com/sig-0/ethtypes

To view examples on using the library, please refer to the relevant Go doc.

Documentation

Overview

Package ethtypes provides a collection of common Ethereum types implemented in Go. These types are essential for building various tools and modules within the Ethereum ecosystem. The library aims to standardize and simplify the development process by offering a consistent set of types that can be reused across different projects.

Index

Constants

View Source
const (
	TransactionLegacy = 0x00
	Transaction2930   = 0x01
	Transaction1559   = 0x02
)

Variables

View Source
var (
	ErrInvalidRLPChainID        = errors.New("invalid chain ID RLP encoding")
	ErrInvalidRLPMaxPriorityFee = errors.New("invalid max priority fee per gas RLP encoding")
	ErrInvalidRLPMaxFeePerGas   = errors.New("invalid max fee per gas RLP encoding")
	ErrInvalidRLPAccessList     = errors.New("invalid access list RLP encoding")
	ErrInvalidRLPYParity        = errors.New("invalid y parity param RLP encoding")
)
View Source
var (
	ErrInvalidRLPNonce    = errors.New("invalid nonce RLP encoding")
	ErrInvalidRLPGasPrice = errors.New("invalid gas price RLP encoding")
	ErrInvalidRLPGasLimit = errors.New("invalid gas limit RLP encoding")
	ErrInvalidRLPValue    = errors.New("invalid value RLP encoding")
	ErrInvalidRLPData     = errors.New("invalid data RLP encoding")
	ErrInvalidRLPVParam   = errors.New("invalid v sig param RLP encoding")
	ErrInvalidRLPRParam   = errors.New("invalid r sig param RLP encoding")
	ErrInvalidRLPSParam   = errors.New("invalid s sig param RLP encoding")
)
View Source
var ZeroAddress = Address{}

ZeroAddress represents a placeholder address value that contains all zero bytes

View Source
var ZeroCodeHash = BytesToHash(keccak.New().Hash([]byte{}))
View Source
var ZeroHash = Hash{}

ZeroHash represents a placeholder hash value that contains all zero bytes

Functions

This section is empty.

Types

type AccessList added in v0.2.0

type AccessList []AccessListEntry

AccessList is a wrapper for access list entries

func (*AccessList) FromProto added in v0.2.0

func (l *AccessList) FromProto(protoList []*txProto.AccessListEntry)

FromProto parses the access list from the proto representation

func (*AccessList) FromRLP added in v0.2.0

func (l *AccessList) FromRLP(data []byte) error

FromRLP parses the RLP encoding of an access list

func (*AccessList) FromRLPValues added in v0.2.0

func (l *AccessList) FromRLPValues(listValues []ethrlp.Value) error

FromRLPValues parses the raw RLP values of an access list

func (AccessList) ToProto added in v0.2.0

func (l AccessList) ToProto() []*txProto.AccessListEntry

ToProto converts the access list to a proto representation

func (AccessList) ToRLP added in v0.2.0

func (l AccessList) ToRLP() []byte

ToRLP encodes the access list to RLP (RLP list)

type AccessListEntry

type AccessListEntry struct {
	StorageKeys []Hash
	Address     Address
}

AccessListEntry is a single access list entry, denoting the accessed storage keys (hashes) and address

func (*AccessListEntry) FromProto added in v0.2.0

func (e *AccessListEntry) FromProto(protoEntry *txProto.AccessListEntry)

FromProto parses the proto representation of an access list entry

func (*AccessListEntry) FromRLP added in v0.2.0

func (e *AccessListEntry) FromRLP(data []byte) error

FromRLP parses the RLP encoding of a single access list entry

func (AccessListEntry) ToProto added in v0.2.0

ToProto converts the single access list entry to a proto representation

func (AccessListEntry) ToRLP added in v0.2.0

func (e AccessListEntry) ToRLP() []byte

ToRLP encodes the single access list entry to RLP (RLP Bytes)

type Account added in v0.2.0

type Account struct {
	Balance     *big.Int // balance in wei
	Nonce       uint64   // account sequence
	StorageRoot Hash     // hash of the root node of the account storage trie
	CodeHash    Hash     // hash of the EVM code of this account. For EOAs, this is the hash of an empty string
}

func (Account) IsEOA added in v0.2.0

func (a Account) IsEOA() bool

IsEOA returns a flag indicating if an account is an EOA (Externally Owned Account). Conditions for an account being an EOA:

1. The storage root is empty (zero hash)

2. The code hash is the keccak hash of an empty string

type Address

type Address [addressSize]byte

func BytesToAddress

func BytesToAddress(address []byte) Address

BytesToAddress converts the specified address bytes into an Address representation (20B)

func StringToAddress

func StringToAddress(input string) Address

StringToAddress converts the specified address string into an Address representation

func (Address) ToBytes

func (a Address) ToBytes() []byte

ToBytes converts the specific address to a bytes representation

func (Address) ToString

func (a Address) ToString() string

ToString converts the specific address to a string representation (checksummed)

type Block

type Block struct {
	// contains filtered or unexported fields
}

type Bloom

type Bloom [bloomSize]byte

func (Bloom) ToBytes

func (b Bloom) ToBytes() []byte

type Hash

type Hash [hashSize]byte

func BytesToHash

func BytesToHash(hash []byte) Hash

BytesToHash converts the specified hash bytes into a Hash representation (32B)

func StringToHash

func StringToHash(input string) Hash

StringToHash converts the specified hash string into a Hash representation

func (Hash) ToBytes

func (h Hash) ToBytes() []byte

ToBytes converts the specific hash to a bytes representation

func (Hash) ToString

func (h Hash) ToString() string

ToString converts the specific hash to a string hex representation

type Header struct {
	ParentHash       Hash
	UncleHash        Hash
	Beneficiary      Address
	StateRoot        Hash
	TransactionsRoot Hash
	ReceiptsRoot     Hash
	LogsBloom        Bloom
	Difficulty       uint64
	Number           uint64
	GasLimit         uint64
	GasUsed          uint64
	Time             uint64
	MixDigest        Hash
	Nonce            Nonce
}

type Log

type Log struct {
	Topics  []Hash
	Data    []byte
	Address Address
}

type Nonce

type Nonce [8]byte

type Transaction

type Transaction struct {
	// contains filtered or unexported fields
}

func (*Transaction) AccessList added in v0.2.0

func (t *Transaction) AccessList() []*AccessListEntry

func (*Transaction) ChainID added in v0.2.0

func (t *Transaction) ChainID() uint64

func (*Transaction) Cost

func (t *Transaction) Cost() *big.Int

func (*Transaction) Data added in v0.2.0

func (t *Transaction) Data() []byte

func (*Transaction) Gas added in v0.2.0

func (t *Transaction) Gas() uint64

func (*Transaction) GasPrice added in v0.2.0

func (t *Transaction) GasPrice() *big.Int

func (*Transaction) Hash added in v0.2.0

func (t *Transaction) Hash() Hash

func (*Transaction) MaxFeePerGas added in v0.2.0

func (t *Transaction) MaxFeePerGas() *big.Int

func (*Transaction) MaxPriorityFeePerGas added in v0.2.0

func (t *Transaction) MaxPriorityFeePerGas() *big.Int

func (*Transaction) Nonce added in v0.2.0

func (t *Transaction) Nonce() uint64

func (*Transaction) SigValues added in v0.2.0

func (t *Transaction) SigValues() (v, r, s *big.Int)

func (*Transaction) To added in v0.2.0

func (t *Transaction) To() Address

To returns the To address of the transaction. If none is set (ex. contract deployment), it returns ZeroAddress

func (*Transaction) Type added in v0.2.0

func (t *Transaction) Type() byte

func (*Transaction) Value added in v0.2.0

func (t *Transaction) Value() *big.Int

type TransactionData

type TransactionData interface {
	// contains filtered or unexported methods
}

type TxEIP1559 added in v0.2.0

type TxEIP1559 struct {
	ChainID              *big.Int   // the ID of the chain
	MaxPriorityFeePerGas *big.Int   // the max priority fee per gas
	MaxFeePerGas         *big.Int   // the max fee per gas
	Value                *big.Int   // value of the transfer (if any)
	YParity, R, S        *big.Int   // the signature fields
	To                   *[]byte    // recipient address (nil means contract deployment)
	Data                 []byte     // data (payload) of the tx, if any
	AccessList           AccessList // the allowed access list for the tx, if any
	Nonce                uint64     // the sender nonce
	Gas                  uint64     // the gas limit of the tx
}

TxEIP1559 represents a dynamic fee transaction, introduced as part of EIP-1559 (London hardfork) https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md

func (*TxEIP1559) FromRLP added in v0.2.0

func (tx *TxEIP1559) FromRLP(txData []byte) error

FromRLP parses an EIP-1559 transaction from the transaction data RLP encoding. It expects the structure to be:

[chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gas, to, value, data, accessList, yParity, r, s] (12 elements)

func (*TxEIP1559) SignaturePayload added in v0.2.0

func (tx *TxEIP1559) SignaturePayload() []byte

SignaturePayload constructs the EIP-1559 transaction signature payload. The signature payload (what's being signed) is the keccak of a specific RLP encoding.

RLP structure that needs to be constructed and keccak-ed later: rlp([chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, data, accessList])

The type of tx needs to be appended before keccaking (in this case 0x02, for EIP-1559 txs):

keccak256(0x02 || rlp([chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, data, accessList]))

type TxEIP2930 added in v0.2.0

type TxEIP2930 struct {
	ChainID    *big.Int   // the ID of the chain
	GasPrice   *big.Int   // the gas price of the tx
	Value      *big.Int   // value of the transfer (if any)
	V, R, S    *big.Int   // the signature fields
	To         *[]byte    // recipient address (nil means contract deployment)
	Data       []byte     // data (payload) of the tx, if any
	AccessList AccessList // the allowed access list of the tx, if any
	Nonce      uint64     // the sender nonce
	Gas        uint64     // the gas limit of the tx
}

TxEIP2930 represents an access list transaction, introduced as part of EIP-2930 https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2930.md

func (*TxEIP2930) FromRLP added in v0.2.0

func (tx *TxEIP2930) FromRLP(txData []byte) error

FromRLP parses an EIP-2930 transaction from the transaction data RLP encoding. It expects the structure to be:

[chainID, nonce, gasPrice, gas, to, value, data, accessList, yParity, r, s] (11 elements)

func (*TxEIP2930) SignaturePayload added in v0.2.0

func (tx *TxEIP2930) SignaturePayload() []byte

SignaturePayload constructs the EIP-2930 transaction signature payload. The signature payload (what's being signed) is the keccak of a specific RLP encoding.

RLP structure that needs to be constructed and keccak-ed later: rlp([chainID, nonce, gasPrice, gas, to, value, data, accessList])

The type of tx needs to be appended before keccaking (in this case 0x01, for EIP-2930 txs):

keccak256(0x01 || rlp([chainID, nonce, gasPrice, gas, to, value, data, accessList]))

type TxLegacy added in v0.2.0

type TxLegacy struct {
	GasPrice *big.Int // the gas price of the tx
	Value    *big.Int // value of the transfer (if any)
	V, R, S  *big.Int // the signature fields
	To       *[]byte  // recipient address (nil means contract deployment)
	Data     []byte   // data (payload) of the tx, if any
	Nonce    uint64   // the sender nonce
	Gas      uint64   // the gas limit of the tx
}

TxLegacy represents the legacy transaction type (pre EIP-2930, and pre EIP-1559)

func (*TxLegacy) FromRLP added in v0.2.0

func (tx *TxLegacy) FromRLP(txPayload []byte) error

FromRLP parses a legacy transaction from the transaction data RLP encoding. It expects the structure to be:

[nonce, gasPrice, gas, to, value, data, v, r, s] (9 elements)

func (*TxLegacy) SignaturePayload added in v0.2.0

func (tx *TxLegacy) SignaturePayload() []byte

SignaturePayload constructs the legacy transaction signature payload. The signature payload (what's being signed) is the keccak of a specific RLP encoding.

RLP structure that needs to be constructed and keccak-ed later:

- Pre-EIP-155 (Homestead) RLP: [nonce, gasPrice, gas, to, value, data] (6 elements)

- EIP-155 RLP: [nonce, gasPrice, gas, to, value, data, chainID, 0, 0] (9 elements)

Directories

Path Synopsis
Package hex provides the common hex manipulation functionality.
Package hex provides the common hex manipulation functionality.
Package keccak provides a utility wrapper for the sha256 legacy keccak implementation.
Package keccak provides a utility wrapper for the sha256 legacy keccak implementation.
proto

Jump to

Keyboard shortcuts

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