xrpl-py
A pure Python implementation for interacting with the XRP Ledger.
The xrpl-py
library simplifies the hardest parts of XRP Ledger interaction, like serialization and transaction signing. It also provides native Python methods and models for XRP Ledger transactions and core server API (rippled
) objects.
As an example, this is how you would use this library to send a payment on testnet:
from xrpl.account import get_balance
from xrpl.clients import JsonRpcClient
from xrpl.models import Payment, Tx
from xrpl.transaction import submit_and_wait
from xrpl.wallet import generate_faucet_wallet
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
wallet1 = generate_faucet_wallet(client, debug=True)
wallet2 = generate_faucet_wallet(client, debug=True)
print("Balances of wallets before Payment tx")
print(get_balance(wallet1.address, client))
print(get_balance(wallet2.address, client))
payment_tx = Payment(
account=wallet1.address,
amount="1000",
destination=wallet2.address,
)
payment_response = submit_and_wait(payment_tx, client, wallet1)
print("Transaction was submitted")
tx_response = client.request(Tx(transaction=payment_response.result["hash"]))
print("Validated:", tx_response.result["validated"])
print("Balances of wallets after Payment tx:")
print(get_balance(wallet1.address, client))
print(get_balance(wallet2.address, client))
Installation and supported versions
The xrpl-py
library is available on PyPI. Install with pip
:
pip3 install xrpl-py
The library supports Python 3.7 and later.
Features
Use xrpl-py
to build Python applications that leverage the XRP Ledger. The library helps with all aspects of interacting with the XRP Ledger, including:
- Key and wallet management
- Serialization
- Transaction Signing
xrpl-py
also provides:
- A network client — See
xrpl.clients
for more information. - Methods for inspecting accounts — See XRPL Account Methods for more information.
- Codecs for encoding and decoding addresses and other objects — See Core Codecs for more information.
See the complete xrpl-py
reference documentation on Read the Docs.
Usage
The following sections describe some of the most commonly used modules in the xrpl-py
library and provide sample code.
Network client
Use the xrpl.clients
library to create a network client for connecting to the XRP Ledger.
from xrpl.clients import JsonRpcClient
JSON_RPC_URL = "https://s.altnet.rippletest.net:51234"
client = JsonRpcClient(JSON_RPC_URL)
Manage keys and wallets
xrpl.wallet
Use the xrpl.wallet
module to create a wallet from a given seed or or via a Testnet faucet.
To create a wallet from a seed (in this case, the value generated using xrpl.keypairs
):
wallet_from_seed = xrpl.wallet.Wallet.from_seed(seed)
print(wallet_from_seed)
To create a wallet from a Testnet faucet:
test_wallet = generate_faucet_wallet(client)
test_account = test_wallet.address
print("Classic address:", test_account)
xrpl.core.keypairs
Use the xrpl.core.keypairs
module to generate seeds and derive keypairs and addresses from those seed values.
Here's an example of how to generate a seed
value and derive an XRP Ledger "classic" address from that seed.
from xrpl.core import keypairs
seed = keypairs.generate_seed()
public, private = keypairs.derive_keypair(seed)
test_account = keypairs.derive_classic_address(public)
print("Here's the public key:")
print(public)
print("Here's the private key:")
print(private)
print("Store this in a secure place!")
Note: You can use xrpl.core.keypairs.sign
to sign transactions but xrpl-py
also provides explicit methods for safely signing and submitting transactions. See Transaction Signing and XRPL Transaction Methods for more information.
Serialize and sign transactions
To securely submit transactions to the XRP Ledger, you need to first serialize data from JSON and other formats into the XRP Ledger's canonical format, then to authorize the transaction by digitally signing it with the account's private key. The xrpl-py
library provides several methods to simplify this process.
Use the xrpl.transaction
module to sign and submit transactions. The module offers three ways to do this:
-
sign_and_submit
— Signs a transaction locally, then submits it to the XRP Ledger. This method does not implement reliable transaction submission best practices, so only use it for development or testing purposes.
-
sign
— Signs a transaction locally. This method does not submit the transaction to the XRP Ledger.
-
submit_and_wait
— An implementation of the reliable transaction submission guidelines, this method submits a signed transaction to the XRP Ledger and then verifies that it has been included in a validated ledger (or has failed to do so). Use this method to submit transactions for production purposes.
from xrpl.models.transactions import Payment
from xrpl.transaction import sign, submit_and_wait
from xrpl.ledger import get_latest_validated_ledger_sequence
from xrpl.account import get_next_valid_seq_number
current_validated_ledger = get_latest_validated_ledger_sequence(client)
my_tx_payment = Payment(
account=test_wallet.address,
amount="2200000",
destination="rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe",
last_ledger_sequence=current_validated_ledger + 20,
sequence=get_next_valid_seq_number(test_wallet.address, client),
fee="10",
)
my_tx_payment_signed = sign(my_tx_payment,test_wallet)
tx_response = submit_and_wait(my_tx_payment_signed, client)
Get fee from the XRP Ledger
In most cases, you can specify the minimum transaction cost of "10"
for the fee
field unless you have a strong reason not to. But if you want to get the current load-balanced transaction cost from the network, you can use the get_fee
function:
from xrpl.ledger import get_fee
fee = get_fee(client)
print(fee)
Auto-filled fields
The xrpl-py
library automatically populates the fee
, sequence
and last_ledger_sequence
fields when you create transactions. In the example above, you could omit those fields and let the library fill them in for you.
from xrpl.models.transactions import Payment
from xrpl.transaction import submit_and_wait, autofill_and_sign
my_tx_payment = Payment(
account=test_wallet.address,
amount="2200000",
destination="rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe"
)
my_tx_payment_signed = autofill_and_sign(my_tx_payment, client, test_wallet)
print(my_tx_payment_signed)
tx_response = submit_and_wait(my_tx_payment_signed, client)
Subscribe to ledger updates
You can send subscribe
and unsubscribe
requests only using the WebSocket network client. These request methods allow you to be alerted of certain situations as they occur, such as when a new ledger is declared.
from xrpl.clients import WebsocketClient
url = "wss://s.altnet.rippletest.net/"
from xrpl.models import Subscribe, StreamParameter
req = Subscribe(streams=[StreamParameter.LEDGER])
with WebsocketClient(url) as client:
client.send(req)
for message in client:
print(message)
Asynchronous Code
This library supports Python's asyncio
package, which is used to run asynchronous code. All the async code is in xrpl.asyncio
If you are writing asynchronous code, please note that you will not be able to use any synchronous sugar functions, due to how event loops are handled. However, every synchronous method has a corresponding asynchronous method that you can use.
This sample code is the asynchronous equivalent of the above section on submitting a transaction.
import asyncio
from xrpl.models.transactions import Payment
from xrpl.asyncio.transaction import sign, submit_and_wait
from xrpl.asyncio.ledger import get_latest_validated_ledger_sequence
from xrpl.asyncio.account import get_next_valid_seq_number
from xrpl.asyncio.clients import AsyncJsonRpcClient
async_client = AsyncJsonRpcClient(JSON_RPC_URL)
async def submit_sample_transaction():
current_validated_ledger = await get_latest_validated_ledger_sequence(async_client)
my_tx_payment = Payment(
account=test_wallet.address,
amount="2200000",
destination="rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe",
last_ledger_sequence=current_validated_ledger + 20,
sequence=await get_next_valid_seq_number(test_wallet.address, async_client),
fee="10",
)
tx_response = await submit_and_wait(my_tx_payment_signed, async_client, test_wallet)
asyncio.run(submit_sample_transaction())
Encode addresses
Use xrpl.core.addresscodec
to encode and decode addresses into and from the "classic" and X-address formats.
from xrpl.core import addresscodec
testnet_xaddress = (
addresscodec.classic_address_to_xaddress(
"rMPUKmzmDWEX1tQhzQ8oGFNfAEhnWNFwz",
tag=0,
is_test_network=True,
)
)
print(testnet_xaddress)
Migrating
If you're currently using xrpl-py
version 1, you can use this guide to migrate to v2.
Contributing
If you want to contribute to this project, see CONTRIBUTING.md.
Mailing Lists
We have a low-traffic mailing list for announcements of new xrpl-py
releases. (About 1 email per week)
If you're using the XRP Ledger in production, you should run a rippled server and subscribe to the ripple-server mailing list as well.
Code Samples
Report an issue
Experienced an issue? Report it here.
License
The xrpl-py
library is licensed under the ISC License. See LICENSE for more information.