Add schema to wallet def

This commit is contained in:
ekzyis 2024-06-20 20:43:55 +02:00
parent 0957cb5b83
commit b8b0a4f985
3 changed files with 30 additions and 28 deletions

View File

@ -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)

View File

@ -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')

View File

@ -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 () {
<WalletSecurityBanner />
<Form
initial={initial}
schema={lnbitsSchema}
schema={wallet.schema}
onSubmit={async ({ enabled, ...values }) => {
try {
const newConfig = !wallet.isConfigured