2020-05-02 19:00:01 +02:00
|
|
|
try:
|
2020-10-03 22:27:55 +02:00
|
|
|
import lndgrpc # type: ignore
|
|
|
|
from lndgrpc.common import ln # type: ignore
|
2020-05-02 19:00:01 +02:00
|
|
|
except ImportError: # pragma: nocover
|
2020-10-03 22:27:55 +02:00
|
|
|
lndgrpc = None
|
2020-05-02 19:00:01 +02:00
|
|
|
|
2020-10-04 05:22:37 +02:00
|
|
|
try:
|
|
|
|
import purerpc # type: ignore
|
|
|
|
except ImportError: # pragma: nocover
|
|
|
|
purerpc = None
|
|
|
|
|
2020-10-03 22:27:55 +02:00
|
|
|
import binascii
|
2020-04-02 13:03:10 +02:00
|
|
|
import base64
|
2020-10-03 22:27:55 +02:00
|
|
|
import hashlib
|
2020-04-26 13:28:19 +02:00
|
|
|
from os import getenv
|
2020-10-02 22:13:33 +02:00
|
|
|
from typing import Optional, Dict, AsyncGenerator
|
2020-04-26 13:28:19 +02:00
|
|
|
|
2021-03-24 04:40:32 +01:00
|
|
|
from .base import (
|
|
|
|
StatusResponse,
|
|
|
|
InvoiceResponse,
|
|
|
|
PaymentResponse,
|
|
|
|
PaymentStatus,
|
|
|
|
Wallet,
|
|
|
|
)
|
2020-04-02 08:44:03 +02:00
|
|
|
|
|
|
|
|
2020-10-10 01:55:58 +02:00
|
|
|
def get_ssl_context(cert_path: str):
|
|
|
|
import ssl
|
|
|
|
|
|
|
|
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
|
|
|
|
context.options |= ssl.OP_NO_SSLv2
|
|
|
|
context.options |= ssl.OP_NO_SSLv3
|
|
|
|
context.options |= ssl.OP_NO_TLSv1
|
|
|
|
context.options |= ssl.OP_NO_TLSv1_1
|
|
|
|
context.options |= ssl.OP_NO_COMPRESSION
|
|
|
|
context.set_ciphers(
|
|
|
|
":".join(
|
|
|
|
[
|
|
|
|
"ECDHE+AESGCM",
|
|
|
|
"ECDHE+CHACHA20",
|
|
|
|
"DHE+AESGCM",
|
|
|
|
"DHE+CHACHA20",
|
|
|
|
"ECDH+AESGCM",
|
|
|
|
"DH+AESGCM",
|
|
|
|
"ECDH+AES",
|
|
|
|
"DH+AES",
|
|
|
|
"RSA+AESGCM",
|
|
|
|
"RSA+AES",
|
|
|
|
"!aNULL",
|
|
|
|
"!eNULL",
|
|
|
|
"!MD5",
|
|
|
|
"!DSS",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
context.load_verify_locations(capath=cert_path)
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
def load_macaroon(macaroon_path: str):
|
|
|
|
with open(macaroon_path, "rb") as f:
|
|
|
|
macaroon_bytes = f.read()
|
|
|
|
return macaroon_bytes.hex()
|
|
|
|
|
|
|
|
|
2020-10-02 22:13:33 +02:00
|
|
|
def parse_checking_id(checking_id: str) -> bytes:
|
|
|
|
return base64.b64decode(
|
|
|
|
checking_id.replace("_", "/"),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def stringify_checking_id(r_hash: bytes) -> str:
|
|
|
|
return (
|
|
|
|
base64.b64encode(
|
|
|
|
r_hash,
|
|
|
|
)
|
|
|
|
.decode("utf-8")
|
|
|
|
.replace("/", "_")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-02 13:03:10 +02:00
|
|
|
class LndWallet(Wallet):
|
2020-04-02 08:44:03 +02:00
|
|
|
def __init__(self):
|
2020-10-03 22:27:55 +02:00
|
|
|
if lndgrpc is None: # pragma: nocover
|
2021-03-24 04:40:32 +01:00
|
|
|
raise ImportError(
|
|
|
|
"The `lndgrpc` library must be installed to use `LndWallet`."
|
|
|
|
)
|
2020-05-02 19:00:01 +02:00
|
|
|
|
2020-10-05 13:45:32 +02:00
|
|
|
if purerpc is None: # pragma: nocover
|
2021-03-24 04:40:32 +01:00
|
|
|
raise ImportError(
|
|
|
|
"The `purerpc` library must be installed to use `LndWallet`."
|
|
|
|
)
|
2020-10-04 05:22:37 +02:00
|
|
|
|
2020-04-02 08:44:03 +02:00
|
|
|
endpoint = getenv("LND_GRPC_ENDPOINT")
|
2020-10-04 05:22:37 +02:00
|
|
|
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
|
|
|
|
self.port = int(getenv("LND_GRPC_PORT"))
|
|
|
|
self.cert_path = getenv("LND_GRPC_CERT") or getenv("LND_CERT")
|
2020-10-08 21:03:18 +02:00
|
|
|
|
|
|
|
self.macaroon_path = (
|
|
|
|
getenv("LND_GRPC_MACAROON")
|
|
|
|
or getenv("LND_GRPC_ADMIN_MACAROON")
|
|
|
|
or getenv("LND_ADMIN_MACAROON")
|
|
|
|
or getenv("LND_GRPC_INVOICE_MACAROON")
|
|
|
|
or getenv("LND_INVOICE_MACAROON")
|
|
|
|
)
|
2020-10-02 22:13:33 +02:00
|
|
|
network = getenv("LND_GRPC_NETWORK", "mainnet")
|
|
|
|
|
2020-10-13 03:25:55 +02:00
|
|
|
self.rpc = lndgrpc.LNDClient(
|
2020-10-04 05:22:37 +02:00
|
|
|
f"{self.endpoint}:{self.port}",
|
|
|
|
cert_filepath=self.cert_path,
|
2020-10-02 22:13:33 +02:00
|
|
|
network=network,
|
2020-10-08 21:03:18 +02:00
|
|
|
macaroon_filepath=self.macaroon_path,
|
2020-10-02 22:13:33 +02:00
|
|
|
)
|
2020-04-02 13:43:23 +02:00
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def status(self) -> StatusResponse:
|
2020-10-13 03:25:55 +02:00
|
|
|
try:
|
|
|
|
resp = self.rpc._ln_stub.ChannelBalance(ln.ChannelBalanceRequest())
|
|
|
|
except Exception as exc:
|
|
|
|
return StatusResponse(str(exc), 0)
|
|
|
|
|
|
|
|
return StatusResponse(None, resp.balance * 1000)
|
2020-04-02 08:44:03 +02:00
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def create_invoice(
|
2021-03-24 04:40:32 +01:00
|
|
|
self,
|
|
|
|
amount: int,
|
|
|
|
memo: Optional[str] = None,
|
|
|
|
description_hash: Optional[bytes] = None,
|
2020-10-02 22:13:33 +02:00
|
|
|
) -> InvoiceResponse:
|
2020-08-31 04:48:46 +02:00
|
|
|
params: Dict = {"value": amount, "expiry": 600, "private": True}
|
2020-10-03 22:27:55 +02:00
|
|
|
|
2020-08-31 04:48:46 +02:00
|
|
|
if description_hash:
|
|
|
|
params["description_hash"] = description_hash # as bytes directly
|
|
|
|
else:
|
|
|
|
params["memo"] = memo or ""
|
2020-10-03 22:27:55 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
req = ln.Invoice(**params)
|
2020-10-13 03:25:55 +02:00
|
|
|
resp = self.rpc._ln_stub.AddInvoice(req)
|
2020-10-03 22:27:55 +02:00
|
|
|
except Exception as exc:
|
|
|
|
error_message = str(exc)
|
|
|
|
return InvoiceResponse(False, None, None, error_message)
|
2020-04-02 08:44:03 +02:00
|
|
|
|
2020-10-02 22:13:33 +02:00
|
|
|
checking_id = stringify_checking_id(resp.r_hash)
|
|
|
|
payment_request = str(resp.payment_request)
|
|
|
|
return InvoiceResponse(True, checking_id, payment_request, None)
|
2020-04-02 08:44:03 +02:00
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def pay_invoice(self, bolt11: str) -> PaymentResponse:
|
2020-10-13 03:25:55 +02:00
|
|
|
resp = self.rpc.send_payment(payment_request=bolt11)
|
2020-04-26 13:28:19 +02:00
|
|
|
|
2020-10-02 22:13:33 +02:00
|
|
|
if resp.payment_error:
|
2020-10-13 04:18:37 +02:00
|
|
|
return PaymentResponse(False, "", 0, None, resp.payment_error)
|
2020-04-02 08:44:03 +02:00
|
|
|
|
2020-10-03 22:27:55 +02:00
|
|
|
r_hash = hashlib.sha256(resp.payment_preimage).digest()
|
|
|
|
checking_id = stringify_checking_id(r_hash)
|
2020-10-13 04:18:37 +02:00
|
|
|
fee_msat = resp.payment_route.total_fees_msat
|
|
|
|
preimage = resp.payment_preimage.hex()
|
|
|
|
return PaymentResponse(True, checking_id, fee_msat, preimage, None)
|
2020-04-02 08:44:03 +02:00
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
2020-10-03 22:27:55 +02:00
|
|
|
try:
|
|
|
|
r_hash = parse_checking_id(checking_id)
|
|
|
|
if len(r_hash) != 32:
|
|
|
|
raise binascii.Error
|
|
|
|
except binascii.Error:
|
|
|
|
# this may happen if we switch between backend wallets
|
|
|
|
# that use different checking_id formats
|
|
|
|
return PaymentStatus(None)
|
|
|
|
|
2020-10-13 03:25:55 +02:00
|
|
|
resp = self.rpc.lookup_invoice(r_hash.hex())
|
2020-10-03 22:27:55 +02:00
|
|
|
if resp.settled:
|
|
|
|
return PaymentStatus(True)
|
2020-04-02 08:44:03 +02:00
|
|
|
|
2020-04-26 13:28:19 +02:00
|
|
|
return PaymentStatus(None)
|
2020-04-02 08:44:03 +02:00
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
|
2020-04-02 08:44:03 +02:00
|
|
|
return PaymentStatus(True)
|
2020-10-02 22:13:33 +02:00
|
|
|
|
|
|
|
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
|
2020-10-04 05:22:37 +02:00
|
|
|
async with purerpc.secure_channel(
|
|
|
|
self.endpoint,
|
|
|
|
self.port,
|
|
|
|
get_ssl_context(self.cert_path),
|
|
|
|
) as channel:
|
|
|
|
client = purerpc.Client("lnrpc.Lightning", channel)
|
|
|
|
subscribe_invoices = client.get_method_stub(
|
|
|
|
"SubscribeInvoices",
|
|
|
|
purerpc.RPCSignature(
|
|
|
|
purerpc.Cardinality.UNARY_STREAM,
|
|
|
|
ln.InvoiceSubscription,
|
|
|
|
ln.Invoice,
|
|
|
|
),
|
|
|
|
)
|
2020-10-08 21:03:18 +02:00
|
|
|
macaroon = load_macaroon(self.macaroon_path)
|
2020-10-04 05:22:37 +02:00
|
|
|
|
|
|
|
async for inv in subscribe_invoices(
|
|
|
|
ln.InvoiceSubscription(),
|
|
|
|
metadata=[("macaroon", macaroon)],
|
|
|
|
):
|
|
|
|
if not inv.settled:
|
|
|
|
continue
|
|
|
|
|
|
|
|
checking_id = stringify_checking_id(inv.r_hash)
|
|
|
|
yield checking_id
|
|
|
|
|
2020-10-10 01:55:58 +02:00
|
|
|
print("lost connection to lnd InvoiceSubscription, please restart lnbits.")
|