Skip to content

Commit 990fed2

Browse files
authored
feat: code quality (#14)
* feat: code quality * fixes
1 parent 941b2f1 commit 990fed2

31 files changed

+3106
-275
lines changed

.github/workflows/lint.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: lint
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
8+
jobs:
9+
lint:
10+
uses: lnbits/lnbits/.github/workflows/lint.yml@dev

.github/workflows/release.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
on:
22
push:
33
tags:
4-
- "v[0-9]+.[0-9]+.[0-9]+"
4+
- 'v[0-9]+.[0-9]+.[0-9]+'
55

66
jobs:
7-
87
release:
98
runs-on: ubuntu-latest
109
steps:
@@ -34,12 +33,12 @@ jobs:
3433
- name: Create pull request in extensions repo
3534
env:
3635
GH_TOKEN: ${{ secrets.EXT_GITHUB }}
37-
repo_name: "${{ github.event.repository.name }}"
38-
tag: "${{ github.ref_name }}"
39-
branch: "update-${{ github.event.repository.name }}-${{ github.ref_name }}"
40-
title: "[UPDATE] ${{ github.event.repository.name }} to ${{ github.ref_name }}"
41-
body: "https://github.com/lnbits/${{ github.event.repository.name }}/releases/${{ github.ref_name }}"
42-
archive: "https://github.com/lnbits/${{ github.event.repository.name }}/archive/refs/tags/${{ github.ref_name }}.zip"
36+
repo_name: '${{ github.event.repository.name }}'
37+
tag: '${{ github.ref_name }}'
38+
branch: 'update-${{ github.event.repository.name }}-${{ github.ref_name }}'
39+
title: '[UPDATE] ${{ github.event.repository.name }} to ${{ github.ref_name }}'
40+
body: 'https://github.com/lnbits/${{ github.event.repository.name }}/releases/${{ github.ref_name }}'
41+
archive: 'https://github.com/lnbits/${{ github.event.repository.name }}/archive/refs/tags/${{ github.ref_name }}.zip'
4342
run: |
4443
cd lnbits-extensions
4544
git checkout -b $branch

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
__pycache__
2+
node_modules
3+
.mypy_cache
4+
.venv

.prettierrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"semi": false,
3+
"arrowParens": "avoid",
4+
"insertPragma": false,
5+
"printWidth": 80,
6+
"proseWrap": "preserve",
7+
"singleQuote": true,
8+
"trailingComma": "none",
9+
"useTabs": false,
10+
"bracketSameLine": false,
11+
"bracketSpacing": false
12+
}

