Address

A module that contains address-related classes.

Specifications and references could be found in:
class pycardano.address.AddressType(value)

Bases: enum.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: pycardano.serialization.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) pycardano.address.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) pycardano.address.PointerAddress

Turn a CBOR primitive to its original class type.

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

  • value (Primitive) – A CBOR primitive.

Returns

A CBOR serializable object.

Return type

CBORBase

Raises

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

class pycardano.address.Address(payment_part: Optional[Union[pycardano.hash.VerificationKeyHash, pycardano.hash.ScriptHash]] = None, staking_part: Optional[Union[pycardano.hash.VerificationKeyHash, pycardano.hash.ScriptHash, pycardano.address.PointerAddress]] = None, network: pycardano.network.Network = Network.MAINNET)

Bases: pycardano.serialization.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 payment_part: Optional[Union[pycardano.hash.VerificationKeyHash, pycardano.hash.ScriptHash]]

Payment part of the address.

property staking_part: Optional[Union[pycardano.hash.VerificationKeyHash, pycardano.hash.ScriptHash, pycardano.address.PointerAddress]]

Staking part of the address.

property network: pycardano.network.Network

Network this address belongs to.

property address_type: pycardano.address.AddressType

Address type.

property header_byte: bytes

Header byte that identifies the type of address.

property hrp: str

Human-readable prefix for bech32 encoder.

encode() str

Encode the address in Bech32 format.

More info about Bech32 here.

Returns

Encoded address in Bech32.

Return type

str

Examples

>>> payment_hash = VerificationKeyHash(
...     bytes.fromhex("cc30497f4ff962f4c1dca54cceefe39f86f1d7179668009f8eb71e59"))
>>> print(Address(payment_hash).encode())
addr1v8xrqjtlfluk9axpmjj5enh0uw0cduwhz7txsqyl36m3ukgqdsn8w
classmethod decode(data: str) pycardano.address.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)
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: Union[bytes, str]) pycardano.address.Address

Turn a CBOR primitive to its original class type.

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

  • value (Primitive) – A CBOR primitive.

Returns

A CBOR serializable object.

Return type

CBORBase

Raises

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