Documentation
¶
Overview ¶
Package abi provides Application Binary Interface (ABI) encoding and decoding functionality for Zenon Network embedded smart contracts.
The ABI package handles serialization and deserialization of contract calls, enabling interaction with protocol-level embedded contracts. This is primarily used internally by the embedded contract APIs but can be used directly for custom contract interactions.
Basic Concepts ¶
Embedded contracts in Zenon Network use ABI encoding to:
- Encode method calls into transaction data
- Decode method parameters from transaction data
- Serialize complex data structures
Contract Method Encoding ¶
Encode a contract method call:
// Define method parameters
params := []interface{}{
"pillarName",
big.NewInt(15000),
types.ZnnTokenStandard,
}
// Encode method call
data, err := abi.EncodeMethod("Register", params...)
if err != nil {
log.Fatal(err)
}
Contract Response Decoding ¶
Decode contract response data:
// Define expected return types
var result struct {
Name string
Owner types.Address
Amount *big.Int
}
// Decode response
err := abi.DecodeResponse(responseData, &result)
if err != nil {
log.Fatal(err)
}
Common Data Types ¶
The ABI package handles encoding/decoding of:
- Basic types: int8 through int256, uint8 through uint256, bool, string
- Fixed bytes: bytes1 through bytes32
- Big integers: *big.Int for large numbers
- Addresses: types.Address
- Token standards: types.ZenonTokenStandard
- Hashes: types.Hash
- Complex structures and arrays
Canonical Validation ¶
Encoding requires exactly one value for each declared parameter. Signed and unsigned integers must fit their declared bit width, fixed-byte values must have exactly their declared length, and boolean inputs must have Go type bool. Decoding rejects integer words outside the declared width, non-zero fixed-byte padding, and boolean words other than canonical zero or one. These checks prevent silent truncation and ambiguous wire encodings before a contract call is sent.
Internal Usage ¶
Most developers don't need to use the ABI package directly, as the embedded contract APIs handle encoding automatically:
// High-level API (recommended)
template := client.PillarApi.Register(name, producerAddress, rewardAddress, ...)
// Under the hood, this uses:
// data := abi.EncodeMethod("Register", name, producerAddress, ...)
// template.Data = data
Advanced Usage ¶
For custom contract interactions or debugging, you can use the ABI package directly:
// Create custom contract call
contractAddress := types.ParseAddressPanic("z1qxemdeddedxxxxxxxxxxxxxxxxxxxxxxxxxxx")
data, _ := abi.EncodeMethod("CustomMethod", param1, param2)
template := &nom.AccountBlock{
Address: myAddress,
ToAddress: contractAddress,
Data: data,
TokenStandard: types.ZnnTokenStandard,
Amount: big.NewInt(0),
}
For more information, see https://pkg.go.dev/github.com/0x3639/znn-sdk-go/abi
Index ¶
- Constants
- func DecodeInt(encoded []byte, offset int) (*big.Int, error)
- func DecodeList(params []Param, encoded []byte) ([]interface{}, error)
- func DecodeUint(encoded []byte, offset int) (*big.Int, error)
- func EncodeInt(i int) []byte
- func EncodeIntBig(bigInt *big.Int) []byte
- func EncodeUint(i uint64) ([]byte, error)
- func EncodeUintBig(bigInt *big.Int) ([]byte, error)
- type Abi
- type AbiFunction
- type AbiType
- type AddressType
- func (at *AddressType) Decode(encoded []byte, offset int) (interface{}, error)
- func (at *AddressType) Encode(value interface{}) ([]byte, error)
- func (bt *AddressType) GetCanonicalName() string
- func (bt *AddressType) GetFixedSize() int
- func (bt *AddressType) GetName() string
- func (bt *AddressType) IsDynamicType() bool
- type ArrayType
- type BoolType
- type Bytes32Type
- type BytesType
- func (bt *BytesType) Decode(encoded []byte, offset int) (interface{}, error)
- func (bt *BytesType) Encode(value interface{}) ([]byte, error)
- func (bt *BytesType) GetCanonicalName() string
- func (bt *BytesType) GetFixedSize() int
- func (bt *BytesType) GetName() string
- func (bt *BytesType) IsDynamicType() bool
- type DynamicArrayType
- func (dat *DynamicArrayType) Decode(encoded []byte, origOffset int) (interface{}, error)
- func (dat *DynamicArrayType) DecodeTuple(encoded []byte, origOffset int, length int) ([]interface{}, error)
- func (dat *DynamicArrayType) Encode(value interface{}) ([]byte, error)
- func (dat *DynamicArrayType) EncodeList(values []interface{}) ([]byte, error)
- func (dat *DynamicArrayType) EncodeTuple(values []interface{}) ([]byte, error)
- func (dat *DynamicArrayType) GetCanonicalName() string
- func (dat *DynamicArrayType) GetElementType() AbiType
- func (dat *DynamicArrayType) GetFixedSize() int
- func (bt *DynamicArrayType) GetName() string
- func (dat *DynamicArrayType) IsDynamicType() bool
- type Entry
- type FixedBytesType
- func (bt *FixedBytesType) Decode(encoded []byte, offset int) (interface{}, error)
- func (bt *FixedBytesType) Encode(value interface{}) ([]byte, error)
- func (bt *FixedBytesType) GetCanonicalName() string
- func (bt *FixedBytesType) GetFixedSize() int
- func (bt *FixedBytesType) GetName() string
- func (bt *FixedBytesType) IsDynamicType() bool
- type FunctionType
- type HashType
- func (ht *HashType) Decode(encoded []byte, offset int) (interface{}, error)
- func (ht *HashType) Encode(value interface{}) ([]byte, error)
- func (bt *HashType) GetCanonicalName() string
- func (bt *HashType) GetFixedSize() int
- func (bt *HashType) GetName() string
- func (bt *HashType) IsDynamicType() bool
- type IntType
- type NumericType
- type Param
- type StaticArrayType
- func (sat *StaticArrayType) Decode(encoded []byte, offset int) (interface{}, error)
- func (sat *StaticArrayType) DecodeTuple(encoded []byte, origOffset int, length int) ([]interface{}, error)
- func (sat *StaticArrayType) Encode(value interface{}) ([]byte, error)
- func (sat *StaticArrayType) EncodeList(values []interface{}) ([]byte, error)
- func (sat *StaticArrayType) EncodeTuple(values []interface{}) ([]byte, error)
- func (sat *StaticArrayType) GetCanonicalName() string
- func (sat *StaticArrayType) GetElementType() AbiType
- func (sat *StaticArrayType) GetFixedSize() int
- func (bt *StaticArrayType) GetName() string
- func (bt *StaticArrayType) IsDynamicType() bool
- type StringType
- type TokenStandardType
- func (tst *TokenStandardType) Decode(encoded []byte, offset int) (interface{}, error)
- func (tst *TokenStandardType) Encode(value interface{}) ([]byte, error)
- func (bt *TokenStandardType) GetCanonicalName() string
- func (bt *TokenStandardType) GetFixedSize() int
- func (bt *TokenStandardType) GetName() string
- func (bt *TokenStandardType) IsDynamicType() bool
- type TypeEnum
- type UnsignedIntType
- func (uit *UnsignedIntType) Decode(encoded []byte, offset int) (interface{}, error)
- func (uit *UnsignedIntType) Encode(value interface{}) ([]byte, error)
- func (uit *UnsignedIntType) GetCanonicalName() string
- func (bt *UnsignedIntType) GetFixedSize() int
- func (bt *UnsignedIntType) GetName() string
- func (bt *UnsignedIntType) IsDynamicType() bool
Constants ¶
const ( // MaxTypeNameLength is the longest type declaration GetType accepts. MaxTypeNameLength = 256 // MaxArrayNesting is the maximum number of array dimensions GetType accepts. MaxArrayNesting = 8 // MaxStaticArraySize is the largest static array dimension accepted. MaxStaticArraySize = 1 << 16 )
Limits applied by GetType to untrusted type declarations (CWE-674). Array parsing is recursive and re-slices the declaration at each dimension, so an unbounded declaration could force deep recursion and quadratic allocation.
const (
// EncodedSignLength is the length of the encoded function signature (4 bytes)
EncodedSignLength = 4
)
const (
Int32Size = 32 // Size of encoded values in bytes
)
Constants
Variables ¶
This section is empty.
Functions ¶
func DecodeList ¶
DecodeList decodes a list of encoded values according to parameter types
func DecodeUint ¶
DecodeUint decodes an unsigned integer from encoded bytes at offset
func EncodeIntBig ¶
EncodeIntBig encodes a big.Int to 32 bytes (signed, two's complement)
func EncodeUint ¶
EncodeUint encodes an unsigned int to 32 bytes
Types ¶
type Abi ¶
type Abi struct {
Entries []Entry
}
Abi represents a collection of ABI entries (functions, events)
func (*Abi) DecodeFunction ¶
DecodeFunction decodes a function call by matching signature
type AbiFunction ¶
type AbiFunction struct {
Entry
}
AbiFunction represents an ABI function entry
func NewAbiFunction ¶
func NewAbiFunction(name string, inputs []Param) *AbiFunction
NewAbiFunction creates a new ABI function
func (*AbiFunction) Decode ¶
func (af *AbiFunction) Decode(encoded []byte) ([]interface{}, error)
Decode decodes the encoded function call data (skipping the 4-byte signature)
func (*AbiFunction) Encode ¶
func (af *AbiFunction) Encode(args []interface{}) ([]byte, error)
Encode encodes the function call with signature and arguments
func (*AbiFunction) EncodeSignature ¶
func (af *AbiFunction) EncodeSignature() []byte
EncodeSignature returns the first 4 bytes of the signature hash
type AbiType ¶
type AbiType interface {
GetName() string
GetCanonicalName() string
Encode(value interface{}) ([]byte, error)
Decode(encoded []byte, offset int) (interface{}, error)
GetFixedSize() int
IsDynamicType() bool
}
AbiType is the interface that all ABI types must implement
type AddressType ¶
type AddressType struct {
// contains filtered or unexported fields
}
AddressType represents Zenon address values (20 bytes, left-padded to 32)
func NewAddressType ¶
func NewAddressType() (*AddressType, error)
NewAddressType creates a new address type
func (*AddressType) Decode ¶
func (at *AddressType) Decode(encoded []byte, offset int) (interface{}, error)
Decode decodes an address value from encoded bytes at offset
func (*AddressType) Encode ¶
func (at *AddressType) Encode(value interface{}) ([]byte, error)
Encode encodes an address value to 32 bytes (20-byte address left-padded with 12 zero bytes)
func (*AddressType) GetCanonicalName ¶
func (bt *AddressType) GetCanonicalName() string
func (*AddressType) GetFixedSize ¶
func (bt *AddressType) GetFixedSize() int
func (*AddressType) IsDynamicType ¶
func (bt *AddressType) IsDynamicType() bool
type ArrayType ¶
type ArrayType interface {
AbiType
GetElementType() AbiType
EncodeTuple(values []interface{}) ([]byte, error)
DecodeTuple(encoded []byte, origOffset int, length int) ([]interface{}, error)
EncodeList(values []interface{}) ([]byte, error)
}
ArrayType is an interface for array types (static and dynamic)
type BoolType ¶
type BoolType struct {
IntType
}
BoolType represents boolean values (encoded as uint256: 0 or 1)
func (*BoolType) GetFixedSize ¶
func (bt *BoolType) GetFixedSize() int
func (*BoolType) IsDynamicType ¶
func (bt *BoolType) IsDynamicType() bool
type Bytes32Type ¶
type Bytes32Type = FixedBytesType
Bytes32Type is retained as an alias for compatibility with callers that used the original bytes32-only implementation. Use NewFixedBytesType for any bytes1 through bytes32 declaration.
func NewBytes32Type ¶
func NewBytes32Type(name string) (*Bytes32Type, error)
NewBytes32Type creates a fixed-byte ABI type.
This compatibility constructor historically accepted only "bytes32". It now delegates to NewFixedBytesType, so callers may use it with bytes1 through bytes32. Invalid names return an error.
type BytesType ¶
type BytesType struct {
// contains filtered or unexported fields
}
BytesType represents dynamic byte arrays (length-prefixed, padded to 32-byte multiples)
func (*BytesType) Encode ¶
Encode encodes dynamic bytes with length prefix and padding Format: [32 bytes: length][data padded to 32-byte multiple]
func (*BytesType) GetCanonicalName ¶
func (bt *BytesType) GetCanonicalName() string
func (*BytesType) GetFixedSize ¶
func (bt *BytesType) GetFixedSize() int
func (*BytesType) IsDynamicType ¶
IsDynamicType returns true for BytesType
type DynamicArrayType ¶
type DynamicArrayType struct {
// contains filtered or unexported fields
}
DynamicArrayType represents dynamic arrays like uint256[]
func NewDynamicArrayType ¶
func NewDynamicArrayType(typeName string) (*DynamicArrayType, error)
NewDynamicArrayType creates a new dynamic array type typeName should be in format "elementType[]" e.g. "uint256[]"
func (*DynamicArrayType) Decode ¶
func (dat *DynamicArrayType) Decode(encoded []byte, origOffset int) (interface{}, error)
func (*DynamicArrayType) DecodeTuple ¶
func (dat *DynamicArrayType) DecodeTuple(encoded []byte, origOffset int, length int) ([]interface{}, error)
DecodeTuple decodes array elements from a tuple encoding
func (*DynamicArrayType) Encode ¶
func (dat *DynamicArrayType) Encode(value interface{}) ([]byte, error)
Encode encodes a dynamic array
func (*DynamicArrayType) EncodeList ¶
func (dat *DynamicArrayType) EncodeList(values []interface{}) ([]byte, error)
EncodeList encodes a list of values as a dynamic array with length prefix
func (*DynamicArrayType) EncodeTuple ¶
func (dat *DynamicArrayType) EncodeTuple(values []interface{}) ([]byte, error)
EncodeTuple encodes array elements as a tuple
func (*DynamicArrayType) GetCanonicalName ¶
func (dat *DynamicArrayType) GetCanonicalName() string
GetCanonicalName returns the canonical type name
func (*DynamicArrayType) GetElementType ¶
func (dat *DynamicArrayType) GetElementType() AbiType
GetElementType returns the element type
func (*DynamicArrayType) GetFixedSize ¶
func (dat *DynamicArrayType) GetFixedSize() int
GetFixedSize returns 0 as dynamic arrays don't have a fixed size
func (*DynamicArrayType) IsDynamicType ¶
func (dat *DynamicArrayType) IsDynamicType() bool
IsDynamicType returns true as dynamic arrays are dynamic types
type Entry ¶
Entry represents an ABI entry (function, event, etc.)
func (*Entry) EncodeArguments ¶
EncodeArguments encodes function arguments with proper head/tail separation for dynamic types
func (*Entry) EncodeSignature ¶
EncodeSignature returns the full signature hash
func (*Entry) FingerprintSignature ¶
FingerprintSignature returns the SHA3-256 hash of the signature
func (*Entry) FormatSignature ¶
FormatSignature formats the entry signature as "name(type1,type2,...)"
type FixedBytesType ¶ added in v0.2.0
type FixedBytesType struct {
// contains filtered or unexported fields
}
FixedBytesType represents a fixed bytes1 through bytes32 ABI value.
Values must contain exactly the declared number of bytes. They are encoded left-aligned in a 32-byte ABI word with zero padding on the right.
func NewFixedBytesType ¶ added in v0.2.0
func NewFixedBytesType(name string) (*FixedBytesType, error)
NewFixedBytesType creates a fixed bytes1 through bytes32 ABI type.
Parameters:
- name: Canonical fixed-byte name, such as "bytes4" or "bytes32".
NewFixedBytesType returns the parsed type or an error when the width is missing, non-numeric, or outside the inclusive range 1 through 32.
Example:
typeObject, err := NewFixedBytesType("bytes4")
encoded, err := typeObject.Encode([]byte{0xde, 0xad, 0xbe, 0xef})
Note: Fixed bytes are distinct from dynamic BytesType values and require an exact input length.
func (*FixedBytesType) Decode ¶ added in v0.2.0
func (bt *FixedBytesType) Decode(encoded []byte, offset int) (interface{}, error)
Decode decodes a fixed byte value and rejects non-zero right padding.
func (*FixedBytesType) Encode ¶ added in v0.2.0
func (bt *FixedBytesType) Encode(value interface{}) ([]byte, error)
Encode encodes an exact-length fixed byte value into one 32-byte ABI word.
func (*FixedBytesType) GetCanonicalName ¶ added in v0.2.0
func (bt *FixedBytesType) GetCanonicalName() string
func (*FixedBytesType) GetFixedSize ¶ added in v0.2.0
func (bt *FixedBytesType) GetFixedSize() int
func (*FixedBytesType) IsDynamicType ¶ added in v0.2.0
func (bt *FixedBytesType) IsDynamicType() bool
type FunctionType ¶
type FunctionType struct {
Bytes32Type
}
FunctionType represents a 24-byte function selector (extends Bytes32Type)
func NewFunctionType ¶
func NewFunctionType() (*FunctionType, error)
NewFunctionType creates a new function type
func (*FunctionType) Decode ¶
func (ft *FunctionType) Decode(encoded []byte, offset int) (interface{}, error)
Decode is unimplemented for FunctionType
func (*FunctionType) Encode ¶
func (ft *FunctionType) Encode(value interface{}) ([]byte, error)
Encode encodes a 24-byte function selector The input must be exactly 24 bytes, which will be padded to 32 bytes
type HashType ¶
type HashType struct {
// contains filtered or unexported fields
}
HashType represents hash values (32 bytes, no padding needed)
func (*HashType) GetCanonicalName ¶
func (bt *HashType) GetCanonicalName() string
func (*HashType) GetFixedSize ¶
func (bt *HashType) GetFixedSize() int
func (*HashType) IsDynamicType ¶
func (bt *HashType) IsDynamicType() bool
type IntType ¶
type IntType struct {
NumericType
// contains filtered or unexported fields
}
IntType represents signed integer types (int8 to int256)
func NewIntType ¶
NewIntType creates a new signed integer type
func (*IntType) GetCanonicalName ¶
GetCanonicalName returns the canonical name (int defaults to int256)
func (*IntType) GetFixedSize ¶
func (bt *IntType) GetFixedSize() int
func (*IntType) IsDynamicType ¶
func (bt *IntType) IsDynamicType() bool
type NumericType ¶
type NumericType struct {
// contains filtered or unexported fields
}
NumericType is the base for all numeric types (int, uint)
func (*NumericType) EncodeInternal ¶
func (nt *NumericType) EncodeInternal(value interface{}) (*big.Int, error)
EncodeInternal converts various value types to big.Int
func (*NumericType) GetCanonicalName ¶
func (bt *NumericType) GetCanonicalName() string
func (*NumericType) GetFixedSize ¶
func (bt *NumericType) GetFixedSize() int
func (*NumericType) IsDynamicType ¶
func (bt *NumericType) IsDynamicType() bool
type StaticArrayType ¶
type StaticArrayType struct {
// contains filtered or unexported fields
}
StaticArrayType represents fixed-size arrays like uint256[3]
func NewStaticArrayType ¶
func NewStaticArrayType(typeName string) (*StaticArrayType, error)
NewStaticArrayType creates a new static array type typeName should be in format "elementType[size]" e.g. "uint256[3]"
func (*StaticArrayType) Decode ¶
func (sat *StaticArrayType) Decode(encoded []byte, offset int) (interface{}, error)
Decode decodes a static array from encoded data
func (*StaticArrayType) DecodeTuple ¶
func (sat *StaticArrayType) DecodeTuple(encoded []byte, origOffset int, length int) ([]interface{}, error)
DecodeTuple decodes array elements from a tuple encoding
func (*StaticArrayType) Encode ¶
func (sat *StaticArrayType) Encode(value interface{}) ([]byte, error)
Encode encodes a static array
func (*StaticArrayType) EncodeList ¶
func (sat *StaticArrayType) EncodeList(values []interface{}) ([]byte, error)
EncodeList encodes a list of values as a static array
func (*StaticArrayType) EncodeTuple ¶
func (sat *StaticArrayType) EncodeTuple(values []interface{}) ([]byte, error)
EncodeTuple encodes array elements as a tuple
func (*StaticArrayType) GetCanonicalName ¶
func (sat *StaticArrayType) GetCanonicalName() string
GetCanonicalName returns the canonical type name
func (*StaticArrayType) GetElementType ¶
func (sat *StaticArrayType) GetElementType() AbiType
GetElementType returns the element type
func (*StaticArrayType) GetFixedSize ¶
func (sat *StaticArrayType) GetFixedSize() int
GetFixedSize returns the total size (element size * count)
func (*StaticArrayType) IsDynamicType ¶
func (bt *StaticArrayType) IsDynamicType() bool
type StringType ¶
type StringType struct {
BytesType
}
StringType represents UTF-8 encoded strings (extends BytesType)
func NewStringType ¶
func NewStringType() (*StringType, error)
NewStringType creates a new string type
func (*StringType) Decode ¶
func (st *StringType) Decode(encoded []byte, offset int) (interface{}, error)
Decode decodes a string from encoded bytes at offset
func (*StringType) Encode ¶
func (st *StringType) Encode(value interface{}) ([]byte, error)
Encode encodes a string as UTF-8 bytes using BytesType encoding
func (*StringType) GetCanonicalName ¶
func (bt *StringType) GetCanonicalName() string
func (*StringType) GetFixedSize ¶
func (st *StringType) GetFixedSize() int
GetFixedSize returns 0 as string is a dynamic type
type TokenStandardType ¶
type TokenStandardType struct {
// contains filtered or unexported fields
}
TokenStandardType represents Zenon token standard values (10 bytes, left-padded to 32)
func NewTokenStandardType ¶
func NewTokenStandardType() (*TokenStandardType, error)
NewTokenStandardType creates a new token standard type
func (*TokenStandardType) Decode ¶
func (tst *TokenStandardType) Decode(encoded []byte, offset int) (interface{}, error)
Decode decodes a token standard value from encoded bytes at offset
func (*TokenStandardType) Encode ¶
func (tst *TokenStandardType) Encode(value interface{}) ([]byte, error)
Encode encodes a token standard value to 32 bytes (10-byte ZTS left-padded with 22 zero bytes)
func (*TokenStandardType) GetCanonicalName ¶
func (bt *TokenStandardType) GetCanonicalName() string
func (*TokenStandardType) GetFixedSize ¶
func (bt *TokenStandardType) GetFixedSize() int
func (*TokenStandardType) IsDynamicType ¶
func (bt *TokenStandardType) IsDynamicType() bool
type UnsignedIntType ¶
type UnsignedIntType struct {
NumericType
// contains filtered or unexported fields
}
UnsignedIntType represents unsigned integer types (uint8 to uint256)
func NewUnsignedIntType ¶
func NewUnsignedIntType(name string) (*UnsignedIntType, error)
NewUnsignedIntType creates a new unsigned integer type
func (*UnsignedIntType) Decode ¶
func (uit *UnsignedIntType) Decode(encoded []byte, offset int) (interface{}, error)
Decode decodes an unsigned integer value
func (*UnsignedIntType) Encode ¶
func (uit *UnsignedIntType) Encode(value interface{}) ([]byte, error)
Encode encodes an unsigned integer value
func (*UnsignedIntType) GetCanonicalName ¶
func (uit *UnsignedIntType) GetCanonicalName() string
GetCanonicalName returns the canonical name (uint defaults to uint256)
func (*UnsignedIntType) GetFixedSize ¶
func (bt *UnsignedIntType) GetFixedSize() int
func (*UnsignedIntType) IsDynamicType ¶
func (bt *UnsignedIntType) IsDynamicType() bool