abi

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 11 Imported by: 4

Documentation

Overview

Package abi provides an implementation of the Algorand ARC-4 ABI type system.

See https://arc.algorand.foundation/ARCs/arc-0004 for the corresponding specification.

Basic Operations

This package can parse ABI type names using the `abi.TypeOf()` function.

`abi.TypeOf()` returns an `abi.Type` struct. The `abi.Type` struct's `Encode` and `Decode` methods can convert between Go values and encoded ABI byte strings.

Index

Constants

View Source
const AccountReferenceType = "account"

AccountReferenceType is the ABI argument type string for account references

View Source
const AnyTransactionType = "txn"

AnyTransactionType is the ABI argument type string for a nonspecific transaction argument

View Source
const ApplicationCallTransactionType = "appl"

ApplicationCallTransactionType is the ABI argument type string for an application call transaction argument

View Source
const ApplicationReferenceType = "application"

ApplicationReferenceType is the ABI argument type string for application references

View Source
const AssetConfigTransactionType = "acfg"

AssetConfigTransactionType is the ABI argument type string for an asset configuration transaction argument

View Source
const AssetFreezeTransactionType = "afrz"

AssetFreezeTransactionType is the ABI argument type string for an asset freeze transaction argument

View Source
const AssetReferenceType = "asset"

AssetReferenceType is the ABI argument type string for asset references

View Source
const AssetTransferTransactionType = "axfer"

AssetTransferTransactionType is the ABI argument type string for an asset transfer transaction argument

View Source
const KeyRegistrationTransactionType = "keyreg"

KeyRegistrationTransactionType is the ABI argument type string for a key registration transaction argument

View Source
const PaymentTransactionType = "pay"

PaymentTransactionType is the ABI argument type string for a payment transaction argument

View Source
const VoidReturnType = "void"

VoidReturnType is the ABI return type string for a method that does not return any value

Variables

This section is empty.

Functions

func IsReferenceType

func IsReferenceType(s string) bool

IsReferenceType checks if a type string represents a reference type argument, such as "account", "asset", or "application".

func IsTransactionType

func IsTransactionType(s string) bool

IsTransactionType checks if a type string represents a transaction type argument, such as "txn", "pay", "keyreg", etc.

func ParseMethodSignature

func ParseMethodSignature(methodSig string) (name string, argTypes []string, returnType string, err error)

ParseMethodSignature parses a method of format `method(argType1,argType2,...)retType` into `method` {`argType1`,`argType2`,...} and `retType`

NOTE: This function **DOES NOT** verify that the argument or return type strings represent valid ABI types. Consider using `VerifyMethodSignature` prior to calling this function if you wish to verify those types.

func VerifyMethodSignature

func VerifyMethodSignature(methodSig string) error

VerifyMethodSignature checks if a method signature and its referenced types can be parsed properly

Types

type Type

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

Type is the struct that represents an ABI type.

Do not use the zero value of this struct. Use the `TypeOf` function to create an instance of an ABI type.

func MakeTupleType

func MakeTupleType(argumentTypes []Type) (Type, error)

MakeTupleType makes tuple ABI type by taking an array of tuple element types as argument.

func TypeOf

func TypeOf(str string) (Type, error)

TypeOf parses an ABI type string. For example: `TypeOf("(uint64,byte[])")`

Note: this function only supports "basic" ABI types. Reference types and transaction types are not supported and will produce an error.

func (Type) ByteLen

func (t Type) ByteLen() (int, error)

ByteLen method calculates the byte length of a static ABI type.

func (Type) Decode

func (t Type) Decode(encoded []byte) (interface{}, error)

Decode is an ABI type method to decode bytes to Go values.

To decode an encoded ABI value to a Go interface value, this function stores the result in one of these interface values:

bool, for ABI `bool` types
uint8/byte, for ABI `byte`, `uint8`, and `ufixed8x<M>` types, for all `M`
uint16, for ABI `uint16` and `ufixed16x<M>` types, for all `M`
uint32, for ABI `uint24`, `uint32`, `ufixed24x<M>`, and `ufixed32x<M>` types, for all `M`
uint64, for ABI `uint48`, `uint56`, `uint64`, `ufixed48x<M>`, `ufixed56x<M>`, `ufixed64x<M>`, for all `M`
*big.Int, for ABI `uint<N>` and `ufixed<N>x<M>`, for all 72 <= `N` <= 512, and all `M`
string, for ABI `string` types
[]byte, for ABI `address` types
[]interface{}, for ABI static array, dynamic array, and tuple types

func (Type) Encode

func (t Type) Encode(value interface{}) ([]byte, error)

Encode is an ABI type method to encode Go values into bytes.

Depending on the ABI type instance, different values are acceptable for this method.

The ABI `bool` type accepts Go bool types.

The ABI `byte` type accepts Go byte/uint8 types.

The ABI `uint<N>` and `ufixed<N>x<M>` types accept all native Go integer types (uint/uint8/uint16/uint32/uint64/int/int8/int16/int32/int64) and *big.Int. However, an error will be returned if a negative value is given.

The ABI `string` type accepts Go string types.

The ABI `address`, static array, dynamic array, and tuple types accept slices and arrays of interfaces or specific types that are compatible with the contents of the ABI type's contained types. For example, the `address` type accepts Go types []interface{}, [32]interface{}, []byte, and [32]byte.

func (Type) Equal

func (t Type) Equal(t0 Type) bool

Equal method decides the equality of two types: t == t0.

func (Type) IsDynamic

func (t Type) IsDynamic() bool

IsDynamic method decides if an ABI type is dynamic or static.

func (Type) MarshalToJSON

func (t Type) MarshalToJSON(value interface{}) ([]byte, error)

MarshalToJSON convert golang value to JSON format from ABI type

func (Type) String

func (t Type) String() string

String serialize an ABI Type to a string in ABI encoding.

func (Type) UnmarshalFromJSON

func (t Type) UnmarshalFromJSON(jsonEncoded []byte) (interface{}, error)

UnmarshalFromJSON convert bytes to golang value following ABI type and encoding rules

type TypeKind

type TypeKind uint32

TypeKind is an enum value which indicates the kind of an ABI type.

const (
	// InvalidType represents an invalid and unused TypeKind.
	InvalidType TypeKind = iota
	// Uint is kind for ABI unsigned integer types, i.e. `uint<N>`.
	Uint
	// Byte is kind for the ABI `byte` type.
	Byte
	// Ufixed is the kind for ABI unsigned fixed point decimal types, i.e. `ufixed<N>x<M>`.
	Ufixed
	// Bool is the kind for the ABI `bool` type.
	Bool
	// ArrayStatic is the kind for ABI static array types, i.e. `<type>[<length>]`.
	ArrayStatic
	// Address is the kind for the ABI `address` type.
	Address
	// ArrayDynamic is the kind for ABI dynamic array types, i.e. `<type>[]`.
	ArrayDynamic
	// String is the kind for the ABI `string` type.
	String
	// Tuple is the kind for ABI tuple types, i.e. `(<type 0>,...,<type k>)`.
	Tuple
)

Jump to

Keyboard shortcuts

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