Metadatum
- class cometa.auxiliary_data.metadatum.Metadatum(ptr)[source]
Bases:
objectRepresents a transaction metadatum in Cardano.
Metadatum can be one of five types: map, list, integer, bytes, or text. This is used to attach arbitrary data to transactions.
Example
>>> # Integer metadatum >>> meta = Metadatum.from_int(42) >>> meta.kind <MetadatumKind.INTEGER: 2>
>>> # Text metadatum >>> meta = Metadatum.from_string("Hello") >>> meta.to_str() 'Hello'
- classmethod from_int(value)[source]
Creates a metadatum from a signed integer.
- Parameters:
value (int) – The integer value.
- Returns:
A new Metadatum containing the integer.
- Raises:
CardanoError – If creation fails.
- Return type:
Example
>>> meta = Metadatum.from_int(42)
- classmethod from_uint(value)[source]
Creates a metadatum from an unsigned integer.
- Parameters:
value (int) – The unsigned integer value.
- Returns:
A new Metadatum containing the integer.
- Raises:
CardanoError – If creation fails.
- Return type:
- classmethod from_integer_string(string, base=10)[source]
Creates a metadatum from a string representation of an integer.
This is useful for large integers that don’t fit in int64/uint64.
- Parameters:
string (str) – The string representation of the integer.
base (int) – The numeric base (2-36). Defaults to 10.
- Returns:
A new Metadatum containing the integer.
- Raises:
CardanoError – If creation fails.
- Return type:
Example
>>> meta = Metadatum.from_integer_string("12345678901234567890")
- classmethod from_bytes(data)[source]
Creates a metadatum from bytes.
- Parameters:
data (bytes | bytearray) – The byte data (max 64 bytes per chunk).
- Returns:
A new Metadatum containing the bytes.
- Raises:
CardanoError – If creation fails.
- Return type:
Example
>>> meta = Metadatum.from_bytes(b"\xde\xad\xbe\xef")
- classmethod from_hex(hex_string)[source]
Creates a metadatum from a hexadecimal string.
- Parameters:
hex_string (str) – The hex-encoded bytes.
- Returns:
A new Metadatum containing the bytes.
- Raises:
CardanoError – If creation fails.
- Return type:
Example
>>> meta = Metadatum.from_hex("deadbeef")
- classmethod from_string(text)[source]
Creates a metadatum from a text string.
- Parameters:
text (str) – The text string (max 64 bytes when UTF-8 encoded).
- Returns:
A new Metadatum containing the text.
- Raises:
CardanoError – If creation fails.
- Return type:
Example
>>> meta = Metadatum.from_string("Hello, Cardano!")
- classmethod from_cbor(reader)[source]
Deserializes a Metadatum from CBOR data.
- Parameters:
reader (CborReader) – A CborReader positioned at the metadatum data.
- Returns:
A new Metadatum deserialized from the CBOR data.
- Raises:
CardanoError – If deserialization fails.
- Return type:
- classmethod from_json(json)[source]
Creates a metadatum from a JSON string.
- Parameters:
json (str) – The JSON-encoded metadatum.
- Returns:
A new Metadatum deserialized from the JSON data.
- Raises:
CardanoError – If parsing fails.
- Return type:
Example
>>> meta = Metadatum.from_json('{"int": 42}')
- classmethod from_map(metadatum_map)[source]
Creates a metadatum from a MetadatumMap.
- Parameters:
metadatum_map (MetadatumMap) – The MetadatumMap to convert.
- Returns:
A new Metadatum containing the map.
- Raises:
CardanoError – If creation fails.
- Return type:
Example
>>> meta_map = MetadatumMap() >>> meta_map.insert(Metadatum.from_string("key"), Metadatum.from_int(42)) >>> meta = Metadatum.from_map(meta_map) >>> meta.kind <MetadatumKind.MAP: 0>
- classmethod from_list(metadatum_list)[source]
Creates a metadatum from a MetadatumList or a Python list.
- Parameters:
metadatum_list (Union[MetadatumList, List[MetadatumLike]]) – The MetadatumList or a Python list of metadatum values to convert.
- Returns:
A new Metadatum containing the list.
- Raises:
CardanoError – If creation fails.
- Return type:
Example
>>> meta_list = MetadatumList() >>> meta_list.add(Metadatum.from_int(1)) >>> meta_list.add(Metadatum.from_int(2)) >>> meta = Metadatum.from_list(meta_list) >>> meta.kind <MetadatumKind.LIST: 1>
>>> # Or using a Python list directly >>> meta = Metadatum.from_list([1, "hello", b"bytes"])
- property kind: MetadatumKind
Returns the kind of this metadatum.
- Returns:
The MetadatumKind enum value.
- to_integer()[source]
Converts this metadatum to a BigInt.
- Returns:
The BigInt value.
- Raises:
CardanoError – If this metadatum is not an integer type.
- Return type:
- to_bytes()[source]
Converts this metadatum to bytes.
- Returns:
The byte data.
- Raises:
CardanoError – If this metadatum is not a bytes type.
- Return type:
bytes
- to_str()[source]
Converts this metadatum to a string.
- Returns:
The text string.
- Raises:
CardanoError – If this metadatum is not a text type.
- Return type:
str
- to_json()[source]
Converts this metadatum to a JSON string.
- Returns:
The JSON representation.
- Return type:
str
- to_map()[source]
Converts this metadatum to a MetadatumMap.
- Returns:
The MetadatumMap representation.
- Raises:
CardanoError – If this metadatum is not a map type.
- Return type:
Example
>>> meta = Metadatum.from_map(some_map) >>> retrieved_map = meta.to_map()
- to_list()[source]
Converts this metadatum to a MetadatumList.
- Returns:
The MetadatumList representation.
- Raises:
CardanoError – If this metadatum is not a list type.
- Return type:
Example
>>> meta = Metadatum.from_list(some_list) >>> retrieved_list = meta.to_list()
- to_cip116_json(writer)[source]
Serializes this metadatum to CIP-116 JSON format.
CIP-116 defines a standard JSON representation for transaction metadata.
- Parameters:
writer (JsonWriter) – A JsonWriter to write the serialized data to.
- Raises:
CardanoError – If serialization fails.
- Return type:
None
Example
>>> meta = Metadatum.from_int(42) >>> writer = JsonWriter() >>> meta.to_cip116_json(writer) >>> json_str = writer.encode()
- to_cbor(writer)[source]
Serializes the metadatum to CBOR format.
- Parameters:
writer (CborWriter) – A CborWriter to write the serialized data to.
- Raises:
CardanoError – If serialization fails.
- Return type:
None