types

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseABIFromInterface

func ParseABIFromInterface(i any) (*abi.ABI, error)

ParseABIFromInterface parses a generic object into an abi.ABI and returns it, or an error if one occurs.

func RemoveContractMetadata added in v0.1.1

func RemoveContractMetadata(bytecode []byte) []byte

RemoveContractMetadata takes bytecode and attempts to detect contract metadata within it, splitting it where the metadata is found. If contract metadata could be located, this method returns the bytecode solely (no contract metadata, and no constructor arguments, which tend to follow). Otherwise, this method returns the provided input as-is.

Types

type Compilation

type Compilation struct {
	// Sources describes the CompiledSource objects provided in a compilation, housing information regarding source
	// files, mappings, ASTs, and contracts.
	Sources map[string]CompiledSource

	// SourceList describes the CompiledSource keys in Sources, in order. The file identifier used for a SourceMap
	// corresponds to an index in this list.
	SourceList []string

	// SourceCode is a lookup of a source file path from SourceList to source code. This is populated by
	// CacheSourceCode.
	SourceCode map[string][]byte
}

Compilation represents the artifacts of a smart contract compilation.

func NewCompilation

func NewCompilation() *Compilation

NewCompilation returns a new, empty Compilation object.

func (*Compilation) CacheSourceCode added in v0.1.1

func (c *Compilation) CacheSourceCode() error

CacheSourceCode caches source code for each CompiledSource in the compilation in the CompiledSource.SourceCode field. This method will attempt to populate each CompiledSource.SourceCode which has not yet been populated (is nil) before returning an error, if one occurs.

func (*Compilation) GetSourceFileId added in v0.1.1

func (c *Compilation) GetSourceFileId(sourcePath string) int

GetSourceFileId obtains the file identifier for a given source file path. This simply checks the index of the source file path in SourceList. Returns the identifier of the source file, or -1 if it could not be found.

type CompiledContract

type CompiledContract struct {
	// Abi describes a contract's application binary interface, a structure used to describe information needed
	// to interact with the contract such as constructor and function definitions with input/output variable
	// information, event declarations, and fallback and receive methods.
	Abi abi.ABI

	// InitBytecode describes the bytecode used to deploy a contract.
	InitBytecode []byte

	// RuntimeBytecode represents the rudimentary bytecode to be expected once the contract has been successfully
	// deployed. This may differ at runtime based on constructor arguments, immutables, linked libraries, etc.
	RuntimeBytecode []byte

	// SrcMapsInit describes the source mappings to associate source file and bytecode segments in InitBytecode.
	SrcMapsInit string

	// SrcMapsRuntime describes the source mappings to associate source file and bytecode segments in RuntimeBytecode.
	SrcMapsRuntime string
}

CompiledContract represents a single contract unit from a smart contract compilation.

func (*CompiledContract) GetDeploymentMessageData

func (c *CompiledContract) GetDeploymentMessageData(args []any) ([]byte, error)

GetDeploymentMessageData is a helper method used create contract deployment message data for the given contract. This data can be set in transaction/message structs "data" field to indicate the packed init bytecode and constructor argument data to use.

func (*CompiledContract) IsMatch

func (c *CompiledContract) IsMatch(initBytecode []byte, runtimeBytecode []byte) bool

IsMatch returns a boolean indicating whether provided contract bytecode is a match to this compiled contract definition.

type CompiledSource

type CompiledSource struct {
	// Ast describes the abstract syntax tree artifact of a source file compilation, providing tokenization of the
	// source file components.
	Ast any

	// Contracts describes a mapping of contract names to contract definition structures which are contained within
	// the source.
	Contracts map[string]CompiledContract
}

CompiledSource represents a source descriptor for a smart contract compilation, including AST and contained CompiledContract instances.

type ContractMetadata

type ContractMetadata map[string]any

ContractMetadata is an CBOR-encoded structure describing contract information which is embedded within smart contract bytecode by the Solidity compiler (unless explicitly directed not to). Reference: https://docs.soliditylang.org/en/v0.8.16/metadata.html

func ExtractContractMetadata

func ExtractContractMetadata(bytecode []byte) *ContractMetadata

ExtractContractMetadata extracts contract metadata from provided byte code and returns it. If contract metadata could not be extracted, nil is returned.

func (ContractMetadata) ExtractBytecodeHash

func (m ContractMetadata) ExtractBytecodeHash() []byte

ExtractBytecodeHash extracts the bytecode hash from given contract metadata and returns the bytes representing the hash. If it could not be detected or extracted, nil is returned.

type SourceMap added in v0.1.1

type SourceMap []SourceMapElement

SourceMap describes a list of elements which correspond to instruction indexes in compiled bytecode, describing which source files and the start/end range of the source code which the instruction maps to.

func ParseSourceMap added in v0.1.1

func ParseSourceMap(sourceMapStr string) (SourceMap, error)

ParseSourceMap takes a source mapping string returned by the compiler and parses it into an array of SourceMapElement objects. Returns the list of SourceMapElement objects.

func (SourceMap) GetInstructionIndexToOffsetLookup added in v0.1.1

func (s SourceMap) GetInstructionIndexToOffsetLookup(bytecode []byte) ([]int, error)

GetInstructionIndexToOffsetLookup obtains a slice where each index of the slice corresponds to an instruction index, and the element of the slice represents the instruction offset. Returns the slice lookup, or an error if one occurs.

type SourceMapElement added in v0.1.1

type SourceMapElement struct {
	// Index refers to the index of the SourceMapElement within its parent SourceMap. This is not actually a field
	// saved in the SourceMap, but is provided for convenience so the user may remove SourceMapElement objects during
	// analysis.
	Index int

	// Offset refers to the byte offset which marks the start of the source range the instruction maps to.
	Offset int

	// Length refers to the byte length of the source range the instruction maps to.
	Length int

	// FileID refers to an identifier for the CompiledSource file which houses the relevant source code.
	FileID int

	// JumpType refers to the SourceMapJumpType which provides information about any type of jump that occurred.
	JumpType SourceMapJumpType

	// ModifierDepth refers to the depth in which code has executed a modifier function. This is used to assist
	// debuggers, e.g. understanding if the same modifier is re-used multiple times in a call.
	ModifierDepth int
}

SourceMapElement describes an individual element of a source mapping output by the compiler. The index of each element in a source map corresponds to an instruction index (not to be mistaken with offset). It describes portion of a source file the instruction references.

type SourceMapJumpType added in v0.1.1

type SourceMapJumpType string

SourceMapJumpType describes the type of jump operation occurring within a SourceMapElement if the instruction is jumping.

const (
	// SourceMapJumpTypeNone indicates no jump occurred.
	SourceMapJumpTypeNone SourceMapJumpType = ""

	// SourceMapJumpTypeJumpIn indicates a jump into a function occurred.
	SourceMapJumpTypeJumpIn SourceMapJumpType = "i"

	// SourceMapJumpTypeJumpOut indicates a return from a function occurred.
	SourceMapJumpTypeJumpOut SourceMapJumpType = "o"

	// SourceMapJumpTypeJumpWithin indicates a jump occurred within the same function, e.g. for loops.
	SourceMapJumpTypeJumpWithin SourceMapJumpType = "-"
)

Jump to

Keyboard shortcuts

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