Skip to content

Commit 395d79b

Browse files
committed
Add documentation page about Python SDK
1 parent b5f3e8e commit 395d79b

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
title: Python SDK
3+
---
4+
5+
[`cashscript-py`](https://pypi.org/project/cashscript-py/) is a community-maintained Python SDK for CashScript development. It is a port of the official [TypeScript SDK](../sdk/typescript-sdk.md) and aims to give Python developers (for example those building server-side tools, bots, or Electron Cash plugins) a familiar API for constructing and interacting with CashScript contracts on BCH without re-implementing low-level details.
6+
7+
:::info
8+
The Python SDK is maintained independently from the official CashScript project. For the full API reference, guides and runnable examples, see the links under [Documentation](#documentation) below.
9+
:::
10+
11+
## When to use the Python SDK
12+
13+
Use the Python SDK when you want to build and send CashScript transactions from a Python codebase. It mirrors the structure of the TypeScript SDK, so the same four core classes are available. See the [TypeScript SDK documentation](../sdk/typescript-sdk.md) for more details.
14+
15+
- the `Contract` class
16+
- the `TransactionBuilder` class
17+
- the `NetworkProvider` class (e.g. `ElectrumNetworkProvider`)
18+
- the `SignatureTemplate` class
19+
20+
Contracts are still compiled with `cashc` and loaded from the resulting artifact JSON. The Python SDK only handles instantiation, transaction building and network interaction.
21+
22+
## Installation
23+
24+
Install from PyPI:
25+
26+
```bash
27+
pip install cashscript-py
28+
```
29+
30+
Or, with [`uv`](https://docs.astral.sh/uv/):
31+
32+
```bash
33+
uv add cashscript-py
34+
```
35+
36+
## SDK usage
37+
38+
The usage of the 4 classes in your code is as follows: before using the SDK you create one or multiple contract artifacts compiled by `cashc`. Then to start using the SDK, you instantiate a `NetworkProvider`, which you then provide to instantiate a `Contract` from an `Artifact`. Once you have a `Contract` instance, you can use it in the `TransactionBuilder`. During transaction building you might need to generate a signature, in which case you would instantiate a `SignatureTemplate`.
39+
40+
For more complete examples of the SDK flow, refer to the [runnable examples](https://gitlab.com/cashscript-py/cashscript-py/-/tree/master/examples) in the Python SDK repository.
41+
42+
#### example
43+
44+
```python
45+
from cashscript_py import Contract, ElectrumNetworkProvider, Output, SignatureTemplate, TransactionBuilder
46+
from .artifact import p2pkh_artifact
47+
from .somewhere import contract_arguments, alice_wif, send_amount, recipient_address
48+
49+
provider = ElectrumNetworkProvider("chipnet")
50+
contract = Contract(p2pkh_artifact, contract_arguments, provider)
51+
52+
alice_signature_template = SignatureTemplate(alice_wif)
53+
unlocker = contract.unlock["transfer"](alice_signature_template)
54+
55+
contract_utxos = await contract.get_utxos()
56+
contract_utxo = contract_utxos[0]
57+
58+
transaction_builder = TransactionBuilder(provider)
59+
transaction_builder.add_input(contract_utxo, unlocker)
60+
transaction_builder.add_output(Output(amount=send_amount, to=recipient_address))
61+
62+
tx_details = await transaction_builder.send()
63+
```
64+
65+
## Feature parity with the TypeScript SDK
66+
67+
The Python SDK and the TypeScript SDK are developed independently and have independent versioning, so APIs and available functionality may drift between the two. The sections below call out the most notable gaps at the time of writing, but for anything beyond the basic flow you should always consult the [Python SDK documentation](https://cashscript-py.readthedocs.io/) rather than assuming parity with the TypeScript SDK.
68+
69+
The Python SDK covers the core `Contract`, `TransactionBuilder`, `NetworkProvider` and `SignatureTemplate` APIs, but a few features from the TypeScript SDK are not (yet) available:
70+
71+
- **`debug()`**: the TypeScript SDK can locally evaluate and debug transactions via a `debug()` API. The Python SDK does not currently provide an equivalent.
72+
- **`get_bitauth_uri()`**: the TypeScript SDK can generate a [BitAuth IDE](https://ide.bitauth.com/) URI via `getBitauthUri()`. The Python SDK does not currently provide this helper.
73+
- **`generate_wc_transaction_object()`**: the TypeScript SDK can generate a WalletConnect signing payload. The Python SDK does not currently provide this feature.
74+
75+
See the [`transaction-builder.md` guide](https://gitlab.com/cashscript-py/cashscript-py/-/blob/master/docs/guide/transaction-builder.md) in the Python SDK repository for the full details on each of these sections.
76+
77+
## Documentation
78+
79+
The Python SDK has its own documentation site and examples.
80+
81+
- **PyPI package:** [cashscript-py](https://pypi.org/project/cashscript-py/)
82+
- **User guide:** [getting-started.md](https://gitlab.com/cashscript-py/cashscript-py/-/blob/master/docs/guide/getting-started.md)
83+
- **API reference:** [cashscript-py.readthedocs.io](https://cashscript-py.readthedocs.io/)
84+
- **Runnable examples:** [examples/](https://gitlab.com/cashscript-py/cashscript-py/-/tree/master/examples)
85+
- **Source code:** [gitlab.com/cashscript-py/cashscript-py](https://gitlab.com/cashscript-py/cashscript-py)

website/sidebars.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const sidebars: SidebarsConfig = {
5555
'sdk/examples',
5656
],
5757
},
58+
'third-party-sdks/python-sdk',
5859
{
5960
type: 'category',
6061
label: 'Guides',

0 commit comments

Comments
 (0)