From 0724893650ef2a91b874ca1835e60966d6919184 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Mon, 7 Nov 2022 16:16:15 +0100 Subject: [PATCH] camera for tokens --- .../cashu/templates/cashu/wallet.html | 178 +++++++++++------- 1 file changed, 105 insertions(+), 73 deletions(-) diff --git a/lnbits/extensions/cashu/templates/cashu/wallet.html b/lnbits/extensions/cashu/templates/cashu/wallet.html index a0861ae70..65e3690be 100644 --- a/lnbits/extensions/cashu/templates/cashu/wallet.html +++ b/lnbits/extensions/cashu/templates/cashu/wallet.html @@ -321,7 +321,8 @@ page_container %} @click="showInvoicesDialog" > - + +
@@ -512,11 +513,12 @@ page_container %} unelevated icon="photo_camera" class="q-mx-0" + v-if="hasCamera" @click="showCamera" > CancelClose
@@ -537,7 +539,7 @@ page_container %} - +
Receive Tokens + Close @@ -825,6 +834,11 @@ page_container %} bolt11: '', hash: '' }, + camera: { + data: null, + show: false, + camera: 'auto' + }, invoiceCheckListener: () => {}, payInvoiceData: { blocking: false, @@ -1089,10 +1103,15 @@ page_container %} return row.payment_hash + row.amount }, closeCamera: function () { - this.payInvoiceData.camera.show = false + this.camera.show = false }, showCamera: function () { - this.payInvoiceData.camera.show = true + this.camera.show = true + }, + hasCamera: function () { + navigator.permissions.query({name: 'camera'}).then(res => { + return res.state == 'granted' + }) }, showChart: function () { this.paymentsChart.show = true @@ -1124,7 +1143,7 @@ page_container %} this.payInvoiceData.data.request = '' this.payInvoiceData.data.comment = '' this.payInvoiceData.data.paymentChecker = null - this.payInvoiceData.camera.show = false + this.camera.show = false this.focusInput('pasteInput') }, showDisclaimerDialog: function () { @@ -1203,83 +1222,96 @@ page_container %} }) }, decodeQR: function (res) { - this.payInvoiceData.data.request = res + this.camera.data = res + // this.payInvoiceData.data.request = res this.decodeRequest() - this.payInvoiceData.camera.show = false + this.camera.show = false }, decodeRequest: function () { - this.payInvoiceData.show = true - let req = this.payInvoiceData.data.request.toLowerCase() - if ( - this.payInvoiceData.data.request - .toLowerCase() - .startsWith('lightning:') - ) { - this.payInvoiceData.data.request = this.payInvoiceData.data.request.slice( - 10 - ) - } else if ( - this.payInvoiceData.data.request.toLowerCase().startsWith('lnurl:') - ) { - this.payInvoiceData.data.request = this.payInvoiceData.data.request.slice( - 6 - ) + // let req = this.payInvoiceData.data.request.toLowerCase() + reqtype = null + let req = this.camera.data + if (req.toLowerCase().startsWith('lnbc')) { + this.payInvoiceData.data.request = req + reqtype = 'bolt11' + } else if (req.toLowerCase().startsWith('lightning:')) { + this.payInvoiceData.data.request = req.slice(10) + reqtype = 'bolt11' + } else if (req.toLowerCase().startsWith('lnurl:')) { + this.payInvoiceData.data.request = req.slice(6) + reqtype = 'lnurl' } else if (req.indexOf('lightning=lnurl1') !== -1) { - this.payInvoiceData.data.request = this.payInvoiceData.data.request + this.payInvoiceData.data.request = req .split('lightning=')[1] .split('&')[0] - } - - if ( - this.payInvoiceData.data.request.toLowerCase().startsWith('lnurl1') || - this.payInvoiceData.data.request.match(/[\w.+-~_]+@[\w.+-~_]/) + reqtype = 'lnurl' + } else if ( + req.toLowerCase().startsWith('lnurl1') || + req.match(/[\w.+-~_]+@[\w.+-~_]/) ) { + this.payInvoiceData.data.request = req + reqtype = 'lnurl' return + } else if (req.indexOf('W3siaWQ') !== 1) { + // very dirty way of parsing cashu tokens + + this.receiveData.tokensBase64 = req.slice(req.indexOf('W3siaWQ')) + reqtype = 'cashu' } - let invoice - try { - invoice = decode(this.payInvoiceData.data.request) - } catch (error) { - this.$q.notify({ - timeout: 3000, - type: 'warning', - message: error + '.', - caption: '400 BAD REQUEST' - }) - this.payInvoiceData.show = false - throw error - return - } - - let cleanInvoice = { - msat: invoice.human_readable_part.amount, - sat: invoice.human_readable_part.amount / 1000, - fsat: LNbits.utils.formatSat( - invoice.human_readable_part.amount / 1000 - ) - } - - _.each(invoice.data.tags, tag => { - if (_.isObject(tag) && _.has(tag, 'description')) { - if (tag.description === 'payment_hash') { - cleanInvoice.hash = tag.value - } else if (tag.description === 'description') { - cleanInvoice.description = tag.value - } else if (tag.description === 'expiry') { - var expireDate = new Date( - (invoice.data.time_stamp + tag.value) * 1000 - ) - cleanInvoice.expireDate = Quasar.utils.date.formatDate( - expireDate, - 'YYYY-MM-DDTHH:mm:ss.SSSZ' - ) - cleanInvoice.expired = false // TODO - } + if (reqtype == 'bolt11') { + console.log('#### QR CODE: BOLT11') + this.payInvoiceData.show = true + let invoice + try { + invoice = decode(this.payInvoiceData.data.request) + } catch (error) { + this.$q.notify({ + timeout: 3000, + type: 'warning', + message: error + '.', + caption: '400 BAD REQUEST' + }) + this.payInvoiceData.show = false + throw error + return } - }) - this.payInvoiceData.invoice = Object.freeze(cleanInvoice) + let cleanInvoice = { + msat: invoice.human_readable_part.amount, + sat: invoice.human_readable_part.amount / 1000, + fsat: LNbits.utils.formatSat( + invoice.human_readable_part.amount / 1000 + ) + } + + _.each(invoice.data.tags, tag => { + if (_.isObject(tag) && _.has(tag, 'description')) { + if (tag.description === 'payment_hash') { + cleanInvoice.hash = tag.value + } else if (tag.description === 'description') { + cleanInvoice.description = tag.value + } else if (tag.description === 'expiry') { + var expireDate = new Date( + (invoice.data.time_stamp + tag.value) * 1000 + ) + cleanInvoice.expireDate = Quasar.utils.date.formatDate( + expireDate, + 'YYYY-MM-DDTHH:mm:ss.SSSZ' + ) + cleanInvoice.expired = false // TODO + } + } + }) + + this.payInvoiceData.invoice = Object.freeze(cleanInvoice) + } else if (reqtype == 'lnurl') { + console.log('#### QR CODE: LNURL') + // not supported yet + } else if (reqtype == 'cashu') { + console.log('#### QR CODE: CASHU TOKEN') + this.showReceiveTokens = true + } }, payInvoice: function () { let dismissPaymentMsg = this.$q.notify({ @@ -1348,7 +1380,7 @@ page_container %} this.payInvoiceData.invoice = '' this.payInvoiceData.data.request = '' this.showPayInvoice = true - this.payInvoiceData.camera.show = false + this.camera.show = false }, showTokenDialog: function (token) {