mirror of https://github.com/lukechilds/lnbits.git
Eneko Illarramendi
5 years ago
15 changed files with 206 additions and 166 deletions
@ -1,15 +1,13 @@ |
|||||
|
import importlib |
||||
import os |
import os |
||||
|
|
||||
from .wallets import OpenNodeWallet # OR LndWallet OR OpennodeWallet |
|
||||
|
|
||||
WALLET = OpenNodeWallet(endpoint=os.getenv("OPENNODE_API_ENDPOINT"),admin_key=os.getenv("OPENNODE_ADMIN_KEY"),invoice_key=os.getenv("OPENNODE_INVOICE_KEY")) |
|
||||
#WALLET = LntxbotWallet(endpoint=os.getenv("LNTXBOT_API_ENDPOINT"),admin_key=os.getenv("LNTXBOT_ADMIN_KEY"),invoice_key=os.getenv("LNTXBOT_INVOICE_KEY")) |
|
||||
#WALLET = LndWallet(endpoint=os.getenv("LND_API_ENDPOINT"),admin_macaroon=os.getenv("LND_ADMIN_MACAROON"),invoice_macaroon=os.getenv("LND_INVOICE_MACAROON"),read_macaroon=os.getenv("LND_READ_MACAROON")) |
|
||||
#WALLET = LNPayWallet(endpoint=os.getenv("LNPAY_API_ENDPOINT"),admin_key=os.getenv("LNPAY_ADMIN_KEY"),invoice_key=os.getenv("LNPAY_INVOICE_KEY"),api_key=os.getenv("LNPAY_API_KEY"),read_key=os.getenv("LNPAY_READ_KEY")) |
|
||||
|
|
||||
|
wallets_module = importlib.import_module(f"lnbits.wallets") |
||||
|
wallet_class = getattr(wallets_module, os.getenv("LNBITS_BACKEND_WALLET_CLASS", "LntxbotWallet")) |
||||
|
|
||||
LNBITS_PATH = os.path.dirname(os.path.realpath(__file__)) |
LNBITS_PATH = os.path.dirname(os.path.realpath(__file__)) |
||||
LNBITS_DATA_FOLDER = os.getenv("LNBITS_DATA_FOLDER", os.path.join(LNBITS_PATH, "data")) |
LNBITS_DATA_FOLDER = os.getenv("LNBITS_DATA_FOLDER", os.path.join(LNBITS_PATH, "data")) |
||||
|
|
||||
|
WALLET = wallet_class() |
||||
DEFAULT_WALLET_NAME = os.getenv("LNBITS_DEFAULT_WALLET_NAME", "LNbits wallet") |
DEFAULT_WALLET_NAME = os.getenv("LNBITS_DEFAULT_WALLET_NAME", "LNbits wallet") |
||||
FEE_RESERVE = float(os.getenv("LNBITS_FEE_RESERVE", 0)) |
FEE_RESERVE = float(os.getenv("LNBITS_FEE_RESERVE", 0)) |
||||
|
@ -1,47 +1,60 @@ |
|||||
|
from os import getenv |
||||
from requests import get, post |
from requests import get, post |
||||
|
|
||||
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet |
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet |
||||
|
|
||||
|
|
||||
class OpenNodeWallet(Wallet): |
class OpenNodeWallet(Wallet): |
||||
"""https://api.lightning.community/rest/index.html#lnd-rest-api-reference""" |
"""https://developers.opennode.com/""" |
||||
|
|
||||
def __init__(self, *, endpoint: str, admin_key: str, invoice_key: str): |
def __init__(self): |
||||
|
endpoint = getenv("OPENNODE_API_ENDPOINT") |
||||
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint |
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint |
||||
self.auth_admin = {"Authorization": admin_key} |
self.auth_admin = {"Authorization": getenv("OPENNODE_ADMIN_KEY")} |
||||
self.auth_invoice = {"Authorization": invoice_key} |
self.auth_invoice = {"Authorization": getenv("OPENNODE_INVOICE_KEY")} |
||||
|
|
||||
def create_invoice(self, amount: int, memo: str = "") -> InvoiceResponse: |
def create_invoice(self, amount: int, memo: str = "") -> InvoiceResponse: |
||||
payment_hash, payment_request = None, None |
|
||||
r = post( |
r = post( |
||||
url=f"{self.endpoint}/v1/charges", |
url=f"{self.endpoint}/v1/charges", |
||||
headers=self.auth_invoice, |
headers=self.auth_invoice, |
||||
json={"amount": f"{amount}", "description": memo}, # , "private": True}, |
json={"amount": f"{amount}", "description": memo}, # , "private": True}, |
||||
) |
) |
||||
|
ok, checking_id, payment_request, error_message = r.ok, None, None, None |
||||
|
|
||||
if r.ok: |
if r.ok: |
||||
data = r.json() |
data = r.json()["data"] |
||||
payment_hash, payment_request = data["data"]["id"], data["data"]["lightning_invoice"]["payreq"] |
checking_id, payment_request = data["id"], data["lightning_invoice"]["payreq"] |
||||
|
else: |
||||
|
error_message = r.json()["message"] |
||||
|
|
||||
return InvoiceResponse(r, payment_hash, payment_request) |
return InvoiceResponse(ok, checking_id, payment_request, error_message) |
||||
|
|
||||
def pay_invoice(self, bolt11: str) -> PaymentResponse: |
def pay_invoice(self, bolt11: str) -> PaymentResponse: |
||||
r = post(url=f"{self.endpoint}/v2/withdrawals", headers=self.auth_admin, json={"type": "ln", "address": bolt11}) |
r = post(url=f"{self.endpoint}/v2/withdrawals", headers=self.auth_admin, json={"type": "ln", "address": bolt11}) |
||||
return PaymentResponse(r, not r.ok) |
ok, checking_id, fee_msat, error_message = r.ok, None, 0, None |
||||
|
|
||||
|
if r.ok: |
||||
|
data = r.json()["data"] |
||||
|
checking_id, fee_msat = data["id"], data["fee"] * 1000 |
||||
|
else: |
||||
|
error_message = r.json()["message"] |
||||
|
|
||||
|
return PaymentResponse(ok, checking_id, fee_msat, error_message) |
||||
|
|
||||
def get_invoice_status(self, payment_hash: str) -> PaymentStatus: |
def get_invoice_status(self, checking_id: str) -> PaymentStatus: |
||||
r = get(url=f"{self.endpoint}/v1/charge/{payment_hash}", headers=self.auth_invoice) |
r = get(url=f"{self.endpoint}/v1/charge/{checking_id}", headers=self.auth_invoice) |
||||
|
|
||||
if not r.ok: |
if not r.ok: |
||||
return PaymentStatus(r, None) |
return PaymentStatus(None) |
||||
|
|
||||
statuses = {"processing": None, "paid": True, "unpaid": False} |
statuses = {"processing": None, "paid": True, "unpaid": False} |
||||
return PaymentStatus(r, statuses[r.json()["data"]["status"]]) |
return PaymentStatus(statuses[r.json()["data"]["status"]]) |
||||
|
|
||||
def get_payment_status(self, payment_hash: str) -> PaymentStatus: |
def get_payment_status(self, checking_id: str) -> PaymentStatus: |
||||
r = get(url=f"{self.endpoint}/v1/withdrawal/{payment_hash}", headers=self.auth_admin) |
r = get(url=f"{self.endpoint}/v1/withdrawal/{checking_id}", headers=self.auth_admin) |
||||
|
|
||||
if not r.ok: |
if not r.ok: |
||||
return PaymentStatus(r, None) |
return PaymentStatus(None) |
||||
|
|
||||
statuses = {"pending": None, "confirmed": True, "error": False, "failed": False} |
statuses = {"pending": None, "confirmed": True, "error": False, "failed": False} |
||||
return PaymentStatus(r, statuses[r.json()["data"]["status"]]) |
return PaymentStatus(statuses[r.json()["data"]["status"]]) |
||||
|
Loading…
Reference in new issue