mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2024-11-20 10:39:59 +01:00
Awaits seem to be working in watchonly
This commit is contained in:
parent
93417e5bdf
commit
913f2a37c1
@ -23,165 +23,150 @@ from binascii import unhexlify, hexlify, a2b_base64, b2a_base64
|
||||
|
||||
########################ADDRESSES#######################
|
||||
|
||||
def get_derive_address(wallet_id: str, num: int):
|
||||
async def get_derive_address(wallet_id: str, num: int):
|
||||
|
||||
wallet = get_watch_wallet(wallet_id)
|
||||
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
|
||||
|
||||
def get_fresh_address(wallet_id: str) -> Addresses:
|
||||
wallet = get_watch_wallet(wallet_id)
|
||||
async def get_fresh_address(wallet_id: str) -> Addresses:
|
||||
wallet = await get_watch_wallet(wallet_id)
|
||||
|
||||
address = get_derive_address(wallet_id, wallet[4] + 1)
|
||||
address = await get_derive_address(wallet_id, wallet[4] + 1)
|
||||
|
||||
update_watch_wallet(wallet_id = wallet_id, address_no = wallet[4] + 1)
|
||||
with open_ext_db("watchonly") as db:
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO addresses (
|
||||
address,
|
||||
wallet,
|
||||
amount
|
||||
)
|
||||
VALUES (?, ?, ?)
|
||||
""",
|
||||
(address, wallet_id, 0),
|
||||
await update_watch_wallet(wallet_id = wallet_id, address_no = wallet[4] + 1)
|
||||
await db.execute(
|
||||
"""
|
||||
INSERT INTO addresses (
|
||||
address,
|
||||
wallet,
|
||||
amount
|
||||
)
|
||||
VALUES (?, ?, ?)
|
||||
""",
|
||||
(address, wallet_id, 0),
|
||||
)
|
||||
|
||||
return get_address(address)
|
||||
return await get_address(address)
|
||||
|
||||
|
||||
def get_address(address: str) -> Addresses:
|
||||
with open_ext_db("watchonly") as db:
|
||||
row = db.fetchone("SELECT * FROM addresses WHERE address = ?", (address,))
|
||||
async def get_address(address: str) -> Addresses:
|
||||
row = await db.fetchone("SELECT * FROM addresses WHERE address = ?", (address,))
|
||||
return Addresses.from_row(row) if row else None
|
||||
|
||||
|
||||
def get_addresses(wallet_id: str) -> List[Addresses]:
|
||||
with open_ext_db("watchonly") as db:
|
||||
rows = db.fetchall("SELECT * FROM addresses WHERE wallet = ?", (wallet_id,))
|
||||
async def get_addresses(wallet_id: str) -> List[Addresses]:
|
||||
rows = await db.fetchall("SELECT * FROM addresses WHERE wallet = ?", (wallet_id,))
|
||||
return [Addresses(**row) for row in rows]
|
||||
|
||||
|
||||
##########################WALLETS####################
|
||||
|
||||
def create_watch_wallet(*, user: str, masterpub: str, title: str) -> Wallets:
|
||||
async def create_watch_wallet(*, user: str, masterpub: str, title: str) -> Wallets:
|
||||
wallet_id = urlsafe_short_hash()
|
||||
with open_ext_db("watchonly") as db:
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO wallets (
|
||||
id,
|
||||
user,
|
||||
masterpub,
|
||||
title,
|
||||
address_no,
|
||||
amount
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(wallet_id, user, masterpub, title, 0, 0),
|
||||
await db.execute(
|
||||
"""
|
||||
INSERT INTO wallets (
|
||||
id,
|
||||
user,
|
||||
masterpub,
|
||||
title,
|
||||
address_no,
|
||||
amount
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(wallet_id, user, masterpub, title, 0, 0),
|
||||
)
|
||||
# weallet_id = db.cursor.lastrowid
|
||||
address = get_fresh_address(wallet_id)
|
||||
return get_watch_wallet(wallet_id)
|
||||
address = await get_fresh_address(wallet_id)
|
||||
return await get_watch_wallet(wallet_id)
|
||||
|
||||
|
||||
def get_watch_wallet(wallet_id: str) -> Wallets:
|
||||
with open_ext_db("watchonly") as db:
|
||||
row = db.fetchone("SELECT * FROM wallets WHERE id = ?", (wallet_id,))
|
||||
async def get_watch_wallet(wallet_id: str) -> Wallets:
|
||||
row = await db.fetchone("SELECT * FROM wallets WHERE id = ?", (wallet_id,))
|
||||
return Wallets.from_row(row) if row else None
|
||||
|
||||
def get_watch_wallets(user: str) -> List[Wallets]:
|
||||
with open_ext_db("watchonly") as db:
|
||||
rows = db.fetchall("SELECT * FROM wallets WHERE user = ?", (user,))
|
||||
async def get_watch_wallets(user: str) -> List[Wallets]:
|
||||
rows = await db.fetchall("SELECT * FROM wallets WHERE user = ?", (user,))
|
||||
return [Wallets(**row) for row in rows]
|
||||
|
||||
def update_watch_wallet(wallet_id: str, **kwargs) -> Optional[Wallets]:
|
||||
async def update_watch_wallet(wallet_id: str, **kwargs) -> Optional[Wallets]:
|
||||
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
|
||||
|
||||
with open_ext_db("watchonly") as db:
|
||||
db.execute(f"UPDATE wallets SET {q} WHERE id = ?", (*kwargs.values(), wallet_id))
|
||||
row = db.fetchone("SELECT * FROM wallets WHERE id = ?", (wallet_id,))
|
||||
await db.execute(f"UPDATE wallets SET {q} WHERE id = ?", (*kwargs.values(), wallet_id))
|
||||
row = await db.fetchone("SELECT * FROM wallets WHERE id = ?", (wallet_id,))
|
||||
return Wallets.from_row(row) if row else None
|
||||
|
||||
|
||||
def delete_watch_wallet(wallet_id: str) -> None:
|
||||
with open_ext_db("watchonly") as db:
|
||||
db.execute("DELETE FROM wallets WHERE id = ?", (wallet_id,))
|
||||
async def delete_watch_wallet(wallet_id: str) -> None:
|
||||
await db.execute("DELETE FROM wallets WHERE id = ?", (wallet_id,))
|
||||
|
||||
|
||||
###############PAYMENTS##########################
|
||||
|
||||
def create_payment(*, user: str, ex_key: str, description: str, amount: int) -> Payments:
|
||||
async def create_payment(*, user: str, ex_key: str, description: str, amount: int) -> Payments:
|
||||
|
||||
address = get_fresh_address(ex_key)
|
||||
address = await get_fresh_address(ex_key)
|
||||
payment_id = urlsafe_short_hash()
|
||||
with open_ext_db("watchonly") as db:
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO payments (
|
||||
payment_id,
|
||||
user,
|
||||
ex_key,
|
||||
address,
|
||||
amount
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
""",
|
||||
(payment_id, user, ex_key, address, amount),
|
||||
await db.execute(
|
||||
"""
|
||||
INSERT INTO payments (
|
||||
payment_id,
|
||||
user,
|
||||
ex_key,
|
||||
address,
|
||||
amount
|
||||
)
|
||||
payment_id = db.cursor.lastrowid
|
||||
return get_payment(payment_id)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
""",
|
||||
(payment_id, user, ex_key, address, amount),
|
||||
)
|
||||
payment_id = db.cursor.lastrowid
|
||||
return await get_payment(payment_id)
|
||||
|
||||
|
||||
def get_payment(payment_id: str) -> Payments:
|
||||
with open_ext_db("watchonly") as db:
|
||||
row = db.fetchone("SELECT * FROM payments WHERE id = ?", (payment_id,))
|
||||
async def get_payment(payment_id: str) -> Payments:
|
||||
row = await db.fetchone("SELECT * FROM payments WHERE id = ?", (payment_id,))
|
||||
return Payments.from_row(row) if row else None
|
||||
|
||||
|
||||
def get_payments(user: str) -> List[Payments]:
|
||||
with open_ext_db("watchonly") as db:
|
||||
rows = db.fetchall("SELECT * FROM payments WHERE user IN ?", (user,))
|
||||
async def get_payments(user: str) -> List[Payments]:
|
||||
rows = await db.fetchall("SELECT * FROM payments WHERE user IN ?", (user,))
|
||||
return [Payments.from_row(row) for row in rows]
|
||||
|
||||
|
||||
def delete_payment(payment_id: str) -> None:
|
||||
with open_ext_db("watchonly") as db:
|
||||
db.execute("DELETE FROM payments WHERE id = ?", (payment_id,))
|
||||
async def delete_payment(payment_id: str) -> None:
|
||||
await db.execute("DELETE FROM payments WHERE id = ?", (payment_id,))
|
||||
|
||||
|
||||
######################MEMPOOL#######################
|
||||
|
||||
def create_mempool(user: str) -> Mempool:
|
||||
with open_ext_db("watchonly") as db:
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO mempool (
|
||||
user,
|
||||
endpoint
|
||||
)
|
||||
VALUES (?, ?)
|
||||
""",
|
||||
(user, 'https://mempool.space'),
|
||||
)
|
||||
row = db.fetchone("SELECT * FROM mempool WHERE user = ?", (user,))
|
||||
async def create_mempool(user: str) -> Mempool:
|
||||
await db.execute(
|
||||
"""
|
||||
INSERT INTO mempool (
|
||||
user,
|
||||
endpoint
|
||||
)
|
||||
VALUES (?, ?)
|
||||
""",
|
||||
(user, 'https://mempool.space'),
|
||||
)
|
||||
row = await db.fetchone("SELECT * FROM mempool WHERE user = ?", (user,))
|
||||
return Mempool.from_row(row) if row else None
|
||||
|
||||
def update_mempool(user: str, **kwargs) -> Optional[Mempool]:
|
||||
async def update_mempool(user: str, **kwargs) -> Optional[Mempool]:
|
||||
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
|
||||
|
||||
with open_ext_db("watchonly") as db:
|
||||
db.execute(f"UPDATE mempool SET {q} WHERE user = ?", (*kwargs.values(), user))
|
||||
row = db.fetchone("SELECT * FROM mempool WHERE user = ?", (user,))
|
||||
await db.execute(f"UPDATE mempool SET {q} WHERE user = ?", (*kwargs.values(), user))
|
||||
row = await db.fetchone("SELECT * FROM mempool WHERE user = ?", (user,))
|
||||
return Mempool.from_row(row) if row else None
|
||||
|
||||
|
||||
def get_mempool(user: str) -> Mempool:
|
||||
with open_ext_db("watchonly") as db:
|
||||
row = db.fetchone("SELECT * FROM mempool WHERE user = ?", (user,))
|
||||
async def get_mempool(user: str) -> Mempool:
|
||||
row = await db.fetchone("SELECT * FROM mempool WHERE user = ?", (user,))
|
||||
return Mempool.from_row(row) if row else None
|
||||
|
@ -43,7 +43,7 @@ async def api_wallets_retrieve():
|
||||
@watchonly_ext.route("/api/v1/wallet/<wallet_id>", methods=["GET"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_wallet_retrieve(wallet_id):
|
||||
wallet = get_watch_wallet(wallet_id)
|
||||
wallet = await get_watch_wallet(wallet_id)
|
||||
|
||||
if not wallet:
|
||||
return jsonify({"message": "wallet does not exist"}), HTTPStatus.NOT_FOUND
|
||||
@ -63,26 +63,25 @@ async def api_wallet_retrieve(wallet_id):
|
||||
async def api_wallet_create_or_update(wallet_id=None):
|
||||
print("g.data")
|
||||
if not wallet_id:
|
||||
wallet = create_watch_wallet(user=g.wallet.user, masterpub=g.data["masterpub"], title=g.data["title"])
|
||||
mempool = get_mempool(g.wallet.user)
|
||||
wallet = await create_watch_wallet(user=g.wallet.user, masterpub=g.data["masterpub"], title=g.data["title"])
|
||||
mempool = await get_mempool(g.wallet.user)
|
||||
if not mempool:
|
||||
create_mempool(user=g.wallet.user)
|
||||
return jsonify(wallet._asdict()), HTTPStatus.CREATED
|
||||
|
||||
else:
|
||||
wallet = update_watch_wallet(wallet_id=wallet_id, **g.data)
|
||||
wallet = await update_watch_wallet(wallet_id=wallet_id, **g.data)
|
||||
return jsonify(wallet._asdict()), HTTPStatus.OK
|
||||
|
||||
|
||||
@watchonly_ext.route("/api/v1/wallet/<wallet_id>", methods=["DELETE"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_wallet_delete(wallet_id):
|
||||
wallet = get_watch_wallet(wallet_id)
|
||||
wallet = await get_watch_wallet(wallet_id)
|
||||
|
||||
if not wallet:
|
||||
return jsonify({"message": "Wallet link does not exist."}), HTTPStatus.NOT_FOUND
|
||||
|
||||
delete_watch_wallet(wallet_id)
|
||||
await delete_watch_wallet(wallet_id)
|
||||
|
||||
return jsonify({"deleted": "true"}), HTTPStatus.NO_CONTENT
|
||||
|
||||
@ -92,7 +91,7 @@ async def api_wallet_delete(wallet_id):
|
||||
@watchonly_ext.route("/api/v1/address/<wallet_id>", methods=["GET"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_fresh_address(wallet_id):
|
||||
address = get_fresh_address(wallet_id)
|
||||
address = await get_fresh_address(wallet_id)
|
||||
|
||||
if not address:
|
||||
return jsonify({"message": "something went wrong"}), HTTPStatus.NOT_FOUND
|
||||
@ -103,8 +102,7 @@ async def api_fresh_address(wallet_id):
|
||||
@watchonly_ext.route("/api/v1/addresses/<wallet_id>", methods=["GET"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_get_addresses(wallet_id):
|
||||
addresses = get_addresses(wallet_id)
|
||||
print(addresses)
|
||||
addresses = await get_addresses(wallet_id)
|
||||
if not addresses:
|
||||
return jsonify({"message": "wallet does not exist"}), HTTPStatus.NOT_FOUND
|
||||
|
||||
@ -152,23 +150,23 @@ async def api_payment_retrieve(payment_id):
|
||||
async def api_payment_create_or_update(payment_id=None):
|
||||
|
||||
if not payment_id:
|
||||
payment = create_payment(g.wallet.user, g.data.ex_key, g.data.pub_key, g.data.amount)
|
||||
payment = await create_payment(g.wallet.user, g.data.ex_key, g.data.pub_key, g.data.amount)
|
||||
return jsonify(get_payment(payment)), HTTPStatus.CREATED
|
||||
|
||||
else:
|
||||
payment = update_payment(payment_id, g.data)
|
||||
payment = await update_payment(payment_id, g.data)
|
||||
return jsonify({payment}), HTTPStatus.OK
|
||||
|
||||
|
||||
@watchonly_ext.route("/api/v1/payment/<payment_id>", methods=["DELETE"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_payment_delete(payment_id):
|
||||
payment = get_watch_wallet(payment_id)
|
||||
payment = await get_watch_wallet(payment_id)
|
||||
|
||||
if not payment:
|
||||
return jsonify({"message": "Wallet link does not exist."}), HTTPStatus.NOT_FOUND
|
||||
|
||||
delete_watch_wallet(payment_id)
|
||||
await delete_watch_wallet(payment_id)
|
||||
|
||||
return "", HTTPStatus.NO_CONTENT
|
||||
|
||||
@ -182,13 +180,13 @@ async def api_payment_delete(payment_id):
|
||||
}
|
||||
)
|
||||
async def api_update_mempool():
|
||||
mempool = update_mempool(user=g.wallet.user, **g.data)
|
||||
mempool = await update_mempool(user=g.wallet.user, **g.data)
|
||||
return jsonify(mempool._asdict()), HTTPStatus.OK
|
||||
|
||||
@watchonly_ext.route("/api/v1/mempool", methods=["GET"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_get_mempool():
|
||||
mempool = get_mempool(g.wallet.user)
|
||||
mempool = await get_mempool(g.wallet.user)
|
||||
if not mempool:
|
||||
mempool = create_mempool(user=g.wallet.user)
|
||||
mempool = await create_mempool(user=g.wallet.user)
|
||||
return jsonify(mempool._asdict()), HTTPStatus.OK
|
Loading…
Reference in New Issue
Block a user