From 5eeedd755ea462800bd1d13a783d0c8298ca1ade Mon Sep 17 00:00:00 2001 From: Stefan Stammberger Date: Sun, 13 Jun 2021 10:16:54 +0200 Subject: [PATCH] feat: implement first bitcoin core REST calls --- .env | 7 ++++++- app/routers/bitcoin.py | 45 ++++++++++++++++++++++++++++++++++++++++++ app/utils.py | 28 ++++++++++++++++++++++++++ main.py | 3 ++- requirements.txt | 5 +++-- 5 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 app/routers/bitcoin.py create mode 100644 app/utils.py diff --git a/.env b/.env index 02bc05d..402a5a4 100644 --- a/.env +++ b/.env @@ -1,2 +1,7 @@ secret=please_please_update_me_please -algorithm=HS256 \ No newline at end of file +algorithm=HS256 + +bitcoind_ip=127.0.0.1 +bitcoind_port=8332 +bitcoind_user=raspibolt +bitcoind_pw=please_please_update_me_please \ No newline at end of file diff --git a/app/routers/bitcoin.py b/app/routers/bitcoin.py new file mode 100644 index 0000000..b4baa72 --- /dev/null +++ b/app/routers/bitcoin.py @@ -0,0 +1,45 @@ +from fastapi import APIRouter, HTTPException, status, Request +from fastapi.params import Depends + +from app.utils import bitcoin_rpc +from app.auth.auth_bearer import JWTBearer + +router = APIRouter( + prefix="/bitcoin", + tags=["Bitcoin Core"] +) + + +@router.get("/getblockcount", summary="Get the current block count", + description="See documentation on [bitcoincore.org](https://bitcoincore.org/en/doc/0.21.0/rpc/blockchain/getblockcount/)", + response_description="""A JSON String with relevant information.\n +```json +{ + \"result\": 682621, + \"error\": null, + \"id\": 0 +} +""", + dependencies=[Depends(JWTBearer())], + status_code=status.HTTP_200_OK) +def getblockcount(): + r = bitcoin_rpc("getblockcount") + + if(r.status_code == status.HTTP_200_OK): + return r.content + else: + raise HTTPException(r.status_code, detail=r.reason) + + +@router.get("/getblockchaininfo", summary="Get the current blockchain status", + description="See documentation on [bitcoincore.org](https://bitcoincore.org/en/doc/0.21.0/rpc/blockchain/getblockchaininfo/)", + response_description="A JSON String with relevant information.", + dependencies=[Depends(JWTBearer())], + status_code=status.HTTP_200_OK) +def getblockchaininfo(): + r = bitcoin_rpc("getblockchaininfo") + + if(r.status_code == status.HTTP_200_OK): + return r.content + else: + raise HTTPException(r.status_code, detail=r.reason) diff --git a/app/utils.py b/app/utils.py new file mode 100644 index 0000000..712eddc --- /dev/null +++ b/app/utils.py @@ -0,0 +1,28 @@ +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) diff --git a/main.py b/main.py index 89b78af..dec7520 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,12 @@ from fastapi import FastAPI -from app.routers import system +from app.routers import bitcoin, system # start server with "uvicorn main:app --reload" app = FastAPI() +app.include_router(bitcoin.router) app.include_router(system.router) diff --git a/requirements.txt b/requirements.txt index 1392ef3..7be726f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,7 @@ pydantic==1.8.2 uvicorn==0.14.0 PyJWT==2.1.0 python-decouple==3.4 -autopep +autopep8==1.5.7 psutil==5.8.0 -sse_starlette==0.7.2 \ No newline at end of file +sse_starlette==0.7.2 +requests==2.25.1 \ No newline at end of file