blitz_api/app/utils.py
2021-06-13 10:16:54 +02:00

28 lines
868 B
Python

import requests
import json
from decouple import config
BITCOIND_IP = config("bitcoind_ip")
BITCOIND_PORT = config("bitcoind_port")
BITCOIND_USER = config("bitcoind_user")
BITCOIND_PW = config("bitcoind_pw")
def bitcoin_rpc(method: str, params: list = []) -> requests.Response:
"""Make an RPC request to the Bitcoin daemon
Connection parameters are read from the .env file.
Parameters
----------
method : str
The method to call.
params : list, optional
Any parameters to include with the call
"""
url = f"http://{BITCOIND_IP}:{BITCOIND_PORT}"
auth = (BITCOIND_USER, BITCOIND_PW)
headers = {"Content-type": "text/plain"}
data = '{"jsonrpc": "2.0", "method": "' + \
method + '", "id":"0", "params":' + json.dumps(params) + '}'
return requests.post(url, auth=auth, headers=headers, data=data)