diff --git a/components/wallet/lnbits.js b/components/wallet/lnbits.js index 0eb88d0d..c6349d57 100644 --- a/components/wallet/lnbits.js +++ b/components/wallet/lnbits.js @@ -1,3 +1,6 @@ +import { TOR_REGEXP } from '@/lib/url' +import { object, string } from 'yup' + export const name = 'lnbits' export const fields = [ @@ -22,6 +25,31 @@ export async function validate ({ logger, ...config }) { return await getInfo({ logger, ...config }) } +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) +}) + async function getInfo ({ logger, ...config }) { logger.info('trying to fetch wallet') const response = await getWallet(config.url, config.adminKey) diff --git a/lib/validate.js b/lib/validate.js index d3fba4b3..c84fb48b 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -10,7 +10,7 @@ import { msatsToSats, numWithUnits, abbrNum, ensureB64, B64_URL_REGEX } from './ import * as usersFragments from '@/fragments/users' import * as subsFragments from '@/fragments/subs' import { isInvoicableMacaroon, isInvoiceMacaroon } from './macaroon' -import { TOR_REGEXP, parseNwcUrl } from './url' +import { parseNwcUrl } from './url' import { datePivot } from './time' import { decodeRune } from '@/lib/cln' import bip39Words from './bip39-words' @@ -600,31 +600,6 @@ export const lnAddrSchema = ({ payerData, min, max, commentAllowed } = {}) => return accum }, {}))) -export const lnbitsSchema = 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) -}) - export const nwcSchema = object({ nwcUrl: string() .required('required') diff --git a/pages/settings/wallets/[wallet].js b/pages/settings/wallets/[wallet].js index 3a184b31..c8b95069 100644 --- a/pages/settings/wallets/[wallet].js +++ b/pages/settings/wallets/[wallet].js @@ -2,7 +2,6 @@ import { getGetServerSideProps } from '@/api/ssrApollo' import { Form, ClientInput, ClientCheckbox, PasswordInput } from '@/components/form' import { CenterLayout } from '@/components/layout' import { WalletButtonBar } from '@/components/wallet-card' -import { lnbitsSchema } from '@/lib/validate' import { WalletSecurityBanner } from '@/components/banners' import { WalletLogs } from '@/components/wallet-logger' import { useToast } from '@/components/toast' @@ -33,7 +32,7 @@ export default function WalletSettings () {
{ try { const newConfig = !wallet.isConfigured