stacker.news/components/wallet/lnbits.js

145 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-06-20 18:43:55 +00:00
import { TOR_REGEXP } from '@/lib/url'
import { object, string } from 'yup'
2024-06-03 22:41:15 +00:00
export const name = 'lnbits'
export const fields = [
{
name: 'url',
label: 'lnbits url',
type: 'text'
},
{
name: 'adminKey',
label: 'admin key',
type: 'password'
}
]
export const card = {
title: 'LNbits',
2024-06-20 19:52:07 +00:00
subtitle: 'use LNbits for payments',
2024-06-03 22:41:15 +00:00
badges: ['send only', 'non-custodialish']
}
export async function validate ({ logger, ...config }) {
return await getInfo({ logger, ...config })
}
2024-06-20 18:43:55 +00:00
export const schema = object({
url: process.env.NODE_ENV === 'development'
? string()
.or([string().matches(/^(http:\/\/)?localhost:\d+$/), string().url()], 'invalid url')
.required('required').trim()
: string().url().required('required').trim()
.test(async (url, context) => {
if (TOR_REGEXP.test(url)) {
// allow HTTP and HTTPS over Tor
if (!/^https?:\/\//.test(url)) {
return context.createError({ message: 'http or https required' })
}
return true
}
try {
// force HTTPS over clearnet
await string().https().validate(url)
} catch (err) {
return context.createError({ message: err.message })
}
return true
}),
adminKey: string().length(32)
})
2024-06-03 22:41:15 +00:00
async function getInfo ({ logger, ...config }) {
logger.info('trying to fetch wallet')
2024-06-03 22:41:15 +00:00
const response = await getWallet(config.url, config.adminKey)
logger.ok('wallet found')
2024-06-03 22:41:15 +00:00
return {
node: {
alias: response.name,
pubkey: ''
},
methods: [
'getInfo',
'getBalance',
'sendPayment'
],
version: '1.0',
supports: ['lightning']
}
}
export async function sendPayment ({ bolt11, config }) {
2024-06-03 22:41:15 +00:00
const { url, adminKey } = config
const response = await postPayment(url, adminKey, bolt11)
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')
2024-06-03 22:41:15 +00:00
}
const preimage = checkResponse.preimage
return { preimage }
2024-06-03 22:41:15 +00:00
}
async function getWallet (baseUrl, adminKey) {
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)
}
const wallet = await res.json()
return wallet
}
async function postPayment (baseUrl, adminKey, bolt11) {
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)
}
const payment = await res.json()
return payment
}
async function getPayment (baseUrl, adminKey, paymentHash) {
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)
}
const payment = await res.json()
return payment
}