ethcoder

package
v1.29.7 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2024 License: LGPL-3.0, MIT Imports: 20 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMerkleTreeOptions = Options{

	SortLeaves: true,
	SortPairs:  true,
}

Functions

func ABIEncodeMethodCalldata added in v1.28.0

func ABIEncodeMethodCalldata(methodSig string, argValues []interface{}) ([]byte, error)

func ABIEncodeMethodCalldataFromStringValues added in v1.28.0

func ABIEncodeMethodCalldataFromStringValues(methodSig string, argStringValues []string) ([]byte, error)

func ABIEncodeMethodCalldataFromStringValuesAny added in v1.28.0

func ABIEncodeMethodCalldataFromStringValuesAny(methodSig string, argStringValues []any) ([]byte, error)

func ABIMarshalStringValues added in v1.28.0

func ABIMarshalStringValues(argTypes []string, input []byte) ([]string, error)

func ABIPackArguments added in v1.28.0

func ABIPackArguments(argTypes []string, argValues []interface{}) ([]byte, error)

func ABIPackArgumentsHex added in v1.28.0

func ABIPackArgumentsHex(argTypes []string, argValues []interface{}) (string, error)

func ABIUnmarshalStringValues added in v1.28.0

func ABIUnmarshalStringValues(argTypes []string, stringValues []string) ([]any, error)

AbiUnmarshalStringValues will take an array of ethereum types as string values, and decode the string values to runtime objects. This allows simple string value input from an app or user, and converts them to the appropriate runtime objects.

The common use for this method is to pass a JSON object of string values for an abi method and have it properly encode to the native abi types.

func ABIUnmarshalStringValuesAny added in v1.28.0

func ABIUnmarshalStringValuesAny(argTypes []string, stringValues []any) ([]any, error)

AbiUnmarshalStringValuesAny will take an array of ethereum types as string values, and decode the string values to runtime objects. This allows simple string value input from an app or user, and converts them to the appropriate runtime objects.

NOTE: this is a variant of AbiUnmarshalStringValues but the `stringValues` argument type is []any, in order to support input types of array of strings for abi types like `address[]` and tuples.

