lnbits-legend/lnbits/static/js/base.js

351 lines
9.1 KiB
JavaScript
Raw Normal View History

2020-10-13 18:57:26 +02:00
/* globals crypto, moment, Vue, axios, Quasar, _ */
2020-10-13 18:57:26 +02:00
window.LOCALE = 'en'
window.EventHub = new Vue()
window.LNbits = {
api: {
request: function (method, url, apiKey, data) {
return axios({
method: method,
url: url,
headers: {
2020-04-16 17:27:36 +02:00
'X-Api-Key': apiKey
},
data: data
2020-05-03 18:55:17 +02:00
})
},
createInvoice: function (wallet, amount, memo, lnurlCallback = null) {
return this.request('post', '/api/v1/payments', wallet.inkey, {
out: false,
amount: amount,
memo: memo,
lnurl_callback: lnurlCallback
2020-05-03 18:55:17 +02:00
})
},
payInvoice: function (wallet, bolt11) {
2020-04-01 22:18:46 +02:00
return this.request('post', '/api/v1/payments', wallet.adminkey, {
out: true,
bolt11: bolt11
2020-05-03 18:55:17 +02:00
})
},
payLnurl: function (
wallet,
callback,
description_hash,
amount,
2020-10-13 20:18:34 +02:00
description = '',
comment = ''
) {
return this.request('post', '/api/v1/payments/lnurl', wallet.adminkey, {
callback,
description_hash,
amount,
2020-10-13 20:18:34 +02:00
comment,
description
})
},
authLnurl: function (wallet, callback) {
return this.request('post', '/api/v1/lnurlauth', wallet.adminkey, {
callback
})
},
getWallet: function (wallet) {
return this.request('get', '/api/v1/wallet', wallet.inkey)
},
getPayments: function (wallet, checkPending) {
2020-05-03 18:55:17 +02:00
var query_param = checkPending ? '?check_pending' : ''
return this.request(
'get',
['/api/v1/payments', query_param].join(''),
wallet.inkey
)
},
getPayment: function (wallet, paymentHash) {
return this.request(
'get',
'/api/v1/payments/' + paymentHash,
wallet.inkey
)
}
},
events: {
onInvoicePaid: function (wallet, cb) {
if (!this.pis) {
this.pis = new EventSource(
'/api/v1/payments/sse?api-key=' + wallet.inkey
)
}
this.pis.addEventListener('payment-received', ev =>
cb(JSON.parse(ev.data))
)
}
},
href: {
createWallet: function (walletName, userId) {
2020-05-03 18:55:17 +02:00
window.location.href =
'/wallet?' + (userId ? 'usr=' + userId + '&' : '') + 'nme=' + walletName
},
deleteWallet: function (walletId, userId) {
2020-05-03 18:55:17 +02:00
window.location.href = '/deletewallet?usr=' + userId + '&wal=' + walletId
}
},
map: {
extension: function (data) {
2020-05-03 18:55:17 +02:00
var obj = _.object(
['code', 'isValid', 'name', 'shortDescription', 'icon'],
data
)
obj.url = ['/', obj.code, '/'].join('')
return obj
},
user: function (data) {
2020-05-03 18:55:17 +02:00
var obj = _.object(['id', 'email', 'extensions', 'wallets'], data)
var mapWallet = this.wallet
obj.wallets = obj.wallets
.map(function (obj) {
2020-05-03 18:55:17 +02:00
return mapWallet(obj)
})
.sort(function (a, b) {
2020-05-03 18:55:17 +02:00
return a.name.localeCompare(b.name)
})
obj.walletOptions = obj.wallets.map(function (obj) {
2020-04-05 12:19:25 +02:00
return {
2020-04-18 23:17:16 +02:00
label: [obj.name, ' - ', obj.id].join(''),
2020-04-05 12:19:25 +02:00
value: obj.id
2020-05-03 18:55:17 +02:00
}
})
return obj
},
wallet: function (data) {
2020-05-03 18:55:17 +02:00
var obj = _.object(
['id', 'name', 'user', 'adminkey', 'inkey', 'balance'],
data
)
obj.msat = obj.balance
obj.sat = Math.round(obj.balance / 1000)
2020-10-13 18:57:26 +02:00
obj.fsat = new Intl.NumberFormat(window.LOCALE).format(obj.sat)
2020-05-03 18:55:17 +02:00
obj.url = ['/wallet?usr=', obj.user, '&wal=', obj.id].join('')
return obj
},
payment: function (data) {
2020-05-03 18:55:17 +02:00
var obj = _.object(
[
'checking_id',
'pending',
'amount',
'fee',
'memo',
'time',
'bolt11',
'preimage',
'payment_hash',
'extra',
'wallet_id',
'webhook',
'webhook_status'
],
2020-05-03 18:55:17 +02:00
data
)
obj.date = Quasar.utils.date.formatDate(
new Date(obj.time * 1000),
'YYYY-MM-DD HH:mm'
)
obj.dateFrom = moment(obj.date).fromNow()
2020-05-03 18:55:17 +02:00
obj.msat = obj.amount
obj.sat = obj.msat / 1000
obj.tag = obj.extra.tag
2020-10-13 18:57:26 +02:00
obj.fsat = new Intl.NumberFormat(window.LOCALE).format(obj.sat)
2020-05-03 18:55:17 +02:00
obj.isIn = obj.amount > 0
obj.isOut = obj.amount < 0
obj.isPaid = obj.pending === 0
2020-05-03 18:55:17 +02:00
obj._q = [obj.memo, obj.sat].join(' ').toLowerCase()
return obj
}
},
utils: {
confirmDialog: function (msg) {
return Quasar.plugins.Dialog.create({
message: msg,
ok: {
flat: true,
color: 'orange'
},
cancel: {
flat: true,
color: 'grey'
}
2020-05-03 18:55:17 +02:00
})
},
formatCurrency: function (value, currency) {
2020-10-13 18:57:26 +02:00
return new Intl.NumberFormat(window.LOCALE, {
2020-05-03 18:55:17 +02:00
style: 'currency',
currency: currency
}).format(value)
},
formatSat: function (value) {
2020-10-13 18:57:26 +02:00
return new Intl.NumberFormat(window.LOCALE).format(value)
},
notifyApiError: function (error) {
var types = {
400: 'warning',
401: 'warning',
500: 'negative'
2020-05-03 18:55:17 +02:00
}
Quasar.plugins.Notify.create({
timeout: 5000,
type: types[error.response.status] || 'warning',
2020-04-21 23:47:16 +02:00
message: error.response.data.message || null,
2020-05-03 18:55:17 +02:00
caption:
[error.response.status, ' ', error.response.statusText]
.join('')
.toUpperCase() || null,
icon: null
2020-05-03 18:55:17 +02:00
})
2020-04-01 22:18:46 +02:00
},
search: function (data, q, field, separator) {
try {
2020-05-03 18:55:17 +02:00
var queries = q.toLowerCase().split(separator || ' ')
return data.filter(function (obj) {
2020-05-03 18:55:17 +02:00
var matches = 0
_.each(queries, function (q) {
2020-05-03 18:55:17 +02:00
if (obj[field].indexOf(q) !== -1) matches++
})
return matches === queries.length
2020-05-03 18:55:17 +02:00
})
} catch (err) {
2020-05-03 18:55:17 +02:00
return data
}
},
exportCSV: function (columns, data) {
var wrapCsvValue = function (val, formatFn) {
2020-05-03 18:55:17 +02:00
var formatted = formatFn !== void 0 ? formatFn(val) : val
2020-04-01 22:18:46 +02:00
2020-05-03 18:55:17 +02:00
formatted =
formatted === void 0 || formatted === null ? '' : String(formatted)
2020-04-01 22:18:46 +02:00
2020-05-03 18:55:17 +02:00
formatted = formatted.split('"').join('""')
2020-04-01 22:18:46 +02:00
2020-05-03 18:55:17 +02:00
return `"${formatted}"`
2020-04-01 22:18:46 +02:00
}
2020-05-03 18:55:17 +02:00
var content = [
columns.map(function (col) {
2020-05-03 18:55:17 +02:00
return wrapCsvValue(col.label)
})
]
.concat(
data.map(function (row) {
2020-05-03 18:55:17 +02:00
return columns
.map(function (col) {
2020-05-03 18:55:17 +02:00
return wrapCsvValue(
typeof col.field === 'function'
? col.field(row)
: row[col.field === void 0 ? col.name : col.field],
col.format
)
})
.join(',')
})
)
.join('\r\n')
2020-04-01 22:18:46 +02:00
2020-05-03 18:55:17 +02:00
var status = Quasar.utils.exportFile(
'table-export.csv',
content,
'text/csv'
)
2020-04-01 22:18:46 +02:00
if (status !== true) {
Quasar.plugins.Notify.create({
message: 'Browser denied file download...',
color: 'negative',
icon: null
2020-05-03 18:55:17 +02:00
})
2020-04-01 22:18:46 +02:00
}
}
}
2020-05-03 18:55:17 +02:00
}
2020-10-13 18:57:26 +02:00
window.windowMixin = {
data: function () {
return {
g: {
visibleDrawer: false,
extensions: [],
user: null,
wallet: null,
2020-05-03 18:55:17 +02:00
payments: []
}
2020-05-03 18:55:17 +02:00
}
},
methods: {
toggleDarkMode: function () {
2020-05-03 18:55:17 +02:00
this.$q.dark.toggle()
this.$q.localStorage.set('lnbits.darkMode', this.$q.dark.isActive)
},
copyText: function (text, message, position) {
2020-05-03 18:55:17 +02:00
var notify = this.$q.notify
Quasar.utils.copyToClipboard(text).then(function () {
2020-05-03 18:55:17 +02:00
notify({
message: message || 'Copied to clipboard!',
position: position || 'bottom'
})
})
}
},
created: function () {
2020-05-03 18:55:17 +02:00
this.$q.dark.set(this.$q.localStorage.getItem('lnbits.darkMode'))
if (window.user) {
2020-10-13 18:57:26 +02:00
this.g.user = Object.freeze(window.LNbits.map.user(window.user))
}
if (window.wallet) {
2020-10-13 18:57:26 +02:00
this.g.wallet = Object.freeze(window.LNbits.map.wallet(window.wallet))
}
if (window.extensions) {
2020-05-03 18:55:17 +02:00
var user = this.g.user
this.g.extensions = Object.freeze(
window.extensions
.map(function (data) {
2020-10-13 18:57:26 +02:00
return window.LNbits.map.extension(data)
2020-05-03 18:55:17 +02:00
})
.map(function (obj) {
2020-05-03 18:55:17 +02:00
if (user) {
obj.isEnabled = user.extensions.indexOf(obj.code) !== -1
2020-05-03 18:55:17 +02:00
} else {
obj.isEnabled = false
}
return obj
})
.sort(function (a, b) {
2020-05-03 18:55:17 +02:00
return a.name > b.name
})
)
}
}
2020-05-03 18:55:17 +02:00
}
2020-10-13 18:57:26 +02:00
window.decryptLnurlPayAES = function (success_action, preimage) {
let keyb = new Uint8Array(
preimage.match(/[\da-f]{2}/gi).map(h => parseInt(h, 16))
)
return crypto.subtle
.importKey('raw', keyb, {name: 'AES-CBC', length: 256}, false, ['decrypt'])
.then(key => {
let ivb = Uint8Array.from(window.atob(success_action.iv), c =>
c.charCodeAt(0)
)
let ciphertextb = Uint8Array.from(
window.atob(success_action.ciphertext),
c => c.charCodeAt(0)
)
return crypto.subtle.decrypt({name: 'AES-CBC', iv: ivb}, key, ciphertextb)
})
.then(valueb => {
let decoder = new TextDecoder('utf-8')
return decoder.decode(valueb)
})
}