diff --git a/templates/decoder/index.html b/templates/decoder/index.html index 4d31dbd..8f6d9e7 100644 --- a/templates/decoder/index.html +++ b/templates/decoder/index.html @@ -34,24 +34,32 @@ - - - - Invoice:{% raw %} {{ invoice }}{% endraw %} - - - - - LNURL:{% raw %} {{ lnurl }}{% endraw %} - - + + + + Invoice:{% raw %} {{ invoice }}{% endraw %} + + + + + LNURL:{% raw %} {{ lnurl }}{% endraw %} + + + + + Lightning Address:{% raw %} {{ lnaddress }}{% endraw %} + + + + + + {% raw %} {{ key }} {% endraw %}: {% raw %} {{ innerKey }} {% endraw %}:{% raw %} {{ innerValue }}{% endraw %} - @@ -60,24 +68,42 @@ {% raw %} {{ value }} {% endraw %} - - - - - - {% raw %} {{ key }} {% endraw %}: - - - {% raw %} {{ innerKey }} {% endraw %}:{% raw %} {{ innerValue }}{% endraw %} - - + + + + + + {% raw %} {{ key }} {% endraw %}: + + + {% raw %} {{ innerKey }} {% endraw %}:{% raw %} {{ innerValue }}{% endraw %} + + + + + {% raw %} {{ key }} {% endraw %}: + {% raw %} {{ value }} {% endraw %} + + - - {% raw %} {{ key }} {% endraw %}: - {% raw %} {{ value }} {% endraw %} + + + + + {% raw %} {{ key }} {% endraw %}: + + + {% raw %} {{ innerKey }} {% endraw %}:{% raw %} {{ innerValue }}{% endraw %} + + + + + {% raw %} {{ key }} {% endraw %}: + {% raw %} {{ value }} {% endraw %} + + - - + @@ -113,21 +139,54 @@ }, data: function () { return { - decoderData: '', input: '', invoice: '', + decoderData: '', lnurl: '', - lnaddress: '', lnurlData: '', + lnaddress: '', + lnaddressData: '', }; }, mounted() { }, methods: { sendFormData() { - var info = this.decoderFunction({"data": this.input}) + // check if its a ln address first + let url = this.isValidLNaddress(this.input) + console.log('LN address Result: ', url); + + if (url !='invalid') { + this.lnaddress = this.input + this.lnurl = '' + this.invoice = '' + this.decoderData = '' + this.lnurlData = '' + + this.getLNAddressData(url) + console.log("LN addressData", this.lnaddressData) + + } else { // try lnurl or bolt11 invoice + var info = this.decoderFunction({"data": this.input}) + } }, - getLNURLData: async function(url) { + isValidLNaddress: function(address) { + // Regular expression for email validation + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; + let isValid = emailRegex.test(address); + + if (isValid) { + console.log('LN address is valid'); + const [name, domain] = address.split('@'); + const url = `https://${domain}/.well-known/lnurlp/${name}`; + console.log("url", url) + return url; + } else { + console.log('Input is not a LN address'); + return 'invalid' + } + }, + getResponseData: async function(url) { // console.log("inside get LNURL Data") try { const response = await fetch(url) @@ -136,16 +195,23 @@ throw new Error('Network response was not ok'); } const data = await response.json() - // console.log("LNURL response object: ", data) - this.lnurlData = data + console.log("get ResponseData: ", data) + // this.lnurlData = data return data } catch (error){ console.error('There was a problem with the fetch operation:', error); } }, + getLNURLData: async function(data) { + this.lnurlData = await this.getResponseData(data) + console.log(" lnurl response: ", this.lnurlData) + }, + getLNAddressData: async function(data) { + this.lnaddressData = await this.getResponseData(data) + console.log('ln address response: ', this.lnaddressData) + }, decoderFunction: async function (data) { - // check if its a ln address first LNbits.api .request( 'POST', @@ -157,15 +223,19 @@ // console.log("decoderFunction response: ", response.data) this.decoderData = response.data + this.lnaddress = '' + this.lnaddressData = '' + if (this.decoderData.hasOwnProperty("domain")) { + // Handle LNURL // console.log("Key 'domain' exists! Is probably a lnurl"); this.lnurl = this.input this.invoice = '' let url = this.decoderData["domain"] // console.log("url: ", url) - let lnurlResponse = this.getLNURLData(url) - // console.log("decoder: lnurl response: ", lnurlResponse) - } else { + this.getLNURLData(url) + } else { + // Handle Invoice // console.log("Key 'domain' does not exists!"); this.invoice = this.input this.lnurl = ''