Documentation
¶
Overview ¶
Package bytecode provides tools for decoding and analyzing Ethereum contract, transaction, events, and log bytecode. The package is designed to extract and interpret metadata from Ethereum contract creation bytecode. It provides a Metadata struct that represents the metadata contained in the bytecode, as defined by the Solidity compiler. This includes information such as the IPFS hash of the metadata, the Swarm hash of the metadata, experimental metadata, and the version of the Solidity compiler used.
Index ¶
- type Argument
- type Constructor
- type Log
- type Metadata
- func (m *Metadata) AuxFound(b []byte) bool
- func (m *Metadata) GetAuxBytecode() []byte
- func (m *Metadata) GetBzzr0() string
- func (m *Metadata) GetBzzr1() string
- func (m *Metadata) GetCborLength() int16
- func (m *Metadata) GetCompilerVersion() string
- func (m *Metadata) GetExecutionBytecode() []byte
- func (m *Metadata) GetExperimental() bool
- func (m *Metadata) GetIPFS() string
- func (m *Metadata) GetUrls() []string
- func (m *Metadata) ToProto() *metadata_pb.BytecodeMetadata
- type Topic
- type Transaction
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Argument ¶
type Argument struct { Name string `json:"name"` // Name of the argument Type string `json:"type"` // Type of the argument Value string `json:"value"` // Value of the argument Indexed bool `json:"indexed"` // Indicates if the argument is indexed }
Argument represents a single argument in a contract constructor. It includes the argument's name, type, value, and whether it is indexed.
type Constructor ¶
type Constructor struct { Abi string `json:"abi"` // ABI of the constructor Parsed abi.ABI `json:"-"` // Parsed ABI of the constructor SignatureRaw string `json:"signature_raw"` // Raw signature of the constructor Arguments []Argument `json:"arguments"` // List of arguments in the constructor UnpackedArguments []interface{} `json:"unpacked_arguments"` // List of unpacked arguments in the constructor }
Constructor represents a contract constructor. It includes the ABI of the constructor, the raw signature, and the arguments.
func DecodeConstructorFromAbi ¶
func DecodeConstructorFromAbi(bytecode []byte, constructorAbi string) (*Constructor, error)
DecodeConstructorFromAbi decodes the constructor from the provided ABI and bytecode. It returns a Constructor object and an error if any occurred during the decoding process.
The function first checks if the bytecode is empty or does not start with '['. If so, it prepends '[' to the constructorAbi. Then it attempts to parse the ABI using the abi.JSON function from the go-ethereum library. If the ABI parsing is successful, it unpacks the values from the bytecode using the UnpackValues function. It then checks if the number of unpacked values matches the number of inputs in the constructor. If they match, it creates an Argument object for each input and adds it to the arguments slice. Finally, it returns a Constructor object containing the ABI, raw signature, and arguments.
func (*Constructor) Pack ¶
func (c *Constructor) Pack() ([]byte, error)
Pack packs the arguments in the constructor into a byte slice for future use and verification.
type Log ¶
type Log struct { Event *abi.Event `json:"-"` // ABI definition of the log's event. Address common.Address `json:"address"` // Address of the contract that emitted the event. Abi string `json:"abi"` // ABI string of the event. SignatureHex common.Hash `json:"signature_hex"` // Hex-encoded signature of the event. Signature string `json:"signature"` // Signature of the event. Type utils.LogEventType `json:"type"` // Type of the event, classified by solgo. Name string `json:"name"` // Name of the event. Data map[string]any `json:"data"` // Decoded event data. Topics []Topic `json:"topics"` // Decoded topics of the event. }
Log encapsulates a decoded Ethereum event log. It includes the event's details such as its name, signature, the contract that emitted the event, and the decoded data and topics.
func DecodeLogFromAbi ¶
DecodeLogFromAbi decodes an Ethereum event log using the provided ABI data. It returns a Log instance containing the decoded event name, data, and topics. The function requires the event log and its ABI as inputs. It handles errors such as missing topics or failure to parse the ABI.
type Metadata ¶
type Metadata struct { Ipfs []byte `cbor:"ipfs"` // The IPFS hash of the metadata, if present Bzzr1 []byte `cbor:"bzzr1"` // The Swarm hash of the metadata, if present (version 1) Bzzr0 []byte `cbor:"bzzr0"` // The Swarm hash of the metadata, if present (version 0) Experimental interface{} `cbor:"experimental"` // Experimental metadata, if present Solc []byte `cbor:"solc"` // The version of the Solidity compiler used // contains filtered or unexported fields }
Metadata represents the metadata contained in Ethereum contract creation bytecode. The structure and encoding of the metadata is defined by the Solidity compiler. More information can be found at https://docs.soliditylang.org/en/v0.8.20/metadata.html#encoding-of-the-metadata-hash-in-the-bytecode
func DecodeContractMetadata ¶
DecodeContractMetadata decodes the metadata from Ethereum contract creation bytecode. It returns a Metadata object and an error, if any occurred during decoding.
func (*Metadata) AuxFound ¶
AuxFound returns whether the CBOR metadata bytes are contained in the provided byte slice. This is used to verify if contract extracted cbor metadata can be found in the deployed/execution contract bytecode.
func (*Metadata) GetAuxBytecode ¶
GetAuxBytecode returns the raw CBOR metadata of the contract.
func (*Metadata) GetBzzr0 ¶
GetBzzr0 returns the Swarm (version 0) hash of the contract's metadata, if present.
func (*Metadata) GetBzzr1 ¶
GetBzzr1 returns the Swarm (version 1) hash of the contract's metadata, if present.
func (*Metadata) GetCborLength ¶
GetCborLength returns the length of the CBOR metadata.
func (*Metadata) GetCompilerVersion ¶
GetCompilerVersion returns the version of the Solidity compiler used to compile the contract.
func (*Metadata) GetExecutionBytecode ¶
GetExecutionBytecode returns the execution bytecode of the contract.
func (*Metadata) GetExperimental ¶
GetExperimental returns whether the contract includes experimental metadata.
func (*Metadata) ToProto ¶
func (m *Metadata) ToProto() *metadata_pb.BytecodeMetadata
ToProto converts the Metadata instance into a protobuf representation, suitable for serialization and transmission across different systems or networks.
type Topic ¶
type Topic struct { Name string `json:"name"` // The name of the topic. Value any `json:"value"` // The value of the topic, decoded into the appropriate Go data type. }
Topic represents a single decoded topic from an Ethereum event log. Topics are attributes of an event, such as the method signature and indexed parameters.
func GetTopicByName ¶
GetTopicByName searches for and returns a Topic by its name from a slice of Topic instances. It facilitates accessing specific topics directly by name rather than iterating over the slice. If the topic is not found, it returns nil.
type Transaction ¶
type Transaction struct { Abi string `json:"abi"` // ABI string of the transaction's method. SignatureBytes []byte `json:"signature_bytes"` // Raw signature bytes of the transaction. Signature string `json:"signature"` // Human-readable signature of the transaction's method. Type utils.TransactionMethodType `json:"type"` // Type of the transaction, classified by its method name. Name string `json:"name"` // Name of the transaction's method. Method *abi.Method `json:"-"` // ABI method information, not serialized to JSON. Inputs map[string]interface{} `json:"inputs"` // Decoded arguments passed to the transaction's method. }
Transaction encapsulates a decoded Ethereum transaction, providing detailed information about the transaction's method, its arguments, and the associated ABI. This structured format makes it easier to work with Ethereum transactions programmatically.
func DecodeTransactionFromAbi ¶
func DecodeTransactionFromAbi(data []byte, abiData []byte) (*Transaction, error)
DecodeTransactionFromAbi decodes an Ethereum transaction using the provided ABI. It extracts the method signature and arguments from the raw transaction data, constructing a Transaction object that includes this information along with the method's ABI.
The function requires the raw transaction data (`data`) and the ABI of the smart contract (`abiData`) in JSON format. It returns a pointer to a Transaction object, populated with the decoded method information and its arguments, or an error if decoding fails.
This function simplifies the process of interacting with raw Ethereum transactions, making it easier to analyze and use the transaction data programmatically.