2022-06-27 00:11:46 +02:00
|
|
|
import random
|
|
|
|
|
import string
|
2026-03-31 09:48:43 +03:00
|
|
|
from io import BytesIO
|
2022-08-03 14:10:32 +02:00
|
|
|
|
2026-03-31 09:48:43 +03:00
|
|
|
from bolt11.types import MilliSatoshi
|
|
|
|
|
from fastapi import UploadFile
|
|
|
|
|
from httpx import AsyncClient
|
|
|
|
|
from lnurl import LnurlPayResponse
|
|
|
|
|
from lnurl.types import CallbackUrl, LnurlPayMetadata
|
|
|
|
|
from PIL import Image
|
|
|
|
|
from pydantic import BaseModel, parse_obj_as
|
|
|
|
|
from starlette.datastructures import Headers
|
2024-10-29 09:58:22 +01:00
|
|
|
|
2026-03-31 09:48:43 +03:00
|
|
|
from lnbits.core.models.extensions import (
|
|
|
|
|
ExtensionMeta,
|
|
|
|
|
ExtensionRelease,
|
|
|
|
|
InstallableExtension,
|
|
|
|
|
PayToEnableInfo,
|
|
|
|
|
ReleasePaymentInfo,
|
|
|
|
|
)
|
|
|
|
|
from lnbits.core.models.extensions_builder import (
|
|
|
|
|
ActionFields,
|
|
|
|
|
ClientDataFields,
|
|
|
|
|
DataField,
|
|
|
|
|
DataFields,
|
|
|
|
|
ExtensionData,
|
|
|
|
|
OwnerDataFields,
|
|
|
|
|
PublicPageFields,
|
|
|
|
|
SettingsFields,
|
|
|
|
|
)
|
2024-04-15 09:02:21 +02:00
|
|
|
from lnbits.wallets import get_funding_source, set_funding_source
|
2021-11-17 10:53:32 -06:00
|
|
|
|
2022-06-01 14:53:05 +02:00
|
|
|
|
2024-10-29 09:58:22 +01:00
|
|
|
class DbTestModel(BaseModel):
|
2024-03-12 13:55:38 +01:00
|
|
|
id: int
|
|
|
|
|
name: str
|
2025-08-21 16:17:19 +02:00
|
|
|
value: str | None = None
|
2024-03-12 13:55:38 +01:00
|
|
|
|
|
|
|
|
|
2024-10-29 09:58:22 +01:00
|
|
|
class DbTestModel2(BaseModel):
|
|
|
|
|
id: int
|
|
|
|
|
label: str
|
2025-08-21 16:17:19 +02:00
|
|
|
description: str | None = None
|
2024-10-29 09:58:22 +01:00
|
|
|
child: DbTestModel
|
2024-11-01 10:12:18 +01:00
|
|
|
child_list: list[DbTestModel]
|
2024-10-29 09:58:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class DbTestModel3(BaseModel):
|
|
|
|
|
id: int
|
|
|
|
|
user: str
|
|
|
|
|
child: DbTestModel2
|
|
|
|
|
active: bool = False
|
2024-11-01 10:12:18 +01:00
|
|
|
children: list[DbTestModel]
|
|
|
|
|
children_ids: list[int] = []
|
2024-10-29 09:58:22 +01:00
|
|
|
|
|
|
|
|
|
2024-04-15 09:02:21 +02:00
|
|
|
def get_random_string(iterations: int = 10):
|
2022-06-27 00:11:46 +02:00
|
|
|
return "".join(
|
|
|
|
|
random.SystemRandom().choice(string.ascii_uppercase + string.digits)
|
2024-04-15 09:02:21 +02:00
|
|
|
for _ in range(iterations)
|
2022-06-27 00:11:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_random_invoice_data():
|
|
|
|
|
return {"out": False, "amount": 10, "memo": f"test_memo_{get_random_string(10)}"}
|
Boltz.exchange Extension (#922)
* initial commit and still draft, ready for review
* forgot to uncomment this line
* fee estimation and blockheight
* resolve conversation with michael, to use mempool websockets instead of boltz status event
* Update lnbits/extensions/boltz/boltz.py
Co-authored-by: michael1011 <me@michael1011.at>
* add status to swaps, add sorting and data into listing
* add swap status checks, change urls to docker test setup, dynamic minimum and maximum limits
* fix docker hosts for development
* add api endpoints to _api_docs
* add wallet name and id, to list and status information
* fix status_update for reverse_swaps
* chore: format with black
* more blackformatting and refactoring create_swap()
* fix variable bug
* check if swap is already refunded
* use create_task instead of ensure_future
* add mempool and boltz urls depending on DEBUG .env
* raise exception in mempool fails
* fix onchain txs, sending funds to wrong address and add a refund address for normal swaps beforehand
* add status to swaps, add sorting and data into listing
* add swap status checks, change urls to docker test setup, dynamic minimum and maximum limits
* add wallet name and id, to list and status information
* fix status_update for reverse_swaps
* chore: format with black
* use create_task instead of ensure_future
* add mempool and boltz urls depending on DEBUG .env
* fix onchain txs, sending funds to wrong address and add a refund address for normal swaps beforehand
* black formatting
* add some logging with loguru, and remove function duplication
* cleanup readme
* updates/suggestions from calle
Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>
* remove unused comments
* Update API Endpoints
Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>
* un-factor get_boltz_pairs
* added a explaination for the onchain tx
* remove unused template file
* rename api endpoints
* fix isort and prettier
* more verbose logging!!
* add boltz to mock_data.zip
* new mockdata
* remove comment
* better readme
* fix mempool urls
* change /refund /check /status to post requests
* first step in tests2
* add first tests
* change refund,check,status to post requests
* next try on tests
* overall code improvements
* just testing tests
* throw http exceptions in views_api
* require admincheck for refund,check,status and added fastapi documentation for those
* added more tests
* black
* many code improvements
* adding tests
* temp fix test
* fix race condition when pay_invoice fails
* test are working
* add boltz env variables
* add startup check, bugfixes, improvements
* improve on status checking
* remove check_invoice_status
* more fixes and tests
* testing testing testing
* make tests run again inside regtest
* fix bad error :O
* fix postgres boolean bug and add swap test
* Update README.md
Update README.md
Update README.md
Update README.md
* some mypy
* blacked
* the missing commit?
* fix api_docs readme link
* better refunding error catching
fix
* check swaps now also shows pending reverse swap, ui improvements, tooltips
* add backend check for boltz limits
fixup
* many improvements, startup check for swaps working, reverse needs more testing
* little last fixes
* remove unused logic
* fastapi documentation
fixup
* formatting and remove unused tests
* fix test
* fix swapstatus model
* Update lnbits/extensions/boltz/tasks.py
Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>
* Update lnbits/extensions/boltz/views_api.py
Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>
* balance check msg, format
* fix mypy data override
* fix swapstatus, remove can refund column
* Update lnbits/extensions/boltz/README.md
Co-authored-by: michael1011 <me@michael1011.at>
* empty lines
* fix error message when swap is not found
* remove preimage_hash from database
* fix api_docs html
fix api_docs html
* catch boltz network exceptions better
* formatting
* check for timeout on swap at get request
Co-authored-by: michael1011 <me@michael1011.at>
Co-authored-by: fusion44 <some.fusion@gmail.com>
Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>
2022-08-30 12:51:17 +02:00
|
|
|
|
|
|
|
|
|
2026-03-31 09:48:43 +03:00
|
|
|
def get_png_bytes(*, color: str = "blue", size: tuple[int, int] = (32, 32)) -> bytes:
|
|
|
|
|
image = Image.new("RGB", size, color=color)
|
|
|
|
|
buffer = BytesIO()
|
|
|
|
|
image.save(buffer, format="PNG")
|
|
|
|
|
return buffer.getvalue()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_upload_file(
|
|
|
|
|
contents: bytes,
|
|
|
|
|
*,
|
|
|
|
|
filename: str,
|
|
|
|
|
content_type: str | None,
|
|
|
|
|
) -> UploadFile:
|
|
|
|
|
headers = (
|
|
|
|
|
Headers({"content-type": content_type}) if content_type is not None else None
|
|
|
|
|
)
|
|
|
|
|
return UploadFile(BytesIO(contents), filename=filename, headers=headers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_user_token_headers(client: AsyncClient, user_id: str) -> dict[str, str]:
|
|
|
|
|
response = await client.post("/api/v1/auth/usr", json={"usr": user_id})
|
|
|
|
|
client.cookies.clear()
|
|
|
|
|
return {
|
|
|
|
|
"Authorization": f"Bearer {response.json()['access_token']}",
|
|
|
|
|
"Content-type": "application/json",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_extension_data(ext_id: str = "demoext") -> ExtensionData:
|
|
|
|
|
return ExtensionData(
|
|
|
|
|
id=ext_id,
|
|
|
|
|
name="Demo Extension",
|
|
|
|
|
stub_version="0.1.0",
|
|
|
|
|
short_description="Generated extension",
|
|
|
|
|
owner_data=DataFields(
|
|
|
|
|
name="OwnerData",
|
|
|
|
|
fields=[DataField(name="wallet_id", type="wallet")],
|
|
|
|
|
),
|
|
|
|
|
client_data=DataFields(
|
|
|
|
|
name="ClientData",
|
|
|
|
|
fields=[DataField(name="amount", type="int")],
|
|
|
|
|
),
|
|
|
|
|
settings_data=SettingsFields(name="SettingsData", fields=[]),
|
|
|
|
|
public_page=PublicPageFields(
|
|
|
|
|
owner_data_fields=OwnerDataFields(),
|
|
|
|
|
client_data_fields=ClientDataFields(),
|
|
|
|
|
action_fields=ActionFields(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_extension_release(ext_id: str, version: str = "1.0.0") -> ExtensionRelease:
|
|
|
|
|
return ExtensionRelease(
|
|
|
|
|
name=ext_id,
|
|
|
|
|
version=version,
|
|
|
|
|
archive=f"https://example.com/{ext_id}.zip",
|
|
|
|
|
source_repo="org/repo",
|
|
|
|
|
hash=f"hash-{ext_id}",
|
|
|
|
|
details_link=f"https://example.com/{ext_id}/details.json",
|
|
|
|
|
repo=f"https://github.com/org/{ext_id}",
|
|
|
|
|
icon=f"/{ext_id}/static/icon.png",
|
|
|
|
|
pay_link=f"https://pay.example/{ext_id}",
|
|
|
|
|
is_github_release=False,
|
|
|
|
|
is_version_compatible=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_installable_extension(
|
|
|
|
|
ext_id: str,
|
|
|
|
|
*,
|
|
|
|
|
version: str = "1.0.0",
|
|
|
|
|
compatible: bool = True,
|
|
|
|
|
active: bool = True,
|
|
|
|
|
pay_to_enable: PayToEnableInfo | None = None,
|
|
|
|
|
dependencies: list[str] | None = None,
|
|
|
|
|
payments: list[ReleasePaymentInfo] | None = None,
|
|
|
|
|
) -> InstallableExtension:
|
|
|
|
|
release = make_extension_release(ext_id, version)
|
|
|
|
|
release.is_version_compatible = compatible
|
|
|
|
|
return InstallableExtension(
|
|
|
|
|
id=ext_id,
|
|
|
|
|
name=f"Extension {ext_id}",
|
|
|
|
|
version=version,
|
|
|
|
|
active=active,
|
|
|
|
|
short_description="Demo extension",
|
|
|
|
|
icon=release.icon,
|
|
|
|
|
meta=ExtensionMeta(
|
|
|
|
|
installed_release=release,
|
|
|
|
|
pay_to_enable=pay_to_enable,
|
|
|
|
|
dependencies=dependencies or [],
|
|
|
|
|
payments=payments or [],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_lnurl_pay_response(
|
|
|
|
|
*,
|
|
|
|
|
min_sendable_msat: int = 1_000,
|
|
|
|
|
max_sendable_msat: int = 10_000,
|
|
|
|
|
text: str = "Test payment",
|
|
|
|
|
identifier: str = "alice@example.com",
|
|
|
|
|
callback: str = "https://example.com/callback",
|
|
|
|
|
) -> LnurlPayResponse:
|
|
|
|
|
return LnurlPayResponse(
|
|
|
|
|
callback=parse_obj_as(CallbackUrl, callback),
|
|
|
|
|
minSendable=MilliSatoshi(min_sendable_msat),
|
|
|
|
|
maxSendable=MilliSatoshi(max_sendable_msat),
|
|
|
|
|
metadata=LnurlPayMetadata(
|
|
|
|
|
f"[["
|
|
|
|
|
f'"text/plain","{text}"'
|
|
|
|
|
f"],["
|
|
|
|
|
f'"text/identifier","{identifier}"'
|
|
|
|
|
f"]]"
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-04-15 09:02:21 +02:00
|
|
|
set_funding_source()
|
2026-03-31 09:48:43 +03:00
|
|
|
|
2024-04-15 09:02:21 +02:00
|
|
|
funding_source = get_funding_source()
|
|
|
|
|
is_fake: bool = funding_source.__class__.__name__ == "FakeWallet"
|
Boltz.exchange Extension (#922)
* initial commit and still draft, ready for review
* forgot to uncomment this line
* fee estimation and blockheight
* resolve conversation with michael, to use mempool websockets instead of boltz status event
* Update lnbits/extensions/boltz/boltz.py
Co-authored-by: michael1011 <me@michael1011.at>
* add status to swaps, add sorting and data into listing
* add swap status checks, change urls to docker test setup, dynamic minimum and maximum limits
* fix docker hosts for development
* add api endpoints to _api_docs
* add wallet name and id, to list and status information
* fix status_update for reverse_swaps
* chore: format with black
* more blackformatting and refactoring create_swap()
* fix variable bug
* check if swap is already refunded
* use create_task instead of ensure_future
* add mempool and boltz urls depending on DEBUG .env
* raise exception in mempool fails
* fix onchain txs, sending funds to wrong address and add a refund address for normal swaps beforehand
* add status to swaps, add sorting and data into listing
* add swap status checks, change urls to docker test setup, dynamic minimum and maximum limits
* add wallet name and id, to list and status information
* fix status_update for reverse_swaps
* chore: format with black
* use create_task instead of ensure_future
* add mempool and boltz urls depending on DEBUG .env
* fix onchain txs, sending funds to wrong address and add a refund address for normal swaps beforehand
* black formatting
* add some logging with loguru, and remove function duplication
* cleanup readme
* updates/suggestions from calle
Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>
* remove unused comments
* Update API Endpoints
Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>
* un-factor get_boltz_pairs
* added a explaination for the onchain tx
* remove unused template file
* rename api endpoints
* fix isort and prettier
* more verbose logging!!
* add boltz to mock_data.zip
* new mockdata
* remove comment
* better readme
* fix mempool urls
* change /refund /check /status to post requests
* first step in tests2
* add first tests
* change refund,check,status to post requests
* next try on tests
* overall code improvements
* just testing tests
* throw http exceptions in views_api
* require admincheck for refund,check,status and added fastapi documentation for those
* added more tests
* black
* many code improvements
* adding tests
* temp fix test
* fix race condition when pay_invoice fails
* test are working
* add boltz env variables
* add startup check, bugfixes, improvements
* improve on status checking
* remove check_invoice_status
* more fixes and tests
* testing testing testing
* make tests run again inside regtest
* fix bad error :O
* fix postgres boolean bug and add swap test
* Update README.md
Update README.md
Update README.md
Update README.md
* some mypy
* blacked
* the missing commit?
* fix api_docs readme link
* better refunding error catching
fix
* check swaps now also shows pending reverse swap, ui improvements, tooltips
* add backend check for boltz limits
fixup
* many improvements, startup check for swaps working, reverse needs more testing
* little last fixes
* remove unused logic
* fastapi documentation
fixup
* formatting and remove unused tests
* fix test
* fix swapstatus model
* Update lnbits/extensions/boltz/tasks.py
Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>
* Update lnbits/extensions/boltz/views_api.py
Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>
* balance check msg, format
* fix mypy data override
* fix swapstatus, remove can refund column
* Update lnbits/extensions/boltz/README.md
Co-authored-by: michael1011 <me@michael1011.at>
* empty lines
* fix error message when swap is not found
* remove preimage_hash from database
* fix api_docs html
fix api_docs html
* catch boltz network exceptions better
* formatting
* check for timeout on swap at get request
Co-authored-by: michael1011 <me@michael1011.at>
Co-authored-by: fusion44 <some.fusion@gmail.com>
Co-authored-by: calle <93376500+callebtc@users.noreply.github.com>
2022-08-30 12:51:17 +02:00
|
|
|
is_regtest: bool = not is_fake
|