mirror of
https://github.com/lnbits/lnbits.git
synced 2025-09-28 21:02:31 +02:00
refactor: move logic out of crud
into the api
layer
This commit is contained in:
@@ -12,19 +12,8 @@ from .helpers import parse_key, derive_address
|
|||||||
##########################WALLETS####################
|
##########################WALLETS####################
|
||||||
|
|
||||||
|
|
||||||
async def create_watch_wallet(user: str, masterpub: str, title: str) -> WalletAccount:
|
async def create_watch_wallet(w: WalletAccount) -> WalletAccount:
|
||||||
# check the masterpub is fine, it will raise an exception if not
|
|
||||||
(descriptor, _) = parse_key(masterpub)
|
|
||||||
|
|
||||||
type = descriptor.scriptpubkey_type()
|
|
||||||
fingerprint = descriptor.keys[0].fingerprint.hex()
|
|
||||||
wallet_id = urlsafe_short_hash()
|
wallet_id = urlsafe_short_hash()
|
||||||
|
|
||||||
wallets = await get_watch_wallets(user)
|
|
||||||
w = next((w for w in wallets if w.fingerprint == fingerprint), None)
|
|
||||||
if w:
|
|
||||||
raise ValueError("Account '{}' has the same master pulic key".format(w.title))
|
|
||||||
|
|
||||||
await db.execute(
|
await db.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO watchonly.wallets (
|
INSERT INTO watchonly.wallets (
|
||||||
@@ -39,8 +28,16 @@ async def create_watch_wallet(user: str, masterpub: str, title: str) -> WalletAc
|
|||||||
)
|
)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
# address_no is -1 so fresh address on empty wallet can get address with index 0
|
(
|
||||||
(wallet_id, user, masterpub, fingerprint, title, type, -1, 0),
|
wallet_id,
|
||||||
|
w.user,
|
||||||
|
w.masterpub,
|
||||||
|
w.fingerprint,
|
||||||
|
w.title,
|
||||||
|
w.type,
|
||||||
|
w.address_no,
|
||||||
|
w.balance,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
return await get_watch_wallet(wallet_id)
|
return await get_watch_wallet(wallet_id)
|
||||||
|
@@ -31,7 +31,7 @@ from .crud import (
|
|||||||
get_config,
|
get_config,
|
||||||
update_config,
|
update_config,
|
||||||
)
|
)
|
||||||
from .models import CreateWallet, CreatePsbt, Config
|
from .models import CreateWallet, CreatePsbt, Config, WalletAccount
|
||||||
from .helpers import parse_key
|
from .helpers import parse_key
|
||||||
|
|
||||||
|
|
||||||
@@ -66,9 +66,32 @@ async def api_wallet_create_or_update(
|
|||||||
data: CreateWallet, w: WalletTypeInfo = Depends(require_admin_key)
|
data: CreateWallet, w: WalletTypeInfo = Depends(require_admin_key)
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
wallet = await create_watch_wallet(
|
(descriptor, _) = parse_key(data.masterpub)
|
||||||
user=w.wallet.user, masterpub=data.masterpub, title=data.title
|
|
||||||
|
new_wallet = WalletAccount(
|
||||||
|
id="none",
|
||||||
|
user=w.wallet.user,
|
||||||
|
masterpub=data.masterpub,
|
||||||
|
fingerprint=descriptor.keys[0].fingerprint.hex(),
|
||||||
|
type=descriptor.scriptpubkey_type(),
|
||||||
|
title=data.title,
|
||||||
|
address_no=-1, # so fresh address on empty wallet can get address with index 0
|
||||||
|
balance=0,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
wallets = await get_watch_wallets(w.wallet.user)
|
||||||
|
existing_wallet = next(
|
||||||
|
(ew for ew in wallets if ew.fingerprint == new_wallet.fingerprint), None
|
||||||
|
)
|
||||||
|
if existing_wallet:
|
||||||
|
raise ValueError(
|
||||||
|
"Account '{}' has the same master pulic key".format(
|
||||||
|
existing_wallet.title
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
wallet = await create_watch_wallet(new_wallet)
|
||||||
|
|
||||||
await api_get_addresses(wallet.id, w)
|
await api_get_addresses(wallet.id, w)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(e))
|
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(e))
|
||||||
|
Reference in New Issue
Block a user