This repository provides a FastAPI-based application to interact with the Divi Blockchain using its RPC interface. The API allows you to perform various blockchain operations, such as retrieving block information, sending transactions, and more, all through HTTP endpoints.
- Fetch blockchain information such as block count, block data, and transaction details.
- Retrieve UTXOs, transaction history, and balances for addresses.
- Send raw transactions to the network.
- Mempool information and lottery block winners information.
- Cross-origin support (CORS) for local development.
- Python 3.8 or later
- A running Divi blockchain node with RPC enabled
- RPC credentials configured either in a configuration file (
divi.conf
) or as environment variables.
-
Clone the repository:
git clone https://github.com/7h3v01c3/python-divi-rpc.git cd python-divi-rpc
-
Install the required dependencies:
pip install -r requirements.txt
-
Configure your Divi node's RPC credentials:
There are three ways to configure your Divi node's RPC credentials:
-
Using a
divi.conf
fileEnsure you have a
divi.conf
file with the properrpcuser
,rpcpassword
, andrpcport
configured. The config file is typically located in:- Windows:
C:\Users\YourUser\AppData\Roaming\DIVI\divi.conf
- macOS:
~/Library/Application Support/DIVI/divi.conf
- Linux:
~/.divi/divi.conf
- Windows:
-
Using environment variables
# These define where to connect to the Divi node export RPC_USER=your_rpc_user export RPC_PASS=your_rpc_password export RPC_PORT=your_rpc_port # default is 51473 export RPC_HOST=your_rpc_host # default is 127.0.0.1 # These define where to bind the API server export HOST=your_host # default is 127.0.0.1 export PORT=your_port # default is 8000 export IGNORE_DIVID_CONF=TRUE # Use this to skip checking for a divid.conf file
-
Using a
.env
fileCreate a
.env.local
file in the root of the project with the following content:RPC_USER=your_rpc_user RPC_PASS=your_rpc_password RPC_PORT=your_rpc_port # default is 51473 RPC_HOST=your_rpc_host # default is 127.0.0.1 HOST=your_host # default is 127.0.0.1 PORT=your_port # default is 8000 IGNORE_DIVID_CONF=TRUE # Use this to skip checking for a divid.conf file
-
To run the API using Docker, follow these steps:
-
Build the Docker image:
docker build -t divi_api_image -f docker/Dockerfile .
-
Run the Docker container:
docker run -d \ --env-file .env.local \ -p 8000:8000 \ --name divi_api_container \ divi_api_image
-
Start the FastAPI server using
uvicorn
:uvicorn divi_api_server:app --reload
The server will run on
http://127.0.0.1:8000/
. -
Visit the automatically generated API docs at
http://127.0.0.1:8000/docs
to interact with the API.
Below are some example API calls that can be made using curl
or any HTTP client:
-
Ping the server:
curl http://127.0.0.1:8000/ping
-
Get the current block count:
curl http://127.0.0.1:8000/blockcount
-
Get block information by hash:
curl http://127.0.0.1:8000/block/{block_hash}
-
Send a raw transaction:
curl -X POST "http://127.0.0.1:8000/sendrawtransaction" \ -H "Content-Type: application/json" \ -d '{"hexstring": "your_raw_transaction_hex"}'
The following is a summary of the available API endpoints:
GET /ping
: Ping the server to ensure it's running.GET /blockcount
: Get the current block count of the Divi blockchain.GET /block/{hash}
: Fetch block information by its hash.GET /tx/{txid}
: Get transaction details by transaction ID.GET /getaddressbalance/{address}/{isVault}
: Get the balance for a given address.POST /sendrawtransaction
: Broadcast a raw transaction to the blockchain.
For a complete list of endpoints, visit the API documentation at http://127.0.0.1:8000/docs
.
The RPC credentials are read from either the divi.conf
file or environment variables. The following environment variables can be used:
RPC_USER
: Your Divi node's RPC username.RPC_PASS
: Your Divi node's RPC password.RPC_PORT
: The RPC port your Divi node is using (default: 51473).
Yes, logging in Python can easily be made optional by allowing users to configure it. You can provide a mechanism to either disable logging completely or set a desired logging level (such as only logging errors). This can be done by making the logging setup in the main.py
or rpc_client.py
file configurable, and adding instructions in the README.md
for how to do so.
You can modify the main.py
and rpc_client.py
to allow for optional logging using environment variables or config parameters. Here's a way to make it configurable:
import os
import logging
# Set logging level based on environment variable (default to INFO)
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
# Configure logging with the set log level
logging.basicConfig(level=getattr(logging, LOG_LEVEL, logging.INFO))
# Example API definition continues...
Since main.py
configures the logging globally, no changes are needed in rpc_client.py
. But if you want fine-grained control over logging, you can wrap logging calls with a condition:
import logging
# Check if logging is enabled globally (this can also be configured using an environment variable)
if logging.getLogger().isEnabledFor(logging.DEBUG):
logging.debug(f"Debug log message here")
By default, the API logs all RPC requests and responses for debugging and monitoring purposes. You can control the logging level or disable logging entirely using an environment variable.
To disable logging, set the environment variable LOG_LEVEL
to ERROR
, which will only log critical errors and suppress other information or debug logs.
-
Linux/macOS (using
bash
):export LOG_LEVEL=ERROR
-
Windows (using
cmd
):set LOG_LEVEL=ERROR
Alternatively, to disable logging completely, set the LOG_LEVEL
to CRITICAL
:
export LOG_LEVEL=CRITICAL
The following logging levels are available, listed in increasing order of severity:
DEBUG
: Logs all debugging information, useful for troubleshooting.INFO
: Logs general information (default).WARNING
: Logs warning messages.ERROR
: Logs only error messages.CRITICAL
: Logs only critical errors (most severe).
To set a custom logging level, use:
export LOG_LEVEL=DEBUG # or INFO, WARNING, ERROR, CRITICAL
CORS is enabled by default in this application to allow cross-origin requests, which is helpful during development. However, enabling CORS for all origins can expose your API to security vulnerabilities, especially in production environments.
-
Disabling CORS (Recommended for production):
To disable CORS entirely (no external domains allowed to make requests), comment out or remove the following block in
main.py
:# app.add_middleware( # CORSMiddleware, # allow_origins=["*"], # allow_credentials=True, # allow_methods=["*"], # allow_headers=["*"], # )
-
Restricting CORS to Trusted Domains:
If you need to allow only specific trusted domains to make requests, modify the
allow_origins
list:app.add_middleware( CORSMiddleware, allow_origins=["https://trusted-domain.com"], # Replace with your domain allow_credentials=True, allow_methods=["GET", "POST"], # Limit allowed methods for more security allow_headers=["Content-Type"], # Limit allowed headers )
-
Enabling CORS for Development:
During development, you can leave CORS enabled for all origins:
app.add_middleware( CORSMiddleware, allow_origins=["*"], # Allows all domains (only for development) allow_credentials=True, allow_methods=["*"], allow_headers=["*"], )
- Allowing
allow_origins=["*"]
in production can expose your API to security risks. Ensure you review and restrict CORS as needed based on your use case.
This project is licensed under the MIT License.