AssetId

class cometa.assets.asset_id.AssetId(ptr)[source]

Bases: object

Represents a unique asset identifier in the Cardano blockchain.

An asset ID is composed of a policy ID (Blake2b-224 hash) and an asset name. The special asset ID for Lovelace (ADA) has no policy ID or asset name.

Example

>>> policy_id = Blake2bHash.from_hex("00" * 28)
>>> asset_name = AssetName.from_string("MyToken")
>>> asset_id = AssetId.new(policy_id, asset_name)
__init__(ptr)[source]
Return type:

None

__enter__()[source]
Return type:

AssetId

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

None

__repr__()[source]

Return repr(self).

Return type:

str

classmethod new(policy_id, asset_name)[source]

Creates a new asset ID from a policy ID and asset name.

Parameters:
  • policy_id (Blake2bHash) – The minting policy ID (Blake2b-224 hash).

  • asset_name (AssetName) – The name of the asset.

Returns:

A new AssetId instance.

Raises:

CardanoError – If creation fails.

Return type:

AssetId

Example

>>> policy_id = Blake2bHash.from_hex("00" * 28)
>>> asset_name = AssetName.from_string("MyToken")
>>> asset_id = AssetId.new(policy_id, asset_name)
classmethod new_lovelace()[source]

Creates an asset ID representing Lovelace (ADA).

Lovelace is the native currency of Cardano and has no associated policy ID or asset name.

Returns:

A new AssetId representing Lovelace.

Raises:

CardanoError – If creation fails.

Return type:

AssetId

Example

>>> lovelace = AssetId.new_lovelace()
>>> lovelace.is_lovelace
True
classmethod from_bytes(data)[source]

Creates an asset ID from raw bytes.

The bytes consist of the policy ID (28 bytes) followed by the asset name (0-32 bytes).

Parameters:

data (bytes | bytearray) – The asset ID as raw bytes (minimum 28 bytes).

Returns:

A new AssetId instance.

Raises:

CardanoError – If creation fails.

Return type:

AssetId

classmethod from_hex(hex_string)[source]

Creates an asset ID from a hexadecimal string.

Parameters:

hex_string (str) – The asset ID as a hexadecimal string.

Returns:

A new AssetId instance.

Raises:

CardanoError – If creation fails or hex is invalid.

Return type:

AssetId

Example

>>> asset_id = AssetId.from_hex(policy_hex + asset_name_hex)
to_bytes()[source]

Returns the asset ID as raw bytes.

Returns:

The asset ID bytes (policy ID + asset name).

Return type:

bytes

to_hex()[source]

Returns the asset ID as a hexadecimal string.

Returns:

The asset ID as a hex string.

Return type:

str

property is_lovelace: bool

Checks if this asset ID represents Lovelace (ADA).

Returns:

True if this is the Lovelace asset ID, False otherwise.

property policy_id: Blake2bHash | None

Retrieves the policy ID from this asset ID.

Returns:

The policy ID, or None if this is the Lovelace asset.

Note

Call is_lovelace first to check if policy_id will be available.

property asset_name: AssetName | None

Retrieves the asset name from this asset ID.

Returns:

The asset name, or None if this is the Lovelace asset.

Note

Call is_lovelace first to check if asset_name will be available.

__eq__(other)[source]

Checks equality with another AssetId.

Parameters:

other (object)

Return type:

bool

__hash__()[source]

Returns a Python hash for use in sets and dicts.

Return type:

int

__str__()[source]

Returns the hexadecimal representation.

Return type:

str