diff --git a/__init__.py b/__init__.py index cc58200..ae8bf9c 100644 --- a/__init__.py +++ b/__init__.py @@ -17,5 +17,6 @@ __all__ = [ "db", + "decoder_ext", "decoder_static_files", ] diff --git a/config.json b/config.json index fd185e8..ada80ad 100644 --- a/config.json +++ b/config.json @@ -9,7 +9,7 @@ "role": "Lead dev" } ], - "version": "1.0.5", - "min_lnbits_version": "1.3.0", + "version": "1.1.0", + "min_lnbits_version": "1.5.0", "license": "MIT" } diff --git a/static/js/index.js b/static/js/index.js new file mode 100644 index 0000000..b5d773e --- /dev/null +++ b/static/js/index.js @@ -0,0 +1,120 @@ +window.PageDecoder = { + template: '#page-decoder', + data() { + return { + input: '', + invoice: '', + decoderData: '', + lnurl: '', + lnurlData: '', + lnaddress: '', + lnaddressData: '', + invoiceData: '' + } + }, + methods: { + blankallFields() { + this.invoice = '' + this.invoiceData = '' + this.lnurl = '' + this.lnurlData = '' + this.lnaddress = '' + this.lnaddressData = '' + }, + sendFormData() { + let url = this.isValidLNaddress(this.input) + if (url != 'invalid') { + this.lnaddress = this.input + this.invoice = '' + this.invoiceData = '' + this.lnurl = '' + this.lnurlData = '' + this.decoderData = '' + this.getLNAddressData(url) + } else { + this.decoderFunction({data: this.input}) + } + }, + isValidLNaddress(address) { + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ + let isValid = emailRegex.test(address) + if (isValid) { + const [name, domain] = address.split('@') + const url = `https://${domain}/.well-known/lnurlp/${name}` + return url + } else { + return 'invalid' + } + }, + async getResponseData(url) { + try { + const response = await fetch(url) + if (!response.ok) { + throw new Error('Network response was not ok') + } + const data = await response.json() + return data + } catch (error) { + console.error('There was a problem with the fetch operation:', error) + } + }, + async formatJSONToHTML(jsonData) { + let html = '' + return html + }, + async getLNURLData(data) { + let response = await this.getResponseData(data) + this.lnurlData = await this.formatJSONToHTML(response) + }, + async getLNAddressData(data) { + let response = await this.getResponseData(data) + if (response === undefined) { + this.lnaddressData = await this.formatJSONToHTML({ + error: 'Lightning address not valid' + }) + return + } + this.lnaddressData = await this.formatJSONToHTML(response) + }, + async getInvoiceData(data) { + this.invoiceData = await this.formatJSONToHTML(data) + }, + async decoderFunction(data) { + LNbits.api + .request( + 'POST', + '/api/v1/payments/decode', + this.g.user.wallets[0].inkey, + data + ) + .then(async response => { + this.decoderData = response.data + this.blankallFields() + if (this.decoderData.hasOwnProperty('domain')) { + this.lnurl = this.input + let url = this.decoderData['domain'] + this.getLNURLData(url) + } else { + this.invoice = this.input + this.getInvoiceData(response.data) + } + }) + .catch(error => { + LNbits.utils.notifyApiError(error) + }) + }, + isObject(val) { + return val !== null && typeof val === 'object' && !Array.isArray(val) + } + } +} diff --git a/static/js/index.vue b/static/js/index.vue new file mode 100644 index 0000000..dd73e78 --- /dev/null +++ b/static/js/index.vue @@ -0,0 +1,90 @@ + diff --git a/static/routes.json b/static/routes.json new file mode 100644 index 0000000..44367e1 --- /dev/null +++ b/static/routes.json @@ -0,0 +1,8 @@ +[ + { + "path": "/decoder/", + "name": "PageDecoder", + "template": "/decoder/static/js/index.vue", + "component": "/decoder/static/js/index.js" + } +] diff --git a/views.py b/views.py index 0c0872d..a9eba9b 100644 --- a/views.py +++ b/views.py @@ -1,22 +1,12 @@ -from fastapi import APIRouter, Depends, Request -from fastapi.responses import HTMLResponse -from lnbits.core.models import User -from lnbits.decorators import check_user_exists -from lnbits.helpers import template_renderer - - -def decoder_renderer(): - return template_renderer(["decoder/templates"]) - +from fastapi import APIRouter, Depends +from lnbits.core.views.generic import index +from lnbits.decorators import check_account_id_exists decoder_generic_router: APIRouter = APIRouter() - -@decoder_generic_router.get("/", response_class=HTMLResponse) -async def index( - request: Request, - user: User = Depends(check_user_exists), -): - return decoder_renderer().TemplateResponse( - "decoder/index.html", {"request": request, "user": user.json()} - ) +decoder_generic_router.add_api_route( + "/", + methods=["GET"], + endpoint=index, + dependencies=[Depends(check_account_id_exists)], +)