From 4b75243cdea6834e257134e2754cd7cf1d779b64 Mon Sep 17 00:00:00 2001 From: Stefan Stammberger Date: Wed, 21 Jul 2021 19:38:49 +0200 Subject: [PATCH] feat: add lightning AddInvoice endpoint Though, only LND is implemented at the moment. --- app/models/lightning.py | 8 ++++++++ app/repositories/lightning.py | 11 +++++++++-- app/repositories/ln_impl/clightning.py | 7 +++++++ app/repositories/ln_impl/lnd.py | 21 +++++++++++++++++++++ app/routers/lightning.py | 17 ++++++++++++++++- 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 app/models/lightning.py diff --git a/app/models/lightning.py b/app/models/lightning.py new file mode 100644 index 0000000..ed869cd --- /dev/null +++ b/app/models/lightning.py @@ -0,0 +1,8 @@ +from pydantic import BaseModel + + +class Invoice(BaseModel): + r_hash: str + payment_request: str + add_index: int + payment_addr: str diff --git a/app/repositories/lightning.py b/app/repositories/lightning.py index c0a9069..20f5e7f 100644 --- a/app/repositories/lightning.py +++ b/app/repositories/lightning.py @@ -1,10 +1,17 @@ +from app.models.lightning import Invoice from app.utils import lightning_config if lightning_config.ln_node == "lnd": - from app.repositories.ln_impl.lnd import get_wallet_balance_impl + from app.repositories.ln_impl.lnd import (add_invoice_impl, + get_wallet_balance_impl) else: - from app.repositories.ln_impl.clightning import get_wallet_balance_impl + from app.repositories.ln_impl.clightning import (add_invoice_impl, + get_wallet_balance_impl) def get_wallet_balance(): return get_wallet_balance_impl() + + +def add_invoice(value_msat: int, memo: str = "", expiry: int = 3600, is_keysend: bool = False) -> Invoice: + return add_invoice_impl(memo, value_msat, expiry, is_keysend) diff --git a/app/repositories/ln_impl/clightning.py b/app/repositories/ln_impl/clightning.py index 901bd1f..09b725c 100644 --- a/app/repositories/ln_impl/clightning.py +++ b/app/repositories/ln_impl/clightning.py @@ -1,2 +1,9 @@ +from app.models.lightning import Invoice + + def get_wallet_balance_impl(): raise NotImplementedError("c-lightning not yet implemented") + + +def add_invoice_impl(value_msat: int, memo: str = "", expiry: int = 3600, is_keysend: bool = False) -> Invoice: + raise NotImplementedError("c-lightning not yet implemented") diff --git a/app/repositories/ln_impl/lnd.py b/app/repositories/ln_impl/lnd.py index 2b83454..63d5b2f 100644 --- a/app/repositories/ln_impl/lnd.py +++ b/app/repositories/ln_impl/lnd.py @@ -1,4 +1,5 @@ import app.repositories.ln_impl.protos.rpc_pb2 as ln +from app.models.lightning import Invoice from app.utils import lightning_config as lncfg @@ -13,3 +14,23 @@ def get_wallet_balance_impl() -> object: "total_balance": response.total_balance, "unconfirmed_balance": response.unconfirmed_balance, } + + +def add_invoice_impl(value_msat: int, memo: str = "", expiry: int = 3600, is_keysend: bool = False) -> Invoice: + i = ln.Invoice( + memo=memo, + value_msat=value_msat, + expiry=expiry, + is_keysend=is_keysend, + ) + + response = lncfg.lnd_stub.AddInvoice(i) + + invoice = Invoice( + r_hash=response.r_hash.hex(), + payment_request=response.payment_request, + add_index=response.add_index, + payment_addr=response.payment_addr.hex(), + ) + + return invoice diff --git a/app/routers/lightning.py b/app/routers/lightning.py index 83dc733..ea7ed66 100644 --- a/app/routers/lightning.py +++ b/app/routers/lightning.py @@ -1,5 +1,6 @@ from app.auth.auth_bearer import JWTBearer -from app.repositories.lightning import get_wallet_balance +from app.models.lightning import Invoice +from app.repositories.lightning import add_invoice, get_wallet_balance from fastapi import APIRouter, HTTPException, status from fastapi.params import Depends @@ -9,6 +10,20 @@ router = APIRouter( ) +@router.post("/addinvoice", summary="Addinvoice adds a new Invoice to the database.", + description="For additional information see [LND docs](https://api.lightning.community/#addinvoice)", + dependencies=[Depends(JWTBearer())], + status_code=status.HTTP_200_OK, + response_model=Invoice) +def addinvoice(value_msat: int, memo: str = "", expiry: int = 3600, is_keysend: bool = False): + try: + return add_invoice(memo, value_msat, expiry, is_keysend) + except HTTPException as r: + raise HTTPException(r.status_code, detail=r.reason) + except NotImplementedError as r: + raise HTTPException(status.HTTP_501_NOT_IMPLEMENTED, detail=r.args[0]) + + @router.get("/getwalletbalance", summary="Get the current on chain balances of the lighting wallet.", response_description="A JSON String with on chain wallet balances.", dependencies=[Depends(JWTBearer())],