Skip to main content
Version: 0.9.1

Micheline Support

Tezos uses Micheline for encoding smart contract data and code. Micheline is strongly typed, but it is rather complex to use. The TzGo micheline package helps you decode, analyze and construct compliant Micheline data structures directly in Go.

Micheline uses basic primitives for encoding types and values. Primitives for a tree with a single root. Each contract storage, call parameter, bigmap key or bigmap value us enoded as a primitive. Primitives are serialized as JSON or binary and TzGo can translate between both representations efficiently. Micheline supports type annotations which are used to express field names, records, bigmap names, etc. TzGo makes use of annotations when translating Micheline values to Go structs.

TzGo defines a basic Prim data type to work with Micheline primitives directly:

type Prim struct {
Type PrimType // primitive type
OpCode OpCode // primitive opcode (invalid on sequences, strings, bytes, int)
Args []Prim // optional nested arguments
Anno []string // optional type annotations
Int *big.Int // decoded value when Prim is an int
String string // decoded value when Prim is a string
Bytes []byte // decoded value when Prim is a byte sequence
WasPacked bool // true when content has been unpacked
}

Since Micheline value encoding is quite verbose and hard to use, TzGo can translate Micheline to map[string]interface{}, Go structs (using struct tags) or by manually calling a TzGo helper functions like GetInt64() or GetAddress(). To facilitate translation, the micheline package exports the following types:

  • Type is a TzGo wrapper primitives which contain annotated type info
  • Value is a TzGo wrapper primitives representing Micheline values in combination with their Type
  • Key is a TzGo wrapper for values that are used as map or bigmap keys

Micheline values can appear in packed form (as output from the Michelson PACK instruction) which is essentially a byte sequences prefixed with 0x05. To unpack such data we provide prim.Unpack() and prim.UnpackAll() functions. Oftentimes the contenst is just an UTF8 string, but in case the data is more complex, there is a helper prim.BuildType() which produces a compatible type primitive to be able to decode packed data via Value.