Datum
- class cometa.common.datum.Datum(ptr)[source]
Bases:
objectRepresents a piece of data attached to a UTxO for Plutus script interaction.
Datums act as state for UTxOs, allowing Plutus scripts to perform complex logic based on stored data when the UTxO is being spent. There are two types:
DATA_HASH: A hash reference to off-chain datum data
INLINE_DATA: The actual Plutus data stored directly on-chain
Example
>>> datum = Datum.from_data_hash_hex("00" * 32) >>> datum.datum_type DatumType.DATA_HASH
- classmethod from_data_hash(hash_value)[source]
Creates a datum from a Blake2b hash reference.
- Parameters:
hash_value (Blake2bHash) – The Blake2b hash of the datum data.
- Returns:
A new Datum instance with type DATA_HASH.
- Raises:
CardanoError – If creation fails.
- Return type:
Example
>>> hash_val = Blake2bHash.from_hex("00" * 32) >>> datum = Datum.from_data_hash(hash_val)
- classmethod from_data_hash_hex(hex_string)[source]
Creates a datum from a hexadecimal hash string.
- Parameters:
hex_string (str) – The hash as a hexadecimal string.
- Returns:
A new Datum instance with type DATA_HASH.
- Raises:
CardanoError – If creation fails or hash is invalid.
- Return type:
Example
>>> datum = Datum.from_data_hash_hex("abcd1234" * 8)
- classmethod from_data_hash_bytes(data)[source]
Creates a datum from raw hash bytes.
- Parameters:
data (bytes | bytearray) – The hash as raw bytes.
- Returns:
A new Datum instance with type DATA_HASH.
- Raises:
CardanoError – If creation fails.
- Return type:
Example
>>> datum = Datum.from_data_hash_bytes(bytes(32))
- classmethod from_inline_data(data)[source]
Creates a datum from inline Plutus data.
- Parameters:
data – The Plutus data to store inline (PlutusData instance).
- Returns:
A new Datum instance with type INLINE_DATA.
- Raises:
CardanoError – If creation fails.
- Return type:
Example
>>> plutus_data = PlutusData.from_int(42) >>> datum = Datum.from_inline_data(plutus_data)
- classmethod from_cbor(reader)[source]
Deserializes a Datum from CBOR data.
- Parameters:
reader (CborReader) – A CborReader positioned at the datum data.
- Returns:
A new Datum deserialized from the CBOR data.
- Raises:
CardanoError – If deserialization fails.
- Return type:
- property data_hash: Blake2bHash | None
Returns the hash associated with this datum.
Returns None if this is an inline datum without a hash.
- property data_hash_hex: str
Returns the data hash as a hexadecimal string.
- property data_hash_bytes: bytes
Returns the data hash as raw bytes.
- get_inline_data()[source]
Returns the inline Plutus data if this is an inline datum.
Returns None if this is a data hash datum.
Note: This method requires the PlutusData module to be available. Import PlutusData from cometa.plutus_data before calling this method.
- Raises:
ImportError – If PlutusData module is not available.
- to_cbor(writer)[source]
Serializes the datum to CBOR format.
- Parameters:
writer (CborWriter) – A CborWriter to write the serialized data to.
- Raises:
CardanoError – If serialization fails.
- Return type:
None
- to_cip116_json(writer)[source]
Converts this object to CIP-116 compliant JSON representation.
CIP-116 defines a standard JSON format for Cardano data structures.
- Parameters:
writer (JsonWriter) – A JsonWriter to write the serialized data to.
- Raises:
CardanoError – If conversion fails.
- Return type:
None