Address

A module that contains address-related classes.

Specifications and references could be found in:
class pycardano.address.AddressType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: Enum

Address type definition.

BYRON = 8

Byron address

KEY_KEY = 0

Payment key hash + Stake key hash

SCRIPT_KEY = 1

Script hash + Stake key hash

KEY_SCRIPT = 2

Payment key hash + Script hash

SCRIPT_SCRIPT = 3

Script hash + Script hash

KEY_POINTER = 4

Payment key hash + Pointer address

SCRIPT_POINTER = 5

Script hash + Pointer address

KEY_NONE = 6

Payment key hash only

SCRIPT_NONE = 7

Script hash for payment part only

NONE_KEY = 14

Stake key hash for stake part only

NONE_SCRIPT = 15

Script hash for stake part only

class pycardano.address.PointerAddress(slot: int, tx_index: int, cert_index: int)

Bases: CBORSerializable

Pointer address.

It refers to a point of the chain containing a stake key registration certificate.

Parameters:
  • slot (int) – Slot in which the staking certificate was posted.

  • tx_index (int) – The transaction index (within that slot).

  • cert_index (int) – A (delegation) certificate index (within that transaction).

property slot: int
property tx_index: int
property cert_index: int
encode() bytes

Encode the pointer address to bytes.

The encoding follows CIP-0019#Pointers.

Returns:

Encoded bytes.

Return type:

bytes

Examples

>>> PointerAddress(1, 2, 3).encode()
b'\x01\x02\x03'
>>> PointerAddress(123456789, 2, 3).encode()
b'\xba\xef\x9a\x15\x02\x03'
classmethod decode(data: bytes) PointerAddress

Decode bytes into a PointerAddress.

Parameters:

data (bytes) – The data to be decoded.

Returns:

Decoded pointer address.

Return type:

PointerAddress

Examples

>>> PointerAddress.decode(b'\x01\x02\x03')
PointerAddress(1, 2, 3)
>>> PointerAddress.decode(b'\xba\xef\x9a\x15\x02\x03')
PointerAddress(123456789, 2, 3)
to_primitive() bytes

Convert the instance and its elements to CBOR primitives recursively.

Returns:

A CBOR primitive.

Return type:

Primitive

Raises:

SerializeException – When the object or its elements could not be converted to CBOR primitive types.

classmethod from_primitive(value: bytes) PointerAddress

Turn a CBOR primitive to its original class type.

Parameters:
  • cls (CBORBase) – The original class type.

  • value (Primitive) – A CBOR primitive.

  • type_args (Optional[tuple]) – Type arguments for the class.

Returns:

A CBOR serializable object.

Return type:

CBORBase

Raises:

DeserializeException – When the object could not be restored from primitives.

Address(payment_part: ~pycardano.hash.VerificationKeyHash | ~pycardano.hash.ScriptHash | None = None, staking_part: ~pycardano.hash.VerificationKeyHash | ~pycardano.hash.ScriptHash | ~pycardano.address.PointerAddress | None = None, network: ~pycardano.network.Network = {
Address  '__objclass__': <enum 'Network'>,
Address  '_name_': 'MAINNET',
Address  '_sort_order_': 1,
Address  '_value_': 1,
Address})

Bases: CBORSerializable

A shelley address. It consists of two parts: payment part and staking part.

Either of the parts could be None, but they cannot be None at the same time.

Parameters:
property Address.is_byron: bool

Check if this is a Byron-era address.

Returns:

True if this is a Byron address, False if Shelley/later.

Return type:

bool

property Address.payment_part: VerificationKeyHash | ScriptHash | None

Payment part of the address.

property Address.staking_part: VerificationKeyHash | ScriptHash | PointerAddress | None

Staking part of the address.

property Address.network: Network

Network this address belongs to.

property Address.address_type: AddressType

Address type.

property Address.header_byte: bytes | None

Header byte that identifies the type of address. None for Byron addresses.

property Address.hrp: str | None

Human-readable prefix for bech32 encoder. None for Byron addresses.

property Address.payload_hash: bytes | None

Byron address payload hash (28 bytes). None for Shelley addresses.

property Address.byron_attributes: dict | None

Byron address attributes. None for Shelley addresses.

property Address.byron_type: int | None

Byron address type (0=Public Key, 2=Redemption). None for Shelley addresses.

property Address.crc32_checksum: int | None

Byron address CRC32 checksum. None for Shelley addresses.

Address.encode() str

Encode the address in Bech32 format (Shelley) or Base58 format (Byron).

More info about Bech32 here.

Returns:

Encoded address in Bech32 (Shelley) or Base58 (Byron).

Return type:

str

Examples

>>> payment_hash = VerificationKeyHash(
...     bytes.fromhex("cc30497f4ff962f4c1dca54cceefe39f86f1d7179668009f8eb71e59"))
>>> print(Address(payment_hash).encode())
addr1v8xrqjtlfluk9axpmjj5enh0uw0cduwhz7txsqyl36m3ukgqdsn8w
classmethod Address.decode(data: str) Address

Decode a bech32 string into an address object.

Parameters:

data (str) – Bech32-encoded string.

Returns:

Decoded address.

Return type:

Address

Raises:

DecodingException – When the input string is not a valid Shelley address.

Examples

>>> addr = Address.decode("addr1v8xrqjtlfluk9axpmjj5enh0uw0cduwhz7txsqyl36m3ukgqdsn8w")
>>> khash = VerificationKeyHash(bytes.fromhex("cc30497f4ff962f4c1dca54cceefe39f86f1d7179668009f8eb71e59"))
>>> assert addr == Address(khash)
Address.to_primitive() bytes

Convert the instance and its elements to CBOR primitives recursively.

Returns:

A CBOR primitive.

Return type:

Primitive

Raises:

SerializeException – When the object or its elements could not be converted to CBOR primitive types.

classmethod Address.from_primitive(value: bytes | str) Address

Turn a CBOR primitive to its original class type.

Parameters:
  • cls (CBORBase) – The original class type.

  • value (Primitive) – A CBOR primitive.

  • type_args (Optional[tuple]) – Type arguments for the class.

Returns:

A CBOR serializable object.

Return type:

CBORBase

Raises:

DeserializeException – When the object could not be restored from primitives.

Address.save(path: str, key_type: str | None = None, description: str | None = None, **kwargs)

Save the Address object to a file.

This method writes the object’s JSON representation to the specified file path.

It raises an error if the file already exists and is not empty.

Parameters:
  • path (str) – The file path to save the object to.

  • key_type (str, optional) – Not used in this context, but can be included for consistency.

  • description (str, optional) – Not used in this context, but can be included for consistency.

  • **kwargs – Additional keyword arguments (not used here).

Raises:

IOError – If the file already exists and is not empty.

classmethod Address.load(path: str) Address

Load an Address object from a file.

Parameters:

path (str) – The file path to load the object from.

Returns:

The loaded Address object.

Return type:

Address