Makefile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
all: format check
2+
3+
format: prettier black ruff
4+
5+
check: mypy pyright checkblack checkruff checkprettier
6+
7+
prettier:
8+
poetry run ./node_modules/.bin/prettier --write .
9+
pyright:
10+
poetry run ./node_modules/.bin/pyright
11+
12+
mypy:
13+
poetry run mypy .
14+
15+
black:
16+
poetry run black .
17+
18+
ruff:
19+
poetry run ruff check . --fix
20+
21+
checkruff:
22+
poetry run ruff check .
23+
24+
checkprettier:
25+
poetry run ./node_modules/.bin/prettier --check .
26+
27+
checkblack:
28+
poetry run black --check .
29+
30+
checkeditorconfig:
31+
editorconfig-checker
32+
33+
test:
34+
PYTHONUNBUFFERED=1 \
35+
DEBUG=true \
36+
poetry run pytest
37+
install-pre-commit-hook:
38+
@echo "Installing pre-commit hook to git"
39+
@echo "Uninstall the hook with poetry run pre-commit uninstall"
40+
poetry run pre-commit install
41+
42+
pre-commit:
43+
poetry run pre-commit run --all-files
44+
45+
46+
checkbundle:
47+
@echo "skipping checkbundle"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Onchain Wallet (watch-only) - <small>[LNbits](https://github.com/lnbits/lnbits) extension</small>
2+
23
<small>For more about LNBits extension check [this tutorial](https://github.com/lnbits/lnbits/wiki/LNbits-Extensions)</small>
34

45
## Monitor an onchain wallet and generate addresses for onchain payments

__init__.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from fastapi import APIRouter
22

3-
from lnbits.db import Database
4-
from lnbits.helpers import template_renderer
5-
6-
db = Database("ext_watchonly")
3+
from .crud import db
4+
from .views import watchonly_generic_router
5+
from .views_api import watchonly_api_router
76

87
watchonly_static_files = [
98
{
@@ -13,11 +12,7 @@
1312
]
1413

1514
watchonly_ext: APIRouter = APIRouter(prefix="/watchonly", tags=["watchonly"])
15+
watchonly_ext.include_router(watchonly_generic_router)
16+
watchonly_ext.include_router(watchonly_api_router)
1617

17-
18-
def watchonly_renderer():
19-
return template_renderer(["watchonly/templates"])
20-
21-
22-
from .views import * # noqa: F401,F403
23-
from .views_api import * # noqa: F401,F403
18+
__all__ = ["watchonly_ext", "watchonly_static_files", "db"]

crud.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import json
2-
from typing import List, Optional
2+
from typing import Optional
33

4+
from lnbits.db import Database
45
from lnbits.helpers import urlsafe_short_hash
56

6-
from . import db
77
from .helpers import derive_address
88
from .models import Address, Config, WalletAccount
99

10-
##########################WALLETS####################
10+
db = Database("ext_watchonly")
1111

1212

1313
async def create_watch_wallet(user: str, w: WalletAccount) -> WalletAccount:
@@ -53,7 +53,7 @@ async def get_watch_wallet(wallet_id: str) -> Optional[WalletAccount]:
5353
return WalletAccount.from_row(row) if row else None
5454

5555

56-
async def get_watch_wallets(user: str, network: str) -> List[WalletAccount]:
56+
async def get_watch_wallets(user: str, network: str) -> list[WalletAccount]:
5757
rows = await db.fetchall(
5858
"""SELECT * FROM watchonly.wallets WHERE "user" = ? AND network = ?""",
5959
(user, network),
@@ -120,7 +120,7 @@ async def create_fresh_addresses(
120120
start_address_index: int,
121121
end_address_index: int,
122122
change_address=False,
123-
) -> List[Address]:
123+
) -> list[Address]:
124124
if start_address_index > end_address_index:
125125
return []
126126

@@ -152,7 +152,8 @@ async def create_fresh_addresses(
152152
rows = await db.fetchall(
153153
"""
154154
SELECT * FROM watchonly.addresses
155-
WHERE wallet = ? AND branch_index = ? AND address_index >= ? AND address_index < ?
155+
WHERE wallet = ? AND branch_index = ?
156+
AND address_index >= ? AND address_index < ?
156157
ORDER BY branch_index, address_index
157158
""",
158159
(wallet_id, branch_index, start_address_index, end_address_index),
@@ -185,7 +186,7 @@ async def get_address_at_index(
185186
return Address.from_row(row) if row else None
186187

187188

188-
async def get_addresses(wallet_id: str) -> List[Address]:
189+
async def get_addresses(wallet_id: str) -> list[Address]:
189190
rows = await db.fetchall(
190191
"""
191192
SELECT * FROM watchonly.addresses WHERE wallet = ?
@@ -197,22 +198,24 @@ async def get_addresses(wallet_id: str) -> List[Address]:
197198
return [Address(**row) for row in rows]
198199

199200

200-
async def update_address(id: str, **kwargs) -> Optional[Address]:
201+
async def update_address(address_id: str, **kwargs) -> Address:
201202
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
202203

203204
await db.execute(
204205
f"""UPDATE watchonly.addresses SET {q} WHERE id = ? """,
205-
(*kwargs.values(), id),
206+
(*kwargs.values(), address_id),
206207
)
207-
row = await db.fetchone("SELECT * FROM watchonly.addresses WHERE id = ?", (id,))
208-
return Address.from_row(row) if row else None
208+
row = await db.fetchone(
209+
"SELECT * FROM watchonly.addresses WHERE id = ?", (address_id,)
210+
)
211+
assert row, "updated address not found"
212+
return Address.from_row(row)
209213

210214

211215
async def delete_addresses_for_wallet(wallet_id: str) -> None:
212216
await db.execute("DELETE FROM watchonly.addresses WHERE wallet = ?", (wallet_id,))
213217

214218

215-
######################CONFIG#######################
216219
async def create_config(user: str) -> Config:
217220
config = Config()
218221
await db.execute(

description.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Can be used directly with LNbits $10 hardware wallet/
44

55
Monitor an extended public key and generate deterministic fresh public keys with this simple watch only wallet. Invoice payments can also be generated, both through a publically shareable page and API.
66

7-
Other extensions can make use of this extension to add onchain functionality.
7+
Other extensions can make use of this extension to add onchain functionality.

helpers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def parse_key(masterpub: str) -> Tuple[Descriptor, Optional[dict]]:
3030
# check depth
3131
if k.key.depth != 3:
3232
raise ValueError(
33-
"Non-standard depth. Only bip44, bip49 and bip84 are supported with bare xpubs. For custom derivation paths use descriptors."
33+
"Non-standard depth. Only bip44, bip49 and bip84 are supported "
34+
"with bare xpubs. For custom derivation paths use descriptors."
3435
)
3536
# if allowed derivation is not provided use default /{0,1}/*
3637
if k.allowed_derivation is None:
@@ -43,11 +44,11 @@ def parse_key(masterpub: str) -> Tuple[Descriptor, Optional[dict]]:
4344
if version in [net["xpub"], net["ypub"], net["zpub"]]:
4445
network = net
4546
if version == net["xpub"]:
46-
desc = Descriptor.from_string("pkh(%s)" % str(k))
47+
desc = Descriptor.from_string(f"pkh({k!s})")
4748
elif version == net["ypub"]:
48-
desc = Descriptor.from_string("sh(wpkh(%s))" % str(k))
49+
desc = Descriptor.from_string(f"sh(wpkh({k!s}))")
4950
elif version == net["zpub"]:
50-
desc = Descriptor.from_string("wpkh(%s)" % str(k))
51+
desc = Descriptor.from_string(f"wpkh({k!s}")
5152
break
5253
# we didn't find correct version
5354
if not network:

0 commit comments

Comments
 (0)