diff --git a/Makefile b/Makefile index 80ae00c20..6b2fdeb7e 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,10 @@ test: poetry run pytest test-real-wallet: + BOLTZ_NETWORK="regtest" \ + BOLTZ_URL="http://127.0.0.1:9001" \ + BOLTZ_MEMPOOL_SPACE_URL="http://127.0.0.1:8080" \ + BOLTZ_MEMPOOL_SPACE_URL_WS="ws://127.0.0.1:8080" \ LNBITS_DATA_FOLDER="./tests/data" \ PYTHONUNBUFFERED=1 \ DEBUG=true \ diff --git a/lnbits/app.py b/lnbits/app.py index fb750eb32..f612c32c3 100644 --- a/lnbits/app.py +++ b/lnbits/app.py @@ -122,10 +122,10 @@ def check_funding_source(app: FastAPI) -> None: f"The backend for {WALLET.__class__.__name__} isn't working properly: '{error_message}'", RuntimeWarning, ) - logger.info("Retrying connection to backend in 5 seconds...") - await asyncio.sleep(5) except: pass + logger.info("Retrying connection to backend in 5 seconds...") + await asyncio.sleep(5) signal.signal(signal.SIGINT, original_sigint_handler) logger.info( f"✔️ Backend {WALLET.__class__.__name__} connected and with a balance of {balance} msat." diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index f150270a8..cba41f602 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -365,6 +365,11 @@ async def create_payment( webhook: Optional[str] = None, conn: Optional[Connection] = None, ) -> Payment: + + # todo: add this when tests are fixed + # previous_payment = await get_wallet_payment(wallet_id, payment_hash, conn=conn) + # assert previous_payment is None, "Payment already exists" + await (conn or db).execute( """ INSERT INTO apipayments @@ -404,6 +409,40 @@ async def update_payment_status( ) +async def update_payment_details( + checking_id: str, + pending: Optional[bool] = None, + fee: Optional[int] = None, + preimage: Optional[str] = None, + new_checking_id: Optional[str] = None, + conn: Optional[Connection] = None, +) -> None: + + set_clause: List[str] = [] + set_variables: List[Any] = [] + + if new_checking_id is not None: + set_clause.append("checking_id = ?") + set_variables.append(new_checking_id) + if pending is not None: + set_clause.append("pending = ?") + set_variables.append(pending) + if fee is not None: + set_clause.append("fee = ?") + set_variables.append(fee) + if preimage is not None: + set_clause.append("preimage = ?") + set_variables.append(preimage) + + set_variables.append(checking_id) + + await (conn or db).execute( + f"UPDATE apipayments SET {', '.join(set_clause)} WHERE checking_id = ?", + tuple(set_variables), + ) + return + + async def delete_payment(checking_id: str, conn: Optional[Connection] = None) -> None: await (conn or db).execute( "DELETE FROM apipayments WHERE checking_id = ?", (checking_id,) diff --git a/lnbits/core/models.py b/lnbits/core/models.py index c019d941d..4dc15bbc9 100644 --- a/lnbits/core/models.py +++ b/lnbits/core/models.py @@ -11,6 +11,7 @@ from pydantic import BaseModel from lnbits.helpers import url_for from lnbits.settings import WALLET +from lnbits.wallets.base import PaymentStatus class Wallet(BaseModel): @@ -128,8 +129,16 @@ class Payment(BaseModel): @property def is_uncheckable(self) -> bool: - return self.checking_id.startswith("temp_") or self.checking_id.startswith( - "internal_" + return self.checking_id.startswith("internal_") + + async def update_status(self, status: PaymentStatus) -> None: + from .crud import update_payment_details + + await update_payment_details( + checking_id=self.checking_id, + pending=status.pending, + fee=status.fee_msat, + preimage=status.preimage, ) async def set_pending(self, pending: bool) -> None: @@ -137,9 +146,9 @@ class Payment(BaseModel): await update_payment_status(self.checking_id, pending) - async def check_pending(self) -> None: + async def check_status(self) -> PaymentStatus: if self.is_uncheckable: - return + return PaymentStatus(None) logger.debug( f"Checking {'outgoing' if self.is_out else 'incoming'} pending payment {self.checking_id}" @@ -153,7 +162,7 @@ class Payment(BaseModel): logger.debug(f"Status: {status}") if self.is_out and status.failed: - logger.info( + logger.warning( f"Deleting outgoing failed payment {self.checking_id}: {status}" ) await self.delete() @@ -161,7 +170,8 @@ class Payment(BaseModel): logger.info( f"Marking '{'in' if self.is_in else 'out'}' {self.checking_id} as not pending anymore: {status}" ) - await self.set_pending(status.pending) + await self.update_status(status) + return status async def delete(self) -> None: from .crud import delete_payment diff --git a/lnbits/core/services.py b/lnbits/core/services.py index 90f621862..a6e0b43a3 100644 --- a/lnbits/core/services.py +++ b/lnbits/core/services.py @@ -31,8 +31,10 @@ from .crud import ( delete_payment, get_wallet, get_wallet_payment, + update_payment_details, update_payment_status, ) +from .models import Payment try: from typing import TypedDict # type: ignore @@ -101,11 +103,20 @@ async def pay_invoice( description: str = "", conn: Optional[Connection] = None, ) -> str: + """ + Pay a Lightning invoice. + First, we create a temporary payment in the database with fees set to the reserve fee. + We then check whether the balance of the payer would go negative. + We then attempt to pay the invoice through the backend. + If the payment is successful, we update the payment in the database with the payment details. + If the payment is unsuccessful, we delete the temporary payment. + If the payment is still in flight, we hope that some other process will regularly check for the payment. + """ invoice = bolt11.decode(payment_request) fee_reserve_msat = fee_reserve(invoice.amount_msat) async with (db.reuse_conn(conn) if conn else db.connect()) as conn: - temp_id = f"temp_{urlsafe_short_hash()}" - internal_id = f"internal_{urlsafe_short_hash()}" + temp_id = invoice.payment_hash + internal_id = f"internal_{invoice.payment_hash}" if invoice.amount_msat == 0: raise ValueError("Amountless invoices not supported.") @@ -185,30 +196,41 @@ async def pay_invoice( payment: PaymentResponse = await WALLET.pay_invoice( payment_request, fee_reserve_msat ) + + if payment.checking_id and payment.checking_id != temp_id: + logger.warning( + f"backend sent unexpected checking_id (expected: {temp_id} got: {payment.checking_id})" + ) + logger.debug(f"backend: pay_invoice finished {temp_id}") - if payment.ok and payment.checking_id: - logger.debug(f"creating final payment {payment.checking_id}") + if payment.checking_id and payment.ok != False: + # payment.ok can be True (paid) or None (pending)! + logger.debug(f"updating payment {temp_id}") async with db.connect() as conn: - await create_payment( - checking_id=payment.checking_id, + await update_payment_details( + checking_id=temp_id, + pending=payment.ok != True, fee=payment.fee_msat, preimage=payment.preimage, - pending=payment.ok == None, + new_checking_id=payment.checking_id, conn=conn, - **payment_kwargs, ) - logger.debug(f"deleting temporary payment {temp_id}") - await delete_payment(temp_id, conn=conn) - else: - logger.debug(f"backend payment failed") + logger.debug(f"payment successful {payment.checking_id}") + elif payment.checking_id is None and payment.ok == False: + # payment failed + logger.warning(f"backend sent payment failure") async with db.connect() as conn: logger.debug(f"deleting temporary payment {temp_id}") await delete_payment(temp_id, conn=conn) raise PaymentFailure( - payment.error_message - or "Payment failed, but backend didn't give us an error message." + f"payment failed: {payment.error_message}" + or "payment failed, but backend didn't give us an error message" ) - logger.debug(f"payment successful {payment.checking_id}") + else: + logger.warning( + f"didn't receive checking_id from backend, payment may be stuck in database: {temp_id}" + ) + return invoice.payment_hash @@ -344,23 +366,16 @@ async def perform_lnurlauth( async def check_transaction_status( wallet_id: str, payment_hash: str, conn: Optional[Connection] = None ) -> PaymentStatus: - payment = await get_wallet_payment(wallet_id, payment_hash, conn=conn) + payment: Optional[Payment] = await get_wallet_payment( + wallet_id, payment_hash, conn=conn + ) if not payment: return PaymentStatus(None) - if payment.is_out: - status = await WALLET.get_payment_status(payment.checking_id) - else: - status = await WALLET.get_invoice_status(payment.checking_id) if not payment.pending: - return status - if payment.is_out and status.failed: - logger.info(f"deleting outgoing failed payment {payment.checking_id}: {status}") - await payment.delete() - elif not status.pending: - logger.info( - f"marking '{'in' if payment.is_in else 'out'}' {payment.checking_id} as not pending anymore: {status}" - ) - await payment.set_pending(status.pending) + # note: before, we still checked the status of the payment again + return PaymentStatus(True) + + status: PaymentStatus = await payment.check_status() return status diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index d764d3d2b..830cc16a8 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -378,7 +378,7 @@ async def subscribe(request: Request, wallet: Wallet): while True: payment: Payment = await payment_queue.get() if payment.wallet_id == this_wallet_id: - logger.debug("payment receieved", payment) + logger.debug("payment received", payment) await send_queue.put(("payment-received", payment)) asyncio.create_task(payment_received()) @@ -402,6 +402,10 @@ async def subscribe(request: Request, wallet: Wallet): async def api_payments_sse( request: Request, wallet: WalletTypeInfo = Depends(get_key_type) ): + if wallet is None or wallet.wallet is None: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, detail="Wallet does not exist." + ) return EventSourceResponse( subscribe(request, wallet.wallet), ping=20, media_type="text/event-stream" ) @@ -436,7 +440,7 @@ async def api_payment(payment_hash, X_Api_Key: Optional[str] = Header(None)): return {"paid": True, "preimage": payment.preimage} try: - await payment.check_pending() + await payment.check_status() except Exception: if wallet and wallet.id == payment.wallet_id: return {"paid": False, "details": payment} diff --git a/lnbits/extensions/boltz/README.md b/lnbits/extensions/boltz/README.md new file mode 100644 index 000000000..28289b73a --- /dev/null +++ b/lnbits/extensions/boltz/README.md @@ -0,0 +1,40 @@ +# Swap on [Boltz](https://boltz.exchange) +providing **trustless** and **account-free** swap services since **2018.** +move **IN** and **OUT** of the **lightning network** and remain in control of your bitcoin, at all times. +* [Lightning Node](https://amboss.space/node/026165850492521f4ac8abd9bd8088123446d126f648ca35e60f88177dc149ceb2) +* [Documentation](https://docs.boltz.exchange/en/latest/) +* [Discord](https://discord.gg/d6EK85KK) +* [Twitter](https://twitter.com/Boltzhq) + +# usage +This extension lets you create swaps, reverse swaps and in the case of failure refund your onchain funds. + +## create normal swap +1. click on "Swap (IN)" button to open following dialog, select a wallet, choose a proper amount in the min-max range and choose a onchain address to do your refund to if the swap fails after you already commited onchain funds. +--- +![create swap](https://imgur.com/OyOh3Nm.png) +--- +2. after you confirm your inputs, following dialog with the QR code for the onchain transaction, onchain- address and amount, will pop up. +--- +![pay onchain tx](https://imgur.com/r2UhwCY.png) +--- +3. after you pay this onchain address with the correct amount, boltz will see it and will pay your invoice and the sats will appear on your wallet. + +if anything goes wrong when boltz is trying to pay your invoice, the swap will fail and you will need to refund your onchain funds after the timeout block height hit. (if boltz can pay the invoice, it wont be able to redeem your onchain funds either). + +## create reverse swap +1. click on "Swap (OUT)" button to open following dialog, select a wallet, choose a proper amount in the min-max range and choose a onchain address to receive your funds to. Instant settlement: means that LNbits will create the onchain claim transaction if it sees the boltz lockup transaction in the mempool, but it is not confirmed yet. it is advised to leave this checked because it is faster and the longer is takes to settle, the higher the chances are that the lightning invoice expires and the swap fails. +--- +![reverse swap](https://imgur.com/UEAPpbs.png) +--- +if this swap fails, boltz is doing the onchain refunding, because they have to commit onchain funds. + +# refund locked onchain funds from a normal swap +if for some reason the normal swap fails and you already paid onchain, you can easily refund your btc. +this can happen if boltz is not able to pay your lightning invoice after you locked up your funds. +in case that happens, there is a info icon in the Swap (In) List which opens following dialog. +--- +![refund](https://imgur.com/pN81ltf.png) +---- +if the timeout block height is exceeded you can either press refund and lnbits will do the refunding to the address you specified when creating the swap. Or download the refundfile so you can manually refund your onchain directly on the boltz.exchange website. +if you think there is something wrong and/or you are unsure, you can ask for help either in LNbits telegram or in Boltz [Discord](https://discord.gg/d6EK85KK) diff --git a/lnbits/extensions/boltz/__init__.py b/lnbits/extensions/boltz/__init__.py new file mode 100644 index 000000000..7eb2e5a70 --- /dev/null +++ b/lnbits/extensions/boltz/__init__.py @@ -0,0 +1,26 @@ +import asyncio + +from fastapi import APIRouter + +from lnbits.db import Database +from lnbits.helpers import template_renderer +from lnbits.tasks import catch_everything_and_restart + +db = Database("ext_boltz") + +boltz_ext: APIRouter = APIRouter(prefix="/boltz", tags=["boltz"]) + + +def boltz_renderer(): + return template_renderer(["lnbits/extensions/boltz/templates"]) + + +from .tasks import check_for_pending_swaps, wait_for_paid_invoices +from .views import * # noqa +from .views_api import * # noqa + + +def boltz_start(): + loop = asyncio.get_event_loop() + loop.create_task(check_for_pending_swaps()) + loop.create_task(catch_everything_and_restart(wait_for_paid_invoices)) diff --git a/lnbits/extensions/boltz/boltz.py b/lnbits/extensions/boltz/boltz.py new file mode 100644 index 000000000..4e5fecd00 --- /dev/null +++ b/lnbits/extensions/boltz/boltz.py @@ -0,0 +1,424 @@ +import asyncio +import os +from binascii import hexlify, unhexlify +from hashlib import sha256 +from typing import Awaitable, Union + +import httpx +from embit import ec, script +from embit.networks import NETWORKS +from embit.transaction import SIGHASH, Transaction, TransactionInput, TransactionOutput +from loguru import logger + +from lnbits.core.services import create_invoice, pay_invoice +from lnbits.helpers import urlsafe_short_hash +from lnbits.settings import BOLTZ_NETWORK, BOLTZ_URL + +from .crud import update_swap_status +from .mempool import ( + get_fee_estimation, + get_mempool_blockheight, + get_mempool_fees, + get_mempool_tx, + get_mempool_tx_from_txs, + send_onchain_tx, + wait_for_websocket_message, +) +from .models import ( + CreateReverseSubmarineSwap, + CreateSubmarineSwap, + ReverseSubmarineSwap, + SubmarineSwap, + SwapStatus, +) +from .utils import check_balance, get_timestamp, req_wrap + +net = NETWORKS[BOLTZ_NETWORK] +logger.debug(f"BOLTZ_URL: {BOLTZ_URL}") +logger.debug(f"Bitcoin Network: {net['name']}") + + +async def create_swap(data: CreateSubmarineSwap) -> SubmarineSwap: + if not check_boltz_limits(data.amount): + msg = f"Boltz - swap not in boltz limits" + logger.warning(msg) + raise Exception(msg) + + swap_id = urlsafe_short_hash() + try: + payment_hash, payment_request = await create_invoice( + wallet_id=data.wallet, + amount=data.amount, + memo=f"swap of {data.amount} sats on boltz.exchange", + extra={"tag": "boltz", "swap_id": swap_id}, + ) + except Exception as exc: + msg = f"Boltz - create_invoice failed {str(exc)}" + logger.error(msg) + raise + + refund_privkey = ec.PrivateKey(os.urandom(32), True, net) + refund_pubkey_hex = hexlify(refund_privkey.sec()).decode("UTF-8") + + res = req_wrap( + "post", + f"{BOLTZ_URL}/createswap", + json={ + "type": "submarine", + "pairId": "BTC/BTC", + "orderSide": "sell", + "refundPublicKey": refund_pubkey_hex, + "invoice": payment_request, + "referralId": "lnbits", + }, + headers={"Content-Type": "application/json"}, + ) + res = res.json() + logger.info( + f"Boltz - created normal swap, boltz_id: {res['id']}. wallet: {data.wallet}" + ) + return SubmarineSwap( + id=swap_id, + time=get_timestamp(), + wallet=data.wallet, + amount=data.amount, + payment_hash=payment_hash, + refund_privkey=refund_privkey.wif(net), + refund_address=data.refund_address, + boltz_id=res["id"], + status="pending", + address=res["address"], + expected_amount=res["expectedAmount"], + timeout_block_height=res["timeoutBlockHeight"], + bip21=res["bip21"], + redeem_script=res["redeemScript"], + ) + + +""" +explanation taken from electrum +send on Lightning, receive on-chain +- User generates preimage, RHASH. Sends RHASH to server. +- Server creates an LN invoice for RHASH. +- User pays LN invoice - except server needs to hold the HTLC as preimage is unknown. +- Server creates on-chain output locked to RHASH. +- User spends on-chain output, revealing preimage. +- Server fulfills HTLC using preimage. +Note: expected_onchain_amount_sat is BEFORE deducting the on-chain claim tx fee. +""" + + +async def create_reverse_swap( + data: CreateReverseSubmarineSwap, +) -> [ReverseSubmarineSwap, asyncio.Task]: + if not check_boltz_limits(data.amount): + msg = f"Boltz - reverse swap not in boltz limits" + logger.warning(msg) + raise Exception(msg) + + swap_id = urlsafe_short_hash() + + if not await check_balance(data): + logger.error(f"Boltz - reverse swap, insufficient balance.") + return False + + claim_privkey = ec.PrivateKey(os.urandom(32), True, net) + claim_pubkey_hex = hexlify(claim_privkey.sec()).decode("UTF-8") + preimage = os.urandom(32) + preimage_hash = sha256(preimage).hexdigest() + + res = req_wrap( + "post", + f"{BOLTZ_URL}/createswap", + json={ + "type": "reversesubmarine", + "pairId": "BTC/BTC", + "orderSide": "buy", + "invoiceAmount": data.amount, + "preimageHash": preimage_hash, + "claimPublicKey": claim_pubkey_hex, + "referralId": "lnbits", + }, + headers={"Content-Type": "application/json"}, + ) + res = res.json() + + logger.info( + f"Boltz - created reverse swap, boltz_id: {res['id']}. wallet: {data.wallet}" + ) + + swap = ReverseSubmarineSwap( + id=swap_id, + amount=data.amount, + wallet=data.wallet, + onchain_address=data.onchain_address, + instant_settlement=data.instant_settlement, + claim_privkey=claim_privkey.wif(net), + preimage=preimage.hex(), + status="pending", + boltz_id=res["id"], + timeout_block_height=res["timeoutBlockHeight"], + lockup_address=res["lockupAddress"], + onchain_amount=res["onchainAmount"], + redeem_script=res["redeemScript"], + invoice=res["invoice"], + time=get_timestamp(), + ) + logger.debug(f"Boltz - waiting for onchain tx, reverse swap_id: {swap.id}") + task = create_task_log_exception( + swap.id, wait_for_onchain_tx(swap, swap_websocket_callback_initial) + ) + return swap, task + + +def start_onchain_listener(swap: ReverseSubmarineSwap) -> asyncio.Task: + return create_task_log_exception( + swap.id, wait_for_onchain_tx(swap, swap_websocket_callback_restart) + ) + + +async def start_confirmation_listener( + swap: ReverseSubmarineSwap, mempool_lockup_tx +) -> asyncio.Task: + logger.debug(f"Boltz - reverse swap, waiting for confirmation...") + + tx, txid, *_ = mempool_lockup_tx + + confirmed = await wait_for_websocket_message({"track-tx": txid}, "txConfirmed") + if confirmed: + logger.debug(f"Boltz - reverse swap lockup transaction confirmed! claiming...") + await create_claim_tx(swap, mempool_lockup_tx) + else: + logger.debug(f"Boltz - reverse swap lockup transaction still not confirmed.") + + +def create_task_log_exception(swap_id: str, awaitable: Awaitable) -> asyncio.Task: + async def _log_exception(awaitable): + try: + return await awaitable + except Exception as e: + logger.error(f"Boltz - reverse swap failed!: {swap_id} - {e}") + await update_swap_status(swap_id, "failed") + + return asyncio.create_task(_log_exception(awaitable)) + + +async def swap_websocket_callback_initial(swap): + wstask = asyncio.create_task( + wait_for_websocket_message( + {"track-address": swap.lockup_address}, "address-transactions" + ) + ) + logger.debug( + f"Boltz - created task, waiting on mempool websocket for address: {swap.lockup_address}" + ) + + # create_task is used because pay_invoice is stuck as long as boltz does not + # see the onchain claim tx and it ends up in deadlock + task: asyncio.Task = create_task_log_exception( + swap.id, + pay_invoice( + wallet_id=swap.wallet, + payment_request=swap.invoice, + description=f"reverse swap for {swap.amount} sats on boltz.exchange", + extra={"tag": "boltz", "swap_id": swap.id, "reverse": True}, + ), + ) + logger.debug(f"Boltz - task pay_invoice created, reverse swap_id: {swap.id}") + + done, pending = await asyncio.wait( + [task, wstask], return_when=asyncio.FIRST_COMPLETED + ) + message = done.pop().result() + + # pay_invoice already failed, do not wait for onchain tx anymore + if message is None: + logger.debug(f"Boltz - pay_invoice already failed cancel websocket task.") + wstask.cancel() + raise + + return task, message + + +async def swap_websocket_callback_restart(swap): + logger.debug(f"Boltz - swap_websocket_callback_restart called...") + message = await wait_for_websocket_message( + {"track-address": swap.lockup_address}, "address-transactions" + ) + return None, message + + +async def wait_for_onchain_tx(swap: ReverseSubmarineSwap, callback): + task, txs = await callback(swap) + mempool_lockup_tx = get_mempool_tx_from_txs(txs, swap.lockup_address) + if mempool_lockup_tx: + tx, txid, *_ = mempool_lockup_tx + if swap.instant_settlement or tx["status"]["confirmed"]: + logger.debug( + f"Boltz - reverse swap instant settlement, claiming immediatly..." + ) + await create_claim_tx(swap, mempool_lockup_tx) + else: + await start_confirmation_listener(swap, mempool_lockup_tx) + try: + if task: + await task + except: + logger.error( + f"Boltz - could not await pay_invoice task, but sent onchain. should never happen!" + ) + else: + logger.error(f"Boltz - mempool lockup tx not found.") + + +async def create_claim_tx(swap: ReverseSubmarineSwap, mempool_lockup_tx): + tx = await create_onchain_tx(swap, mempool_lockup_tx) + await send_onchain_tx(tx) + logger.debug(f"Boltz - onchain tx sent, reverse swap completed") + await update_swap_status(swap.id, "complete") + + +async def create_refund_tx(swap: SubmarineSwap): + mempool_lockup_tx = get_mempool_tx(swap.address) + tx = await create_onchain_tx(swap, mempool_lockup_tx) + await send_onchain_tx(tx) + + +def check_block_height(block_height: int): + current_block_height = get_mempool_blockheight() + if current_block_height <= block_height: + msg = f"refund not possible, timeout_block_height ({block_height}) is not yet exceeded ({current_block_height})" + logger.debug(msg) + raise Exception(msg) + + +""" +a submarine swap consists of 2 onchain tx's a lockup and a redeem tx. +we create a tx to redeem the funds locked by the onchain lockup tx. +claim tx for reverse swaps, refund tx for normal swaps they are the same +onchain redeem tx, the difference between them is the private key, onchain_address, +input sequence and input script_sig +""" + + +async def create_onchain_tx( + swap: Union[ReverseSubmarineSwap, SubmarineSwap], mempool_lockup_tx +) -> Transaction: + is_refund_tx = type(swap) == SubmarineSwap + if is_refund_tx: + check_block_height(swap.timeout_block_height) + privkey = ec.PrivateKey.from_wif(swap.refund_privkey) + onchain_address = swap.refund_address + preimage = b"" + sequence = 0xFFFFFFFE + else: + privkey = ec.PrivateKey.from_wif(swap.claim_privkey) + preimage = unhexlify(swap.preimage) + onchain_address = swap.onchain_address + sequence = 0xFFFFFFFF + + locktime = swap.timeout_block_height + redeem_script = unhexlify(swap.redeem_script) + + fees = get_fee_estimation() + + tx, txid, vout_cnt, vout_amount = mempool_lockup_tx + + script_pubkey = script.address_to_scriptpubkey(onchain_address) + + vin = [TransactionInput(unhexlify(txid), vout_cnt, sequence=sequence)] + vout = [TransactionOutput(vout_amount - fees, script_pubkey)] + tx = Transaction(vin=vin, vout=vout) + + if is_refund_tx: + tx.locktime = locktime + + # TODO: 2 rounds for fee calculation, look at vbytes after signing and do another TX + s = script.Script(data=redeem_script) + for i, inp in enumerate(vin): + if is_refund_tx: + rs = bytes([34]) + bytes([0]) + bytes([32]) + sha256(redeem_script).digest() + tx.vin[i].script_sig = script.Script(data=rs) + h = tx.sighash_segwit(i, s, vout_amount) + sig = privkey.sign(h).serialize() + bytes([SIGHASH.ALL]) + witness_items = [sig, preimage, redeem_script] + tx.vin[i].witness = script.Witness(items=witness_items) + + return tx + + +def get_swap_status(swap: Union[SubmarineSwap, ReverseSubmarineSwap]) -> SwapStatus: + swap_status = SwapStatus( + wallet=swap.wallet, + swap_id=swap.id, + ) + + try: + boltz_request = get_boltz_status(swap.boltz_id) + swap_status.boltz = boltz_request["status"] + except httpx.HTTPStatusError as exc: + json = exc.response.json() + swap_status.boltz = json["error"] + if "could not find" in swap_status.boltz: + swap_status.exists = False + + if type(swap) == SubmarineSwap: + swap_status.reverse = False + swap_status.address = swap.address + else: + swap_status.reverse = True + swap_status.address = swap.lockup_address + + swap_status.block_height = get_mempool_blockheight() + swap_status.timeout_block_height = ( + f"{str(swap.timeout_block_height)} -> current: {str(swap_status.block_height)}" + ) + + if swap_status.block_height >= swap.timeout_block_height: + swap_status.hit_timeout = True + + mempool_tx = get_mempool_tx(swap_status.address) + swap_status.lockup = mempool_tx + if mempool_tx == None: + swap_status.has_lockup = False + swap_status.confirmed = False + swap_status.mempool = "transaction.unknown" + swap_status.message = "lockup tx not in mempool" + else: + swap_status.has_lockup = True + tx, *_ = mempool_tx + if tx["status"]["confirmed"] == True: + swap_status.mempool = "transaction.confirmed" + swap_status.confirmed = True + else: + swap_status.confirmed = False + swap_status.mempool = "transaction.unconfirmed" + + return swap_status + + +def check_boltz_limits(amount): + try: + pairs = get_boltz_pairs() + limits = pairs["pairs"]["BTC/BTC"]["limits"] + return amount >= limits["minimal"] and amount <= limits["maximal"] + except: + return False + + +def get_boltz_pairs(): + res = req_wrap( + "get", + f"{BOLTZ_URL}/getpairs", + headers={"Content-Type": "application/json"}, + ) + return res.json() + + +def get_boltz_status(boltzid): + res = req_wrap( + "post", + f"{BOLTZ_URL}/swapstatus", + json={"id": boltzid}, + ) + return res.json() diff --git a/lnbits/extensions/boltz/config.json b/lnbits/extensions/boltz/config.json new file mode 100644 index 000000000..6a11da2da --- /dev/null +++ b/lnbits/extensions/boltz/config.json @@ -0,0 +1,6 @@ +{ + "name": "Boltz", + "short_description": "Perform onchain/offchain swaps via https://boltz.exchange/", + "icon": "swap_horiz", + "contributors": ["dni"] +} diff --git a/lnbits/extensions/boltz/crud.py b/lnbits/extensions/boltz/crud.py new file mode 100644 index 000000000..1bb4286dc --- /dev/null +++ b/lnbits/extensions/boltz/crud.py @@ -0,0 +1,225 @@ +from http import HTTPStatus +from typing import List, Optional, Union + +from loguru import logger +from starlette.exceptions import HTTPException + +from . import db +from .models import ( + CreateReverseSubmarineSwap, + CreateSubmarineSwap, + ReverseSubmarineSwap, + SubmarineSwap, +) + +""" +Submarine Swaps +""" + + +async def get_submarine_swaps(wallet_ids: Union[str, List[str]]) -> List[SubmarineSwap]: + if isinstance(wallet_ids, str): + wallet_ids = [wallet_ids] + + q = ",".join(["?"] * len(wallet_ids)) + rows = await db.fetchall( + f"SELECT * FROM boltz.submarineswap WHERE wallet IN ({q}) order by time DESC", + (*wallet_ids,), + ) + + return [SubmarineSwap(**row) for row in rows] + + +async def get_pending_submarine_swaps( + wallet_ids: Union[str, List[str]] +) -> List[SubmarineSwap]: + if isinstance(wallet_ids, str): + wallet_ids = [wallet_ids] + + q = ",".join(["?"] * len(wallet_ids)) + rows = await db.fetchall( + f"SELECT * FROM boltz.submarineswap WHERE wallet IN ({q}) and status='pending' order by time DESC", + (*wallet_ids,), + ) + return [SubmarineSwap(**row) for row in rows] + + +async def get_all_pending_submarine_swaps() -> List[SubmarineSwap]: + rows = await db.fetchall( + f"SELECT * FROM boltz.submarineswap WHERE status='pending' order by time DESC", + ) + return [SubmarineSwap(**row) for row in rows] + + +async def get_submarine_swap(swap_id) -> SubmarineSwap: + row = await db.fetchone( + "SELECT * FROM boltz.submarineswap WHERE id = ?", (swap_id,) + ) + return SubmarineSwap(**row) if row else None + + +async def create_submarine_swap(swap: SubmarineSwap) -> Optional[SubmarineSwap]: + + await db.execute( + """ + INSERT INTO boltz.submarineswap ( + id, + wallet, + payment_hash, + status, + boltz_id, + refund_privkey, + refund_address, + expected_amount, + timeout_block_height, + address, + bip21, + redeem_script, + amount + ) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + """, + ( + swap.id, + swap.wallet, + swap.payment_hash, + swap.status, + swap.boltz_id, + swap.refund_privkey, + swap.refund_address, + swap.expected_amount, + swap.timeout_block_height, + swap.address, + swap.bip21, + swap.redeem_script, + swap.amount, + ), + ) + return await get_submarine_swap(swap.id) + + +async def delete_submarine_swap(swap_id): + await db.execute("DELETE FROM boltz.submarineswap WHERE id = ?", (swap_id,)) + + +async def get_reverse_submarine_swaps( + wallet_ids: Union[str, List[str]] +) -> List[ReverseSubmarineSwap]: + if isinstance(wallet_ids, str): + wallet_ids = [wallet_ids] + + q = ",".join(["?"] * len(wallet_ids)) + rows = await db.fetchall( + f"SELECT * FROM boltz.reverse_submarineswap WHERE wallet IN ({q}) order by time DESC", + (*wallet_ids,), + ) + + return [ReverseSubmarineSwap(**row) for row in rows] + + +async def get_pending_reverse_submarine_swaps( + wallet_ids: Union[str, List[str]] +) -> List[ReverseSubmarineSwap]: + if isinstance(wallet_ids, str): + wallet_ids = [wallet_ids] + + q = ",".join(["?"] * len(wallet_ids)) + rows = await db.fetchall( + f"SELECT * FROM boltz.reverse_submarineswap WHERE wallet IN ({q}) and status='pending' order by time DESC", + (*wallet_ids,), + ) + + return [ReverseSubmarineSwap(**row) for row in rows] + + +async def get_all_pending_reverse_submarine_swaps() -> List[ReverseSubmarineSwap]: + rows = await db.fetchall( + f"SELECT * FROM boltz.reverse_submarineswap WHERE status='pending' order by time DESC" + ) + + return [ReverseSubmarineSwap(**row) for row in rows] + + +async def get_reverse_submarine_swap(swap_id) -> SubmarineSwap: + row = await db.fetchone( + "SELECT * FROM boltz.reverse_submarineswap WHERE id = ?", (swap_id,) + ) + return ReverseSubmarineSwap(**row) if row else None + + +async def create_reverse_submarine_swap( + swap: ReverseSubmarineSwap, +) -> Optional[ReverseSubmarineSwap]: + + await db.execute( + """ + INSERT INTO boltz.reverse_submarineswap ( + id, + wallet, + status, + boltz_id, + instant_settlement, + preimage, + claim_privkey, + lockup_address, + invoice, + onchain_amount, + onchain_address, + timeout_block_height, + redeem_script, + amount + ) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + """, + ( + swap.id, + swap.wallet, + swap.status, + swap.boltz_id, + swap.instant_settlement, + swap.preimage, + swap.claim_privkey, + swap.lockup_address, + swap.invoice, + swap.onchain_amount, + swap.onchain_address, + swap.timeout_block_height, + swap.redeem_script, + swap.amount, + ), + ) + return await get_reverse_submarine_swap(swap.id) + + +async def update_swap_status(swap_id: str, status: str): + + reverse = "" + swap = await get_submarine_swap(swap_id) + if swap is None: + swap = await get_reverse_submarine_swap(swap_id) + + if swap is None: + return None + + if type(swap) == SubmarineSwap: + await db.execute( + "UPDATE boltz.submarineswap SET status='" + + status + + "' WHERE id='" + + swap.id + + "'" + ) + if type(swap) == ReverseSubmarineSwap: + reverse = "reverse" + await db.execute( + "UPDATE boltz.reverse_submarineswap SET status='" + + status + + "' WHERE id='" + + swap.id + + "'" + ) + + message = f"Boltz - {reverse} swap status change: {status}. boltz_id: {swap.boltz_id}, wallet: {swap.wallet}" + logger.info(message) + + return swap diff --git a/lnbits/extensions/boltz/mempool.py b/lnbits/extensions/boltz/mempool.py new file mode 100644 index 000000000..ee3052577 --- /dev/null +++ b/lnbits/extensions/boltz/mempool.py @@ -0,0 +1,97 @@ +import asyncio +import json +from binascii import hexlify + +import httpx +import websockets +from embit.transaction import Transaction +from loguru import logger + +from lnbits.settings import BOLTZ_MEMPOOL_SPACE_URL, BOLTZ_MEMPOOL_SPACE_URL_WS + +from .utils import req_wrap + +logger.debug(f"BOLTZ_MEMPOOL_SPACE_URL: {BOLTZ_MEMPOOL_SPACE_URL}") +logger.debug(f"BOLTZ_MEMPOOL_SPACE_URL_WS: {BOLTZ_MEMPOOL_SPACE_URL_WS}") + +websocket_url = f"{BOLTZ_MEMPOOL_SPACE_URL_WS}/api/v1/ws" + + +async def wait_for_websocket_message(send, message_string): + async for websocket in websockets.connect(websocket_url): + try: + await websocket.send(json.dumps({"action": "want", "data": ["blocks"]})) + await websocket.send(json.dumps(send)) + async for raw in websocket: + message = json.loads(raw) + if message_string in message: + return message.get(message_string) + except websockets.ConnectionClosed: + continue + + +def get_mempool_tx(address): + res = req_wrap( + "get", + f"{BOLTZ_MEMPOOL_SPACE_URL}/api/address/{address}/txs", + headers={"Content-Type": "text/plain"}, + ) + txs = res.json() + return get_mempool_tx_from_txs(txs, address) + + +def get_mempool_tx_from_txs(txs, address): + if len(txs) == 0: + return None + tx = txid = vout_cnt = vout_amount = None + for a_tx in txs: + for i, vout in enumerate(a_tx["vout"]): + if vout["scriptpubkey_address"] == address: + tx = a_tx + txid = a_tx["txid"] + vout_cnt = i + vout_amount = vout["value"] + # should never happen + if tx == None: + raise Exception("mempool tx not found") + if txid == None: + raise Exception("mempool txid not found") + return tx, txid, vout_cnt, vout_amount + + +def get_fee_estimation() -> int: + # TODO: hardcoded maximum tx size, in the future we try to get the size of the tx via embit + # we need a function like Transaction.vsize() + tx_size_vbyte = 200 + mempool_fees = get_mempool_fees() + return mempool_fees * tx_size_vbyte + + +def get_mempool_fees() -> int: + res = req_wrap( + "get", + f"{BOLTZ_MEMPOOL_SPACE_URL}/api/v1/fees/recommended", + headers={"Content-Type": "text/plain"}, + ) + fees = res.json() + return int(fees["economyFee"]) + + +def get_mempool_blockheight() -> int: + res = req_wrap( + "get", + f"{BOLTZ_MEMPOOL_SPACE_URL}/api/blocks/tip/height", + headers={"Content-Type": "text/plain"}, + ) + return int(res.text) + + +async def send_onchain_tx(tx: Transaction): + raw = hexlify(tx.serialize()) + logger.debug(f"Boltz - mempool sending onchain tx...") + req_wrap( + "post", + f"{BOLTZ_MEMPOOL_SPACE_URL}/api/tx", + headers={"Content-Type": "text/plain"}, + content=raw, + ) diff --git a/lnbits/extensions/boltz/migrations.py b/lnbits/extensions/boltz/migrations.py new file mode 100644 index 000000000..e4026dd04 --- /dev/null +++ b/lnbits/extensions/boltz/migrations.py @@ -0,0 +1,46 @@ +async def m001_initial(db): + await db.execute( + """ + CREATE TABLE boltz.submarineswap ( + id TEXT PRIMARY KEY, + wallet TEXT NOT NULL, + payment_hash TEXT NOT NULL, + amount INT NOT NULL, + status TEXT NOT NULL, + boltz_id TEXT NOT NULL, + refund_address TEXT NOT NULL, + refund_privkey TEXT NOT NULL, + expected_amount INT NOT NULL, + timeout_block_height INT NOT NULL, + address TEXT NOT NULL, + bip21 TEXT NOT NULL, + redeem_script TEXT NOT NULL, + time TIMESTAMP NOT NULL DEFAULT """ + + db.timestamp_now + + """ + ); + """ + ) + await db.execute( + """ + CREATE TABLE boltz.reverse_submarineswap ( + id TEXT PRIMARY KEY, + wallet TEXT NOT NULL, + onchain_address TEXT NOT NULL, + amount INT NOT NULL, + instant_settlement BOOLEAN NOT NULL, + status TEXT NOT NULL, + boltz_id TEXT NOT NULL, + timeout_block_height INT NOT NULL, + redeem_script TEXT NOT NULL, + preimage TEXT NOT NULL, + claim_privkey TEXT NOT NULL, + lockup_address TEXT NOT NULL, + invoice TEXT NOT NULL, + onchain_amount INT NOT NULL, + time TIMESTAMP NOT NULL DEFAULT """ + + db.timestamp_now + + """ + ); + """ + ) diff --git a/lnbits/extensions/boltz/models.py b/lnbits/extensions/boltz/models.py new file mode 100644 index 000000000..c8ec5646d --- /dev/null +++ b/lnbits/extensions/boltz/models.py @@ -0,0 +1,75 @@ +import json +from typing import Dict, List, Optional + +from fastapi.params import Query +from pydantic.main import BaseModel +from sqlalchemy.engine import base # type: ignore + + +class SubmarineSwap(BaseModel): + id: str + wallet: str + amount: int + payment_hash: str + time: int + status: str + refund_privkey: str + refund_address: str + boltz_id: str + expected_amount: int + timeout_block_height: int + address: str + bip21: str + redeem_script: str + + +class CreateSubmarineSwap(BaseModel): + wallet: str = Query(...) # type: ignore + refund_address: str = Query(...) # type: ignore + amount: int = Query(...) # type: ignore + + +class ReverseSubmarineSwap(BaseModel): + id: str + wallet: str + amount: int + onchain_address: str + instant_settlement: bool + time: int + status: str + boltz_id: str + preimage: str + claim_privkey: str + lockup_address: str + invoice: str + onchain_amount: int + timeout_block_height: int + redeem_script: str + + +class CreateReverseSubmarineSwap(BaseModel): + wallet: str = Query(...) # type: ignore + amount: int = Query(...) # type: ignore + instant_settlement: bool = Query(...) # type: ignore + # validate on-address, bcrt1 for regtest addresses + onchain_address: str = Query( + ..., regex="^(bcrt1|bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$" + ) # type: ignore + + +class SwapStatus(BaseModel): + swap_id: str + wallet: str + status: str = "" + message: str = "" + boltz: str = "" + mempool: str = "" + address: str = "" + block_height: int = 0 + timeout_block_height: str = "" + lockup: Optional[dict] = {} + has_lockup: bool = False + hit_timeout: bool = False + confirmed: bool = True + exists: bool = True + reverse: bool = False diff --git a/lnbits/extensions/boltz/tasks.py b/lnbits/extensions/boltz/tasks.py new file mode 100644 index 000000000..d6f72edf9 --- /dev/null +++ b/lnbits/extensions/boltz/tasks.py @@ -0,0 +1,153 @@ +import asyncio + +import httpx +from loguru import logger + +from lnbits.core.models import Payment +from lnbits.core.services import check_transaction_status +from lnbits.tasks import register_invoice_listener + +from .boltz import ( + create_claim_tx, + create_refund_tx, + get_swap_status, + start_confirmation_listener, + start_onchain_listener, +) +from .crud import ( + get_all_pending_reverse_submarine_swaps, + get_all_pending_submarine_swaps, + get_reverse_submarine_swap, + get_submarine_swap, + update_swap_status, +) + +""" +testcases for boltz startup +A. normal swaps + 1. test: create -> kill -> start -> startup invoice listeners -> pay onchain funds -> should complete + 2. test: create -> kill -> pay onchain funds -> start -> startup check -> should complete + 3. test: create -> kill -> mine blocks and hit timeout -> start -> should go timeout/failed + 4. test: create -> kill -> pay to less onchain funds -> mine blocks hit timeout -> start lnbits -> should be refunded + +B. reverse swaps + 1. test: create instant -> kill -> boltz does lockup -> not confirmed -> start lnbits -> should claim/complete + 2. test: create instant -> kill -> no lockup -> start lnbits -> should start onchain listener -> boltz does lockup -> should claim/complete (difficult to test) + 3. test: create -> kill -> boltz does lockup -> not confirmed -> start lnbits -> should start tx listener -> after confirmation -> should claim/complete + 4. test: create -> kill -> boltz does lockup -> confirmed -> start lnbits -> should claim/complete + 5. test: create -> kill -> boltz does lockup -> hit timeout -> boltz refunds -> start -> should timeout +""" + + +async def check_for_pending_swaps(): + try: + swaps = await get_all_pending_submarine_swaps() + reverse_swaps = await get_all_pending_reverse_submarine_swaps() + if len(swaps) > 0 or len(reverse_swaps) > 0: + logger.debug(f"Boltz - startup swap check") + except: + # database is not created yet, do nothing + return + + if len(swaps) > 0: + logger.debug(f"Boltz - {len(swaps)} pending swaps") + for swap in swaps: + try: + swap_status = get_swap_status(swap) + # should only happen while development when regtest is reset + if swap_status.exists is False: + logger.warning(f"Boltz - swap: {swap.boltz_id} does not exist.") + await update_swap_status(swap.id, "failed") + continue + + payment_status = await check_transaction_status( + swap.wallet, swap.payment_hash + ) + + if payment_status.paid: + logger.debug( + f"Boltz - swap: {swap.boltz_id} got paid while offline." + ) + await update_swap_status(swap.id, "complete") + else: + if swap_status.hit_timeout: + if not swap_status.has_lockup: + logger.warning( + f"Boltz - swap: {swap.id} hit timeout, but no lockup tx..." + ) + await update_swap_status(swap.id, "timeout") + else: + logger.debug(f"Boltz - refunding swap: {swap.id}...") + await create_refund_tx(swap) + await update_swap_status(swap.id, "refunded") + + except Exception as exc: + logger.error(f"Boltz - swap: {swap.id} - {str(exc)}") + + if len(reverse_swaps) > 0: + logger.debug(f"Boltz - {len(reverse_swaps)} pending reverse swaps") + for reverse_swap in reverse_swaps: + try: + swap_status = get_swap_status(reverse_swap) + + if swap_status.exists is False: + logger.debug( + f"Boltz - reverse_swap: {reverse_swap.boltz_id} does not exist." + ) + await update_swap_status(reverse_swap.id, "failed") + continue + + # if timeout hit, boltz would have already refunded + if swap_status.hit_timeout: + logger.debug( + f"Boltz - reverse_swap: {reverse_swap.boltz_id} timeout." + ) + await update_swap_status(reverse_swap.id, "timeout") + continue + + if not swap_status.has_lockup: + # start listener for onchain address + logger.debug( + f"Boltz - reverse_swap: {reverse_swap.boltz_id} restarted onchain address listener." + ) + await start_onchain_listener(reverse_swap) + continue + + if reverse_swap.instant_settlement or swap_status.confirmed: + await create_claim_tx(reverse_swap, swap_status.lockup) + else: + logger.debug( + f"Boltz - reverse_swap: {reverse_swap.boltz_id} restarted confirmation listener." + ) + await start_confirmation_listener(reverse_swap, swap_status.lockup) + + except Exception as exc: + logger.error(f"Boltz - reverse swap: {reverse_swap.id} - {str(exc)}") + + +async def wait_for_paid_invoices(): + invoice_queue = asyncio.Queue() + register_invoice_listener(invoice_queue) + + while True: + payment = await invoice_queue.get() + await on_invoice_paid(payment) + + +async def on_invoice_paid(payment: Payment) -> None: + if "boltz" != payment.extra.get("tag"): + # not a boltz invoice + return + + await payment.set_pending(False) + swap_id = payment.extra.get("swap_id") + swap = await get_submarine_swap(swap_id) + + if not swap: + logger.error(f"swap_id: {swap_id} not found.") + return + + logger.info( + f"Boltz - lightning invoice is paid, normal swap completed. swap_id: {swap_id}" + ) + await update_swap_status(swap_id, "complete") diff --git a/lnbits/extensions/boltz/templates/boltz/_api_docs.html b/lnbits/extensions/boltz/templates/boltz/_api_docs.html new file mode 100644 index 000000000..eea35ab6a --- /dev/null +++ b/lnbits/extensions/boltz/templates/boltz/_api_docs.html @@ -0,0 +1,236 @@ + + + + + +
+ Boltz.exchange: Do onchain to offchain and vice-versa swaps +
+

+ Submarine and Reverse Submarine Swaps on LNbits via boltz.exchange + API
+

+

+ Link : + https://boltz.exchange + +

+

+ More details +

+

+ Created by, + dni +

+
+
+
+ + + + + GET + /boltz/api/v1/swap/reverse +
+ Returns 200 OK (application/json) +
+ JSON list of reverse submarine swaps +
Curl example
+ curl -X GET {{ root_url }}/boltz/api/v1/swap/reverse -H "X-Api-Key: + {{ user.wallets[0].adminkey }}" + +
+
+
+ + + + POST + /boltz/api/v1/swap/reverse +
Body (application/json)
+ {"wallet": <string>, "onchain_address": <string>, + "amount": <integer>, "instant_settlement": + <boolean>} +
+ Returns 200 OK (application/json) +
+ JSON create a reverse-submarine swaps +
Curl example
+ curl -X POST {{ root_url }}/boltz/api/v1/swap/reverse -H "X-Api-Key: + {{ user.wallets[0].adminkey }}" + +
+
+
+ + + + GET /boltz/api/v1/swap +
+ Returns 200 OK (application/json) +
+ JSON list of submarine swaps +
Curl example
+ curl -X GET {{ root_url }}/boltz/api/v1/swap -H "X-Api-Key: {{ + user.wallets[0].adminkey }}" + +
+
+
+ + + + POST /boltz/api/v1/swap +
Body (application/json)
+ {"wallet": <string>, "refund_address": <string>, + "amount": <integer>} +
+ Returns 200 OK (application/json) +
+ JSON create a submarine swaps +
Curl example
+ curl -X POST {{ root_url }}/boltz/api/v1/swap -H "X-Api-Key: {{ + user.wallets[0].adminkey }}" + +
+
+
+ + + + POST + /boltz/api/v1/swap/refund/{swap_id} +
+ Returns 200 OK (application/json) +
+ JSON submarine swap +
Curl example
+ curl -X GET {{ root_url }}/boltz/api/v1/swap/refund/{swap_id} -H + "X-Api-Key: {{ user.wallets[0].adminkey }}" + +
+
+
+ + + + POST + /boltz/api/v1/swap/status/{swap_id} +
+ Returns 200 OK (text/plain) +
+ swap status +
Curl example
+ curl -X GET {{ root_url }}/boltz/api/v1/swap/status/{swap_id} -H + "X-Api-Key: {{ user.wallets[0].adminkey }}" + +
+
+
+ + + + GET + /boltz/api/v1/swap/check +
+ Returns 200 OK (application/json) +
+ JSON pending swaps +
Curl example
+ curl -X GET {{ root_url }}/boltz/api/v1/swap/check -H "X-Api-Key: {{ + user.wallets[0].adminkey }}" + +
+
+
+ + + + GET + /boltz/api/v1/swap/boltz +
+ Returns 200 OK (text/plain) +
+ JSON boltz config +
Curl example
+ curl -X GET {{ root_url }}/boltz/api/v1/swap/boltz -H "X-Api-Key: {{ + user.wallets[0].inkey }}" + +
+
+
+ + + + GET + /boltz/api/v1/swap/mempool +
+ Returns 200 OK (text/plain) +
+ mempool url +
Curl example
+ curl -X GET {{ root_url }}/boltz/api/v1/swap/mempool -H "X-Api-Key: + {{ user.wallets[0].inkey }}" + +
+
+
+
diff --git a/lnbits/extensions/boltz/templates/boltz/index.html b/lnbits/extensions/boltz/templates/boltz/index.html new file mode 100644 index 000000000..b7312de7a --- /dev/null +++ b/lnbits/extensions/boltz/templates/boltz/index.html @@ -0,0 +1,1005 @@ +{% extends "base.html" %} {% from "macros.jinja" import window_vars with context +%} {% block page %} +
+
+ + + + + Send onchain funds offchain (BTC -> LN) + + + + + Send offchain funds to onchain address (LN -> BTC) + + + + + Check all pending swaps if they can be refunded. + + + + + + +
+
+
Swaps (In)
+
+
+ Export to CSV +
+
+ + {% raw %} + + + {% endraw %} + +
+
+ + +
+
+
Reverse Swaps (Out)
+
+
+ Export to CSV +
+
+ + {% raw %} + + + {% endraw %} + +
+
+
+
+ + +
{{SITE_TITLE}} Boltz extension
+
+ + + {% include "boltz/_api_docs.html" %} + +
+
+ + + + + + + + +
+ + + Cancel +
+
+
+
+ + + + + + + +
+
+ + + Create Onchain TX when transaction is in mempool, but not + confirmed yet. + + +
+
+ +
+ + + Cancel +
+
+
+
+ + + + + +
+ {% raw %} + Bitcoin On-Chain TX
+ Expected amount (sats): {{ qrCodeDialog.data.expected_amount }} +
+ Expected amount (btc): {{ qrCodeDialog.data.expected_amount_btc + }}
+ Onchain Address: {{ qrCodeDialog.data.address }}
+ {% endraw %} +
+
+ Copy On-Chain Address + Close +
+
+
+ + +
+ {% raw %} + Wallet: {{ statusDialog.data.wallet }}
+ Boltz Status: {{ statusDialog.data.boltz }}
+ Mempool Status: {{ statusDialog.data.mempool }}
+ Blockheight timeout: {{ statusDialog.data.timeout_block_height + }}
+ {% endraw %} +
+
+ Refund + + Download refundfile + Close +
+
+
+ + +
pending swaps
+ + {% raw %} + + + {% endraw %} + +
pending reverse swaps
+ + {% raw %} + + + {% endraw %} + +
+ Close +
+
+
+
+{% endblock %} {% block scripts %} {{ window_vars(user) }} + +{% endblock %} diff --git a/lnbits/extensions/boltz/utils.py b/lnbits/extensions/boltz/utils.py new file mode 100644 index 000000000..4fb2edda2 --- /dev/null +++ b/lnbits/extensions/boltz/utils.py @@ -0,0 +1,44 @@ +import calendar +import datetime + +import httpx +from loguru import logger + +from lnbits.core.services import fee_reserve, get_wallet + + +async def check_balance(data) -> bool: + # check if we can pay the invoice before we create the actual swap on boltz + amount_msat = data.amount * 1000 + fee_reserve_msat = fee_reserve(amount_msat) + wallet = await get_wallet(data.wallet) + assert wallet + if wallet.balance_msat - fee_reserve_msat < amount_msat: + return False + return True + + +def get_timestamp(): + date = datetime.datetime.utcnow() + return calendar.timegm(date.utctimetuple()) + + +def req_wrap(funcname, *args, **kwargs): + try: + try: + func = getattr(httpx, funcname) + except AttributeError: + logger.error('httpx function not found "%s"' % funcname) + else: + res = func(*args, timeout=30, **kwargs) + res.raise_for_status() + return res + except httpx.RequestError as exc: + msg = f"Unreachable: {exc.request.url!r}." + logger.error(msg) + raise + except httpx.HTTPStatusError as exc: + msg = f"HTTP Status Error: {exc.response.status_code} while requesting {exc.request.url!r}." + logger.error(msg) + logger.error(exc.response.json()["error"]) + raise diff --git a/lnbits/extensions/boltz/views.py b/lnbits/extensions/boltz/views.py new file mode 100644 index 000000000..b6864113c --- /dev/null +++ b/lnbits/extensions/boltz/views.py @@ -0,0 +1,23 @@ +from urllib.parse import urlparse + +from fastapi import Request +from fastapi.params import Depends +from fastapi.templating import Jinja2Templates +from starlette.responses import HTMLResponse + +from lnbits.core.models import Payment, User +from lnbits.decorators import check_user_exists + +from . import boltz_ext, boltz_renderer + +templates = Jinja2Templates(directory="templates") + + +@boltz_ext.get("/", response_class=HTMLResponse) +async def index(request: Request, user: User = Depends(check_user_exists)): + root_url = urlparse(str(request.url)).netloc + wallet_ids = [wallet.id for wallet in user.wallets] + return boltz_renderer().TemplateResponse( + "boltz/index.html", + {"request": request, "user": user.dict(), "root_url": root_url}, + ) diff --git a/lnbits/extensions/boltz/views_api.py b/lnbits/extensions/boltz/views_api.py new file mode 100644 index 000000000..a4b7d3181 --- /dev/null +++ b/lnbits/extensions/boltz/views_api.py @@ -0,0 +1,338 @@ +from datetime import datetime +from http import HTTPStatus +from typing import List + +import httpx +from fastapi import status +from fastapi.encoders import jsonable_encoder +from fastapi.param_functions import Body +from fastapi.params import Depends, Query +from loguru import logger +from pydantic import BaseModel +from starlette.exceptions import HTTPException +from starlette.requests import Request + +from lnbits.core.crud import get_user +from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key +from lnbits.settings import BOLTZ_MEMPOOL_SPACE_URL + +from . import boltz_ext +from .boltz import ( + create_refund_tx, + create_reverse_swap, + create_swap, + get_boltz_pairs, + get_swap_status, +) +from .crud import ( + create_reverse_submarine_swap, + create_submarine_swap, + get_pending_reverse_submarine_swaps, + get_pending_submarine_swaps, + get_reverse_submarine_swap, + get_reverse_submarine_swaps, + get_submarine_swap, + get_submarine_swaps, + update_swap_status, +) +from .models import ( + CreateReverseSubmarineSwap, + CreateSubmarineSwap, + ReverseSubmarineSwap, + SubmarineSwap, +) +from .utils import check_balance + + +@boltz_ext.get( + "/api/v1/swap/mempool", + name=f"boltz.get /swap/mempool", + summary="get a the mempool url", + description=""" + This endpoint gets the URL from mempool.space + """, + response_description="mempool.space url", + response_model=str, +) +async def api_mempool_url(): + return BOLTZ_MEMPOOL_SPACE_URL + + +# NORMAL SWAP +@boltz_ext.get( + "/api/v1/swap", + name=f"boltz.get /swap", + summary="get a list of swaps a swap", + description=""" + This endpoint gets a list of normal swaps. + """, + response_description="list of normal swaps", + dependencies=[Depends(get_key_type)], + response_model=List[SubmarineSwap], +) +async def api_submarineswap( + g: WalletTypeInfo = Depends(get_key_type), + all_wallets: bool = Query(False), +): + wallet_ids = [g.wallet.id] + if all_wallets: + wallet_ids = (await get_user(g.wallet.user)).wallet_ids + + for swap in await get_pending_submarine_swaps(wallet_ids): + swap_status = get_swap_status(swap) + if swap_status.hit_timeout: + if not swap_status.has_lockup: + logger.warning( + f"Boltz - swap: {swap.id} hit timeout, but no lockup tx..." + ) + await update_swap_status(swap.id, "timeout") + + return [swap.dict() for swap in await get_submarine_swaps(wallet_ids)] + + +@boltz_ext.post( + "/api/v1/swap/refund", + name=f"boltz.swap_refund", + summary="refund of a swap", + description=""" + This endpoint attempts to refund a normal swaps, creates onchain tx and sets swap status ro refunded. + """, + response_description="refunded swap with status set to refunded", + dependencies=[Depends(require_admin_key)], + response_model=SubmarineSwap, + responses={ + 400: {"description": "when swap_id is missing"}, + 404: {"description": "when swap is not found"}, + 405: {"description": "when swap is not pending"}, + 500: { + "description": "when something goes wrong creating the refund onchain tx" + }, + }, +) +async def api_submarineswap_refund( + swap_id: str, + g: WalletTypeInfo = Depends(require_admin_key), # type: ignore +): + if swap_id == None: + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, detail="swap_id missing" + ) + + swap = await get_submarine_swap(swap_id) + if swap == None: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, detail="swap does not exist." + ) + + if swap.status != "pending": + raise HTTPException( + status_code=HTTPStatus.METHOD_NOT_ALLOWED, detail="swap is not pending." + ) + + try: + await create_refund_tx(swap) + except httpx.RequestError as exc: + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail=f"Unreachable: {exc.request.url!r}.", + ) + except Exception as exc: + raise HTTPException(status_code=HTTPStatus.METHOD_NOT_ALLOWED, detail=str(exc)) + + await update_swap_status(swap.id, "refunded") + return swap + + +@boltz_ext.post( + "/api/v1/swap", + status_code=status.HTTP_201_CREATED, + name=f"boltz.post /swap", + summary="create a submarine swap", + description=""" + This endpoint creates a submarine swap + """, + response_description="create swap", + response_model=SubmarineSwap, + responses={ + 405: {"description": "not allowed method, insufficient balance"}, + 500: {"description": "boltz error"}, + }, +) +async def api_submarineswap_create( + data: CreateSubmarineSwap, + wallet: WalletTypeInfo = Depends(require_admin_key), # type: ignore +): + try: + swap_data = await create_swap(data) + except httpx.RequestError as exc: + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail=f"Unreachable: {exc.request.url!r}.", + ) + except Exception as exc: + raise HTTPException(status_code=HTTPStatus.METHOD_NOT_ALLOWED, detail=str(exc)) + except httpx.HTTPStatusError as exc: + raise HTTPException( + status_code=exc.response.status_code, detail=exc.response.json()["error"] + ) + swap = await create_submarine_swap(swap_data) + return swap.dict() + + +# REVERSE SWAP +@boltz_ext.get( + "/api/v1/swap/reverse", + name=f"boltz.get /swap/reverse", + summary="get a list of reverse swaps a swap", + description=""" + This endpoint gets a list of reverse swaps. + """, + response_description="list of reverse swaps", + dependencies=[Depends(get_key_type)], + response_model=List[ReverseSubmarineSwap], +) +async def api_reverse_submarineswap( + g: WalletTypeInfo = Depends(get_key_type), # type:ignore + all_wallets: bool = Query(False), +): + wallet_ids = [g.wallet.id] + if all_wallets: + wallet_ids = (await get_user(g.wallet.user)).wallet_ids + return [swap.dict() for swap in await get_reverse_submarine_swaps(wallet_ids)] + + +@boltz_ext.post( + "/api/v1/swap/reverse", + status_code=status.HTTP_201_CREATED, + name=f"boltz.post /swap/reverse", + summary="create a reverse submarine swap", + description=""" + This endpoint creates a reverse submarine swap + """, + response_description="create reverse swap", + response_model=ReverseSubmarineSwap, + responses={ + 405: {"description": "not allowed method, insufficient balance"}, + 500: {"description": "boltz error"}, + }, +) +async def api_reverse_submarineswap_create( + data: CreateReverseSubmarineSwap, + wallet: WalletTypeInfo = Depends(require_admin_key), +): + + if not await check_balance(data): + raise HTTPException( + status_code=HTTPStatus.METHOD_NOT_ALLOWED, detail="Insufficient balance." + ) + + try: + swap_data, task = await create_reverse_swap(data) + except httpx.RequestError as exc: + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail=f"Unreachable: {exc.request.url!r}.", + ) + except httpx.HTTPStatusError as exc: + raise HTTPException( + status_code=exc.response.status_code, detail=exc.response.json()["error"] + ) + except Exception as exc: + raise HTTPException(status_code=HTTPStatus.METHOD_NOT_ALLOWED, detail=str(exc)) + + swap = await create_reverse_submarine_swap(swap_data) + return swap.dict() + + +@boltz_ext.post( + "/api/v1/swap/status", + name=f"boltz.swap_status", + summary="shows the status of a swap", + description=""" + This endpoint attempts to get the status of the swap. + """, + response_description="status of swap json", + responses={ + 404: {"description": "when swap_id is not found"}, + }, +) +async def api_swap_status( + swap_id: str, wallet: WalletTypeInfo = Depends(require_admin_key) # type: ignore +): + swap = await get_submarine_swap(swap_id) or await get_reverse_submarine_swap( + swap_id + ) + if swap == None: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, detail="swap does not exist." + ) + try: + status = get_swap_status(swap) + except httpx.RequestError as exc: + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail=f"Unreachable: {exc.request.url!r}.", + ) + except Exception as exc: + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(exc) + ) + return status + + +@boltz_ext.post( + "/api/v1/swap/check", + name=f"boltz.swap_check", + summary="list all pending swaps", + description=""" + This endpoint gives you 2 lists of pending swaps and reverse swaps. + """, + response_description="list of pending swaps", +) +async def api_check_swaps( + g: WalletTypeInfo = Depends(require_admin_key), # type: ignore + all_wallets: bool = Query(False), +): + wallet_ids = [g.wallet.id] + if all_wallets: + wallet_ids = (await get_user(g.wallet.user)).wallet_ids + status = [] + try: + for swap in await get_pending_submarine_swaps(wallet_ids): + status.append(get_swap_status(swap)) + for reverseswap in await get_pending_reverse_submarine_swaps(wallet_ids): + status.append(get_swap_status(reverseswap)) + except httpx.RequestError as exc: + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail=f"Unreachable: {exc.request.url!r}.", + ) + except Exception as exc: + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(exc) + ) + return status + + +@boltz_ext.get( + "/api/v1/swap/boltz", + name=f"boltz.get /swap/boltz", + summary="get a boltz configuration", + description=""" + This endpoint gets configuration for boltz. (limits, fees...) + """, + response_description="dict of boltz config", + response_model=dict, +) +async def api_boltz_config(): + try: + res = get_boltz_pairs() + except httpx.RequestError as exc: + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail=f"Unreachable: {exc.request.url!r}.", + ) + except Exception as e: + raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e)) + + return res["pairs"]["BTC/BTC"] diff --git a/lnbits/extensions/lndhub/views_api.py b/lnbits/extensions/lndhub/views_api.py index a3160fa9a..91371f9af 100644 --- a/lnbits/extensions/lndhub/views_api.py +++ b/lnbits/extensions/lndhub/views_api.py @@ -130,9 +130,8 @@ async def lndhub_gettxs( offset=offset, exclude_uncheckable=True, ): - await payment.set_pending( - (await WALLET.get_payment_status(payment.checking_id)).pending - ) + await payment.check_status() + await asyncio.sleep(0.1) return [ diff --git a/lnbits/settings.py b/lnbits/settings.py index 79c74fb45..25e43eec1 100644 --- a/lnbits/settings.py +++ b/lnbits/settings.py @@ -69,3 +69,13 @@ try: ) except: LNBITS_COMMIT = "unknown" + + +BOLTZ_NETWORK = env.str("BOLTZ_NETWORK", default="main") +BOLTZ_URL = env.str("BOLTZ_URL", default="https://boltz.exchange/api") +BOLTZ_MEMPOOL_SPACE_URL = env.str( + "BOLTZ_MEMPOOL_SPACE_URL", default="https://mempool.space" +) +BOLTZ_MEMPOOL_SPACE_URL_WS = env.str( + "BOLTZ_MEMPOOL_SPACE_URL_WS", default="wss://mempool.space" +) diff --git a/lnbits/tasks.py b/lnbits/tasks.py index f4d0a928d..45e59c4cf 100644 --- a/lnbits/tasks.py +++ b/lnbits/tasks.py @@ -86,6 +86,9 @@ async def check_pending_payments(): incoming = True while True: + logger.debug( + f"Task: checking all pending payments (incoming={incoming}, outgoing={outgoing}) of last 15 days" + ) for payment in await get_payments( since=(int(time.time()) - 60 * 60 * 24 * 15), # 15 days ago complete=False, @@ -94,11 +97,14 @@ async def check_pending_payments(): incoming=incoming, exclude_uncheckable=True, ): - await payment.check_pending() - + await payment.check_status() + logger.debug("Task: pending payments check finished") # we delete expired invoices once upon the first pending check if incoming: + logger.debug("Task: deleting all expired invoices") await delete_expired_invoices() + logger.debug("Task: expired invoice deletion finished") + # after the first check we will only check outgoing, not incoming # that will be handled by the global invoice listeners, hopefully incoming = False diff --git a/lnbits/wallets/base.py b/lnbits/wallets/base.py index f35eb3704..e38b6d8fc 100644 --- a/lnbits/wallets/base.py +++ b/lnbits/wallets/base.py @@ -18,13 +18,15 @@ class PaymentResponse(NamedTuple): # when ok is None it means we don't know if this succeeded ok: Optional[bool] = None checking_id: Optional[str] = None # payment_hash, rcp_id - fee_msat: int = 0 + fee_msat: Optional[int] = None preimage: Optional[str] = None error_message: Optional[str] = None class PaymentStatus(NamedTuple): paid: Optional[bool] = None + fee_msat: Optional[int] = None + preimage: Optional[str] = None @property def pending(self) -> bool: diff --git a/lnbits/wallets/cliche.py b/lnbits/wallets/cliche.py index 9dbe5a229..9b8627945 100644 --- a/lnbits/wallets/cliche.py +++ b/lnbits/wallets/cliche.py @@ -81,31 +81,41 @@ class ClicheWallet(Wallet): data["result"]["invoice"], ) else: - return InvoiceResponse( - False, checking_id, payment_request, "Could not get payment hash" - ) + return InvoiceResponse(False, None, None, "Could not get payment hash") return InvoiceResponse(True, checking_id, payment_request, error_message) async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse: ws = create_connection(self.endpoint) ws.send(f"pay-invoice --invoice {bolt11}") - r = ws.recv() - data = json.loads(r) - checking_id = None - error_message = None + for _ in range(2): + r = ws.recv() + data = json.loads(r) + checking_id, fee_msat, preimage, error_message, payment_ok = ( + None, + None, + None, + None, + None, + ) - if data.get("error") is not None and data["error"].get("message"): - logger.error(data["error"]["message"]) - error_message = data["error"]["message"] - return PaymentResponse(False, None, 0, error_message) + if data.get("error") is not None: + error_message = data["error"].get("message") + return PaymentResponse(False, None, None, None, error_message) - if data.get("result") is not None and data["result"].get("payment_hash"): - checking_id = data["result"]["payment_hash"] - else: - return PaymentResponse(False, checking_id, 0, "Could not get payment hash") + if data.get("method") == "payment_succeeded": + payment_ok = True + checking_id = data["params"]["payment_hash"] + fee_msat = data["params"]["fee_msatoshi"] + preimage = data["params"]["preimage"] + continue - return PaymentResponse(True, checking_id, 0, error_message) + if data.get("result") is None: + return PaymentResponse(None) + + return PaymentResponse( + payment_ok, checking_id, fee_msat, preimage, error_message + ) async def get_invoice_status(self, checking_id: str) -> PaymentStatus: ws = create_connection(self.endpoint) @@ -129,22 +139,30 @@ class ClicheWallet(Wallet): if data.get("error") is not None and data["error"].get("message"): logger.error(data["error"]["message"]) return PaymentStatus(None) - + payment = data["result"] statuses = {"pending": None, "complete": True, "failed": False} - return PaymentStatus(statuses[data["result"]["status"]]) + return PaymentStatus( + statuses[payment["status"]], + payment.get("fee_msatoshi"), + payment.get("preimage"), + ) async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: - try: - ws = await create_connection(self.endpoint) - while True: - r = await ws.recv() - data = json.loads(r) - try: - if data["result"]["status"]: - yield data["result"]["payment_hash"] - except: - continue - except: - pass - logger.error("lost connection to cliche's websocket, retrying in 5 seconds") - await asyncio.sleep(5) + while True: + try: + ws = await create_connection(self.endpoint) + while True: + r = await ws.recv() + data = json.loads(r) + print(data) + try: + if data["result"]["status"]: + yield data["result"]["payment_hash"] + except: + continue + except Exception as exc: + logger.error( + f"lost connection to cliche's invoices stream: '{exc}', retrying in 5 seconds" + ) + await asyncio.sleep(5) + continue diff --git a/lnbits/wallets/cln.py b/lnbits/wallets/cln.py index 4761a59b6..48b96128d 100644 --- a/lnbits/wallets/cln.py +++ b/lnbits/wallets/cln.py @@ -110,29 +110,38 @@ class CoreLightningWallet(Wallet): return InvoiceResponse(True, r["payment_hash"], r["bolt11"], "") except RpcError as exc: - error_message = f"lightningd '{exc.method}' failed with '{exc.error}'." - logger.error("RPC error:", error_message) + error_message = f"CLN method '{exc.method}' failed with '{exc.error.get('message') or exc.error}'." return InvoiceResponse(False, None, None, error_message) except Exception as e: - logger.error("error:", e) return InvoiceResponse(False, None, None, str(e)) async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse: invoice = lnbits_bolt11.decode(bolt11) + + previous_payment = await self.get_payment_status(invoice.payment_hash) + if previous_payment.paid: + return PaymentResponse(False, None, None, None, "invoice already paid") + fee_limit_percent = fee_limit_msat / invoice.amount_msat * 100 payload = { "bolt11": bolt11, "maxfeepercent": "{:.11}".format(fee_limit_percent), - "exemptfee": 0, # so fee_limit_percent is applied even on payments with fee under 5000 millisatoshi (which is default value of exemptfee) + "exemptfee": 0, # so fee_limit_percent is applied even on payments with fee < 5000 millisatoshi (which is default value of exemptfee) } try: wrapped = async_wrap(_pay_invoice) r = await wrapped(self.ln, payload) + except RpcError as exc: + try: + error_message = exc.error["attempts"][-1]["fail_reason"] + except: + error_message = f"CLN method '{exc.method}' failed with '{exc.error.get('message') or exc.error}'." + return PaymentResponse(False, None, None, None, error_message) except Exception as exc: - return PaymentResponse(False, None, 0, None, str(exc)) + return PaymentResponse(False, None, None, None, str(exc)) - fee_msat = r["msatoshi_sent"] - r["msatoshi"] + fee_msat = -int(r["msatoshi_sent"] - r["msatoshi"]) return PaymentResponse( True, r["payment_hash"], fee_msat, r["payment_preimage"], None ) @@ -144,9 +153,16 @@ class CoreLightningWallet(Wallet): return PaymentStatus(None) if not r["invoices"]: return PaymentStatus(None) - if r["invoices"][0]["payment_hash"] == checking_id: - return PaymentStatus(r["invoices"][0]["status"] == "paid") - raise KeyError("supplied an invalid checking_id") + + invoice_resp = r["invoices"][-1] + + if invoice_resp["payment_hash"] == checking_id: + if invoice_resp["status"] == "paid": + return PaymentStatus(True) + elif invoice_resp["status"] == "unpaid": + return PaymentStatus(None) + logger.warning(f"supplied an invalid checking_id: {checking_id}") + return PaymentStatus(None) async def get_payment_status(self, checking_id: str) -> PaymentStatus: try: @@ -155,14 +171,21 @@ class CoreLightningWallet(Wallet): return PaymentStatus(None) if not r["pays"]: return PaymentStatus(None) - if r["pays"][0]["payment_hash"] == checking_id: - status = r["pays"][0]["status"] + payment_resp = r["pays"][-1] + + if payment_resp["payment_hash"] == checking_id: + status = payment_resp["status"] if status == "complete": - return PaymentStatus(True) + fee_msat = -int( + payment_resp["amount_sent_msat"] - payment_resp["amount_msat"] + ) + + return PaymentStatus(True, fee_msat, payment_resp["preimage"]) elif status == "failed": return PaymentStatus(False) return PaymentStatus(None) - raise KeyError("supplied an invalid checking_id") + logger.warning(f"supplied an invalid checking_id: {checking_id}") + return PaymentStatus(None) async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: while True: diff --git a/lnbits/wallets/eclair.py b/lnbits/wallets/eclair.py index 247b96e1e..c03e3f530 100644 --- a/lnbits/wallets/eclair.py +++ b/lnbits/wallets/eclair.py @@ -50,7 +50,7 @@ class EclairWallet(Wallet): async def status(self) -> StatusResponse: async with httpx.AsyncClient() as client: r = await client.post( - f"{self.url}/usablebalances", headers=self.auth, timeout=40 + f"{self.url}/globalbalance", headers=self.auth, timeout=5 ) try: data = r.json() @@ -60,9 +60,11 @@ class EclairWallet(Wallet): ) if r.is_error: - return StatusResponse(data["error"], 0) + return StatusResponse(data.get("error") or "undefined error", 0) + if len(data) == 0: + return StatusResponse("no data", 0) - return StatusResponse(None, data[0]["canSend"] * 1000) + return StatusResponse(None, int(data.get("total") * 100_000_000_000)) async def create_invoice( self, @@ -114,13 +116,18 @@ class EclairWallet(Wallet): except: error_message = r.text pass - return PaymentResponse(False, None, 0, None, error_message) + return PaymentResponse(False, None, None, None, error_message) data = r.json() + if data["type"] == "payment-failed": + return PaymentResponse(False, None, None, None, "payment failed") + checking_id = data["paymentHash"] preimage = data["paymentPreimage"] + # We do all this again to get the fee: + async with httpx.AsyncClient() as client: r = await client.post( f"{self.url}/getsentinfo", @@ -136,15 +143,22 @@ class EclairWallet(Wallet): except: error_message = r.text pass - return PaymentResponse( - True, checking_id, 0, preimage, error_message - ) ## ?? is this ok ?? + return PaymentResponse(None, checking_id, None, preimage, error_message) - data = r.json() - fees = [i["status"] for i in data] - fee_msat = sum([i["feesPaid"] for i in fees]) + statuses = { + "sent": True, + "failed": False, + "pending": None, + } - return PaymentResponse(True, checking_id, fee_msat, preimage, None) + data = r.json()[-1] + if data["status"]["type"] == "sent": + fee_msat = -data["status"]["feesPaid"] + preimage = data["status"]["paymentPreimage"] + + return PaymentResponse( + statuses[data["status"]["type"]], checking_id, fee_msat, preimage, None + ) async def get_invoice_status(self, checking_id: str) -> PaymentStatus: async with httpx.AsyncClient() as client: @@ -155,54 +169,61 @@ class EclairWallet(Wallet): ) data = r.json() - if r.is_error or "error" in data: + if r.is_error or "error" in data or data.get("status") is None: return PaymentStatus(None) - if data["status"]["type"] != "received": - return PaymentStatus(False) - - return PaymentStatus(True) + statuses = { + "received": True, + "expired": False, + "pending": None, + } + return PaymentStatus(statuses.get(data["status"]["type"])) async def get_payment_status(self, checking_id: str) -> PaymentStatus: async with httpx.AsyncClient() as client: r = await client.post( - url=f"{self.url}/getsentinfo", + f"{self.url}/getsentinfo", headers=self.auth, data={"paymentHash": checking_id}, + timeout=40, ) - data = r.json()[0] - if r.is_error: return PaymentStatus(None) - if data["status"]["type"] != "sent": - return PaymentStatus(False) + data = r.json()[-1] - return PaymentStatus(True) + if r.is_error or "error" in data or data.get("status") is None: + return PaymentStatus(None) + + fee_msat, preimage = None, None + if data["status"]["type"] == "sent": + fee_msat = -data["status"]["feesPaid"] + preimage = data["status"]["paymentPreimage"] + + statuses = { + "sent": True, + "failed": False, + "pending": None, + } + return PaymentStatus(statuses.get(data["status"]["type"]), fee_msat, preimage) async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: + while True: + try: + async with connect( + self.ws_url, + extra_headers=[("Authorization", self.auth["Authorization"])], + ) as ws: + while True: + message = await ws.recv() + message = json.loads(message) - try: - async with connect( - self.ws_url, - extra_headers=[("Authorization", self.auth["Authorization"])], - ) as ws: - while True: - message = await ws.recv() - message = json.loads(message) + if message and message["type"] == "payment-received": + yield message["paymentHash"] - if message and message["type"] == "payment-received": - yield message["paymentHash"] - - except ( - OSError, - ConnectionClosedOK, - ConnectionClosedError, - ConnectionClosed, - ) as ose: - logger.error("OSE", ose) - pass - - logger.error("lost connection to eclair's websocket, retrying in 5 seconds") - await asyncio.sleep(5) + except Exception as exc: + logger.error( + f"lost connection to eclair invoices stream: '{exc}', retrying in 5 seconds" + ) + await asyncio.sleep(5) diff --git a/lnbits/wallets/lnbits.py b/lnbits/wallets/lnbits.py index 96b7dbb63..ddd80e770 100644 --- a/lnbits/wallets/lnbits.py +++ b/lnbits/wallets/lnbits.py @@ -62,10 +62,10 @@ class LNbitsWallet(Wallet): data: Dict = {"out": False, "amount": amount} if description_hash: data["description_hash"] = description_hash.hex() - elif unhashed_description: - data["description_hash"] = hashlib.sha256(unhashed_description).hexdigest() - else: - data["memo"] = memo or "" + if unhashed_description: + data["unhashed_description"] = unhashed_description.hex() + + data["memo"] = memo or "" async with httpx.AsyncClient() as client: r = await client.post( @@ -94,15 +94,25 @@ class LNbitsWallet(Wallet): json={"out": True, "bolt11": bolt11}, timeout=None, ) - ok, checking_id, fee_msat, error_message = not r.is_error, None, 0, None + ok, checking_id, fee_msat, preimage, error_message = ( + not r.is_error, + None, + None, + None, + None, + ) if r.is_error: error_message = r.json()["detail"] + return PaymentResponse(None, None, None, None, error_message) else: data = r.json() - checking_id = data["checking_id"] + checking_id = data["payment_hash"] - return PaymentResponse(ok, checking_id, fee_msat, error_message) + # we do this to get the fee and preimage + payment: PaymentStatus = await self.get_payment_status(checking_id) + + return PaymentResponse(ok, checking_id, payment.fee_msat, payment.preimage) async def get_invoice_status(self, checking_id: str) -> PaymentStatus: try: @@ -125,8 +135,11 @@ class LNbitsWallet(Wallet): if r.is_error: return PaymentStatus(None) + data = r.json() + if "paid" not in data and "details" not in data: + return PaymentStatus(None) - return PaymentStatus(r.json()["paid"]) + return PaymentStatus(data["paid"], data["details"]["fee"], data["preimage"]) async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: url = f"{self.endpoint}/api/v1/payments/sse" diff --git a/lnbits/wallets/lndgrpc.py b/lnbits/wallets/lndgrpc.py index 10bd27e7f..a613ac9f2 100644 --- a/lnbits/wallets/lndgrpc.py +++ b/lnbits/wallets/lndgrpc.py @@ -65,14 +65,32 @@ def get_ssl_context(cert_path: str): return context -def parse_checking_id(checking_id: str) -> bytes: +def b64_to_bytes(checking_id: str) -> bytes: return base64.b64decode(checking_id.replace("_", "/")) -def stringify_checking_id(r_hash: bytes) -> str: +def bytes_to_b64(r_hash: bytes) -> str: return base64.b64encode(r_hash).decode("utf-8").replace("/", "_") +def hex_to_b64(hex_str: str) -> str: + try: + return base64.b64encode(bytes.fromhex(hex_str)).decode() + except ValueError: + return "" + + +def hex_to_bytes(hex_str: str) -> bytes: + try: + return bytes.fromhex(hex_str) + except: + return b"" + + +def bytes_to_hex(b: bytes) -> str: + return b.hex() + + # Due to updated ECDSA generated tls.cert we need to let gprc know that # we need to use that cipher suite otherwise there will be a handhsake # error when we communicate with the lnd rpc server. @@ -153,7 +171,7 @@ class LndWallet(Wallet): error_message = str(exc) return InvoiceResponse(False, None, None, error_message) - checking_id = stringify_checking_id(resp.r_hash) + checking_id = bytes_to_hex(resp.r_hash) payment_request = str(resp.payment_request) return InvoiceResponse(True, checking_id, payment_request, None) @@ -168,9 +186,9 @@ class LndWallet(Wallet): try: resp = await self.routerpc.SendPaymentV2(req).read() except RpcError as exc: - return PaymentResponse(False, "", 0, None, exc._details) + return PaymentResponse(False, None, None, None, exc._details) except Exception as exc: - return PaymentResponse(False, "", 0, None, str(exc)) + return PaymentResponse(False, None, None, None, str(exc)) # PaymentStatus from https://github.com/lightningnetwork/lnd/blob/master/channeldb/payments.go#L178 statuses = { @@ -180,29 +198,31 @@ class LndWallet(Wallet): 3: False, # FAILED } - if resp.status in [0, 1, 3]: - fee_msat = 0 - preimage = "" - checking_id = "" - elif resp.status == 2: # SUCCEEDED - fee_msat = resp.htlcs[-1].route.total_fees_msat - preimage = resp.payment_preimage - checking_id = resp.payment_hash + fee_msat = None + preimage = None + checking_id = resp.payment_hash + + if resp.status: # SUCCEEDED + fee_msat = -resp.htlcs[-1].route.total_fees_msat + preimage = bytes_to_hex(resp.payment_preimage) + return PaymentResponse( statuses[resp.status], checking_id, fee_msat, preimage, None ) async def get_invoice_status(self, checking_id: str) -> PaymentStatus: try: - r_hash = parse_checking_id(checking_id) + r_hash = hex_to_bytes(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) - - resp = await self.rpc.LookupInvoice(ln.PaymentHash(r_hash=r_hash)) + try: + resp = await self.rpc.LookupInvoice(ln.PaymentHash(r_hash=r_hash)) + except RpcError as exc: + return PaymentStatus(None) if resp.settled: return PaymentStatus(True) @@ -213,7 +233,7 @@ class LndWallet(Wallet): This routine checks the payment status using routerpc.TrackPaymentV2. """ try: - r_hash = parse_checking_id(checking_id) + r_hash = hex_to_bytes(checking_id) if len(r_hash) != 32: raise binascii.Error except binascii.Error: @@ -221,11 +241,6 @@ class LndWallet(Wallet): # that use different checking_id formats return PaymentStatus(None) - # for some reason our checking_ids are in base64 but the payment hashes - # returned here are in hex, lnd is weird - checking_id = checking_id.replace("_", "/") - checking_id = base64.b64decode(checking_id).hex() - resp = self.routerpc.TrackPaymentV2( router.TrackPaymentRequest(payment_hash=r_hash) ) @@ -240,6 +255,12 @@ class LndWallet(Wallet): try: async for payment in resp: + if statuses[payment.htlcs[-1].status]: + return PaymentStatus( + True, + -payment.htlcs[-1].route.total_fees_msat, + bytes_to_hex(payment.htlcs[-1].preimage), + ) return PaymentStatus(statuses[payment.htlcs[-1].status]) except: # most likely the payment wasn't found return PaymentStatus(None) @@ -248,13 +269,13 @@ class LndWallet(Wallet): async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: while True: - request = ln.InvoiceSubscription() try: + request = ln.InvoiceSubscription() async for i in self.rpc.SubscribeInvoices(request): if not i.settled: continue - checking_id = stringify_checking_id(i.r_hash) + checking_id = bytes_to_hex(i.r_hash) yield checking_id except Exception as exc: logger.error( diff --git a/lnbits/wallets/lndrest.py b/lnbits/wallets/lndrest.py index 6bdbe5e07..1083e48a9 100644 --- a/lnbits/wallets/lndrest.py +++ b/lnbits/wallets/lndrest.py @@ -123,18 +123,15 @@ class LndRestWallet(Wallet): if r.is_error or r.json().get("payment_error"): error_message = r.json().get("payment_error") or r.text - return PaymentResponse(False, None, 0, None, error_message) + return PaymentResponse(False, None, None, None, error_message) data = r.json() - payment_hash = data["payment_hash"] - checking_id = payment_hash + checking_id = base64.b64decode(data["payment_hash"]).hex() fee_msat = int(data["payment_route"]["total_fees_msat"]) preimage = base64.b64decode(data["payment_preimage"]).hex() return PaymentResponse(True, checking_id, fee_msat, preimage, None) async def get_invoice_status(self, checking_id: str) -> PaymentStatus: - checking_id = checking_id.replace("_", "/") - async with httpx.AsyncClient(verify=self.cert) as client: r = await client.get( url=f"{self.endpoint}/v1/invoice/{checking_id}", headers=self.auth @@ -151,10 +148,18 @@ class LndRestWallet(Wallet): """ This routine checks the payment status using routerpc.TrackPaymentV2. """ + # convert checking_id from hex to base64 and some LND magic + try: + checking_id = base64.urlsafe_b64encode(bytes.fromhex(checking_id)).decode( + "ascii" + ) + except ValueError: + return PaymentStatus(None) + url = f"{self.endpoint}/v2/router/track/{checking_id}" # check payment.status: - # https://api.lightning.community/rest/index.html?python#peersynctype + # https://api.lightning.community/?python=#paymentpaymentstatus statuses = { "UNKNOWN": None, "IN_FLIGHT": None, @@ -178,7 +183,11 @@ class LndRestWallet(Wallet): return PaymentStatus(None) payment = line.get("result") if payment is not None and payment.get("status"): - return PaymentStatus(statuses[payment["status"]]) + return PaymentStatus( + paid=statuses[payment["status"]], + fee_msat=payment.get("fee_msat"), + preimage=payment.get("payment_preimage"), + ) else: return PaymentStatus(None) except: @@ -187,10 +196,9 @@ class LndRestWallet(Wallet): return PaymentStatus(None) async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: - url = self.endpoint + "/v1/invoices/subscribe" - while True: try: + url = self.endpoint + "/v1/invoices/subscribe" async with httpx.AsyncClient( timeout=None, headers=self.auth, verify=self.cert ) as client: diff --git a/lnbits/wallets/lnpay.py b/lnbits/wallets/lnpay.py index de0a60a84..5db68e1f2 100644 --- a/lnbits/wallets/lnpay.py +++ b/lnbits/wallets/lnpay.py @@ -100,7 +100,7 @@ class LNPayWallet(Wallet): ) if r.is_error: - return PaymentResponse(False, None, 0, None, data["message"]) + return PaymentResponse(False, None, None, None, data["message"]) checking_id = data["lnTx"]["id"] fee_msat = 0 @@ -113,15 +113,18 @@ class LNPayWallet(Wallet): async def get_payment_status(self, checking_id: str) -> PaymentStatus: async with httpx.AsyncClient() as client: r = await client.get( - url=f"{self.endpoint}/lntx/{checking_id}?fields=settled", + url=f"{self.endpoint}/lntx/{checking_id}", headers=self.auth, ) if r.is_error: return PaymentStatus(None) + data = r.json() + preimage = data["payment_preimage"] + fee_msat = data["fee_msat"] statuses = {0: None, 1: True, -1: False} - return PaymentStatus(statuses[r.json()["settled"]]) + return PaymentStatus(statuses[data["settled"]], fee_msat, preimage) async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: self.queue: asyncio.Queue = asyncio.Queue(0) diff --git a/lnbits/wallets/lntxbot.py b/lnbits/wallets/lntxbot.py index ba310823a..13046d261 100644 --- a/lnbits/wallets/lntxbot.py +++ b/lnbits/wallets/lntxbot.py @@ -97,10 +97,11 @@ class LntxbotWallet(Wallet): except: error_message = r.text pass - - return PaymentResponse(False, None, 0, None, error_message) + return PaymentResponse(False, None, None, None, error_message) data = r.json() + if data.get("type") != "paid_invoice": + return PaymentResponse(None) checking_id = data["payment_hash"] fee_msat = -data["fee_msat"] preimage = data["payment_preimage"] diff --git a/lnbits/wallets/opennode.py b/lnbits/wallets/opennode.py index 9fcf374a7..f7dcba404 100644 --- a/lnbits/wallets/opennode.py +++ b/lnbits/wallets/opennode.py @@ -47,7 +47,7 @@ class OpenNodeWallet(Wallet): if r.is_error: return StatusResponse(data["message"], 0) - return StatusResponse(None, data["balance"]["BTC"] / 100_000_000_000) + return StatusResponse(None, data["balance"]["BTC"] * 1000) async def create_invoice( self, @@ -92,11 +92,15 @@ class OpenNodeWallet(Wallet): if r.is_error: error_message = r.json()["message"] - return PaymentResponse(False, None, 0, None, error_message) + return PaymentResponse(False, None, None, None, error_message) data = r.json()["data"] checking_id = data["id"] - fee_msat = data["fee"] * 1000 + fee_msat = -data["fee"] * 1000 + + if data["status"] != "paid": + return PaymentResponse(None, checking_id, fee_msat, None, "payment failed") + return PaymentResponse(True, checking_id, fee_msat, None, None) async def get_invoice_status(self, checking_id: str) -> PaymentStatus: @@ -106,9 +110,9 @@ class OpenNodeWallet(Wallet): ) if r.is_error: return PaymentStatus(None) - - statuses = {"processing": None, "paid": True, "unpaid": False} - return PaymentStatus(statuses[r.json()["data"]["status"]]) + data = r.json()["data"] + statuses = {"processing": None, "paid": True, "unpaid": None} + return PaymentStatus(statuses[data.get("status")]) async def get_payment_status(self, checking_id: str) -> PaymentStatus: async with httpx.AsyncClient() as client: @@ -119,14 +123,16 @@ class OpenNodeWallet(Wallet): if r.is_error: return PaymentStatus(None) + data = r.json()["data"] statuses = { "initial": None, "pending": None, "confirmed": True, - "error": False, + "error": None, "failed": False, } - return PaymentStatus(statuses[r.json()["data"]["status"]]) + fee_msat = -data.get("fee") * 1000 + return PaymentStatus(statuses[data.get("status")], fee_msat) async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: self.queue: asyncio.Queue = asyncio.Queue(0) diff --git a/lnbits/wallets/spark.py b/lnbits/wallets/spark.py index a26177db9..414d4e470 100644 --- a/lnbits/wallets/spark.py +++ b/lnbits/wallets/spark.py @@ -137,7 +137,7 @@ class SparkWallet(Wallet): pays = listpays["pays"] if len(pays) == 0: - return PaymentResponse(False, None, 0, None, str(exc)) + return PaymentResponse(False, None, None, None, str(exc)) pay = pays[0] payment_hash = pay["payment_hash"] @@ -148,11 +148,9 @@ class SparkWallet(Wallet): ) if pay["status"] == "failed": - return PaymentResponse(False, None, 0, None, str(exc)) + return PaymentResponse(False, None, None, None, str(exc)) elif pay["status"] == "pending": - return PaymentResponse( - None, payment_hash, fee_limit_msat, None, None - ) + return PaymentResponse(None, payment_hash, None, None, None) elif pay["status"] == "complete": r = pay r["payment_preimage"] = pay["preimage"] @@ -163,7 +161,7 @@ class SparkWallet(Wallet): # this is good pass - fee_msat = r["msatoshi_sent"] - r["msatoshi"] + fee_msat = -int(r["msatoshi_sent"] - r["msatoshi"]) preimage = r["payment_preimage"] return PaymentResponse(True, r["payment_hash"], fee_msat, preimage, None) @@ -201,7 +199,10 @@ class SparkWallet(Wallet): if r["pays"][0]["payment_hash"] == checking_id: status = r["pays"][0]["status"] if status == "complete": - return PaymentStatus(True) + fee_msat = -int( + r["pays"][0]["amount_sent_msat"] - r["pays"][0]["amount_msat"] + ) + return PaymentStatus(True, fee_msat, r["pays"][0]["preimage"]) elif status == "failed": return PaymentStatus(False) return PaymentStatus(None) diff --git a/tests/core/views/test_api.py b/tests/core/views/test_api.py index 86c17fa91..e0f6b5762 100644 --- a/tests/core/views/test_api.py +++ b/tests/core/views/test_api.py @@ -13,7 +13,7 @@ from lnbits.core.views.api import ( ) from lnbits.settings import wallet_class -from ...helpers import get_random_invoice_data +from ...helpers import get_random_invoice_data, is_regtest # check if the client is working @@ -162,6 +162,7 @@ async def test_pay_invoice_invoicekey(client, invoice, inkey_headers_from): # check POST /api/v1/payments: payment with admin key [should pass] @pytest.mark.asyncio +@pytest.mark.skipif(is_regtest, reason="this only works in fakewallet") async def test_pay_invoice_adminkey(client, invoice, adminkey_headers_from): data = {"out": True, "bolt11": invoice["payment_request"]} # try payment with admin key diff --git a/tests/data/mock_data.zip b/tests/data/mock_data.zip index 8704eb6d4..d184f94ad 100644 Binary files a/tests/data/mock_data.zip and b/tests/data/mock_data.zip differ diff --git a/tests/extensions/bleskomat/test_lnurl_api.py b/tests/extensions/bleskomat/test_lnurl_api.py index 0100a62da..3f7232669 100644 --- a/tests/extensions/bleskomat/test_lnurl_api.py +++ b/tests/extensions/bleskomat/test_lnurl_api.py @@ -12,7 +12,7 @@ from lnbits.extensions.bleskomat.helpers import ( from lnbits.settings import HOST, PORT from tests.conftest import client from tests.extensions.bleskomat.conftest import bleskomat, lnurl -from tests.helpers import credit_wallet +from tests.helpers import credit_wallet, is_regtest from tests.mocks import WALLET @@ -97,6 +97,7 @@ async def test_bleskomat_lnurl_api_valid_signature(client, bleskomat): @pytest.mark.asyncio +@pytest.mark.skipif(is_regtest, reason="this test is only passes in fakewallet") async def test_bleskomat_lnurl_api_action_insufficient_balance(client, lnurl): bleskomat = lnurl["bleskomat"] secret = lnurl["secret"] @@ -116,6 +117,7 @@ async def test_bleskomat_lnurl_api_action_insufficient_balance(client, lnurl): @pytest.mark.asyncio +@pytest.mark.skipif(is_regtest, reason="this test is only passes in fakewallet") async def test_bleskomat_lnurl_api_action_success(client, lnurl): bleskomat = lnurl["bleskomat"] secret = lnurl["secret"] diff --git a/tests/extensions/boltz/__init__.py b/tests/extensions/boltz/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/extensions/boltz/conftest.py b/tests/extensions/boltz/conftest.py new file mode 100644 index 000000000..b9ef78875 --- /dev/null +++ b/tests/extensions/boltz/conftest.py @@ -0,0 +1,25 @@ +import asyncio +import json +import secrets + +import pytest +import pytest_asyncio + +from lnbits.core.crud import create_account, create_wallet, get_wallet +from lnbits.extensions.boltz.boltz import create_reverse_swap, create_swap +from lnbits.extensions.boltz.models import ( + CreateReverseSubmarineSwap, + CreateSubmarineSwap, +) +from tests.mocks import WALLET + + +@pytest_asyncio.fixture(scope="session") +async def reverse_swap(from_wallet): + data = CreateReverseSubmarineSwap( + wallet=from_wallet.id, + instant_settlement=True, + onchain_address="bcrt1q4vfyszl4p8cuvqh07fyhtxve5fxq8e2ux5gx43", + amount=20_000, + ) + return await create_reverse_swap(data) diff --git a/tests/extensions/boltz/test_api.py b/tests/extensions/boltz/test_api.py new file mode 100644 index 000000000..20b6e5a40 --- /dev/null +++ b/tests/extensions/boltz/test_api.py @@ -0,0 +1,146 @@ +import pytest +import pytest_asyncio + +from tests.helpers import is_fake, is_regtest + + +@pytest.mark.asyncio +async def test_mempool_url(client): + response = await client.get("/boltz/api/v1/swap/mempool") + assert response.status_code == 200 + + +@pytest.mark.asyncio +async def test_boltz_config(client): + response = await client.get("/boltz/api/v1/swap/boltz") + assert response.status_code == 200 + + +@pytest.mark.asyncio +async def test_endpoints_unauthenticated(client): + response = await client.get("/boltz/api/v1/swap?all_wallets=true") + assert response.status_code == 401 + response = await client.get("/boltz/api/v1/swap/reverse?all_wallets=true") + assert response.status_code == 401 + response = await client.post("/boltz/api/v1/swap") + assert response.status_code == 401 + response = await client.post("/boltz/api/v1/swap/reverse") + assert response.status_code == 401 + response = await client.post("/boltz/api/v1/swap/status") + assert response.status_code == 401 + response = await client.post("/boltz/api/v1/swap/check") + assert response.status_code == 401 + + +@pytest.mark.asyncio +async def test_endpoints_inkey(client, inkey_headers_to): + response = await client.get( + "/boltz/api/v1/swap?all_wallets=true", headers=inkey_headers_to + ) + assert response.status_code == 200 + response = await client.get( + "/boltz/api/v1/swap/reverse?all_wallets=true", headers=inkey_headers_to + ) + assert response.status_code == 200 + + response = await client.post("/boltz/api/v1/swap", headers=inkey_headers_to) + assert response.status_code == 401 + response = await client.post("/boltz/api/v1/swap/reverse", headers=inkey_headers_to) + assert response.status_code == 401 + response = await client.post("/boltz/api/v1/swap/refund", headers=inkey_headers_to) + assert response.status_code == 401 + response = await client.post("/boltz/api/v1/swap/status", headers=inkey_headers_to) + assert response.status_code == 401 + response = await client.post("/boltz/api/v1/swap/check", headers=inkey_headers_to) + assert response.status_code == 401 + + +@pytest.mark.asyncio +async def test_endpoints_adminkey_nocontent(client, adminkey_headers_to): + response = await client.post("/boltz/api/v1/swap", headers=adminkey_headers_to) + assert response.status_code == 204 + response = await client.post( + "/boltz/api/v1/swap/reverse", headers=adminkey_headers_to + ) + assert response.status_code == 204 + response = await client.post( + "/boltz/api/v1/swap/refund", headers=adminkey_headers_to + ) + assert response.status_code == 204 + response = await client.post( + "/boltz/api/v1/swap/status", headers=adminkey_headers_to + ) + assert response.status_code == 204 + + +@pytest.mark.asyncio +@pytest.mark.skipif(is_regtest, reason="this test is only passes with fakewallet") +async def test_endpoints_adminkey_fakewallet(client, from_wallet, adminkey_headers_to): + response = await client.post( + "/boltz/api/v1/swap/check", headers=adminkey_headers_to + ) + assert response.status_code == 200 + swap = { + "wallet": from_wallet.id, + "refund_address": "bcrt1q3cwq33y435h52gq3qqsdtczh38ltlnf69zvypm", + "amount": 50_000, + } + response = await client.post( + "/boltz/api/v1/swap", json=swap, headers=adminkey_headers_to + ) + assert response.status_code == 405 + reverse_swap = { + "wallet": from_wallet.id, + "instant_settlement": True, + "onchain_address": "bcrt1q4vfyszl4p8cuvqh07fyhtxve5fxq8e2ux5gx43", + "amount": 50_000, + } + response = await client.post( + "/boltz/api/v1/swap/reverse", json=reverse_swap, headers=adminkey_headers_to + ) + assert response.status_code == 201 + reverse_swap = response.json() + assert reverse_swap["id"] is not None + response = await client.post( + "/boltz/api/v1/swap/status", + params={"swap_id": reverse_swap["id"]}, + headers=adminkey_headers_to, + ) + assert response.status_code == 200 + response = await client.post( + "/boltz/api/v1/swap/status", + params={"swap_id": "wrong"}, + headers=adminkey_headers_to, + ) + assert response.status_code == 404 + response = await client.post( + "/boltz/api/v1/swap/refund", + params={"swap_id": "wrong"}, + headers=adminkey_headers_to, + ) + assert response.status_code == 404 + + +@pytest.mark.asyncio +@pytest.mark.skipif(is_fake, reason="this test is only passes with regtest") +async def test_endpoints_adminkey_regtest(client, from_wallet, adminkey_headers_to): + swap = { + "wallet": from_wallet.id, + "refund_address": "bcrt1q3cwq33y435h52gq3qqsdtczh38ltlnf69zvypm", + "amount": 50_000, + } + response = await client.post( + "/boltz/api/v1/swap", json=swap, headers=adminkey_headers_to + ) + assert response.status_code == 201 + + reverse_swap = { + "wallet": from_wallet.id, + "instant_settlement": True, + "onchain_address": "bcrt1q4vfyszl4p8cuvqh07fyhtxve5fxq8e2ux5gx43", + "amount": 50_000, + } + response = await client.post( + "/boltz/api/v1/swap/reverse", json=reverse_swap, headers=adminkey_headers_to + ) + assert response.status_code == 201 diff --git a/tests/extensions/boltz/test_swap.py b/tests/extensions/boltz/test_swap.py new file mode 100644 index 000000000..ab5954acb --- /dev/null +++ b/tests/extensions/boltz/test_swap.py @@ -0,0 +1,31 @@ +import asyncio + +import pytest +import pytest_asyncio + +from lnbits.extensions.boltz.boltz import create_reverse_swap, create_swap +from lnbits.extensions.boltz.crud import ( + create_reverse_submarine_swap, + create_submarine_swap, + get_reverse_submarine_swap, + get_submarine_swap, +) +from tests.extensions.boltz.conftest import reverse_swap +from tests.helpers import is_fake, is_regtest + + +@pytest.mark.asyncio +@pytest.mark.skipif(is_fake, reason="this test is only passes in regtest") +async def test_create_reverse_swap(client, reverse_swap): + swap, wait_for_onchain = reverse_swap + assert swap.status == "pending" + assert swap.id is not None + assert swap.boltz_id is not None + assert swap.claim_privkey is not None + assert swap.onchain_address is not None + assert swap.lockup_address is not None + newswap = await create_reverse_submarine_swap(swap) + await wait_for_onchain + newswap = await get_reverse_submarine_swap(swap.id) + assert newswap is not None + assert newswap.status == "complete" diff --git a/tests/helpers.py b/tests/helpers.py index 7be26ce37..fc5931bc1 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -4,6 +4,7 @@ import secrets import string from lnbits.core.crud import create_payment +from lnbits.settings import wallet_class async def credit_wallet(wallet_id: str, amount: int): @@ -32,3 +33,7 @@ def get_random_string(N=10): async def get_random_invoice_data(): return {"out": False, "amount": 10, "memo": f"test_memo_{get_random_string(10)}"} + + +is_fake: bool = wallet_class.__name__ == "FakeWallet" +is_regtest: bool = not is_fake diff --git a/tests/mocks.py b/tests/mocks.py index 65e8440a6..3fc0efae2 100644 --- a/tests/mocks.py +++ b/tests/mocks.py @@ -5,7 +5,7 @@ from lnbits.settings import WALLET from lnbits.wallets.base import PaymentResponse, PaymentStatus, StatusResponse from lnbits.wallets.fake import FakeWallet -from .helpers import get_random_string +from .helpers import get_random_string, is_fake # generates an invoice with FakeWallet @@ -16,12 +16,13 @@ async def generate_mock_invoice(**x): return invoice -WALLET.status = AsyncMock( - return_value=StatusResponse( - "", # no error - 1000000, # msats +if is_fake: + WALLET.status = AsyncMock( + return_value=StatusResponse( + "", # no error + 1000000, # msats + ) ) -) # Note: if this line is uncommented, invoices will always be generated by FakeWallet # WALLET.create_invoice = generate_mock_invoice @@ -51,26 +52,27 @@ WALLET.status = AsyncMock( # ) -def pay_invoice_side_effect( - payment_request: str, fee_limit_msat: int -) -> PaymentResponse: - invoice = bolt11.decode(payment_request) - return PaymentResponse( - True, # ok - invoice.payment_hash, # checking_id (i.e. payment_hash) - 0, # fee_msat - "", # no error - ) +if is_fake: + def pay_invoice_side_effect( + payment_request: str, fee_limit_msat: int + ) -> PaymentResponse: + invoice = bolt11.decode(payment_request) + return PaymentResponse( + True, # ok + invoice.payment_hash, # checking_id (i.e. payment_hash) + 0, # fee_msat + "", # no error + ) -WALLET.pay_invoice = AsyncMock(side_effect=pay_invoice_side_effect) -WALLET.get_invoice_status = AsyncMock( - return_value=PaymentStatus( - True, # paid + WALLET.pay_invoice = AsyncMock(side_effect=pay_invoice_side_effect) + WALLET.get_invoice_status = AsyncMock( + return_value=PaymentStatus( + True, # paid + ) ) -) -WALLET.get_payment_status = AsyncMock( - return_value=PaymentStatus( - True, # paid + WALLET.get_payment_status = AsyncMock( + return_value=PaymentStatus( + True, # paid + ) ) -)