From 3933a4f460ab64d0b5d7ac916dceddbfc916ad5f Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 17 Jul 2024 01:21:30 +0200 Subject: [PATCH] Generate validation schema for LNC --- pages/settings/wallets/[wallet].js | 27 +++++++++++++++++++++++++-- wallets/lnc/index.js | 11 +++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/pages/settings/wallets/[wallet].js b/pages/settings/wallets/[wallet].js index 139b1a0f..0da09689 100644 --- a/pages/settings/wallets/[wallet].js +++ b/pages/settings/wallets/[wallet].js @@ -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 diff --git a/wallets/lnc/index.js b/wallets/lnc/index.js index db202948..324442d3 100644 --- a/wallets/lnc/index.js +++ b/wallets/lnc/index.js @@ -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 ```\n\n```$ litcli sessions add --type custom --label --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 ```\n\n```$ litcli sessions add --type custom --label --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