Skip to main content

A Python package for interfacing with Dhali

Project description

Package Tests Release

dhali-py

A Python library for managing payment channels (XRPL & Ethereum) and generating auth tokens (i.e., payment-claims) for use with Dhali APIs.

Includes support for Machine-to-Machine (M2M) payments using seamless off-chain claims.


Installation

pip install dhali-py

Quick Start: Machine-to-Machine Payments

1. XRPL

All signing is performed locally using xrpl-py.

from dhali.dhali_channel_manager import DhaliChannelManager, ChannelNotFound
from dhali.config_utils import get_available_dhali_currencies
from xrpl.wallet import Wallet
from xrpl.clients import JsonRpcClient

seed    = "sXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
wallet  = Wallet.from_secret(seed=seed)
rpc_client = JsonRpcClient("https://testnet.xrpl-labs.com/")

currencies = get_available_dhali_currencies()
# Select the correct currency from the list
xrpl_testnet = next(c for c in currencies if c.network == "XRPL.TESTNET" and c.code == "XRP")

# Use the Factory to get an XRPL Manager
manager = DhaliChannelManager.xrpl(
    wallet=wallet, 
    rpc_client=rpc_client, 
    currency=xrpl_testnet
)

# Generate a claim (Base64 encoded)
try:
    claim = manager.get_auth_token()
except ChannelNotFound:
    manager.deposit(1_000_000)          # deposit 1 XRP
    claim = manager.get_auth_token()    # 🔑 regenerate after deposit
print("XRPL Claim:", claim)

2. Ethereum (EVM)

Supports Ethereum, Sepolia, Holesky, etc. Requires eth-account and web3.py.

from dhali.dhali_channel_manager import DhaliChannelManager, ChannelNotFound
from dhali.config_utils import get_available_dhali_currencies
from eth_account import Account
from web3 import Web3

# 1. Setup Account & Provider
private_key = "0x..."
account = Account.from_key(private_key)
w3 = Web3(Web3.HTTPProvider("https://ethereum-sepolia.publicnode.com"))

# 2. Fetch Available Currencies
currencies = get_available_dhali_currencies()
sepolia_rlusd = next(c for c in currencies if c.network == "SEPOLIA" and c.code == "RLUSD") # or "USDC"

# 3. Instantiate Manager with Dynamic Config
manager = DhaliChannelManager.evm(
    account=account,
    w3=w3,
    currency=sepolia_rlusd
)

# 4. Generate EIP-712 Signed Claim
amount = int(0.1 * 10**sepolia_rlusd.scale) # 0.1 RLUSD

try:
    claim = manager.get_auth_token() 
except ChannelNotFound:
    manager.deposit(amount)
    claim = manager.get_auth_token(amount=amount) 
print("EVM Claim:", claim)

Usage in API Calls

Once you have the claim string, include it in your API request query parameters or headers as required by the Dhali Gateway.

import requests

url = f"https://xrplcluster.dhali.io?payment-claim={claim}"
response = requests.post(url, json={"data": "..."})

if response.status_code == 402:
    print("Payment Required: Channel may need topping up.")

Standardized x402 Payments

For APIs that follow the x402 standard, you must wrap your auth token (claim) with the payment requirement (retrieved from the payment-required header of a 402 response).

from dhali import wrap_as_x402_payment_payload

# 1. Get your claim as usual
claim = manager.get_auth_token()

# 2. Get the payment requirement from the 'payment-required' header of a 402 response
payment_requirement = response.headers.get("payment-required") 

# 3. Wrap into an x402 payload
x402_payload = wrap_as_x402_payment_payload(claim, payment_requirement)

# 4. Use 'x402_payload' in the 'Payment' header

Classes

DhaliChannelManager (Factory)

  • xrpl(wallet, rpc_client, currency, http_client=None, public_config=None) -> DhaliXrplChannelManager
  • evm(account, w3, currency, http_client=None, public_config=None) -> DhaliEthChannelManager

get_available_dhali_currencies()

Fetches current Dhali configuration and returns a list of currencies:

[
    Currency(network="SEPOLIA", code="USDC", scale=6, token_address="..."),
    ...
]

Here’s a clean, minimal addition you can append near the end of the README (for example, just before the “Classes” section or after it):


Async Workflows

Currently, dhali-py provides a synchronous interface for managing payment channels and generating claims.

If you would like to see native async/await support (e.g., async workflows using asyncio, async Web3 providers, or async XRPL clients), please drop us a message and let us know. Community feedback helps us prioritise features 🚀


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

dhali_py-3.0.1.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

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

dhali_py-3.0.1-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file dhali_py-3.0.1.tar.gz.

File metadata

  • Download URL: dhali_py-3.0.1.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dhali_py-3.0.1.tar.gz
Algorithm Hash digest
SHA256 112aa3630c6eaf7016eb6f7de2122ea60f800fdc23ca6b592b484cf4dbc9445b
MD5 a6a6d373d10521217f39be22e3f51c19
BLAKE2b-256 9224fab4279753bcf0ae5ae5b79115a0685b0516e5f4af3a7b208d70430dc785

See more details on using hashes here.

File details

Details for the file dhali_py-3.0.1-py3-none-any.whl.

File metadata

  • Download URL: dhali_py-3.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dhali_py-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 887738384c47b48c5de55d1627a7455f8588a1710d9e81a2bce63a37d5527496
MD5 7393477de57532c847134350d657fe32
BLAKE2b-256 7296a1352ce83f541ab3d93f150fca4b37533f44be4f10a5198d06b5352fe527

See more details on using hashes here.

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