From 36da130b7d815e74486d342a58637fe3983fb558 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Thu, 4 May 2023 23:55:18 -0700 Subject: [PATCH] improve ux on managing wallets --- lws-web/lws/config.py | 3 +- lws-web/lws/models.py | 49 ++++++++++++++++++- lws-web/lws/routes/meta.py | 16 +++++-- lws-web/lws/routes/wallet.py | 26 +++++----- lws-web/lws/templates/includes/base.html | 1 - lws-web/lws/templates/index.html | 61 +++++++++++++++--------- lws-web/lws/templates/wallet/add.html | 4 +- lws-web/lws/templates/wallet/list.html | 11 ----- lws-web/lws/templates/wallet/show.html | 14 ++++-- 9 files changed, 126 insertions(+), 59 deletions(-) delete mode 100644 lws-web/lws/templates/wallet/list.html diff --git a/lws-web/lws/config.py b/lws-web/lws/config.py index 5f49f0a..7a3da44 100644 --- a/lws-web/lws/config.py +++ b/lws-web/lws/config.py @@ -14,4 +14,5 @@ QUART_AUTH_DURATION = 60 * 60 # 1 hour # LWS LWS_URL = env.get("LWS_URL", "http://127.0.0.1:8080") -LWS_ADMIN_URL = env.get("LWS_ADMIN_URL", "http://127.0.0.1:8081") \ No newline at end of file +LWS_ADMIN_URL = env.get("LWS_ADMIN_URL", "http://127.0.0.1:8081") + diff --git a/lws-web/lws/models.py b/lws-web/lws/models.py index 55ffefd..cfb7dbe 100644 --- a/lws-web/lws/models.py +++ b/lws-web/lws/models.py @@ -31,6 +31,28 @@ class Wallet(Model): date_added = DateTimeField(null=True) user = ForeignKeyField(User, backref="wallets") + def is_active(self): + endpoint = f"{config.LWS_ADMIN_URL}/list_accounts" + data = { + "auth": self.user.view_key, + "params": {} + } + try: + req = requests.post(endpoint, json=data, timeout=5) + req.raise_for_status() + if req.ok: + res = req.json() + for _status in res: + for _wallet in res[_status]: + if _wallet["address"] == self.address: + if _status == "active": + return True + return False + return False + except Exception as e: + print(f"Failed to list wallets: {e}") + return False + def check_wallet_lws(self): endpoint = f"{config.LWS_ADMIN_URL}/list_accounts" data = { @@ -81,13 +103,18 @@ class Wallet(Model): print(f"Failed to add wallet {self.address}: {e}") return False - def disable_wallet_lws(self): + def set_active(self, status): endpoint = f"{config.LWS_ADMIN_URL}/modify_account_status" + _status = "" + if status: + _status = "active" + else: + _status = "inactive" data = { "auth": self.user.view_key, "params": { "addresses": [self.address], - "status": "inactive" + "status": _status } } try: @@ -100,6 +127,24 @@ class Wallet(Model): print(f"Failed to add wallet {self.address}: {e}") return False + def enable_wallet_lws(self): + endpoint = f"{config.LWS_ADMIN_URL}/modify_account_status" + data = { + "auth": self.user.view_key, + "params": { + "addresses": [self.address], + "status": "inactive" + } + } + try: + req = requests.post(endpoint, json=data, timeout=5) + req.raise_for_status() + if req.ok: + return True + return False + except Exception as e: + print(f"Failed to add wallet {self.address}: {e}") + return False def get_wallet_info(self): endpoint = f"{config.LWS_URL}/get_address_info" diff --git a/lws-web/lws/routes/meta.py b/lws-web/lws/routes/meta.py index b1b0a5c..c473a5c 100644 --- a/lws-web/lws/routes/meta.py +++ b/lws-web/lws/routes/meta.py @@ -7,18 +7,26 @@ from lws.helpers import LWS from lws import config -bp = Blueprint('meta', 'meta') +bp = Blueprint("meta", "meta") @bp.route("/") @login_required async def index(): - wallets = Wallet.select().order_by(Wallet.date.desc()) + admin = User.select().first() lws = LWS(admin.view_key) + accounts = lws.list_accounts() + data = {} + for status in accounts: + if status == "hidden": + continue + for account in accounts[status]: + account["wallet"] = Wallet.select().where(Wallet.address ** account["address"]).first() + account["status"] = status + data[account["address"]] = account return await render_template( "index.html", config=config, - wallets=wallets, - lws_accounts=lws.list_accounts() + data=data ) diff --git a/lws-web/lws/routes/wallet.py b/lws-web/lws/routes/wallet.py index 4c5fd58..6a02819 100644 --- a/lws-web/lws/routes/wallet.py +++ b/lws-web/lws/routes/wallet.py @@ -18,13 +18,6 @@ bp = Blueprint('wallet', 'wallet') # reject_requests: {"type": "import"|"create", "addresses":[...]} # rescan: {"height":..., "addresses":[...]} - -@bp.route("/wallets") -@login_required -async def list(): - wallets = Wallet.select().order_by(Wallet.id.asc()) - return await render_template("wallet/list.html", wallets=wallets) - @bp.route("/wallet/add", methods=["GET", "POST"]) @login_required async def add(): @@ -73,7 +66,7 @@ async def show(id): wallet = Wallet.select().where(Wallet.id == id).first() if not wallet: await flash("wallet does not exist") - return redirect("/wallets") + return redirect("/") return await render_template( "wallet/show.html", wallet=wallet @@ -85,7 +78,7 @@ async def rescan(id): wallet = Wallet.select().where(Wallet.id == id).first() if not wallet: await flash("wallet does not exist") - return redirect("/wallets") + return redirect("/") wallet.rescan() return redirect(f"/wallet/{id}") @@ -96,7 +89,18 @@ async def disable(id): wallet = Wallet.select().where(Wallet.id == id).first() if not wallet: await flash("wallet does not exist") - return redirect("/wallets") - wallet.disable_wallet_lws() + return redirect("/") + wallet.set_active(False) + return redirect(f"/wallet/{id}") + + +@bp.route("/wallet//enable") +@login_required +async def enable(id): + wallet = Wallet.select().where(Wallet.id == id).first() + if not wallet: + await flash("wallet does not exist") + return redirect("/") + wallet.set_active(True) return redirect(f"/wallet/{id}") diff --git a/lws-web/lws/templates/includes/base.html b/lws-web/lws/templates/includes/base.html index 26950a5..9de0ab1 100644 --- a/lws-web/lws/templates/includes/base.html +++ b/lws-web/lws/templates/includes/base.html @@ -38,7 +38,6 @@ --> diff --git a/lws-web/lws/templates/index.html b/lws-web/lws/templates/index.html index e16a947..c5eb6f6 100644 --- a/lws-web/lws/templates/index.html +++ b/lws-web/lws/templates/index.html @@ -2,32 +2,47 @@ {% block content %}

