Generate validation schema for CLN
This commit is contained in:
parent
08a5ce1a28
commit
c8d91bf42d
|
@ -6,11 +6,10 @@ import {
|
|||
} from './constants'
|
||||
import { SUPPORTED_CURRENCIES } from './currency'
|
||||
import { NOSTR_MAX_RELAY_NUM, NOSTR_PUBKEY_BECH32, NOSTR_PUBKEY_HEX } from './nostr'
|
||||
import { msatsToSats, numWithUnits, abbrNum, ensureB64, B64_URL_REGEX } from './format'
|
||||
import { msatsToSats, numWithUnits, abbrNum, ensureB64 } from './format'
|
||||
import * as usersFragments from '@/fragments/users'
|
||||
import * as subsFragments from '@/fragments/subs'
|
||||
import { datePivot } from './time'
|
||||
import { decodeRune } from '@/lib/cln'
|
||||
import bip39Words from './bip39-words'
|
||||
|
||||
const { SUB } = subsFragments
|
||||
|
@ -303,32 +302,6 @@ export function advSchema (args) {
|
|||
})
|
||||
}
|
||||
|
||||
export function CLNAutowithdrawSchema ({ me } = {}) {
|
||||
return object({
|
||||
socket: string().socket().required('required'),
|
||||
rune: string().matches(B64_URL_REGEX, { message: 'invalid rune' }).required('required')
|
||||
.test({
|
||||
name: 'rune',
|
||||
test: (v, context) => {
|
||||
const decoded = decodeRune(v)
|
||||
if (!decoded) return context.createError({ message: 'invalid rune' })
|
||||
if (decoded.restrictions.length === 0) {
|
||||
return context.createError({ message: 'rune must be restricted to method=invoice' })
|
||||
}
|
||||
if (decoded.restrictions.length !== 1 || decoded.restrictions[0].alternatives.length !== 1) {
|
||||
return context.createError({ message: 'rune must be restricted to method=invoice only' })
|
||||
}
|
||||
if (decoded.restrictions[0].alternatives[0] !== 'method=invoice') {
|
||||
return context.createError({ message: 'rune must be restricted to method=invoice only' })
|
||||
}
|
||||
return true
|
||||
}
|
||||
}),
|
||||
cert: hexOrBase64Validator,
|
||||
...autowithdrawSchemaMembers
|
||||
})
|
||||
}
|
||||
|
||||
export const autowithdrawSchemaMembers = {
|
||||
enabled: boolean(),
|
||||
autoWithdrawThreshold: intValidator.required('required').min(0, 'must be at least 0').max(msatsToSats(BALANCE_LIMIT_MSATS), `must be at most ${abbrNum(msatsToSats(BALANCE_LIMIT_MSATS))}`),
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { array, object, string } from 'yup'
|
||||
import { autowithdrawSchemaMembers, hexOrBase64Validator, lightningAddressValidator } from '@/lib/validate'
|
||||
import { TOR_REGEXP } from '@/lib/url'
|
||||
import { B64_URL_REGEX } from '@/lib/format'
|
||||
|
||||
export function generateResolverName (walletField) {
|
||||
const capitalized = walletField[0].toUpperCase() + walletField.slice(1)
|
||||
|
@ -77,6 +78,8 @@ export function generateSchema (wallet) {
|
|||
|
||||
if (validationType === 'hexOrBase64') validator = hexOrBase64Validator
|
||||
|
||||
if (validationType === 'b64url') validator = string().matches(B64_URL_REGEX, { message: `invalid ${field.name}` })
|
||||
|
||||
if (min !== undefined) validator = validator.min(min)
|
||||
if (max !== undefined) validator = validator.max(max)
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { CLNAutowithdrawSchema } from '@/lib/validate'
|
||||
import { decodeRune } from '@/lib/cln'
|
||||
|
||||
export const name = 'cln'
|
||||
|
||||
|
@ -9,7 +9,10 @@ export const fields = [
|
|||
type: 'text',
|
||||
placeholder: '55.5.555.55:3010',
|
||||
hint: 'tor or clearnet',
|
||||
clear: true
|
||||
clear: true,
|
||||
validate: {
|
||||
type: 'socket'
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'rune',
|
||||
|
@ -20,7 +23,24 @@ export const fields = [
|
|||
type: 'text',
|
||||
placeholder: 'S34KtUW-6gqS_hD_9cc_PNhfF-NinZyBOCgr1aIrark9NCZtZXRob2Q9aW52b2ljZQ==',
|
||||
hint: 'must be restricted to method=invoice',
|
||||
clear: true
|
||||
clear: true,
|
||||
validate: {
|
||||
type: 'b64url',
|
||||
test: (v, context) => {
|
||||
const decoded = decodeRune(v)
|
||||
if (!decoded) return context.createError({ message: 'invalid rune' })
|
||||
if (decoded.restrictions.length === 0) {
|
||||
return context.createError({ message: 'rune must be restricted to method=invoice' })
|
||||
}
|
||||
if (decoded.restrictions.length !== 1 || decoded.restrictions[0].alternatives.length !== 1) {
|
||||
return context.createError({ message: 'rune must be restricted to method=invoice only' })
|
||||
}
|
||||
if (decoded.restrictions[0].alternatives[0] !== 'method=invoice') {
|
||||
return context.createError({ message: 'rune must be restricted to method=invoice only' })
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'cert',
|
||||
|
@ -29,7 +49,10 @@ export const fields = [
|
|||
placeholder: 'LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUNNVENDQWRpZ0F3SUJBZ0lRSHBFdFdrcGJwZHV4RVF2eVBPc3NWVEFLQmdncWhrak9QUVFEQWpBdk1SOHcKSFFZRFZRUUtFeFpzYm1RZ1lYVjBiMmRsYm1WeVlYUmxaQ0JqWlhKME1Rd3dDZ1lEVlFRREV3TmliMkl3SGhjTgpNalF3TVRBM01qQXhORE0wV2hjTk1qVXdNekF6TWpBeE5ETTBXakF2TVI4d0hRWURWUVFLRXhac2JtUWdZWFYwCmIyZGxibVZ5WVhSbFpDQmpaWEowTVF3d0NnWURWUVFERXdOaWIySXdXVEFUQmdjcWhrak9QUUlCQmdncWhrak8KUFFNQkJ3TkNBQVJUS3NMVk5oZnhqb1FLVDlkVVdDbzUzSmQwTnBuL1BtYi9LUE02M1JxbU52dFYvdFk4NjJJZwpSbE41cmNHRnBEajhUeFc2OVhIK0pTcHpjWDdlN3N0Um80SFZNSUhTTUE0R0ExVWREd0VCL3dRRUF3SUNwREFUCkJnTlZIU1VFRERBS0JnZ3JCZ0VGQlFjREFUQVBCZ05WSFJNQkFmOEVCVEFEQVFIL01CMEdBMVVkRGdRV0JCVDAKMnh3V25GeHRUNzI0MWxwZlNoNm9FWi9UMWpCN0JnTlZIUkVFZERCeWdnTmliMktDQ1d4dlkyRnNhRzl6ZElJRApZbTlpZ2d4d2IyeGhjaTF1TVMxaWIyS0NGR2h2YzNRdVpHOWphMlZ5TG1sdWRHVnlibUZzZ2dSMWJtbDRnZ3AxCmJtbDRjR0ZqYTJWMGdnZGlkV1pqYjI1dWh3Ui9BQUFCaHhBQUFBQUFBQUFBQUFBQUFBQUFBQUFCaHdTc0VnQUQKTUFvR0NDcUdTTTQ5QkFNQ0EwY0FNRVFDSUEwUTlkRXdoNXpPRnpwL3hYeHNpemh5SkxNVG5yazU1VWx1NHJPRwo4WW52QWlBVGt4U3p3Y3hZZnFscGx0UlNIbmd0NUJFcDBzcXlHL05nenBzb2pmMGNqQT09Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K',
|
||||
optional: 'optional if from [CA](https://en.wikipedia.org/wiki/Certificate_authority) (e.g. voltage)',
|
||||
hint: 'hex or base64 encoded',
|
||||
clear: true
|
||||
clear: true,
|
||||
validate: {
|
||||
type: 'hexOrBase64'
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
|
@ -39,8 +62,6 @@ export const card = {
|
|||
badges: ['receive only', 'non-custodialish']
|
||||
}
|
||||
|
||||
export const schema = CLNAutowithdrawSchema
|
||||
|
||||
export const walletType = 'CLN'
|
||||
|
||||
export const walletField = 'walletCLN'
|
||||
|
|
Loading…
Reference in New Issue