For example, some valid inputs:

  • AbiUnmarshalStringValuesAny([]string{"address","uint256"}, []any{"0x1234...", "543"}) returns []interface{}{common.HexToAddress("0x1234..."), big.NewInt(543)}
  • AbiUnmarshalStringValuesAny([]string{"address[]", []any{[]string{"0x1234...", "0x5678..."}}) returns []interface{}{[]common.Address{common.HexToAddress("0x1234..."), common.HexToAddress("0x5678...")}}
  • AbiUnmarshalStringValuesAny([]string{"(address,uint256)"}, []any{[]any{"0x1234...", "543"}}) returns []interface{}{[]interface{}{common.HexToAddress("0x1234..."), big.NewInt(543)}}

The common use for this method is to pass a JSON object of string values for an abi method and have it properly encode to the native abi types.

func ABIUnpack added in v1.28.0

func ABIUnpack(exprSig string, input []byte, argValues []interface{}) error

TODO: change expr argument to abiXX like abiExprOrJSON

func ABIUnpackAndStringify added in v1.28.0

func ABIUnpackAndStringify(exprSig string, input []byte) ([]string, error)

TODO: change expr argument to abiXX like abiExprOrJSON

func ABIUnpackArguments added in v1.28.0

func ABIUnpackArguments(argTypes []string, input []byte) ([]interface{}, error)

func ABIUnpackArgumentsByRef added in v1.28.0

func ABIUnpackArgumentsByRef(argTypes []string, input []byte, outArgValues []interface{}) error

func AbiCoder deprecated

func AbiCoder(argTypes []string, argValues []interface{}) ([]byte, error)

Deprecated: use ABIPackArguments instead

func AbiCoderHex deprecated

func AbiCoderHex(argTypes []string, argValues []interface{}) (string, error)

Deprecated: use ABIPackArgumentsHex instead

func AbiDecodeExpr deprecated

func AbiDecodeExpr(expr string, input []byte, argValues []interface{}) error

Deprecated: use ABIUnpack instead

func AbiDecodeExprAndStringify deprecated

func AbiDecodeExprAndStringify(expr string, input []byte) ([]string, error)

Deprecated: use ABIUnpackAndStringify instead

func AbiDecoder deprecated

func AbiDecoder(argTypes []string, input []byte, outArgValues []interface{}) error

Deprecated: use ABIUnpackArgumentsByRef instead

func AbiDecoderWithReturnedValues deprecated

func AbiDecoderWithReturnedValues(argTypes []string, input []byte) ([]interface{}, error)

Deprecated: use ABIUnpackArguments instead

func AbiEncodeMethodCalldata deprecated

func AbiEncodeMethodCalldata(methodExpr string, argValues []interface{}) ([]byte, error)

Deprecated: use ABIEncodeMethodCalldata instead

func AbiEncodeMethodCalldataFromStringValues deprecated

func AbiEncodeMethodCalldataFromStringValues(methodExpr string, argStringValues []string) ([]byte, error)

Deprecated: use ABIEncodeMethodCalldataFromStringValuesAny instead

func AbiEncodeMethodCalldataFromStringValuesAny deprecated added in v1.28.0

func AbiEncodeMethodCalldataFromStringValuesAny(methodSig string, argStringValues []any) ([]byte, error)

Deprecated: use ABIEncodeMethodCalldataFromStringValuesAny instead

func AbiMarshalStringValues deprecated

func AbiMarshalStringValues(argTypes []string, input []byte) ([]string, error)

Deprecated: use ABIMarshalStringValues instead

func AbiUnmarshalStringValues deprecated

func AbiUnmarshalStringValues(argTypes []string, stringValues []string) ([]any, error)

AbiUnmarshalStringValues will take an array of ethereum types as string values, and decode the string values to runtime objects. This allows simple string value input from an app or user, and converts them to the appropriate runtime objects.

The common use for this method is to pass a JSON object of string values for an abi method and have it properly encode to the native abi types.

Deprecated: use ABIUnmarshalStringValues instead

func BytesToBytes32

func BytesToBytes32(slice []byte) [32]byte

func EncodeContractCall added in v1.28.0

func EncodeContractCall(callDef ContractCallDef) (string, error)

EncodeContractCall encodes a contract call as a hex encoded calldata.

func EventTopicHash added in v1.24.11

func EventTopicHash(event string) (ethkit.Hash, string, error)

EventTopicHash returns the keccak256 hash of the event signature

e.g. "Transfer(address indexed from, address indexed to, uint256 value)" will return 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef

func FunctionSignature

func FunctionSignature(functionExpr string) string

func HexDecode

func HexDecode(h string) ([]byte, error)

func HexDecodeBigIntArray

func HexDecodeBigIntArray(bigNumsHex []string) ([]*big.Int, error)

func HexDecodeBytes32

func HexDecodeBytes32(h string) ([32]byte, error)

func HexEncode

func HexEncode(h []byte) string

func HexEncodeBigIntArray

func HexEncodeBigIntArray(bigNums []*big.Int) ([]string, error)

func HexTrimLeadingZeros

func HexTrimLeadingZeros(hex string) (string, error)

func Keccak256

func Keccak256(input []byte) []byte

func Keccak256Hash added in v1.17.0

func Keccak256Hash(input []byte) common.Hash

func MustHexDecode

func MustHexDecode(h string) []byte

func MustNewArrayTypeTuple added in v1.4.1

func MustNewArrayTypeTuple(components []abi.ArgumentMarshaling) abi.Type

func MustNewType added in v1.4.1

func MustNewType(str string) abi.Type

func PadZeros

func PadZeros(array []byte, totalLength int) ([]byte, error)

func PaddedAddress added in v1.28.0

func PaddedAddress(address string) string

func SHA3 added in v1.17.0

func SHA3(input []byte) common.Hash

func SolidityPack

func SolidityPack(argTypes []string, argValues []interface{}) ([]byte, error)

func SolidityPackHex

func SolidityPackHex(argTypes []string, argValues []interface{}) (string, error)

func StringifyValues

func StringifyValues(values []any) ([]string, error)

func ValidateEventSig added in v1.26.3

func ValidateEventSig(eventSig string) (bool, error)

Types

type ABI added in v1.28.0

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

func NewABI added in v1.28.0

func NewABI() ABI

func (*ABI) AddABIBySigOrJSON added in v1.28.0

func (d *ABI) AddABIBySigOrJSON(abiSigOrJSON string, isEvent bool) ([]string, error)

func (*ABI) AddABIFromJSON added in v1.28.0

func (d *ABI) AddABIFromJSON(abiJSON string) error

func (*ABI) AddABISignature added in v1.28.0

func (d *ABI) AddABISignature(abiSig ABISignature, isEvent bool) (string, error)

func (*ABI) AddEvent added in v1.28.0

func (d *ABI) AddEvent(eventSig string) (string, error)

func (*ABI) AddMethod added in v1.28.0

func (d *ABI) AddMethod(methodSig string) (string, error)

func (*ABI) EncodeMethodCalldata added in v1.28.0

func (d *ABI) EncodeMethodCalldata(methodName string, argValues []interface{}) ([]byte, error)

EncodeMethodCalldata encodes the abi method arguments into calldata bytes. It's expected that `argValues` are runtime types, ie. common.Address, or big.Int, etc.

func (*ABI) EncodeMethodCalldataFromStringValues added in v1.28.0

func (d *ABI) EncodeMethodCalldataFromStringValues(methodName string, argStringValues []string) ([]byte, error)

EncodeMethodCalldataFromStringValues decodes the abi method argument string values into runtime types, and then encodes the abi method arguments into calldata bytes.

The difference between this method and EncodeMethodCalldataFromStringValuesAny are just argument types for convenience.

func (*ABI) EncodeMethodCalldataFromStringValuesAny added in v1.28.0

func (d *ABI) EncodeMethodCalldataFromStringValuesAny(methodName string, argStringValues []any) ([]byte, error)

EncodeMethodCalldataFromStringValuesAny decodes the abi method argument string values into runtime types, and then encodes the abi method arguments into calldata bytes.

NOTE: also see the EncodeContractCall() function, which has more capabilities and supports nested encoding.

func (*ABI) GetEventABI added in v1.28.0

func (d *ABI) GetEventABI(eventName string) (abi.Event, bool)

func (*ABI) GetEventABISignature added in v1.28.0

func (d *ABI) GetEventABISignature(eventName string) (ABISignature, bool)

func (*ABI) GetMethodABI added in v1.28.0

func (d *ABI) GetMethodABI(methodName string) (abi.Method, bool)

func (*ABI) GetMethodABISignature added in v1.28.0

func (d *ABI) GetMethodABISignature(methodName string) (ABISignature, bool)

func (*ABI) RawABI added in v1.28.0

func (d *ABI) RawABI() abi.ABI

func (*ABI) SetABI added in v1.28.0

func (d *ABI) SetABI(rawABI abi.ABI) error

type ABISignature added in v1.28.0

type ABISignature struct {
	Name       string   // the method or event name, ie. Transfer
	Signature  string   // the abi signature string, ie. Transfer(address,address,uint256)
	Hash       string   // the method/event topic hash, ie. 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
	ArgTypes   []string // the method/event arg types, ie. [address, address, uint256]
	ArgNames   []string // the method/event arg names, ie. [from, to, value] or ["","",""]
	ArgIndexed []bool   // the event arg indexed flag, ie. [true, false, true]
	NumIndexed int
}

func DecodeTransactionLogByContractABI added in v1.26.2

func DecodeTransactionLogByContractABI(txnLog types.Log, contractABI abi.ABI) (ABISignature, []interface{}, bool, error)

func DecodeTransactionLogByContractABIAsHex added in v1.26.5

func DecodeTransactionLogByContractABIAsHex(txnLog types.Log, contractABI abi.ABI) (ABISignature, []string, bool, error)

func DecodeTransactionLogByContractABIJSON added in v1.26.2

func DecodeTransactionLogByContractABIJSON(txnLog types.Log, contractABIJSON string) (ABISignature, []interface{}, bool, error)

..

func DecodeTransactionLogByEventSig added in v1.26.2

func DecodeTransactionLogByEventSig(txnLog types.Log, eventSig string) (ABISignature, []interface{}, bool, error)

func DecodeTransactionLogByEventSigAsHex added in v1.26.5

func DecodeTransactionLogByEventSigAsHex(txnLog types.Log, eventSig string) (ABISignature, []string, bool, error)

func ParseABISignature added in v1.28.0

func ParseABISignature(abiSignature string) (ABISignature, error)

func (ABISignature) String added in v1.28.0

func (e ABISignature) String() string

func (ABISignature) ToABI added in v1.28.0

func (s ABISignature) ToABI(isEvent bool) (abi.ABI, string, error)

type ContractCallDef added in v1.28.0

type ContractCallDef struct {
	// ABI can be an abi signature ie. "transfer(address,uint256)"
	// or it can be a JSON encoded ABI string
	ABI string `json:"abi"`

	// Func is the name of the function / method in the call.
	// NOTE: this is optional if the abi signature is used, or if the abi
	// only has one method.
	Func string `json:"func"`

	// Args is the arguments to the call, which can be nested.
	Args []any `json:"args"`
}

ContractCallDef is a definition for a contract call. It can be used to encode a contract call as a hex encoded string.

type EventDecoder added in v1.26.5

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

func NewEventDecoder added in v1.26.5

func NewEventDecoder() *EventDecoder

func (*EventDecoder) DecodeLog added in v1.26.5

func (d *EventDecoder) DecodeLog(log types.Log) (ABISignature, []interface{}, bool, error)

func (*EventDecoder) DecodeLogAsHex added in v1.26.5

func (d *EventDecoder) DecodeLogAsHex(log types.Log) (ABISignature, []string, bool, error)

func (*EventDecoder) EventDefList added in v1.27.2

func (d *EventDecoder) EventDefList() []ABISignature

func (*EventDecoder) RegisterContractABI added in v1.26.5

func (d *EventDecoder) RegisterContractABI(contractABI abi.ABI, eventNames ...string) error

func (*EventDecoder) RegisterContractABIJSON added in v1.26.5

func (d *EventDecoder) RegisterContractABIJSON(contractABIJSON string, eventNames ...string) error

func (*EventDecoder) RegisterEventSig added in v1.26.5

func (d *EventDecoder) RegisterEventSig(eventSig ...string) error

func (*EventDecoder) TopicsList added in v1.27.2

func (d *EventDecoder) TopicsList() []string

func (*EventDecoder) TopicsMap added in v1.27.2

func (d *EventDecoder) TopicsMap() map[string]struct{}

type MerkleTree added in v1.26.0

type MerkleTree[TLeaf any] struct {
	// contains filtered or unexported fields
}

func NewMerkleTree added in v1.26.0

func NewMerkleTree[TLeaf any](leaves []TLeaf, hashFn *func(TLeaf) ([]byte, error), options *Options) *MerkleTree[TLeaf]

func (*MerkleTree[TLeaf]) GetHexProof added in v1.26.0

func (mt *MerkleTree[TLeaf]) GetHexProof(leaf TLeaf) [][]byte

func (*MerkleTree[TLeaf]) GetProof added in v1.26.0

func (mt *MerkleTree[TLeaf]) GetProof(leaf TLeaf) ([]Proof, error)

func (*MerkleTree[TLeaf]) GetRoot added in v1.26.0

func (mt *MerkleTree[TLeaf]) GetRoot() []byte

func (*MerkleTree[TLeaf]) Verify added in v1.26.0

func (mt *MerkleTree[TLeaf]) Verify(proof []Proof, leaf TLeaf, root []byte) (bool, error)

type Options added in v1.26.0

type Options struct {
	SortLeaves bool
	SortPairs  bool
}

type Proof added in v1.26.0

type Proof struct {
	IsLeft bool
	Data   []byte
}

type TypedData

type TypedData struct {
	Types       TypedDataTypes         `json:"types"`
	PrimaryType string                 `json:"primaryType"`
	Domain      TypedDataDomain        `json:"domain"`
	Message     map[string]interface{} `json:"message"`
}

func (*TypedData) EncodeDigest

func (t *TypedData) EncodeDigest() ([]byte, error)

func (*TypedData) HashStruct added in v1.3.4

func (t *TypedData) HashStruct(primaryType string, data map[string]interface{}) ([]byte, error)

type TypedDataArgument

type TypedDataArgument struct {
	Name string `json:"name"`
	Type string `json:"type"`
}

type TypedDataDomain

type TypedDataDomain struct {
	Name              string          `json:"name,omitempty"`
	Version           string          `json:"version,omitempty"`
	ChainID           *big.Int        `json:"chainId,omitempty"`
	VerifyingContract *common.Address `json:"verifyingContract,omitempty"`
	Salt              *[32]byte       `json:"salt,omitempty"`
}

func (TypedDataDomain) Map

func (t TypedDataDomain) Map() map[string]interface{}

type TypedDataTypes

type TypedDataTypes map[string][]TypedDataArgument

func (TypedDataTypes) EncodeType

func (t TypedDataTypes) EncodeType(primaryType string) (string, error)

func (TypedDataTypes) TypeHash

func (t TypedDataTypes) TypeHash(primaryType string) ([]byte, error)

Jump to

Keyboard shortcuts

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