Skip to main content

PyTonConnect

PyPI

Python SDK for TON Connect 2.0

⚠️ The library has not been developed for a long time.

Analogue of the @tonconnect/sdk library.

Use it to connect your app to TON wallets via TonConnect protocol. You can find more details and the protocol specification in the docs.

Vulnerable

🛑 A vulnerability was found in the check_proof function. If you use this functionality in your project, consider switching to another library!

Installation

Install Python 3 package: pip3 install pytonconnect

Examples

Add the tonconnect-manifest

App needs to have its manifest to pass meta information to the wallet. Manifest is a JSON file named as tonconnect-manifest.json following format:

{
  "url": "<app-url>",                        // required
  "name": "<app-name>",                      // required
  "iconUrl": "<app-icon-url>",               // required
  "termsOfUseUrl": "<terms-of-use-url>",     // optional
  "privacyPolicyUrl": "<privacy-policy-url>" // optional
}

Make sure that manifest is available to GET by its URL.

Init connector and call restore_connection.

If user connected his wallet before, connector will restore the connection

import asyncio
from pytonconnect import TonConnect

async def main():
    connector = TonConnect(manifest_url='https://raw.githubusercontent.com/XaBbl4/pytonconnect/main/pytonconnect-manifest.json')
    is_connected = await connector.restore_connection()
    print('is_connected:', is_connected)

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())

Fetch wallets list

You can fetch all supported wallets list

wallets_list = connector.get_wallets()

# wallets_list is 
# [
#   {
#     name: str,
#     image: str,
#     about_url: str,
#     bridge_url: str,
#     universal_url: str,
#   },
# ]

You also can get wallets list using get_wallets static method:

wallets_list = TonConnect.get_wallets()

Subscribe to the connection status changes

def status_changed(wallet_info):
    # update state/reactive variables to show updates in the ui
    print('wallet_info:', wallet_info)
    unsubscribe()

unsubscribe = connector.on_status_change(status_changed)
# call unsubscribe() later to save resources when you don't need to listen for updates anymore.

Initialize a wallet connection via universal link

generated_url = await connector.connect(wallets_list[0])
print('generated_url:', generated_url)

Then you have to show this link to user as QR-code, or use it as a deep_link. You will receive an update in connector.on_status_change when user approves connection in the wallet.

Send transaction

transaction = {
    'valid_until': 1681223913,
    'messages': [
        {
            'address': '0:0000000000000000000000000000000000000000000000000000000000000000',
            'amount': '1',
            # 'stateInit': 'base64_YOUR_STATE_INIT' # just for instance. Replace with your transaction state_init or remove
        },
        {
            'address': '0:0000000000000000000000000000000000000000000000000000000000000000',
            'amount': '1',
            # 'payload': 'base64_YOUR_PAYLOAD' # just for instance. Replace with your transaction payload or remove
        }
    ]
}

try:
    result = await connector.send_transaction(transaction)
    print('Transaction was sent successfully')
except Exception as e:
    if isintance(e, UserRejectsError):
        print('You rejected the transaction. Please confirm it to send to the blockchain')
    else:
        print('Unknown error:', e)

Release files for pytonconnect 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytonconnect 0.4.0
File Size Uploaded
pytonconnect-0.4.0.tar.gz 21.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytonconnect 0.4.0
File Interpreter ABI Platform
pytonconnect-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 47.4 kB

Release files / pytonconnect-0.4.0.tar.gz

Download URL pytonconnect-0.4.0.tar.gz
Size 21.7 kB
Tags Source
SHA-256 checksum
How to use checksums
10cd27e8c66515855d6c79c6d8930d76c763e7c58a97d84b91b4697bdd6a2964
BLAKE2b-256 checksum
How to use checksums
db6e6cc7a500d96bdfb1dc4deb6335a85edda3ec35d53899b4dcac8205899ea5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.6

Release files / pytonconnect-0.4.0-py3-none-any.whl

Download URL pytonconnect-0.4.0-py3-none-any.whl
Size 25.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
78af3ae26cbc4729242601dc566696602c09d9b7cb013f13dfc3111e5fbabe60
BLAKE2b-256 checksum
How to use checksums
7c893b925c3c94cd84086a8a7952b02bd0cbfe482e7506be9be8a9f55d08a24d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.6
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page