mirror of
https://github.com/lnbits/lnbits.git
synced 2025-09-25 11:14:02 +02:00
Trying to restore addresses functions, broken
This commit is contained in:
@@ -77,62 +77,45 @@ async def update_watch_wallet(wallet_id: str, **kwargs) -> Optional[Wallets]:
|
|||||||
async def delete_watch_wallet(wallet_id: str) -> None:
|
async def delete_watch_wallet(wallet_id: str) -> None:
|
||||||
await db.execute("DELETE FROM wallets WHERE id = ?", (wallet_id,))
|
await db.execute("DELETE FROM wallets WHERE id = ?", (wallet_id,))
|
||||||
|
|
||||||
|
########################ADDRESSES#######################
|
||||||
|
|
||||||
###############CHARGES##########################
|
async def get_derive_address(wallet_id: str, num: int):
|
||||||
|
|
||||||
|
wallet = await get_watch_wallet(wallet_id)
|
||||||
|
k = bip32.HDKey.from_base58(str(wallet[2]))
|
||||||
|
child = k.derive([0, num])
|
||||||
|
address = script.p2wpkh(child).address()
|
||||||
|
|
||||||
|
return address
|
||||||
|
|
||||||
async def create_charge(walletid: str, user: str, title: Optional[str] = None, time: Optional[int] = None, amount: Optional[int] = None) -> Charges:
|
async def get_fresh_address(wallet_id: str) -> Addresses:
|
||||||
wallet = await get_watch_wallet(walletid)
|
wallet = await get_watch_wallet(wallet_id)
|
||||||
address = await get_derive_address(walletid, wallet[4] + 1)
|
|
||||||
|
address = await get_derive_address(wallet_id, wallet[4] + 1)
|
||||||
|
|
||||||
charge_id = urlsafe_short_hash()
|
await update_watch_wallet(wallet_id = wallet_id, address_no = wallet[4] + 1)
|
||||||
await db.execute(
|
await db.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO charges (
|
INSERT INTO addresses (
|
||||||
id,
|
|
||||||
user,
|
|
||||||
title,
|
|
||||||
wallet,
|
|
||||||
address,
|
address,
|
||||||
time_to_pay,
|
wallet,
|
||||||
amount,
|
amount
|
||||||
balance
|
|
||||||
)
|
)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?)
|
||||||
""",
|
""",
|
||||||
(charge_id, user, title, walletid, address, time, amount, 0),
|
(address, wallet_id, 0),
|
||||||
)
|
)
|
||||||
return await get_charge(charge_id)
|
|
||||||
|
return await get_address(address)
|
||||||
|
|
||||||
|
|
||||||
async def get_charge(charge_id: str) -> Charges:
|
async def get_address(address: str) -> Addresses:
|
||||||
row = await db.fetchone("SELECT * FROM charges WHERE id = ?", (charge_id,))
|
row = await db.fetchone("SELECT * FROM addresses WHERE address = ?", (address,))
|
||||||
return Charges.from_row(row) if row else None
|
return Addresses.from_row(row) if row else None
|
||||||
|
|
||||||
|
async def get_addresses(wallet_id: str) -> List[Addresses]:
|
||||||
async def get_charges(user: str) -> List[Charges]:
|
rows = await db.fetchall("SELECT * FROM addresses WHERE wallet = ?", (wallet_id,))
|
||||||
rows = await db.fetchall("SELECT * FROM charges WHERE user = ?", (user,))
|
return [Addresses(**row) for row in rows]
|
||||||
for row in rows:
|
|
||||||
await check_address_balance(row.address)
|
|
||||||
rows = await db.fetchall("SELECT * FROM charges WHERE user = ?", (user,))
|
|
||||||
return [charges.from_row(row) for row in rows]
|
|
||||||
|
|
||||||
|
|
||||||
async def delete_charge(charge_id: str) -> None:
|
|
||||||
await db.execute("DELETE FROM charges WHERE id = ?", (charge_id,))
|
|
||||||
|
|
||||||
async def check_address_balance(address: str) -> List[Charges]:
|
|
||||||
address_data = await get_address(address)
|
|
||||||
mempool = await get_mempool(address_data.user)
|
|
||||||
|
|
||||||
try:
|
|
||||||
async with httpx.AsyncClient() as client:
|
|
||||||
r = await client.get(mempool.endpoint + "/api/address/" + address)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
amount_paid = r.json()['chain_stats']['funded_txo_sum'] - r.json()['chain_stats']['spent_txo_sum']
|
|
||||||
print(amount_paid)
|
|
||||||
|
|
||||||
######################MEMPOOL#######################
|
######################MEMPOOL#######################
|
||||||
|
|
||||||
|
@@ -82,6 +82,37 @@ async def api_wallet_delete(wallet_id):
|
|||||||
return jsonify({"deleted": "true"}), HTTPStatus.NO_CONTENT
|
return jsonify({"deleted": "true"}), HTTPStatus.NO_CONTENT
|
||||||
|
|
||||||
|
|
||||||
|
#############################ADDRESSES##########################
|
||||||
|
|
||||||
|
@watchonly_ext.route("/api/v1/address/<wallet_id>", methods=["GET"])
|
||||||
|
@api_check_wallet_key("invoice")
|
||||||
|
async def api_fresh_address(wallet_id):
|
||||||
|
await get_fresh_address(wallet_id)
|
||||||
|
|
||||||
|
addresses = await get_addresses(wallet_id)
|
||||||
|
|
||||||
|
return jsonify([address._asdict() for address in addresses]), HTTPStatus.OK
|
||||||
|
|
||||||
|
|
||||||
|
@watchonly_ext.route("/api/v1/addresses/<wallet_id>", methods=["GET"])
|
||||||
|
@api_check_wallet_key("invoice")
|
||||||
|
async def api_get_addresses(wallet_id):
|
||||||
|
print(wallet_id)
|
||||||
|
|
||||||
|
wallet = await get_watch_wallet(wallet_id)
|
||||||
|
|
||||||
|
if not wallet:
|
||||||
|
return jsonify({"message": "wallet does not exist"}), HTTPStatus.NOT_FOUND
|
||||||
|
|
||||||
|
addresses = await get_addresses(wallet_id)
|
||||||
|
|
||||||
|
if not addresses:
|
||||||
|
await get_fresh_address(wallet_id)
|
||||||
|
addresses = await get_addresses(wallet_id)
|
||||||
|
|
||||||
|
return jsonify([address._asdict() for address in addresses]), HTTPStatus.OK
|
||||||
|
|
||||||
|
|
||||||
#############################MEMPOOL##########################
|
#############################MEMPOOL##########################
|
||||||
|
|
||||||
@watchonly_ext.route("/api/v1/mempool", methods=["PUT"])
|
@watchonly_ext.route("/api/v1/mempool", methods=["PUT"])
|
||||||
|
Reference in New Issue
Block a user