Awaits seem to be working in watchonly

This commit is contained in:
benarc
2020-12-01 21:39:33 +00:00
parent 93417e5bdf
commit 913f2a37c1
2 changed files with 100 additions and 117 deletions

View File

@@ -23,165 +23,150 @@ from binascii import unhexlify, hexlify, a2b_base64, b2a_base64
########################ADDRESSES####################### ########################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])) k = bip32.HDKey.from_base58(str(wallet[2]))
child = k.derive([0, num]) child = k.derive([0, num])
address = script.p2wpkh(child).address() address = script.p2wpkh(child).address()
return address return address
def get_fresh_address(wallet_id: str) -> Addresses: async def get_fresh_address(wallet_id: str) -> Addresses:
wallet = get_watch_wallet(wallet_id) 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) await update_watch_wallet(wallet_id = wallet_id, address_no = wallet[4] + 1)
with open_ext_db("watchonly") as db: await db.execute(
db.execute( """
""" INSERT INTO addresses (
INSERT INTO addresses ( address,
address, wallet,
wallet, amount
amount
)
VALUES (?, ?, ?)
""",
(address, wallet_id, 0),
) )
VALUES (?, ?, ?)
""",
(address, wallet_id, 0),
)
return get_address(address) return await get_address(address)
def get_address(address: str) -> Addresses: async def get_address(address: str) -> Addresses:
with open_ext_db("watchonly") as db: row = await db.fetchone("SELECT * FROM addresses WHERE address = ?", (address,))
row = db.fetchone("SELECT * FROM addresses WHERE address = ?", (address,))
return Addresses.from_row(row) if row else None return Addresses.from_row(row) if row else None
def get_addresses(wallet_id: str) -> List[Addresses]: async def get_addresses(wallet_id: str) -> List[Addresses]:
with open_ext_db("watchonly") as db: rows = await db.fetchall("SELECT * FROM addresses WHERE wallet = ?", (wallet_id,))
rows = db.fetchall("SELECT * FROM addresses WHERE wallet = ?", (wallet_id,))
return [Addresses(**row) for row in rows] return [Addresses(**row) for row in rows]
##########################WALLETS#################### ##########################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() wallet_id = urlsafe_short_hash()
with open_ext_db("watchonly") as db: await db.execute(
db.execute( """
""" INSERT INTO wallets (
INSERT INTO wallets ( id,
id, user,
user, masterpub,
masterpub, title,
title, address_no,
address_no, amount
amount
)
VALUES (?, ?, ?, ?, ?, ?)
""",
(wallet_id, user, masterpub, title, 0, 0),
) )
VALUES (?, ?, ?, ?, ?, ?)
""",
(wallet_id, user, masterpub, title, 0, 0),
)
# weallet_id = db.cursor.lastrowid # weallet_id = db.cursor.lastrowid
address = get_fresh_address(wallet_id) address = await get_fresh_address(wallet_id)
return get_watch_wallet(wallet_id) return await get_watch_wallet(wallet_id)
def get_watch_wallet(wallet_id: str) -> Wallets: async def get_watch_wallet(wallet_id: str) -> Wallets:
with open_ext_db("watchonly") as db: row = await db.fetchone("SELECT * FROM wallets WHERE id = ?", (wallet_id,))
row = db.fetchone("SELECT * FROM wallets WHERE id = ?", (wallet_id,))
return Wallets.from_row(row) if row else None return Wallets.from_row(row) if row else None
def get_watch_wallets(user: str) -> List[Wallets]: async def get_watch_wallets(user: str) -> List[Wallets]:
with open_ext_db("watchonly") as db: rows = await db.fetchall("SELECT * FROM wallets WHERE user = ?", (user,))
rows = db.fetchall("SELECT * FROM wallets WHERE user = ?", (user,))
return [Wallets(**row) for row in rows] 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()]) q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
with open_ext_db("watchonly") as db: await db.execute(f"UPDATE wallets SET {q} WHERE id = ?", (*kwargs.values(), wallet_id))
db.execute(f"UPDATE wallets SET {q} WHERE id = ?", (*kwargs.values(), wallet_id)) row = await db.fetchone("SELECT * FROM wallets WHERE id = ?", (wallet_id,))
row = db.fetchone("SELECT * FROM wallets WHERE id = ?", (wallet_id,))
return Wallets.from_row(row) if row else None return Wallets.from_row(row) if row else None
def delete_watch_wallet(wallet_id: str) -> None: async def delete_watch_wallet(wallet_id: str) -> None:
with open_ext_db("watchonly") as db: await db.execute("DELETE FROM wallets WHERE id = ?", (wallet_id,))
db.execute("DELETE FROM wallets WHERE id = ?", (wallet_id,))
###############PAYMENTS########################## ###############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() payment_id = urlsafe_short_hash()
with open_ext_db("watchonly") as db: await db.execute(
db.execute( """
""" INSERT INTO payments (
INSERT INTO payments ( payment_id,
payment_id, user,
user, ex_key,
ex_key, address,
address, amount
amount
)
VALUES (?, ?, ?, ?, ?)
""",
(payment_id, user, ex_key, address, amount),
) )
payment_id = db.cursor.lastrowid VALUES (?, ?, ?, ?, ?)
return get_payment(payment_id) """,
(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: async def get_payment(payment_id: str) -> Payments:
with open_ext_db("watchonly") as db: row = await db.fetchone("SELECT * FROM payments WHERE id = ?", (payment_id,))
row = db.fetchone("SELECT * FROM payments WHERE id = ?", (payment_id,))
return Payments.from_row(row) if row else None return Payments.from_row(row) if row else None
def get_payments(user: str) -> List[Payments]: async def get_payments(user: str) -> List[Payments]:
with open_ext_db("watchonly") as db: rows = await db.fetchall("SELECT * FROM payments WHERE user IN ?", (user,))
rows = db.fetchall("SELECT * FROM payments WHERE user IN ?", (user,))
return [Payments.from_row(row) for row in rows] return [Payments.from_row(row) for row in rows]
def delete_payment(payment_id: str) -> None: async def delete_payment(payment_id: str) -> None:
with open_ext_db("watchonly") as db: await db.execute("DELETE FROM payments WHERE id = ?", (payment_id,))
db.execute("DELETE FROM payments WHERE id = ?", (payment_id,))
######################MEMPOOL####################### ######################MEMPOOL#######################
def create_mempool(user: str) -> Mempool: async def create_mempool(user: str) -> Mempool:
with open_ext_db("watchonly") as db: await db.execute(
db.execute( """
""" INSERT INTO mempool (
INSERT INTO mempool ( user,
user, endpoint
endpoint )
) VALUES (?, ?)
VALUES (?, ?) """,
""", (user, 'https://mempool.space'),
(user, 'https://mempool.space'), )
) row = await db.fetchone("SELECT * FROM mempool WHERE user = ?", (user,))
row = db.fetchone("SELECT * FROM mempool WHERE user = ?", (user,))
return Mempool.from_row(row) if row else None 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()]) q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
with open_ext_db("watchonly") as db: await db.execute(f"UPDATE mempool SET {q} WHERE user = ?", (*kwargs.values(), user))
db.execute(f"UPDATE mempool SET {q} WHERE user = ?", (*kwargs.values(), user)) row = await db.fetchone("SELECT * FROM mempool WHERE user = ?", (user,))
row = db.fetchone("SELECT * FROM mempool WHERE user = ?", (user,))
return Mempool.from_row(row) if row else None return Mempool.from_row(row) if row else None
def get_mempool(user: str) -> Mempool: async def get_mempool(user: str) -> Mempool:
with open_ext_db("watchonly") as db: row = await db.fetchone("SELECT * FROM mempool WHERE user = ?", (user,))
row = db.fetchone("SELECT * FROM mempool WHERE user = ?", (user,))
return Mempool.from_row(row) if row else None return Mempool.from_row(row) if row else None

View File

@@ -43,7 +43,7 @@ async def api_wallets_retrieve():
@watchonly_ext.route("/api/v1/wallet/<wallet_id>", methods=["GET"]) @watchonly_ext.route("/api/v1/wallet/<wallet_id>", methods=["GET"])
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_wallet_retrieve(wallet_id): async def api_wallet_retrieve(wallet_id):
wallet = get_watch_wallet(wallet_id) wallet = await get_watch_wallet(wallet_id)
if not wallet: if not wallet:
return jsonify({"message": "wallet does not exist"}), HTTPStatus.NOT_FOUND 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): async def api_wallet_create_or_update(wallet_id=None):
print("g.data") print("g.data")
if not wallet_id: if not wallet_id:
wallet = create_watch_wallet(user=g.wallet.user, masterpub=g.data["masterpub"], title=g.data["title"]) wallet = await create_watch_wallet(user=g.wallet.user, masterpub=g.data["masterpub"], title=g.data["title"])
mempool = get_mempool(g.wallet.user) mempool = await get_mempool(g.wallet.user)
if not mempool: if not mempool:
create_mempool(user=g.wallet.user) create_mempool(user=g.wallet.user)
return jsonify(wallet._asdict()), HTTPStatus.CREATED return jsonify(wallet._asdict()), HTTPStatus.CREATED
else: 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 return jsonify(wallet._asdict()), HTTPStatus.OK
@watchonly_ext.route("/api/v1/wallet/<wallet_id>", methods=["DELETE"]) @watchonly_ext.route("/api/v1/wallet/<wallet_id>", methods=["DELETE"])
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_wallet_delete(wallet_id): async def api_wallet_delete(wallet_id):
wallet = get_watch_wallet(wallet_id) wallet = await get_watch_wallet(wallet_id)
if not wallet: if not wallet:
return jsonify({"message": "Wallet link does not exist."}), HTTPStatus.NOT_FOUND 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 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"]) @watchonly_ext.route("/api/v1/address/<wallet_id>", methods=["GET"])
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_fresh_address(wallet_id): async def api_fresh_address(wallet_id):
address = get_fresh_address(wallet_id) address = await get_fresh_address(wallet_id)
if not address: if not address:
return jsonify({"message": "something went wrong"}), HTTPStatus.NOT_FOUND 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"]) @watchonly_ext.route("/api/v1/addresses/<wallet_id>", methods=["GET"])
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_get_addresses(wallet_id): async def api_get_addresses(wallet_id):
addresses = get_addresses(wallet_id) addresses = await get_addresses(wallet_id)
print(addresses)
if not addresses: if not addresses:
return jsonify({"message": "wallet does not exist"}), HTTPStatus.NOT_FOUND 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): async def api_payment_create_or_update(payment_id=None):
if not payment_id: 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 return jsonify(get_payment(payment)), HTTPStatus.CREATED
else: else:
payment = update_payment(payment_id, g.data) payment = await update_payment(payment_id, g.data)
return jsonify({payment}), HTTPStatus.OK return jsonify({payment}), HTTPStatus.OK
@watchonly_ext.route("/api/v1/payment/<payment_id>", methods=["DELETE"]) @watchonly_ext.route("/api/v1/payment/<payment_id>", methods=["DELETE"])
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_payment_delete(payment_id): async def api_payment_delete(payment_id):
payment = get_watch_wallet(payment_id) payment = await get_watch_wallet(payment_id)
if not payment: if not payment:
return jsonify({"message": "Wallet link does not exist."}), HTTPStatus.NOT_FOUND 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 return "", HTTPStatus.NO_CONTENT
@@ -182,13 +180,13 @@ async def api_payment_delete(payment_id):
} }
) )
async def api_update_mempool(): 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 return jsonify(mempool._asdict()), HTTPStatus.OK
@watchonly_ext.route("/api/v1/mempool", methods=["GET"]) @watchonly_ext.route("/api/v1/mempool", methods=["GET"])
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_get_mempool(): async def api_get_mempool():
mempool = get_mempool(g.wallet.user) mempool = await get_mempool(g.wallet.user)
if not mempool: if not mempool:
mempool = create_mempool(user=g.wallet.user) mempool = await create_mempool(user=g.wallet.user)
return jsonify(mempool._asdict()), HTTPStatus.OK return jsonify(mempool._asdict()), HTTPStatus.OK