Michelson type encoding

Forging / unforging data

pytezos.michelson.forge.forge_entrypoint(entrypoint) → bytes[source]

Encode Michelson contract entrypoint into the byte form.

Parameters

entrypoint – string

pytezos.michelson.forge.forge_int(value: int) → bytes[source]

Encode signed unbounded integer into the byte form.

Parameters

value – signed unbounded integer

pytezos.michelson.forge.forge_micheline(data) → bytes[source]

Encode a Micheline expression into the byte form.

Parameters

data – Micheline expression

pytezos.michelson.forge.forge_script(script) → bytes[source]

Encode an origination script into the byte form.

Parameters

script – {“code”: “$Micheline_expression”, “storage”: “$Micheline_expression”}

pytezos.michelson.forge.parse_int(data: bytes) -> (<class 'int'>, <class 'int'>)[source]

Decode signed unbounded integer from bytes.

Parameters

data – Encoded integer

Returns

tuple(parsed integer, length in bytes)

pytezos.michelson.forge.unforge_micheline(data: bytes)[source]

Parse Micheline expression from its encoded form (but do not UNPACK).

Parameters

data – Encoded Micheline expression

Returns

Object

pytezos.encoding.base58_decode(v: bytes) → bytes[source]

Decode data using Base58 with checksum + validate binary prefix against known kinds and cut in the end.

Parameters

v – Array of bytes (use string.encode())

Returns

bytes

pytezos.encoding.base58_encode(v: bytes, prefix: bytes) → bytes[source]

Encode data using Base58 with checksum and add an according binary prefix in the end.

Parameters
  • v – Array of bytes

  • prefix – Human-readable prefix (use b’’) e.g. b’tz’, b’KT’, etc

Returns

bytes (use string.decode())

pytezos.encoding.forge_address(value: str, tz_only=False) → bytes[source]

Encode address or key hash into bytes.

Parameters
  • value – base58 encoded address or key_hash

  • tz_only – True indicates that it’s a key_hash (will be encoded in a more compact form)

pytezos.encoding.forge_array(data, len_bytes=4) → bytes[source]

Encode array of bytes (prepend length).

Parameters
  • data – list of bytes

  • len_bytes – number of bytes to store array length

pytezos.encoding.forge_base58(value: str) → bytes[source]

Encode base58 string into bytes.

Parameters

value – base58 encoded value (with checksum)

pytezos.encoding.forge_bool(value: bool) → bytes[source]

Encode boolean value into bytes.

pytezos.encoding.forge_contract(value) → bytes[source]

Encode a value of contract type (address + optional entrypoint) into bytes.

Parameters

value – ‘tz12345’ or ‘tz12345%default’

pytezos.encoding.forge_nat(value) → bytes[source]

Encode a number using LEB128 encoding (Zarith).

Parameters

value (int) – the value to encode

Returns

encoded value

Return type

bytes

pytezos.encoding.forge_public_key(value) → bytes[source]

Encode public key into bytes.

Parameters

value – public key in in base58 form

pytezos.encoding.forge_timestamp(value) → int[source]

Encode timestamp into bytes.

Parameters

value – unix timestamp in seconds (int)

pytezos.encoding.is_bh(v) → bool[source]

Check if value is a block hash.

pytezos.encoding.is_chain_id(v) → bool[source]

Check if value is a chain id.

pytezos.encoding.is_key(v) → bool[source]

Check if value is a public key.

pytezos.encoding.is_kt(v) → bool[source]

Check if value is a KT address.

pytezos.encoding.is_ogh(v) → bool[source]

Check if value is an operation group hash.

pytezos.encoding.is_pkh(v) → bool[source]

Check if value is a public key hash.

pytezos.encoding.is_sig(v) → bool[source]

Check if value is a signature.

pytezos.encoding.parse_address(data: bytes)[source]

Decode address or key_hash from bytes.

Parameters

data – encoded address or key_hash

Returns

base58 encoded address

pytezos.encoding.parse_array(data, len_bytes=4) → tuple[source]

Decode array of bytes.

Parameters
  • data – encoded array

  • len_bytes – number of bytes to store array length

Returns

Tuple[list of bytes, array length]

pytezos.encoding.parse_chain_id(data: bytes)[source]

Decode chain id from byte form.

Parameters

data – encoded chain id.

Returns

base58 encoded chain id

pytezos.encoding.parse_contract(data: bytes)[source]

Decode contract (address + optional entrypoint) from bytes

Parameters

data – encoded contract

Returns

base58 encoded address and entrypoint (if exists) separated by %

pytezos.encoding.parse_public_key(data: bytes) → str[source]

Decode public key from byte form.

Parameters

data – encoded public key.

Returns

base58 encoded public key

pytezos.encoding.parse_signature(data: bytes)[source]

Decode signature from byte form.

Parameters

data – encoded signature.

Returns

base58 encoded signature (generic)

pytezos.encoding.validate_pkh(v)[source]

Ensure parameter is a public key hash (starts with b’tz1’, b’tz2’, b’tz3’)

Parameters

v – string or bytes

Raises

ValueError – if parameter is not a public key hash

pytezos.encoding.validate_sig(v)[source]

Ensure parameter is a signature (starts with b’edsig’, b’spsig’, b’p2sig’, b’sig’)

Parameters

v – string or bytes

Raises

ValueError – if parameter is not a signature

Packing / unpacking data

pytezos.michelson.pack.get_key_hash(val_expr, type_expr, bin_path='') → str[source]

Get Big_map key hash from key and its type

Parameters
  • val_expr – key expression (Micheline expression)

  • type_expr – type expression (can be key type or type of the whole storage)

  • bin_path – binary path to the key (if storage type is passed to the prev argument)

