2024-04-27 02:22:30 +00:00
|
|
|
import { bolt11Tags } from '@/lib/bolt11'
|
2024-02-08 18:33:13 +00:00
|
|
|
|
2024-06-03 22:41:15 +00:00
|
|
|
export const name = 'LNbits'
|
|
|
|
|
|
|
|
export const config = {
|
|
|
|
provider: {
|
|
|
|
url: {
|
|
|
|
label: 'lnbits url',
|
|
|
|
type: 'text'
|
|
|
|
},
|
|
|
|
adminKey: {
|
|
|
|
label: 'admin key',
|
|
|
|
type: 'password'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
card: {
|
|
|
|
title: 'LNbits',
|
|
|
|
badges: ['send only', 'non-custodialish'],
|
|
|
|
href: '/settings/wallets/lnbits'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getInfo ({ config, logger }) {
|
|
|
|
return async function () {
|
|
|
|
const response = await getWallet(config.url, config.adminKey)
|
|
|
|
return {
|
|
|
|
node: {
|
|
|
|
alias: response.name,
|
|
|
|
pubkey: ''
|
|
|
|
},
|
|
|
|
methods: [
|
|
|
|
'getInfo',
|
|
|
|
'getBalance',
|
|
|
|
'sendPayment'
|
|
|
|
],
|
|
|
|
version: '1.0',
|
|
|
|
supports: ['lightning']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-08 18:33:13 +00:00
|
|
|
|
2024-06-03 22:41:15 +00:00
|
|
|
export function sendPayment ({ config, logger }) {
|
|
|
|
return async function (bolt11) {
|
|
|
|
const { url, adminKey } = config
|
|
|
|
|
|
|
|
const hash = bolt11Tags(bolt11).payment_hash
|
|
|
|
logger.info('sending payment:', `payment_hash=${hash}`)
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await postPayment(url, adminKey, bolt11)
|
2024-02-08 18:33:13 +00:00
|
|
|
|
2024-06-03 22:41:15 +00:00
|
|
|
const checkResponse = await getPayment(url, adminKey, response.payment_hash)
|
|
|
|
if (!checkResponse.preimage) {
|
|
|
|
throw new Error('No preimage')
|
|
|
|
}
|
|
|
|
|
|
|
|
const preimage = checkResponse.preimage
|
|
|
|
logger.ok('payment successful:', `payment_hash=${hash}`, `preimage=${preimage}`)
|
|
|
|
return { preimage }
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('payment failed:', `payment_hash=${hash}`, err.message || err.toString?.())
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getWallet (baseUrl, adminKey) {
|
2024-02-08 18:33:13 +00:00
|
|
|
const url = baseUrl.replace(/\/+$/, '')
|
|
|
|
const path = '/api/v1/wallet'
|
|
|
|
|
|
|
|
const headers = new Headers()
|
|
|
|
headers.append('Accept', 'application/json')
|
|
|
|
headers.append('Content-Type', 'application/json')
|
|
|
|
headers.append('X-Api-Key', adminKey)
|
|
|
|
|
|
|
|
const res = await fetch(url + path, { method: 'GET', headers })
|
|
|
|
if (!res.ok) {
|
|
|
|
const errBody = await res.json()
|
|
|
|
throw new Error(errBody.detail)
|
|
|
|
}
|
2024-06-03 22:41:15 +00:00
|
|
|
|
2024-02-08 18:33:13 +00:00
|
|
|
const wallet = await res.json()
|
|
|
|
return wallet
|
|
|
|
}
|
|
|
|
|
2024-06-03 22:41:15 +00:00
|
|
|
async function postPayment (baseUrl, adminKey, bolt11) {
|
2024-02-08 18:33:13 +00:00
|
|
|
const url = baseUrl.replace(/\/+$/, '')
|
|
|
|
const path = '/api/v1/payments'
|
|
|
|
|
|
|
|
const headers = new Headers()
|
|
|
|
headers.append('Accept', 'application/json')
|
|
|
|
headers.append('Content-Type', 'application/json')
|
|
|
|
headers.append('X-Api-Key', adminKey)
|
|
|
|
|
|
|
|
const body = JSON.stringify({ bolt11, out: true })
|
|
|
|
|
|
|
|
const res = await fetch(url + path, { method: 'POST', headers, body })
|
|
|
|
if (!res.ok) {
|
|
|
|
const errBody = await res.json()
|
|
|
|
throw new Error(errBody.detail)
|
|
|
|
}
|
2024-06-03 22:41:15 +00:00
|
|
|
|
2024-02-08 18:33:13 +00:00
|
|
|
const payment = await res.json()
|
|
|
|
return payment
|
|
|
|
}
|
|
|
|
|
2024-06-03 22:41:15 +00:00
|
|
|
async function getPayment (baseUrl, adminKey, paymentHash) {
|
2024-02-08 18:33:13 +00:00
|
|
|
const url = baseUrl.replace(/\/+$/, '')
|
|
|
|
const path = `/api/v1/payments/${paymentHash}`
|
|
|
|
|
|
|
|
const headers = new Headers()
|
|
|
|
headers.append('Accept', 'application/json')
|
|
|
|
headers.append('Content-Type', 'application/json')
|
|
|
|
headers.append('X-Api-Key', adminKey)
|
|
|
|
|
|
|
|
const res = await fetch(url + path, { method: 'GET', headers })
|
|
|
|
if (!res.ok) {
|
|
|
|
const errBody = await res.json()
|
|
|
|
throw new Error(errBody.detail)
|
|
|
|
}
|
2024-06-03 22:41:15 +00:00
|
|
|
|
2024-02-08 18:33:13 +00:00
|
|
|
const payment = await res.json()
|
|
|
|
return payment
|
|
|
|
}
|