lnbits-legend/lnbits/static/js/wallet.js

915 lines
27 KiB
JavaScript
Raw Normal View History

2020-10-13 13:57:26 -03:00
/* globals windowMixin, decode, Vue, VueQrcodeReader, VueQrcode, Quasar, LNbits, _, EventHub, Chart, decryptLnurlPayAES */
2020-05-03 13:55:17 -03:00
Vue.component(VueQrcode.name, VueQrcode)
Vue.use(VueQrcodeReader)
2020-03-05 20:29:27 +01:00
function generateChart(canvas, rawData) {
const data = rawData.reduce(
(previous, current) => {
previous.labels.push(current.date)
previous.income.push(current.income)
previous.spending.push(current.spending)
previous.cumulative.push(current.balance)
return previous
},
{
labels: [],
income: [],
spending: [],
cumulative: []
2020-05-03 13:55:17 -03:00
}
)
2020-03-05 20:29:27 +01:00
return new Chart(canvas.getContext('2d'), {
2020-03-05 20:29:27 +01:00
type: 'bar',
data: {
labels: data.labels,
datasets: [
{
data: data.cumulative,
type: 'line',
label: 'balance',
2020-05-03 13:55:17 -03:00
backgroundColor: '#673ab7', // deep-purple
borderColor: '#673ab7',
2020-03-05 20:29:27 +01:00
borderWidth: 4,
pointRadius: 3,
fill: false
},
{
data: data.income,
2020-03-05 20:29:27 +01:00
type: 'bar',
label: 'in',
barPercentage: 0.75,
backgroundColor: window.Color('rgb(76,175,80)').alpha(0.5).rgbString() // green
},
{
data: data.spending,
type: 'bar',
label: 'out',
barPercentage: 0.75,
backgroundColor: window.Color('rgb(233,30,99)').alpha(0.5).rgbString() // pink
2020-03-05 20:29:27 +01:00
}
]
},
options: {
title: {
text: 'Chart.js Combo Time Scale'
},
tooltips: {
mode: 'index',
2020-05-03 13:55:17 -03:00
intersect: false
2020-03-05 20:29:27 +01:00
},
scales: {
2020-05-03 13:55:17 -03:00
xAxes: [
{
type: 'time',
display: true,
//offset: true,
2020-05-03 13:55:17 -03:00
time: {
minUnit: 'hour',
stepSize: 3
}
2020-03-05 20:29:27 +01:00
}
2020-05-03 13:55:17 -03:00
]
2020-03-05 20:29:27 +01:00
},
// performance tweaks
animation: {
duration: 0
},
elements: {
line: {
tension: 0
}
}
2020-03-05 20:29:27 +01:00
}
2020-05-03 13:55:17 -03:00
})
2020-03-05 20:29:27 +01:00
}
new Vue({
el: '#vue',
mixins: [windowMixin],
data: function () {
return {
user: LNbits.map.user(window.user),
receive: {
show: false,
status: 'pending',
paymentReq: null,
paymentHash: null,
2020-10-11 22:19:27 -03:00
minMax: [0, 2100000000000000],
lnurl: null,
units: ['sat'],
unit: 'sat',
data: {
amount: null,
memo: ''
}
},
2020-10-11 22:19:27 -03:00
parse: {
show: false,
invoice: null,
2020-10-11 22:19:27 -03:00
lnurlpay: null,
lnurlauth: null,
data: {
2020-10-11 22:19:27 -03:00
request: '',
2020-10-13 15:18:34 -03:00
amount: 0,
comment: '',
unit: 'sat'
2020-10-11 22:19:27 -03:00
},
paymentChecker: null,
copy: {
show: false
},
2020-10-11 22:19:27 -03:00
camera: {
show: false,
camera: 'auto'
}
},
payments: [],
paymentsTable: {
columns: [
{
name: 'time',
align: 'left',
label: this.$t('memo') + '/' + this.$t('date'),
2020-05-03 13:55:17 -03:00
field: 'date',
sortable: true
},
{
name: 'amount',
2020-05-03 13:55:17 -03:00
align: 'right',
label: this.$t('amount') + ' (' + LNBITS_DENOMINATION + ')',
2020-05-03 13:55:17 -03:00
field: 'sat',
sortable: true
}
],
pagination: {
rowsPerPage: 10,
page: 1,
sortBy: 'time',
descending: true,
rowsNumber: 10
},
filter: null,
loading: false
2020-03-05 20:29:27 +01:00
},
paymentsCSV: {
columns: [
{
name: 'pending',
align: 'left',
label: 'Pending',
field: 'pending'
},
{
name: 'memo',
align: 'left',
label: this.$t('memo'),
field: 'memo'
},
{
name: 'time',
align: 'left',
label: this.$t('date'),
field: 'date',
sortable: true
},
{
name: 'amount',
align: 'right',
label: this.$t('amount') + ' (' + LNBITS_DENOMINATION + ')',
field: 'sat',
sortable: true
},
{
name: 'fee',
align: 'right',
label: this.$t('fee') + ' (m' + LNBITS_DENOMINATION + ')',
field: 'fee'
},
{
name: 'tag',
align: 'right',
label: this.$t('tag'),
field: 'tag'
},
{
name: 'payment_hash',
align: 'right',
label: this.$t('payment_hash'),
field: 'payment_hash'
},
{
name: 'payment_proof',
align: 'right',
label: this.$t('payment_proof'),
field: 'payment_proof'
},
{
name: 'webhook',
align: 'right',
label: this.$t('webhook'),
field: 'webhook'
},
{
name: 'fiat_currency',
align: 'right',
label: 'Fiat Currency',
field: row => row.extra.wallet_fiat_currency
},
{
name: 'fiat_amount',
align: 'right',
label: 'Fiat Amount',
field: row => row.extra.wallet_fiat_amount
}
],
filter: null,
loading: false
},
paymentsChart: {
show: false,
group: {value: 'hour', label: 'Hour'},
groupOptions: [
{value: 'month', label: 'Month'},
{value: 'day', label: 'Day'},
{value: 'hour', label: 'Hour'}
],
instance: null
},
disclaimerDialog: {
show: false,
location: window.location
2020-09-29 20:04:02 -03:00
},
2021-08-06 11:15:07 +01:00
balance: 0,
fiatBalance: 0,
mobileSimple: false,
credit: 0,
update: {
name: null,
currency: null
}
2020-05-03 13:55:17 -03:00
}
},
computed: {
2020-09-29 20:04:02 -03:00
formattedBalance: function () {
if (LNBITS_DENOMINATION != 'sats') {
return this.balance / 100
} else {
return LNbits.utils.formatSat(this.balance || this.g.wallet.sat)
}
2020-09-29 20:04:02 -03:00
},
formattedFiatBalance() {
if (this.fiatBalance) {
return LNbits.utils.formatCurrency(
this.fiatBalance.toFixed(2),
this.g.wallet.currency
)
}
},
filteredPayments: function () {
2020-05-03 13:55:17 -03:00
var q = this.paymentsTable.filter
if (!q || q === '') return this.payments
2020-05-03 13:55:17 -03:00
return LNbits.utils.search(this.payments, q)
},
paymentsOmitter() {
if (this.$q.screen.lt.md && this.mobileSimple) {
return this.payments.length > 0 ? [this.payments[0]] : []
}
return this.payments
},
canPay: function () {
2020-10-11 22:19:27 -03:00
if (!this.parse.invoice) return false
return this.parse.invoice.sat <= this.balance
},
pendingPaymentsExist: function () {
2021-06-21 23:22:52 -03:00
return this.payments.findIndex(payment => payment.pending) !== -1
}
},
methods: {
2020-10-11 22:19:27 -03:00
msatoshiFormat: function (value) {
return LNbits.utils.formatSat(value / 1000)
},
2020-11-19 14:23:35 -03:00
paymentTableRowKey: function (row) {
return row.payment_hash + row.amount
},
closeCamera: function () {
2020-10-11 22:19:27 -03:00
this.parse.camera.show = false
2020-03-10 23:12:22 +01:00
},
showCamera: function () {
2020-10-11 22:19:27 -03:00
this.parse.camera.show = true
2020-03-10 23:12:22 +01:00
},
showChart: function () {
2020-05-03 13:55:17 -03:00
this.paymentsChart.show = true
LNbits.api
.request(
'GET',
'/api/v1/payments/history?group=' + this.paymentsChart.group.value,
this.g.wallet.adminkey
)
.then(response => {
this.$nextTick(() => {
if (this.paymentsChart.instance) {
this.paymentsChart.instance.destroy()
}
this.paymentsChart.instance = generateChart(
this.$refs.canvas,
response.data
)
})
})
.catch(err => {
LNbits.utils.notifyApiError(err)
this.paymentsChart.show = false
})
},
2022-08-21 22:07:18 +01:00
focusInput(el) {
this.$nextTick(() => this.$refs[el].focus())
},
showReceiveDialog: function () {
2020-10-11 22:19:27 -03:00
this.receive.show = true
this.receive.status = 'pending'
this.receive.paymentReq = null
this.receive.paymentHash = null
2020-10-11 22:19:27 -03:00
this.receive.data.amount = null
this.receive.data.memo = null
this.receive.unit = 'sat'
2020-10-11 22:19:27 -03:00
this.receive.minMax = [0, 2100000000000000]
this.receive.lnurl = null
2022-08-21 22:07:18 +01:00
this.focusInput('setAmount')
},
2020-10-11 22:19:27 -03:00
showParseDialog: function () {
this.parse.show = true
this.parse.invoice = null
this.parse.lnurlpay = null
this.parse.lnurlauth = null
this.parse.copy.show =
window.isSecureContext && navigator.clipboard?.readText !== undefined
2020-10-11 22:19:27 -03:00
this.parse.data.request = ''
2020-10-13 15:18:34 -03:00
this.parse.data.comment = ''
2020-10-11 22:19:27 -03:00
this.parse.data.paymentChecker = null
this.parse.camera.show = false
this.focusInput('textArea')
},
updateBalance: function (credit) {
2022-12-16 10:29:53 +02:00
LNbits.api
.request(
'PUT',
[FEAT] Auth, Login, OAuth, create account with username and password #1653 (#2092) no more superuser url! delete cookie on logout add usr login feature fix node management * Cleaned up login form * CreateUser * information leak * cleaner parsing usr from url * rename decorators * login secret * fix: add back `superuser` command * chore: remove `fastapi_login` * fix: extract `token` from cookie * chore: prepare to extract user * feat: check user * chore: code clean-up * feat: happy flow working * fix: usr only login * fix: user already logged in * feat: check user in URL * fix: verify password at DB level * fix: do not show `Login` controls if user already logged in * fix: separate login endpoints * fix: remove `usr` param * chore: update error message * refactor: register method * feat: logout * chore: move comments * fix: remove user auth check from API * fix: user check unnecessary * fix: redirect after logout * chore: remove garbage files * refactor: simplify constructor call * fix: hide user icon if not authorized * refactor: rename auth env vars * chore: code clean-up * fix: add types for `python-jose` * fix: add types for `passlib` * fix: return type * feat: set default value for `auth_secret_key` to hash of super user * fix: default value * feat: rework login page * feat: ui polishing * feat: google auth * feat: add google auth * chore: remove `authlib` dependency * refactor: extract `_handle_sso_login` method * refactor: convert methods to `properties` * refactor: rename: `user_api` to `auth_api` * feat: store user info from SSO * chore: re-arange the buttons * feat: conditional rendering of login options * feat: correctly render buttons * fix: re-add `Claim Bitcoin` from the main page * fix: create wallet must send new user * fix: no `username-password` auth method * refactor: rename auth method * fix: do not force API level UUID4 validation * feat: add validation for username * feat: add account page * feat: update account * feat: add `has_password` for user * fix: email not editable * feat: validate email for existing account * fix: register check * feat: reset password * chore: code clean-up * feat: handle token expired * fix: only redirect if `text/html` * refactor: remove `OAuth2PasswordRequestForm` * chore: remove `python-multipart` dependency * fix: handle no headers for exception * feat: add back button on error screen * feat: show user profile image * fix: check account creation permissions * fix: auth for internal api call * chore: add some docs * chore: code clean-up * fix: rebase stuff * fix: default value types * refactor: customize error messages * fix: move types libs to dev dependencies * doc: specify the `Authorization callback URL` * fix: pass missing superuser id in node ui test * fix: keep usr param on wallet redirect removing usr param causes an issue if the browser doesnt yet have an access token. * fix: do not redirect if `wal` query param not present * fix: add nativeBuildInputs and buildInputs overrides to flake.nix * bump fastapi-sso to 0.9.0 which fixes some security issues * refactor: move the `lnbits_admin_extensions` to decorators * chore: bring package config from `dev` * chore: re-add dependencies * chore: re-add cev dependencies * chore: re-add mypy ignores * feat: i18n * refactor: move admin ext check to decorator (fix after rebase) * fix: label mapping * fix: re-fetch user after first wallet was created * fix: unlikely case that `user` is not found * refactor translations (move '*' to code) * reorganize deps in pyproject.toml, add comment * update flake.lock and simplify flake.nix after upstreaming overrides for fastapi-sso, types-passlib, types-pyasn1, types-python-jose were upstreamed in https://github.com/nix-community/poetry2nix/pull/1463 * fix: more relaxed email verification (by @prusnak) * fix: remove `\b` (boundaries) since we re using `fullmatch` * chore: `make bundle` --------- Co-authored-by: dni âš¡ <office@dnilabs.com> Co-authored-by: Arc <ben@arc.wales> Co-authored-by: jackstar12 <jkranawetter05@gmail.com> Co-authored-by: Pavol Rusnak <pavol@rusnak.io>
2023-12-12 12:38:19 +02:00
'/admin/api/v1/topup/',
2022-12-16 10:29:53 +02:00
this.g.user.wallets[0].adminkey,
{
amount: credit,
2023-02-23 11:40:24 +01:00
id: this.g.wallet.id
2022-12-16 10:29:53 +02:00
}
)
.then(response => {
this.$q.notify({
type: 'positive',
message:
'Success! Added ' +
credit +
' sats to ' +
this.g.user.wallets[0].id,
icon: null
})
2022-12-16 10:29:53 +02:00
this.balance += parseInt(credit)
})
.catch(function (error) {
LNbits.utils.notifyApiError(error)
})
2022-01-31 16:29:42 +00:00
},
2020-10-11 22:19:27 -03:00
closeParseDialog: function () {
setTimeout(() => {
clearInterval(this.parse.paymentChecker)
2020-10-13 13:57:26 -03:00
}, 10000)
2020-03-10 23:12:22 +01:00
},
onPaymentReceived: function (paymentHash) {
this.fetchPayments()
this.fetchBalance()
if (this.receive.paymentHash === paymentHash) {
this.receive.show = false
this.receive.paymentHash = null
}
},
createInvoice: function () {
2020-05-03 13:55:17 -03:00
this.receive.status = 'loading'
if (LNBITS_DENOMINATION != 'sats') {
this.receive.data.amount = this.receive.data.amount * 100
}
2020-05-03 13:55:17 -03:00
LNbits.api
.createInvoice(
this.g.wallet,
this.receive.data.amount,
this.receive.data.memo,
this.receive.unit,
this.receive.lnurl && this.receive.lnurl.callback
2020-05-03 13:55:17 -03:00
)
2020-10-11 22:19:27 -03:00
.then(response => {
this.receive.status = 'success'
this.receive.paymentReq = response.data.payment_request
this.receive.paymentHash = response.data.payment_hash
2020-10-11 22:19:27 -03:00
if (response.data.lnurl_response !== null) {
if (response.data.lnurl_response === false) {
response.data.lnurl_response = `Unable to connect`
}
if (typeof response.data.lnurl_response === 'string') {
// failure
this.$q.notify({
timeout: 5000,
type: 'warning',
message: `${this.receive.lnurl.domain} lnurl-withdraw call failed.`,
caption: response.data.lnurl_response
})
return
} else if (response.data.lnurl_response === true) {
// success
this.$q.notify({
timeout: 5000,
message: `Invoice sent to ${this.receive.lnurl.domain}!`,
spinner: true
})
}
2020-10-11 22:19:27 -03:00
}
this.fetchPayments()
2020-05-03 13:55:17 -03:00
})
2020-10-11 22:19:27 -03:00
.catch(err => {
LNbits.utils.notifyApiError(err)
this.receive.status = 'pending'
2020-05-03 13:55:17 -03:00
})
},
onInitQR: async function (promise) {
try {
await promise
} catch (error) {
let mapping = {
NotAllowedError: 'ERROR: you need to grant camera access permission',
NotFoundError: 'ERROR: no camera on this device',
NotSupportedError:
'ERROR: secure context required (HTTPS, localhost)',
NotReadableError: 'ERROR: is the camera already in use?',
OverconstrainedError: 'ERROR: installed cameras are not suitable',
StreamApiNotSupportedError:
'ERROR: Stream API is not supported in this browser',
InsecureContextError:
'ERROR: Camera access is only permitted in secure context. Use HTTPS or localhost rather than HTTP.'
}
let valid_error = Object.keys(mapping).filter(key => {
return error.name === key
})
let camera_error = valid_error
? mapping[valid_error]
: `ERROR: Camera error (${error.name})`
this.parse.camera.show = false
this.$q.notify({
message: camera_error,
type: 'negative'
})
}
},
decodeQR: function (res) {
2020-10-11 22:19:27 -03:00
this.parse.data.request = res
this.decodeRequest()
2020-10-11 22:19:27 -03:00
this.parse.camera.show = false
},
decodeRequest: function () {
2020-10-11 22:19:27 -03:00
this.parse.show = true
let req = this.parse.data.request.toLowerCase()
2022-09-12 10:17:45 +02:00
if (this.parse.data.request.toLowerCase().startsWith('lightning:')) {
2020-10-11 22:19:27 -03:00
this.parse.data.request = this.parse.data.request.slice(10)
2022-09-12 10:17:45 +02:00
} else if (this.parse.data.request.toLowerCase().startsWith('lnurl:')) {
2020-10-11 22:19:27 -03:00
this.parse.data.request = this.parse.data.request.slice(6)
} else if (req.indexOf('lightning=lnurl1') !== -1) {
this.parse.data.request = this.parse.data.request
.split('lightning=')[1]
.split('&')[0]
}
2020-09-20 23:50:02 -03:00
2021-07-30 21:01:19 -03:00
if (
this.parse.data.request.toLowerCase().startsWith('lnurl1') ||
this.parse.data.request.match(/[\w.+-~_]+@[\w.+-~_]/)
) {
2020-09-20 23:50:02 -03:00
LNbits.api
.request(
'GET',
2020-10-11 22:19:27 -03:00
'/api/v1/lnurlscan/' + this.parse.data.request,
this.g.wallet.adminkey
)
2020-10-11 22:19:27 -03:00
.catch(err => {
LNbits.utils.notifyApiError(err)
2020-09-20 23:50:02 -03:00
})
2020-10-11 22:19:27 -03:00
.then(response => {
let data = response.data
if (data.status === 'ERROR') {
this.$q.notify({
2020-10-11 22:19:27 -03:00
timeout: 5000,
type: 'warning',
message: `${data.domain} lnurl call failed.`,
caption: data.reason
2020-10-11 22:19:27 -03:00
})
return
}
if (data.kind === 'pay') {
this.parse.lnurlpay = Object.freeze(data)
this.parse.data.amount = data.minSendable / 1000
} else if (data.kind === 'auth') {
this.parse.lnurlauth = Object.freeze(data)
2020-10-11 22:19:27 -03:00
} else if (data.kind === 'withdraw') {
this.parse.show = false
this.receive.show = true
this.receive.status = 'pending'
this.receive.paymentReq = null
this.receive.paymentHash = null
this.receive.data.amount = data.maxWithdrawable / 1000
2020-10-11 22:19:27 -03:00
this.receive.data.memo = data.defaultDescription
this.receive.minMax = [
data.minWithdrawable / 1000,
data.maxWithdrawable / 1000
]
2020-10-11 22:19:27 -03:00
this.receive.lnurl = {
domain: data.domain,
callback: data.callback,
fixed: data.fixed
}
}
2020-09-20 23:50:02 -03:00
})
return
2020-04-27 23:13:42 +02:00
}
2023-04-18 17:02:49 +01:00
// BIP-21 support
if (this.parse.data.request.toLowerCase().includes('lightning')) {
2023-04-18 15:39:53 +01:00
this.parse.data.request = this.parse.data.request.split('lightning=')[1]
2023-04-18 17:02:49 +01:00
2023-04-18 15:39:53 +01:00
// fail safe to check there's nothing after the lightning= part
2023-04-18 17:02:49 +01:00
if (this.parse.data.request.includes('&')) {
2023-04-18 15:39:53 +01:00
this.parse.data.request = this.parse.data.request.split('&')[0]
}
}
2020-05-03 13:55:17 -03:00
let invoice
try {
invoice = decode(this.parse.data.request)
} catch (error) {
this.$q.notify({
timeout: 3000,
type: 'warning',
message: error + '.',
caption: '400 BAD REQUEST'
2020-05-03 13:55:17 -03:00
})
2020-10-11 22:19:27 -03:00
this.parse.show = false
2020-05-03 13:55:17 -03:00
return
}
2020-04-27 23:13:42 +02:00
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)
2020-05-03 13:55:17 -03:00
}
2020-10-11 22:19:27 -03:00
_.each(invoice.data.tags, tag => {
if (_.isObject(tag) && _.has(tag, 'description')) {
if (tag.description === 'payment_hash') {
2020-05-03 13:55:17 -03:00
cleanInvoice.hash = tag.value
} else if (tag.description === 'description') {
2020-05-03 13:55:17 -03:00
cleanInvoice.description = tag.value
} else if (tag.description === 'expiry') {
2020-05-03 13:55:17 -03:00
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
}
}
2020-05-03 13:55:17 -03:00
})
2020-10-11 22:19:27 -03:00
this.parse.invoice = Object.freeze(cleanInvoice)
},
payInvoice: function () {
2020-10-11 22:19:27 -03:00
let dismissPaymentMsg = this.$q.notify({
timeout: 0,
message: this.$t('processing_payment')
2020-10-11 22:19:27 -03:00
})
2020-10-11 22:19:27 -03:00
LNbits.api
.payInvoice(this.g.wallet, this.parse.data.request)
2020-10-11 22:19:27 -03:00
.then(response => {
clearInterval(this.parse.paymentChecker)
setTimeout(() => {
clearInterval(this.parse.paymentChecker)
}, 40000)
2020-10-11 22:19:27 -03:00
this.parse.paymentChecker = setInterval(() => {
LNbits.api
.getPayment(this.g.wallet, response.data.payment_hash)
.then(res => {
if (res.data.paid) {
this.parse.show = false
clearInterval(this.parse.paymentChecker)
dismissPaymentMsg()
this.fetchPayments()
this.fetchBalance()
2020-10-11 22:19:27 -03:00
}
})
}, 2000)
})
.catch(err => {
dismissPaymentMsg()
LNbits.utils.notifyApiError(err)
})
},
payLnurl: function () {
let dismissPaymentMsg = this.$q.notify({
timeout: 0,
message: 'Processing payment...'
2020-05-03 13:55:17 -03:00
})
2020-05-03 13:55:17 -03:00
LNbits.api
.payLnurl(
this.g.wallet,
this.parse.lnurlpay.callback,
this.parse.lnurlpay.description_hash,
this.parse.data.amount * 1000,
2020-10-13 15:18:34 -03:00
this.parse.lnurlpay.description.slice(0, 120),
this.parse.data.comment,
this.parse.data.unit
)
2020-10-11 22:19:27 -03:00
.then(response => {
this.parse.show = false
clearInterval(this.parse.paymentChecker)
setTimeout(() => {
clearInterval(this.parse.paymentChecker)
}, 40000)
2020-10-11 22:19:27 -03:00
this.parse.paymentChecker = setInterval(() => {
2020-05-03 13:55:17 -03:00
LNbits.api
2020-10-11 22:19:27 -03:00
.getPayment(this.g.wallet, response.data.payment_hash)
.then(res => {
2020-05-03 13:55:17 -03:00
if (res.data.paid) {
dismissPaymentMsg()
clearInterval(this.parse.paymentChecker)
2020-10-11 22:19:27 -03:00
this.fetchPayments()
this.fetchBalance()
// show lnurlpay success action
if (response.data.success_action) {
switch (response.data.success_action.tag) {
case 'url':
this.$q.notify({
2020-10-13 13:57:26 -03:00
message: `<a target="_blank" style="color: inherit" href="${response.data.success_action.url}">${response.data.success_action.url}</a>`,
caption: response.data.success_action.description,
html: true,
type: 'positive',
timeout: 0,
closeBtn: true
})
break
case 'message':
this.$q.notify({
message: response.data.success_action.message,
type: 'positive',
timeout: 0,
closeBtn: true
})
break
case 'aes':
2020-10-13 13:57:26 -03:00
LNbits.api
.getPayment(this.g.wallet, response.data.payment_hash)
.then(({data: payment}) =>
decryptLnurlPayAES(
response.data.success_action,
payment.preimage
)
2020-10-13 13:57:26 -03:00
)
.then(value => {
this.$q.notify({
message: value,
caption: response.data.success_action.description,
html: true,
type: 'positive',
2020-10-13 13:57:26 -03:00
timeout: 0,
closeBtn: true
})
})
break
}
}
2020-05-03 13:55:17 -03:00
}
})
}, 2000)
})
2020-10-11 22:19:27 -03:00
.catch(err => {
2020-05-03 13:55:17 -03:00
dismissPaymentMsg()
2020-10-11 22:19:27 -03:00
LNbits.utils.notifyApiError(err)
2020-05-03 13:55:17 -03:00
})
},
authLnurl: function () {
let dismissAuthMsg = this.$q.notify({
timeout: 10,
message: 'Performing authentication...'
})
LNbits.api
.authLnurl(this.g.wallet, this.parse.lnurlauth.callback)
.then(_ => {
dismissAuthMsg()
this.$q.notify({
message: `Authentication successful.`,
type: 'positive',
timeout: 3500
})
this.parse.show = false
})
.catch(err => {
dismissAuthMsg()
2020-11-10 23:01:55 -03:00
if (err.response.data.reason) {
this.$q.notify({
message: `Authentication failed. ${this.parse.lnurlauth.domain} says:`,
caption: err.response.data.reason,
type: 'warning',
timeout: 5000
})
} else {
LNbits.utils.notifyApiError(err)
}
})
},
updateWallet: function (data) {
2021-08-06 11:15:07 +01:00
LNbits.api
.request('PATCH', '/api/v1/wallet', this.g.wallet.adminkey, data)
.then(_ => {
2021-10-22 00:41:30 +01:00
this.$q.notify({
message: `Wallet updated.`,
2021-10-22 00:41:30 +01:00
type: 'positive',
timeout: 3500
})
window.location.reload()
2021-10-22 00:41:30 +01:00
})
.catch(err => {
LNbits.utils.notifyApiError(err)
2021-08-06 11:15:07 +01:00
})
},
deleteWallet: function () {
2020-05-03 13:55:17 -03:00
LNbits.utils
.confirmDialog('Are you sure you want to delete this wallet?')
2020-10-11 22:19:27 -03:00
.onOk(() => {
LNbits.api
.deleteWallet(this.g.wallet)
.then(_ => {
this.$q.notify({
timeout: 3000,
message: `Wallet deleted!`,
spinner: true
})
})
.catch(err => {
this.paymentsTable.loading = false
LNbits.utils.notifyApiError(err)
})
2020-05-03 13:55:17 -03:00
})
},
fetchPayments: function (props) {
// Props are passed by qasar when pagination or sorting changes
if (props) {
this.paymentsTable.pagination = props.pagination
}
2024-01-22 08:03:31 -06:00
const pagination = this.paymentsTable.pagination
this.paymentsTable.loading = true
const query = {
limit: pagination.rowsPerPage,
offset: (pagination.page - 1) * pagination.rowsPerPage,
sortby: pagination.sortBy ?? 'time',
direction: pagination.descending ? 'desc' : 'asc'
}
if (this.paymentsTable.filter) {
query.search = this.paymentsTable.filter
}
return LNbits.api
.getPayments(this.g.wallet, query)
.then(response => {
this.paymentsTable.loading = false
this.paymentsTable.pagination.rowsNumber = response.data.total
this.payments = response.data.data.map(obj => {
return LNbits.map.payment(obj)
})
})
.catch(err => {
this.paymentsTable.loading = false
LNbits.utils.notifyApiError(err)
})
},
2020-09-29 20:04:02 -03:00
fetchBalance: function () {
2020-10-11 22:19:27 -03:00
LNbits.api.getWallet(this.g.wallet).then(response => {
this.balance = Math.floor(response.data.balance / 1000)
2020-09-29 20:04:02 -03:00
EventHub.$emit('update-wallet-balance', [
2020-10-11 22:19:27 -03:00
this.g.wallet.id,
this.balance
2020-09-29 20:04:02 -03:00
])
})
if (this.g.wallet.currency) {
this.updateFiatBalance()
}
},
updateFiatBalance() {
if (!this.g.wallet.currency) return 0
LNbits.api
.request('POST', `/api/v1/conversion`, null, {
amount: this.balance || this.g.wallet.sat,
to: this.g.wallet.currency
})
.then(response => {
this.fiatBalance = response.data[this.g.wallet.currency]
})
.catch(e => console.error(e))
},
formatFiat(currency, amount) {
return LNbits.utils.formatCurrency(amount, currency)
2020-09-29 20:04:02 -03:00
},
exportCSV: function () {
// status is important for export but it is not in paymentsTable
// because it is manually added with payment detail link and icons
// and would cause duplication in the list
2024-01-22 08:03:31 -06:00
const pagination = this.paymentsTable.pagination
const query = {
sortby: pagination.sortBy ?? 'time',
direction: pagination.descending ? 'desc' : 'asc'
}
LNbits.api.getPayments(this.g.wallet, query).then(response => {
const payments = response.data.data.map(LNbits.map.payment)
LNbits.utils.exportCSV(
this.paymentsCSV.columns,
payments,
this.g.wallet.name + '-payments'
)
})
},
pasteToTextArea: function () {
this.$refs.textArea.focus() // Set cursor to textarea
navigator.clipboard.readText().then(text => {
this.$refs.textArea.value = text
})
2020-04-01 22:18:46 +02:00
}
},
watch: {
payments: function () {
2020-09-29 20:04:02 -03:00
this.fetchBalance()
},
'paymentsChart.group': function () {
this.showChart()
}
},
created: function () {
let urlParams = new URLSearchParams(window.location.search)
if (urlParams.has('lightning') || urlParams.has('lnurl')) {
this.parse.data.request =
urlParams.get('lightning') || urlParams.get('lnurl')
this.decodeRequest()
this.parse.show = true
}
if (this.$q.screen.lt.md) {
this.mobileSimple = true
}
2020-09-29 20:04:02 -03:00
this.fetchBalance()
2020-05-03 13:55:17 -03:00
this.fetchPayments()
this.update.name = this.g.wallet.name
this.update.currency = this.g.wallet.currency
LNbits.api
.request('GET', '/api/v1/currencies')
.then(response => {
this.receive.units = ['sat', ...response.data]
})
.catch(err => {
LNbits.utils.notifyApiError(err)
})
},
mounted: function () {
// show disclaimer
2022-05-17 12:16:41 +01:00
if (!this.$q.localStorage.getItem('lnbits.disclaimerShown')) {
2020-05-03 13:55:17 -03:00
this.disclaimerDialog.show = true
this.$q.localStorage.set('lnbits.disclaimerShown', true)
2020-04-21 23:47:16 +02:00
}
// listen to incoming payments
LNbits.events.onInvoicePaid(this.g.wallet, payment =>
this.onPaymentReceived(payment.payment_hash)
)
}
2020-05-03 13:55:17 -03:00
})
if (navigator.serviceWorker != null) {
2022-07-05 16:16:46 -06:00
navigator.serviceWorker
.register('/service-worker.js')
.then(function (registration) {
console.log('Registered events at scope: ', registration.scope)
})
}