LWS Web Admin

-Manage Wallets -

LWS Admin: {{ config.LWS_ADMIN_URL }}

LWS RPC: {{ config.LWS_URL }}

-

Active Accounts

+

Accounts

+Add Wallet - + + + + + + + + + + + {% for address in data %} + {% set _data = data[address] %} - - - + + + + + - - - {% for a in lws_accounts %} - {% if a != 'hidden' %} - {% for acc in lws_accounts[a] %} - - - - - - {% endfor %} - {% endif %} - {% endfor %} - -
StatusWalletAddressDescriptionHeight
StatusAddressHeight + + + {% if _data['wallet'] %} + {{ data[address]['wallet'].name }} + {% else %} + add + {% endif %} + {{ address | shorten }} + {% if _data['wallet'] %} + {{ _data['wallet'].description or "-" }} + {% else %} + - + {% endif %} + {{ data[address]['scan_height'] }}
{{ a | upper }}{{ acc['address'] | shorten }}{{ acc['scan_height'] }}
+ {% endfor %} + + {% endblock %} - diff --git a/lws-web/lws/templates/wallet/add.html b/lws-web/lws/templates/wallet/add.html index ec1e44b..c981aa4 100644 --- a/lws-web/lws/templates/wallet/add.html +++ b/lws-web/lws/templates/wallet/add.html @@ -1,7 +1,7 @@ {% extends 'includes/base.html' %} {% block content %} -Go Back +Go Back

Add a Wallet

@@ -9,7 +9,7 @@ - + diff --git a/lws-web/lws/templates/wallet/list.html b/lws-web/lws/templates/wallet/list.html deleted file mode 100644 index 5b1135e..0000000 --- a/lws-web/lws/templates/wallet/list.html +++ /dev/null @@ -1,11 +0,0 @@ -{% extends 'includes/base.html' %} - -{% block content %} -

Manage Wallets

-Add a Wallet -
- {% for wallet in wallets %} -

{{ wallet.id }} - {{ wallet.name }} - {{ wallet.description }} - {{ wallet.date }}

- {% endfor %} -
-{% endblock %} \ No newline at end of file diff --git a/lws-web/lws/templates/wallet/show.html b/lws-web/lws/templates/wallet/show.html index b09c9e4..75d78e6 100644 --- a/lws-web/lws/templates/wallet/show.html +++ b/lws-web/lws/templates/wallet/show.html @@ -1,7 +1,7 @@ {% extends 'includes/base.html' %} {% block content %} -Go Back +Go Back {% set info = wallet.get_wallet_info() %}

{{ wallet.name }}

{{ wallet.description }}
@@ -11,11 +11,17 @@

Scanned Height: {{ info['scanned_height'] }} ({{ info['blockchain_height'] - info['scanned_height'] }} blocks away from top)

Chain Height: {{ info['blockchain_height'] }}

Spent Outputs: {{ info['spent_outputs'] | length }}

+{% if wallet.is_active() %} +

Status: active

+{% else %} +

Status: inactive

+{% endif %} + rescan -{% if request.args.get('disable') %} - are you sure? +{% if wallet.is_active() %} + disable {% else %} - disable + enable {% endif %} {% endblock %} \ No newline at end of file