feat: add lightning AddInvoice endpoint

Though, only LND is implemented at the moment.
This commit is contained in:
Stefan Stammberger 2021-07-21 19:38:49 +02:00
parent a44717547f
commit 4b75243cde
No known key found for this signature in database
GPG key ID: 645FA807E935D9D5
5 changed files with 61 additions and 3 deletions

8
app/models/lightning.py Normal file
View file

@ -0,0 +1,8 @@
from pydantic import BaseModel
class Invoice(BaseModel):
r_hash: str
payment_request: str
add_index: int
payment_addr: str

View file

@ -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)

View file

@ -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")

View file

@ -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

View file

@ -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())],