ConstrPlutusData

class cometa.plutus_data.constr_plutus_data.ConstrPlutusData(alternative=None, data=None, ptr=None)[source]

Bases: object

Represents the nth constructor of a ‘Sum Type’ along with its arguments.

In Plutus, data types are represented using constructors. This class represents a specific constructor (identified by its alternative index) and its arguments as a PlutusList.

The encoding scheme is: - Alternatives 0-6 -> CBOR tags 121-127 - Alternatives 7-127 -> CBOR tags 1280-1400 - Other alternatives -> CBOR tag 102 with alternative as unsigned integer

Example

>>> # Create a constructor with alternative 0 (e.g., for a "Just" value)
>>> args = PlutusList()
>>> args.append(42)
>>> constr = ConstrPlutusData(0, args)
>>> constr.alternative
0
>>> len(constr.data)
1
>>> # Create without initial data
>>> constr = ConstrPlutusData(1)
>>> constr.alternative
1
>>> len(constr.data)
0
Parameters:
  • alternative (int)

  • data (Union[PlutusList, List[Union['PlutusData', int, str, bytes]]])

__init__(alternative=None, data=None, ptr=None)[source]

Creates a new ConstrPlutusData.

Parameters:
  • alternative (int) – The constructor alternative number (nth constructor of Sum Type).

  • data (Union[PlutusList, List[Union['PlutusData', int, str, bytes]]]) – The arguments as a PlutusList or a Python list. If None, an empty list is used.

  • ptr – Internal pointer for wrapping existing C objects.

Raises:

CardanoError – If creation fails.

Return type:

None

__enter__()[source]
Return type:

ConstrPlutusData

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

None

__repr__()[source]

Return repr(self).

Return type:

str

__eq__(other)[source]

Checks equality with another ConstrPlutusData.

Parameters:

other (object)

Return type:

bool

property alternative: int

Gets the constructor alternative number.

Returns:

The alternative number (nth constructor of Sum Type).

property data: PlutusList

Gets the arguments of this constructor.

Returns:

A PlutusList containing the constructor arguments.

classmethod from_cbor(reader)[source]

Deserializes a ConstrPlutusData from CBOR data.

Parameters:

reader (CborReader) – A CborReader positioned at the constructor data.

Returns:

A new ConstrPlutusData deserialized from the CBOR data.

Raises:

CardanoError – If deserialization fails.

Return type:

ConstrPlutusData

__hash__ = None
to_cbor(writer)[source]

Serializes the constructor 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.

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