PlutusData

class cometa.plutus_data.plutus_data.PlutusData(ptr=None)[source]

Bases: object

A type corresponding to the Plutus Core Data datatype.

The point of this type is to be opaque as to ensure that it is only used in ways that Plutus scripts can handle. Use this type to build any data structures that you want to be representable on-chain.

PlutusData can represent: - Integers (arbitrary precision) - Byte strings - Lists of PlutusData - Maps of PlutusData to PlutusData - Constructor applications (for sum types)

Example

>>> # Create from integer
>>> data = PlutusData.from_int(42)
>>> data.kind
PlutusDataKind.INTEGER
>>> # Create from bytes
>>> data = PlutusData.from_bytes(b"hello")
>>> data.kind
PlutusDataKind.BYTES
>>> # Create from string (UTF-8 encoded as bytes)
>>> data = PlutusData.from_string("hello")
>>> data.kind
PlutusDataKind.BYTES
__init__(ptr=None)[source]
Return type:

None

__enter__()[source]
Return type:

PlutusData

__exit__(exc_type, exc_val, exc_tb)[source]
Return type:

None

__repr__()[source]

Return repr(self).

Return type:

str

__eq__(other)[source]

Return self==value.

Parameters:

other (object)

Return type:

bool

property kind: PlutusDataKind

Gets the kind of this PlutusData.

Returns:

The PlutusDataKind indicating what type of data this holds.

classmethod from_cbor(reader)[source]

Deserializes PlutusData from CBOR.

Parameters:

reader (CborReader) – A CborReader positioned at the PlutusData.

Returns:

A new PlutusData instance.

Raises:

CardanoError – If deserialization fails.

Return type:

PlutusData

to_cbor(writer)[source]

Serializes the PlutusData to CBOR format.

Parameters:

writer (CborWriter) – A CborWriter to write the serialized data to.

Raises:

CardanoError – If serialization fails.

Return type:

None

clear_cbor_cache()[source]

Clears the cached CBOR representation.

This is useful when you have modified the PlutusData after it was created from CBOR and you want to ensure that the next serialization reflects the current state rather than using the original cached CBOR.

Warning

Clearing the CBOR cache may change the binary representation when serialized, which can alter the data and invalidate existing signatures.

Return type:

None

to_cip116_json(writer)[source]

Serializes this object to CIP-116 compliant JSON.

Parameters:

writer (JsonWriter) – The JsonWriter to write the JSON to.

Raises:

CardanoError – If serialization fails.

Return type:

None

classmethod from_int(value)[source]

Creates PlutusData from a Python integer.

Parameters:

value (int) – The integer value (can be arbitrarily large).

Returns:

A new PlutusData containing the integer.

Raises:

CardanoError – If creation fails.

Return type:

PlutusData

classmethod from_bigint(bigint)[source]

Creates PlutusData from a BigInt.

Parameters:

bigint (BigInt) – The BigInt value.

Returns:

A new PlutusData containing the integer.

Raises:

CardanoError – If creation fails.

Return type:

PlutusData

classmethod from_bytes(data)[source]

Creates PlutusData from bytes.

Parameters:

data (bytes) – The byte data.

Returns:

A new PlutusData containing the bytes.

Raises:

CardanoError – If creation fails.

Return type:

PlutusData

classmethod from_hex(hex_string)[source]

Creates PlutusData from a hexadecimal string.

Parameters:

hex_string (str) – The hexadecimal string (e.g., “deadbeef”).

Returns:

A new PlutusData containing the bytes.

Raises:

CardanoError – If creation fails.

Return type:

PlutusData

classmethod from_string(value)[source]

Creates PlutusData from a string (UTF-8 encoded as bytes).

Parameters:

value (str) – The string value.

Returns:

A new PlutusData containing the UTF-8 encoded bytes.

Raises:

CardanoError – If creation fails.

Return type:

PlutusData

classmethod from_list(plutus_list)[source]

Creates PlutusData from a PlutusList or a Python list.

Parameters:

plutus_list (Union[PlutusList, List[Union['PlutusData', int, str, bytes]]]) – The PlutusList or a Python list of PlutusData/native values.

Returns:

A new PlutusData containing the list.

Raises:

CardanoError – If creation fails.

Return type:

PlutusData

Example

>>> # Using a Python list directly
>>> data = PlutusData.from_list([42, "hello", b"bytes"])
classmethod from_map(plutus_map)[source]

Creates PlutusData from a PlutusMap.

Parameters:

plutus_map (PlutusMap) – The PlutusMap.

Returns:

A new PlutusData containing the map.

Raises:

CardanoError – If creation fails.

Return type:

PlutusData

classmethod from_constr(constr)[source]

Creates PlutusData from a ConstrPlutusData.

Parameters:

constr (ConstrPlutusData) – The ConstrPlutusData.

Returns:

A new PlutusData containing the constructor.

Raises:

CardanoError – If creation fails.

Return type:

PlutusData

to_integer()[source]

Converts this PlutusData to a BigInt.

Returns:

The BigInt value if this is an integer.

Raises:

CardanoError – If conversion fails or this is not an integer.

Return type:

BigInt

to_int()[source]

Converts this PlutusData to a Python integer.

Returns:

The integer value if this is an integer.

Raises:

CardanoError – If conversion fails or this is not an integer.

Return type:

int

to_bytes()[source]

Converts this PlutusData to bytes.

Returns:

The bytes value if this is a bytes type.

Raises:

CardanoError – If conversion fails or this is not bytes.

Return type:

bytes

to_string()[source]

Converts this PlutusData bytes to a UTF-8 string.

Returns:

The string value if this is a bytes type with valid UTF-8.

Raises:
  • CardanoError – If conversion fails or this is not bytes.

  • UnicodeDecodeError – If the bytes are not valid UTF-8.

Return type:

str

to_list()[source]

Converts this PlutusData to a PlutusList.

Returns:

The PlutusList if this is a list type.

Raises:

CardanoError – If conversion fails or this is not a list.

Return type:

PlutusList

to_map()[source]

Converts this PlutusData to a PlutusMap.

Returns:

The PlutusMap if this is a map type.

Raises:

CardanoError – If conversion fails or this is not a map.

Return type:

PlutusMap

to_constr()[source]

Converts this PlutusData to a ConstrPlutusData.

Returns:

The ConstrPlutusData if this is a constructor type.

Raises:

CardanoError – If conversion fails or this is not a constructor.

Return type:

ConstrPlutusData

static to_plutus_data(value)[source]

Converts a native Python value or Plutus container to PlutusData.

Supports: - PlutusData - PlutusList - PlutusMap - ConstrPlutusData - int - str (UTF-8) - bytes

Parameters:

value (PlutusDataLike)

Return type:

PlutusData

__hash__ = None