Skip to main content

Python SDK for TON Connect 2.0

Project description

PyTonConnect

PyPI

Python SDK for TON Connect 2.0

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.

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytonconnect_fixed-0.3.0.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytonconnect_fixed-0.3.0-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file pytonconnect_fixed-0.3.0.tar.gz.

File metadata

  • Download URL: pytonconnect_fixed-0.3.0.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for pytonconnect_fixed-0.3.0.tar.gz
Algorithm Hash digest
SHA256 649f0e711fd35bc360f493cfede29681924056f1cf51cd9e1027a259fc084cf8
MD5 06944c8d22999b116fae6993c84c2535
BLAKE2b-256 dab1989af4d77e9f4f2af7ee7ab1aed9391a19badee8572edde4059235a38187

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytonconnect_fixed-0.3.0.tar.gz:

Publisher: publish.yml on ahurov/pytonconnect

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytonconnect_fixed-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytonconnect_fixed-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7defa6b7e80a4f35ad7cbf6d0f400b4c346b0df9e53444ce39a432a9c20c7f55
MD5 b365f4dc8b27b6957ab6b12ea348e935
BLAKE2b-256 46e47e44b9eb16b9dcc4dc062c20c0ac3a4e11ea3db25bd000d628322ee9d5ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytonconnect_fixed-0.3.0-py3-none-any.whl:

Publisher: publish.yml on ahurov/pytonconnect

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page