From 48cf23346c4bfe7d8ee2d303e9d2b2f5bc23c5b1 Mon Sep 17 00:00:00 2001 From: benarc Date: Wed, 2 Dec 2020 22:47:33 +0000 Subject: [PATCH] Added get fresh address button --- .../watchonly/templates/watchonly/index.html | 150 +++++++++++++++--- lnbits/extensions/watchonly/views_api.py | 27 ++-- 2 files changed, 143 insertions(+), 34 deletions(-) diff --git a/lnbits/extensions/watchonly/templates/watchonly/index.html b/lnbits/extensions/watchonly/templates/watchonly/index.html index 3db624d36..c798e7f7a 100644 --- a/lnbits/extensions/watchonly/templates/watchonly/index.html +++ b/lnbits/extensions/watchonly/templates/watchonly/index.html @@ -82,13 +82,14 @@ + @@ -298,14 +299,14 @@ - + - + @@ -313,7 +314,7 @@ @@ -321,14 +322,14 @@
Create Paylink @@ -379,16 +380,33 @@ >

-

- Table of addresses and amount will go here... + + + + + {{ data.address }} + + + + + +

- {% endraw %} +
Get fresh address @@ -396,7 +414,7 @@
- +{% endraw %}
{% endblock %} {% block scripts %} {{ window_vars(user) }} @@ -431,6 +449,7 @@ filter: '', checker: null, walletLinks: [], + current: {}, Addresses: { show: false, data: null @@ -513,7 +532,7 @@ show: false, data: {} }, - formDialogPayLink: { + formDialogPayment: { show: false, data: {} }, @@ -543,6 +562,24 @@ .catch(function (error) { LNbits.utils.notifyApiError(error) }) + }, + getFreshAddress: function (walletID) { + var self = this + + LNbits.api + .request( + 'GET', + '/watchonly/api/v1/address/' + walletID, + this.g.user.wallets[0].inkey + ) + .then(function (response) { + self.Addresses.show = false + getAddresses(walletID) + + }) + .catch(function (error) { + LNbits.utils.notifyApiError(error) + }) }, addressRedirect: function (address){ window.location.href = this.mempool.endpoint + "/address/" + address; @@ -610,6 +647,7 @@ openQrCodeDialog: function (linkId) { var getAddresses = this.getAddresses getAddresses(linkId) + this.current = linkId this.Addresses.show = true }, openUpdateDialog: function (linkId) { @@ -621,20 +659,86 @@ var wallet = this.g.user.wallets[0] var data = _.omit(this.formDialog.data, 'wallet') - data.wait_time = - data.wait_time * - { - seconds: 1, - minutes: 60, - hours: 3600 - }[this.formDialog.secondMultiplier] - if (data.id) { this.updateWalletLink(wallet, data) } else { this.createWalletLink(wallet, data) } }, + sendFormDataPayLink: function () { + var wallet = this.g.user.wallets[0] + var data = _.omit(this.formDialogPayLink.data, 'wallet') + + data.wait_time = + data.wait_time * + { + seconds: 1, + minutes: 60, + hours: 3600 + }[this.formDialogPayLink.secondMultiplier] + + if (data.id) { + this.updatePayLink(wallet, data) + } else { + this.createPayLink(wallet, data) + } + }, + updatePayment: function (wallet, data) { + var self = this + + LNbits.api + .request( + 'PUT', + '/watchonly/api/v1/payment/' + data.id, + wallet.inkey, data) + .then(function (response) { + self.payment = _.reject(self.payment, function (obj) { + return obj.id === data.id + }) + self.payment.push(mapWalletLink(response.data)) + self.formDialogPayLink.show = false + }) + .catch(function (error) { + LNbits.utils.notifyApiError(error) + }) + }, + createPayment: function (wallet, data) { + var self = this + + LNbits.api + .request('POST', '/watchonly/api/v1/payment', wallet.inkey, data) + .then(function (response) { + self.payment.push(mapWalletLink(response.data)) + self.formDialogPayLink.show = false + console.log(response.data[1][1]) + }) + .catch(function (error) { + LNbits.utils.notifyApiError(error) + }) + }, + deletePayment: function (linkId) { + var self = this + var link = _.findWhere(this.payment, {id: linkId}) + console.log(self.g.user.wallets[0].adminkey) + LNbits.utils + .confirmDialog('Are you sure you want to delete this pay link?') + .onOk(function () { + LNbits.api + .request( + 'DELETE', + '/watchonly/api/v1/payment/' + linkId, + self.g.user.wallets[0].inkey + ) + .then(function (response) { + self.payment = _.reject(self.payment, function (obj) { + return obj.id === linkId + })}) + .catch(function (error) { + LNbits.utils.notifyApiError(error) + }) + }) + }, + updateWalletLink: function (wallet, data) { var self = this diff --git a/lnbits/extensions/watchonly/views_api.py b/lnbits/extensions/watchonly/views_api.py index 77a4f0790..f06c11669 100644 --- a/lnbits/extensions/watchonly/views_api.py +++ b/lnbits/extensions/watchonly/views_api.py @@ -32,13 +32,10 @@ async def api_wallets_retrieve(): try: return ( - jsonify([wallet._asdict() for wallet in get_watch_wallets(g.wallet.user)]), HTTPStatus.OK + jsonify([wallet._asdict() for wallet in await get_watch_wallets(g.wallet.user)]), HTTPStatus.OK ) except: - return ( - jsonify({"message": "Cant fetch."}), - HTTPStatus.UPGRADE_REQUIRED, - ) + return "" @watchonly_ext.route("/api/v1/wallet/", methods=["GET"]) @api_check_wallet_key("invoice") @@ -91,21 +88,29 @@ async def api_wallet_delete(wallet_id): @watchonly_ext.route("/api/v1/address/", methods=["GET"]) @api_check_wallet_key("invoice") async def api_fresh_address(wallet_id): - address = await get_fresh_address(wallet_id) + await get_fresh_address(wallet_id) - if not address: - return jsonify({"message": "something went wrong"}), HTTPStatus.NOT_FOUND + addresses = await get_addresses(wallet_id) - return jsonify({address}), HTTPStatus.OK + return jsonify([address._asdict() for address in addresses]), HTTPStatus.OK @watchonly_ext.route("/api/v1/addresses/", methods=["GET"]) @api_check_wallet_key("invoice") async def api_get_addresses(wallet_id): - addresses = await get_addresses(wallet_id) - if not addresses: + 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 #############################PAYEMENTS##########################