<p align="center">
<img src="./.github/logo.png" height=200 width=200 />
</p>
---
## PyCardano
[](https://pypi.python.org/pypi/pycardano/)
[](https://pypi.python.org/pypi/pycardano/)
[](https://pepy.tech/projects/pycardano)
[](https://github.com/Python-Cardano/pycardano/actions/workflows/main.yml)
[](https://codecov.io/gh/Python-Cardano/pycardano)
[](https://pycardano.readthedocs.io/en/latest/?badge=latest)
[](https://discord.gg/qT9Mn9xjgz)
[](https://twitter.com/PyCardano)
PyCardano is a Cardano library written in Python. It allows users to create and sign transactions without
depending on third-party Cardano serialization tools, such as
[cardano-cli](https://github.com/input-output-hk/cardano-node#cardano-cli) and
[cardano-serialization-lib](https://github.com/Emurgo/cardano-serialization-lib), making it a lightweight library, which
is simple and fast to set up in all types of environments.
Current goal of this project is to enable developers to write off-chain code and tests in pure Python for Plutus DApps.
Nevertheless, we see the potential in expanding this project to a full Cardano node client, which
could be beneficial for faster R&D iterations.
### Features
- [x] Shelly address
- [x] Transaction builder
- [x] Transaction signing
- [x] Multi-asset
- [X] Chain backend integration
- [X] Fee calculation
- [X] UTxO selection
- [X] Native script
- [X] Native token
- [X] Metadata
- [X] Plutus script
- [X] Staking certificates
- [X] Reward withdraw
- [X] Mnemonic
- [X] HD Wallet
- [x] Pool certificate
- [x] Protocol proposal update
- [x] Governance actions
- [ ] Byron Address
### Installation
Install the library using [pip](https://pip.pypa.io/en/stable/):
`pip install pycardano`
#### Install cbor2 pure python implementation (Optional)
[cbor2](https://github.com/agronholm/cbor2/tree/master) is a dependency of pycardano. It is used to encode and decode CBOR data.
It has two implementations: one is pure Python and the other is C, which is installed by default. The C implementation is faster, but it is less flexible than the pure Python implementation.
For some users, the C implementation may not work properly when deserializing cbor data. For example, the order of inputs of a transaction isn't guaranteed to be the same as the order of inputs in the original transaction (details could be found in [this issue](https://github.com/Python-Cardano/pycardano/issues/311)). This would result in a different transaction hash when the transaction is serialized again. For users who encounter this issue, we recommend to use the pure Python implementation of cbor2. You can do so by running [ensure_pure_cbor2.sh](./ensure_pure_cbor2.sh), which inspect the cbor2 installed in the running environment and force install pure python implementation if necessary.
```bash
ensure_pure_cbor2.sh
```
### Documentation
https://pycardano.readthedocs.io/en/latest/
### Examples
#### Transaction creation and signing
<details>
<summary>Expand code</summary>
```python
"""Build a transaction using transaction builder"""
from blockfrost import ApiUrls
from pycardano import *
# Use testnet
network = Network.TESTNET
# Read keys to memory
# Assume there is a payment.skey file sitting in current directory
psk = PaymentSigningKey.load("payment.skey")
# Assume there is a stake.skey file sitting in current directory
ssk = StakeSigningKey.load("stake.skey")
pvk = PaymentVerificationKey.from_signing_key(psk)
svk = StakeVerificationKey.from_signing_key(ssk)
# Derive an address from payment verification key and stake verification key
address = Address(pvk.hash(), svk.hash(), network)
# Create a BlockFrost chain context
context = BlockFrostChainContext("your_blockfrost_project_id", base_url=ApiUrls.preprod.value)
# Create a transaction builder
builder = TransactionBuilder(context)
# Tell the builder that transaction input will come from a specific address, assuming that there are some ADA and native
# assets sitting at this address. "add_input_address" could be called multiple times with different address.
builder.add_input_address(address)
# Get all UTxOs currently sitting at this address
utxos = context.utxos(address)
# We can also tell the builder to include a specific UTxO in the transaction.
# Similarly, "add_input" could be called multiple times.
builder.add_input(utxos[0])
# Send 1.5 ADA and a native asset (CHOC) in quantity of 2000 to an address.
builder.add_output(
TransactionOutput(
Address.from_primitive(
"addr_test1vrm9x2zsux7va6w892g38tvchnzahvcd9tykqf3ygnmwtaqyfg52x"
),
Value.from_primitive(
[
1500000,
{
bytes.fromhex(
"57fca08abbaddee36da742a839f7d83a7e1d2419f1507fcbf3916522" # Policy ID
): {
b"CHOC": 2000 # Asset name and amount
}
},
]
),
)
)
# We can add multiple outputs, similar to what we can do with inputs.
# Send 2 ADA and a native asset (CHOC) in quantity of 200 to ourselves
builder.add_output(
TransactionOutput(
address,
Value.from_primitive(
[
2000000,
{
bytes.fromhex(
"57fca08abbaddee36da742a839f7d83a7e1d2419f1507fcbf3916522" # Policy ID
): {
b"CHOC": 200 # Asset name and amount
}
},
]
),
)
)
# Create final signed transaction
signed_tx = builder.build_and_sign([psk], change_address=address)
# Submit signed transaction to the network
context.submit_tx(signed_tx)
```
</details>
See more usages under project [examples](https://github.com/Python-Cardano/pycardano/tree/main/examples).
There is also a collection of examples under [awesome-pycardano](https://github.com/B3nac/awesome-pycardano).
### Development
<details>
<summary>Click to expand</summary>
#### Workspace setup
Clone the repository:
`git clone https://github.com/Python-Cardano/pycardano.git`
PyCardano uses [poetry](https://python-poetry.org/) to manage its dependencies.
Install poetry for osx / linux / bashonwindows:
`curl -sSL https://install.python-poetry.org | python3 -`
Go to [poetry installation](https://python-poetry.org/docs/#installation) for more details.
Change directory into the repo, install all dependencies using poetry, and you are all set!
`cd pycardano && poetry install`
When testing or running any program, it is recommended to enter
a [poetry shell](https://python-poetry.org/docs/cli/#shell) in which all python dependencies are automatically
configured: `poetry shell`.
#### Test
PyCardano uses [pytest](https://docs.pytest.org/en/6.2.x/) for unit testing.
Run all tests:
`make test`
Run all tests in a specific test file:
`poetry run pytest test/pycardano/test_transaction.py`
Run a specific test function:
`poetry run pytest -k "test_transaction_body"`
Run a specific test function in a test file:
`poetry run pytest test/pycardano/test_transaction.py -k "test_transaction_body"`
#### Test coverage
We use [Coverage](https://coverage.readthedocs.io/en/latest/) to calculate the test coverage.
Test coverage could be generated by: `make cov`
A html report could be generated and opened in browser by: `make cov-html`
### Style guidelines
The package uses
[Google style](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html) docstring.
Code could be formatted with command: `make format`
The code style could be checked by [flake8](https://flake8.pycqa.org/en/latest/): `make qa`
### Docs generation
The majority of package documentation is created by the docstrings in python files.
We use [sphinx](https://www.sphinx-doc.org/en/master/) with
[Read the Docs theme](https://sphinx-rtd-theme.readthedocs.io/en/stable/) to generate the
html pages.
Build docs and open the docs in browser:
`make docs`
</details>
## Donation and Sponsor
If you find this project helpful, please consider donate or sponsor us. Your donation and sponsor will allow us to
spend more time on improving PyCardano and adding more features in the future.
You can support us by 1) sponsoring through Github, or 2) donating ADA to our ADA Handle `pycardano` or to the address below:
[`addr1vxa4qadv7hk2dd3jtz9rep7sp92lkgwzefwkmsy3qkglq5qzv8c0d`](https://cardanoscan.io/address/61bb5075acf5eca6b632588a3c87d00955fb21c2ca5d6dc0910591f050)
<p>
<img src="./.github/donate_addr.png" height=150 width=150/>
</p>
## Sponsors :heart:
<p align="left">
<a href="https://github.com/blockfrost"><img src="https://avatars.githubusercontent.com/u/70073210?s=50&v=4"/></a>
</p>
Raw data
{
"_id": null,
"home_page": "https://github.com/Python-Cardano/pycardano",
"name": "pycardano",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0.0,>=3.9.1",
"maintainer_email": null,
"keywords": "python, cardano, blockchain, crypto",
"author": "Jerry",
"author_email": "jerrycgh@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/95/db/dd12e439efed8f3bdccda513bff5c464c2d7bdd5f76edfe5d4e79ee492a0/pycardano-0.15.1.tar.gz",
"platform": null,
"description": "<p align=\"center\">\n <img src=\"./.github/logo.png\" height=200 width=200 />\n</p>\n\n---\n\n## PyCardano\n\n[](https://pypi.python.org/pypi/pycardano/)\n[](https://pypi.python.org/pypi/pycardano/)\n[](https://pepy.tech/projects/pycardano)\n\n[](https://github.com/Python-Cardano/pycardano/actions/workflows/main.yml)\n[](https://codecov.io/gh/Python-Cardano/pycardano)\n[](https://pycardano.readthedocs.io/en/latest/?badge=latest)\n\n[](https://discord.gg/qT9Mn9xjgz)\n[](https://twitter.com/PyCardano)\n\n\nPyCardano is a Cardano library written in Python. It allows users to create and sign transactions without \ndepending on third-party Cardano serialization tools, such as\n[cardano-cli](https://github.com/input-output-hk/cardano-node#cardano-cli) and \n[cardano-serialization-lib](https://github.com/Emurgo/cardano-serialization-lib), making it a lightweight library, which \nis simple and fast to set up in all types of environments.\n\nCurrent goal of this project is to enable developers to write off-chain code and tests in pure Python for Plutus DApps.\nNevertheless, we see the potential in expanding this project to a full Cardano node client, which \ncould be beneficial for faster R&D iterations.\n\n### Features\n\n- [x] Shelly address\n- [x] Transaction builder\n- [x] Transaction signing\n- [x] Multi-asset\n- [X] Chain backend integration\n- [X] Fee calculation\n- [X] UTxO selection\n- [X] Native script\n- [X] Native token\n- [X] Metadata\n- [X] Plutus script\n- [X] Staking certificates\n- [X] Reward withdraw\n- [X] Mnemonic \n- [X] HD Wallet\n- [x] Pool certificate\n- [x] Protocol proposal update\n- [x] Governance actions\n- [ ] Byron Address\n\n\n### Installation\n\nInstall the library using [pip](https://pip.pypa.io/en/stable/):\n\n`pip install pycardano`\n\n#### Install cbor2 pure python implementation (Optional)\n[cbor2](https://github.com/agronholm/cbor2/tree/master) is a dependency of pycardano. It is used to encode and decode CBOR data.\nIt has two implementations: one is pure Python and the other is C, which is installed by default. The C implementation is faster, but it is less flexible than the pure Python implementation.\n\nFor some users, the C implementation may not work properly when deserializing cbor data. For example, the order of inputs of a transaction isn't guaranteed to be the same as the order of inputs in the original transaction (details could be found in [this issue](https://github.com/Python-Cardano/pycardano/issues/311)). This would result in a different transaction hash when the transaction is serialized again. For users who encounter this issue, we recommend to use the pure Python implementation of cbor2. You can do so by running [ensure_pure_cbor2.sh](./ensure_pure_cbor2.sh), which inspect the cbor2 installed in the running environment and force install pure python implementation if necessary.\n\n```bash\nensure_pure_cbor2.sh\n```\n\n### Documentation\n\nhttps://pycardano.readthedocs.io/en/latest/\n\n### Examples\n\n#### Transaction creation and signing\n\n<details>\n <summary>Expand code</summary>\n \n```python\n\"\"\"Build a transaction using transaction builder\"\"\"\n\nfrom blockfrost import ApiUrls\nfrom pycardano import *\n\n# Use testnet\nnetwork = Network.TESTNET\n\n# Read keys to memory\n# Assume there is a payment.skey file sitting in current directory\npsk = PaymentSigningKey.load(\"payment.skey\")\n# Assume there is a stake.skey file sitting in current directory\nssk = StakeSigningKey.load(\"stake.skey\")\n\npvk = PaymentVerificationKey.from_signing_key(psk)\nsvk = StakeVerificationKey.from_signing_key(ssk)\n\n# Derive an address from payment verification key and stake verification key\naddress = Address(pvk.hash(), svk.hash(), network)\n\n# Create a BlockFrost chain context\ncontext = BlockFrostChainContext(\"your_blockfrost_project_id\", base_url=ApiUrls.preprod.value)\n\n# Create a transaction builder\nbuilder = TransactionBuilder(context)\n\n# Tell the builder that transaction input will come from a specific address, assuming that there are some ADA and native\n# assets sitting at this address. \"add_input_address\" could be called multiple times with different address.\nbuilder.add_input_address(address)\n\n# Get all UTxOs currently sitting at this address\nutxos = context.utxos(address)\n\n# We can also tell the builder to include a specific UTxO in the transaction.\n# Similarly, \"add_input\" could be called multiple times.\nbuilder.add_input(utxos[0])\n\n# Send 1.5 ADA and a native asset (CHOC) in quantity of 2000 to an address.\nbuilder.add_output(\n TransactionOutput(\n Address.from_primitive(\n \"addr_test1vrm9x2zsux7va6w892g38tvchnzahvcd9tykqf3ygnmwtaqyfg52x\"\n ),\n Value.from_primitive(\n [\n 1500000,\n {\n bytes.fromhex(\n \"57fca08abbaddee36da742a839f7d83a7e1d2419f1507fcbf3916522\" # Policy ID\n ): {\n b\"CHOC\": 2000 # Asset name and amount\n }\n },\n ]\n ),\n )\n)\n\n# We can add multiple outputs, similar to what we can do with inputs.\n# Send 2 ADA and a native asset (CHOC) in quantity of 200 to ourselves\nbuilder.add_output(\n TransactionOutput(\n address,\n Value.from_primitive(\n [\n 2000000,\n {\n bytes.fromhex(\n \"57fca08abbaddee36da742a839f7d83a7e1d2419f1507fcbf3916522\" # Policy ID\n ): {\n b\"CHOC\": 200 # Asset name and amount\n }\n },\n ]\n ),\n )\n)\n\n# Create final signed transaction\nsigned_tx = builder.build_and_sign([psk], change_address=address)\n\n# Submit signed transaction to the network\ncontext.submit_tx(signed_tx)\n\n```\n</details>\n\nSee more usages under project [examples](https://github.com/Python-Cardano/pycardano/tree/main/examples).\n\nThere is also a collection of examples under [awesome-pycardano](https://github.com/B3nac/awesome-pycardano).\n\n### Development\n\n<details>\n<summary>Click to expand</summary>\n\n#### Workspace setup\n\nClone the repository:\n\n`git clone https://github.com/Python-Cardano/pycardano.git`\n\nPyCardano uses [poetry](https://python-poetry.org/) to manage its dependencies. \nInstall poetry for osx / linux / bashonwindows:\n\n`curl -sSL https://install.python-poetry.org | python3 -`\n\nGo to [poetry installation](https://python-poetry.org/docs/#installation) for more details. \n\n\nChange directory into the repo, install all dependencies using poetry, and you are all set!\n\n`cd pycardano && poetry install`\n\nWhen testing or running any program, it is recommended to enter \na [poetry shell](https://python-poetry.org/docs/cli/#shell) in which all python dependencies are automatically \nconfigured: `poetry shell`.\n\n\n#### Test\n\nPyCardano uses [pytest](https://docs.pytest.org/en/6.2.x/) for unit testing.\n\nRun all tests:\n`make test`\n\nRun all tests in a specific test file:\n`poetry run pytest test/pycardano/test_transaction.py`\n\nRun a specific test function:\n`poetry run pytest -k \"test_transaction_body\"`\n\nRun a specific test function in a test file:\n`poetry run pytest test/pycardano/test_transaction.py -k \"test_transaction_body\"`\n\n#### Test coverage\n\nWe use [Coverage](https://coverage.readthedocs.io/en/latest/) to calculate the test coverage.\n\nTest coverage could be generated by: `make cov`\n\nA html report could be generated and opened in browser by: `make cov-html`\n\n### Style guidelines\n\nThe package uses \n[Google style](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html) docstring.\n\nCode could be formatted with command: `make format`\n\nThe code style could be checked by [flake8](https://flake8.pycqa.org/en/latest/): `make qa`\n\n### Docs generation\n\nThe majority of package documentation is created by the docstrings in python files. \nWe use [sphinx](https://www.sphinx-doc.org/en/master/) with \n[Read the Docs theme](https://sphinx-rtd-theme.readthedocs.io/en/stable/) to generate the \nhtml pages.\n\nBuild docs and open the docs in browser: \n\n`make docs`\n\n</details>\n\n## Donation and Sponsor\nIf you find this project helpful, please consider donate or sponsor us. Your donation and sponsor will allow us to\n spend more time on improving PyCardano and adding more features in the future.\n\nYou can support us by 1) sponsoring through Github, or 2) donating ADA to our ADA Handle `pycardano` or to the address below:\n\n[`addr1vxa4qadv7hk2dd3jtz9rep7sp92lkgwzefwkmsy3qkglq5qzv8c0d`](https://cardanoscan.io/address/61bb5075acf5eca6b632588a3c87d00955fb21c2ca5d6dc0910591f050)\n\n<p>\n <img src=\"./.github/donate_addr.png\" height=150 width=150/>\n</p>\n\n\n## Sponsors :heart:\n\n<p align=\"left\">\n <a href=\"https://github.com/blockfrost\"><img src=\"https://avatars.githubusercontent.com/u/70073210?s=50&v=4\"/></a>\n</p>\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Cardano library in Python",
"version": "0.15.1",
"project_urls": {
"Documentation": "https://pycardano.readthedocs.io/en/latest/",
"Homepage": "https://github.com/Python-Cardano/pycardano",
"Repository": "https://github.com/Python-Cardano/pycardano"
},
"split_keywords": [
"python",
" cardano",
" blockchain",
" crypto"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1d64d03a8fde2c014e98cb01d24682f1b96aec8aa39ff4ae40c84d9137e2fe7f",
"md5": "643ffaaf938e558501d0538631c86612",
"sha256": "0cc424543c81283b0247b65add4e04bb29b614806c8373c934364232cada4e88"
},
"downloads": -1,
"filename": "pycardano-0.15.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "643ffaaf938e558501d0538631c86612",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0.0,>=3.9.1",
"size": 102603,
"upload_time": "2025-08-30T21:21:17",
"upload_time_iso_8601": "2025-08-30T21:21:17.170198Z",
"url": "https://files.pythonhosted.org/packages/1d/64/d03a8fde2c014e98cb01d24682f1b96aec8aa39ff4ae40c84d9137e2fe7f/pycardano-0.15.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "95dbdd12e439efed8f3bdccda513bff5c464c2d7bdd5f76edfe5d4e79ee492a0",
"md5": "108061a2d1cdca79a25237d30f5a141a",
"sha256": "742236327daf489157aa004154b59b2a7b3c20adaf05c8cce38eb1b3b38e022a"
},
"downloads": -1,
"filename": "pycardano-0.15.1.tar.gz",
"has_sig": false,
"md5_digest": "108061a2d1cdca79a25237d30f5a141a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0.0,>=3.9.1",
"size": 87513,
"upload_time": "2025-08-30T21:21:18",
"upload_time_iso_8601": "2025-08-30T21:21:18.253317Z",
"url": "https://files.pythonhosted.org/packages/95/db/dd12e439efed8f3bdccda513bff5c464c2d7bdd5f76edfe5d4e79ee492a0/pycardano-0.15.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-30 21:21:18",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Python-Cardano",
"github_project": "pycardano",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "pycardano"
}