Anchor

class cometa.common.anchor.Anchor(ptr)[source]

Bases: object

Represents an anchor for off-chain metadata in Cardano governance.

An anchor is a pair of: - A URL pointing to a JSON payload of metadata - A Blake2b-256 hash of the contents at that URL

Anchors are used throughout Cardano’s governance system to reference off-chain metadata while maintaining on-chain integrity through hash verification:

  • Governance actions: Justification and context for proposals

  • DRep registration: Optional metadata about the DRep

  • Votes: Supporting information for voting decisions

  • Constitution: Reference to the constitution document

  • Treasury withdrawals: Justification for withdrawal requests

The hash should be computed as the Blake2b-256 hash of the raw bytes received from the URL, ensuring there are no ambiguities in verification.

Note: The on-chain rules do not validate the URL or verify the hash against actual content. Client applications must perform verification when fetching content from the provided URL. If the hash doesn’t match, the metadata should be considered invalid.

Example

>>> anchor = Anchor.from_hash_hex(
...     "https://example.com/metadata.json",
...     "abc123..." * 2  # 32-byte hash as hex
... )
__init__(ptr)[source]
Return type:

None

__enter__()[source]
Return type:

Anchor

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

None

__repr__()[source]

Return repr(self).

Return type:

str

classmethod new(url, hash_value)[source]

Creates a new anchor with a URL and hash.

Parameters:
  • url (str) – The URL pointing to the metadata JSON.

  • hash_value (Blake2bHash) – The Blake2b hash of the metadata content.

Returns:

A new Anchor instance.

Raises:

CardanoError – If creation fails.

Return type:

Anchor

Example

>>> hash_val = Blake2bHash.from_hex("00" * 32)
>>> anchor = Anchor.new("https://example.com/meta.json", hash_val)
classmethod from_hash_hex(url, hash_hex)[source]

Creates an anchor from a URL and hexadecimal hash string.

Parameters:
  • url (str) – The URL pointing to the metadata JSON.

  • hash_hex (str) – The hash as a hexadecimal string.

Returns:

A new Anchor instance.

Raises:

CardanoError – If creation fails or hash is invalid.

Return type:

Anchor

Example

>>> anchor = Anchor.from_hash_hex(
...     "https://example.com/meta.json",
...     "abcd1234" * 8
... )
classmethod from_hash_bytes(url, hash_bytes)[source]

Creates an anchor from a URL and raw hash bytes.

Parameters:
  • url (str) – The URL pointing to the metadata JSON.

  • hash_bytes (bytes | bytearray) – The hash as raw bytes.

Returns:

A new Anchor instance.

Raises:

CardanoError – If creation fails.

Return type:

Anchor

Example

>>> anchor = Anchor.from_hash_bytes(
...     "https://example.com/meta.json",
...     bytes(32)
... )
classmethod from_cbor(reader)[source]

Deserializes an Anchor from CBOR data.

Parameters:

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

Returns:

A new Anchor deserialized from the CBOR data.

Raises:

CardanoError – If deserialization fails.

Return type:

Anchor

property url: str

Returns the URL associated with this anchor.

property hash: Blake2bHash

Returns the hash associated with this anchor.

property hash_hex: str

Returns the hash as a hexadecimal string.

property hash_bytes: bytes

Returns the hash as raw bytes.

to_cbor(writer)[source]

Serializes the anchor 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

__eq__(other)[source]

Checks equality with another Anchor.

Parameters:

other (object)

Return type:

bool

__hash__()[source]

Returns a Python hash for use in sets and dicts.

Return type:

int

__str__()[source]

Returns a string representation of the anchor.

Return type:

str