Returns

Base58 encoded key hash “expr…”

pytezos.michelson.pack.pack(val_expr, type_expr) → bytes[source]

Basically prepack (pack domain types) + forge (encode bytes). Resulting value is prefixed with 0x05.

Parameters
  • val_expr – value expression (Micheline expression)

  • type_expr – type expression (Micheline expression)

pytezos.michelson.pack.prepack_micheline(val_expr, type_expr)[source]

Recursively pack domain types into compact form, e.g. {“string”: “tzabcd”} -> {“bytes”: “0001deadbeef”}

Parameters
  • val_expr – value expression (Micheline expression)

  • type_expr – type expression (Micheline expression)

Returns

Micheline expression

pytezos.michelson.pack.unpack(data: bytes, type_expr)[source]

Unpack bytes (currently without unpacking domain types, so it’s unforging + cutting 0x05 prefix).

Parameters
  • data – Packed data

  • type_expr – type of the packed data (currently unused)

Returns

Micheline expression

Type schema and data encoding

class pytezos.michelson.micheline.Nested(prim, args)
property args

Alias for field number 1

property prim

Alias for field number 0

class pytezos.michelson.micheline.Pair[source]
class pytezos.michelson.micheline.Schema(metadata, bin_types, bin_names, json_to_bin)
property bin_names

Alias for field number 2

property bin_types

Alias for field number 1

property json_to_bin

Alias for field number 3

property metadata

Alias for field number 0

pytezos.michelson.micheline.build_maps(metadata: dict)[source]
pytezos.michelson.micheline.collapse_micheline(code) → dict[source]
pytezos.michelson.micheline.encode_literal(value, prim, binary=False)[source]
pytezos.michelson.micheline.get_flat_nested(nested: pytezos.michelson.micheline.Nested)[source]
pytezos.michelson.micheline.is_optional(schema, bin_path)[source]
pytezos.michelson.micheline.make_default(bin_types: dict, root='0')[source]
pytezos.michelson.micheline.make_micheline(bin_values: dict, bin_types: dict, bin_root='0', binary=False)[source]
pytezos.michelson.micheline.michelson_parser()[source]
pytezos.michelson.micheline.michelson_to_micheline(data, parser=None)[source]

Converts michelson source text into Micheline expression.

Parameters
  • data – Michelson string

  • parser – custom Michelson parser

Returns

Micheline expression

pytezos.michelson.micheline.parse_json(data, schema: pytezos.michelson.micheline.Schema, bin_root='0')[source]
pytezos.michelson.micheline.parse_micheline(val_expr, type_expr, schema: pytezos.michelson.micheline.Schema, bin_root='0')[source]
pytezos.michelson.micheline.skip_nones(**kwargs) → dict[source]

Converting between formats

class pytezos.michelson.converter.BigMapSchema(bin_to_id, id_to_bin)
property bin_to_id

Alias for field number 0

property id_to_bin

Alias for field number 1

exception pytezos.michelson.converter.MichelineSchemaError[source]
pytezos.michelson.converter.build_big_map_schema(data, schema: pytezos.michelson.micheline.Schema)pytezos.michelson.converter.BigMapSchema[source]

Generate Big_map schema from the contract storage

Parameters
  • data – Raw storage (Micheline expression)

  • schema – Storage schema

Returns

Mappings: Big_map id to JSON path and vice versa

Return type

BigMapSchema

pytezos.michelson.converter.build_schema(code)pytezos.michelson.micheline.Schema[source]

Creates internal structures necessary for decoding/encoding micheline: metadata -> micheline tree with collapsed pair, or, and option nodes bin_types -> maps binary path to primitive bin_names -> binary path to key name mapping json_to_bin -> json path to binary path mapping

Parameters

code – parameter or storage section of smart contract source code (in micheline)

Return type

Schema

pytezos.michelson.converter.convert(source, schema: pytezos.michelson.micheline.Schema = None, output='micheline', inline=False)[source]

Convert data between different representations (DO NOT USE FOR STORAGE/PARAMETER, can be ambiguous).

Parameters
  • source – Data, can be one of Michelson (string), Micheline expression, object

  • schema – Needed if decoding/encoding objects (optional)

  • output – Output format, one of ‘micheline’ (default), ‘michelson’, ‘object’

  • inline – Used for michelson output, whether to omit line breaks

pytezos.michelson.converter.decode_micheline(val_expr, type_expr, schema: pytezos.michelson.micheline.Schema, root='0')[source]

Converts Micheline data into Python object.

Parameters
  • val_expr – Micheline value expression

  • type_expr – Michelson type expression for the entire type

  • schema – schema built for particular contract/section

  • root – which binary node to take as root, used to decode BigMap values/diffs

Returns

Object

pytezos.michelson.converter.encode_micheline(data, schema: pytezos.michelson.micheline.Schema, root='0', binary=False)[source]

Converts Python object into Micheline expression.

Parameters
  • data – Python object

  • schema – schema built for particular contract/section

  • root – which binary node to take as root, used to encode BigMap values

  • binary – Encode keys and addresses in bytes rather than strings, default is False

Returns

Micheline expression

pytezos.michelson.converter.is_micheline(value) → bool[source]

Check if value is a Micheline expression (using heuristics, so not 100% accurate).

Parameters

value – Object

Return type

bool

Type docstrings

pytezos.michelson.docstring.generate_docstring(schema: pytezos.michelson.micheline.Schema, title, root='0') → str[source]

Generate Michelson type (of arbitrary complexity) documentation in a more readable form.

Parameters
  • schema – parameter/storage schema

  • title – documentation title

  • root – binary path to the root element (default is ‘0’)

Returns

formatted docstring