Generate validation schema for LNC

This commit is contained in:
ekzyis 2024-07-17 01:21:30 +02:00
parent 667cde6042
commit 3933a4f460
2 changed files with 32 additions and 6 deletions

View File

@ -10,7 +10,7 @@ import Info from '@/components/info'
import Text from '@/components/text'
import { AutowithdrawSettings } from '@/components/autowithdraw-shared'
import dynamic from 'next/dynamic'
import { object, string } from 'yup'
import { array, object, string } from 'yup'
import { autowithdrawSchemaMembers } from '@/lib/validate'
import { useMe } from '@/components/me'
import { TOR_REGEXP } from '@/lib/url'
@ -154,7 +154,7 @@ function generateSchema (wallet, { me }) {
return field.validate.schema
}
const { type: validationType } = field.validate
const { type: validationType, words, min, max } = field.validate
let validator
@ -192,6 +192,29 @@ function generateSchema (wallet, { me }) {
})
}
if (words) {
validator = array()
.transform(function (value, originalValue) {
if (this.isType(value) && value !== null) {
return value
}
return originalValue ? originalValue.trim().split(/[\s]+/) : []
})
.test(async (values, context) => {
for (const v of values) {
try {
await string().oneOf(words).validate(v)
} catch {
return context.createError({ message: `'${v}' is not a valid ${field.label} word` })
}
}
return true
})
}
if (min !== undefined) validator = validator.min(min)
if (max !== undefined) validator = validator.max(max)
if (!field.optional) validator = validator.required('required')
return validator

View File

@ -1,4 +1,4 @@
import { lncSchema } from '@/lib/validate'
import bip39Words from '@/lib/bip39-words'
export const name = 'lnc'
@ -7,7 +7,12 @@ export const fields = [
name: 'pairingPhrase',
label: 'pairing phrase',
type: 'password',
help: 'We only need permissions for the uri `/lnrpc.Lightning/SendPaymentSync`\n\nCreate a budgeted account with narrow permissions:\n\n```$ litcli accounts create --balance <budget>```\n\n```$ litcli sessions add --type custom --label <your label> --account_id <account_id> --uri /lnrpc.Lightning/SendPaymentSync```\n\nGrab the `pairing_secret_mnemonic` from the output and paste it here.'
help: 'We only need permissions for the uri `/lnrpc.Lightning/SendPaymentSync`\n\nCreate a budgeted account with narrow permissions:\n\n```$ litcli accounts create --balance <budget>```\n\n```$ litcli sessions add --type custom --label <your label> --account_id <account_id> --uri /lnrpc.Lightning/SendPaymentSync```\n\nGrab the `pairing_secret_mnemonic` from the output and paste it here.',
validate: {
words: bip39Words,
min: 2,
max: 10
}
},
{
name: 'password',
@ -23,5 +28,3 @@ export const card = {
subtitle: 'use Lightning Node Connect for LND payments',
badges: ['send only', 'non-custodialish', 'budgetable']
}
export const schema = lncSchema