Generate validation schema for NWC
This commit is contained in:
		
							parent
							
								
									fb2b34ce67
								
							
						
					
					
						commit
						6432ea7b44
					
				@ -10,7 +10,6 @@ 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 { parseNwcUrl } from './url'
 | 
			
		||||
import { datePivot } from './time'
 | 
			
		||||
import { decodeRune } from '@/lib/cln'
 | 
			
		||||
import bip39Words from './bip39-words'
 | 
			
		||||
@ -600,32 +599,6 @@ export const lnAddrSchema = ({ payerData, min, max, commentAllowed } = {}) =>
 | 
			
		||||
    return accum
 | 
			
		||||
  }, {})))
 | 
			
		||||
 | 
			
		||||
export const nwcSchema = object({
 | 
			
		||||
  nwcUrl: string()
 | 
			
		||||
    .required('required')
 | 
			
		||||
    .test(async (nwcUrl, context) => {
 | 
			
		||||
      // run validation in sequence to control order of errors
 | 
			
		||||
      // inspired by https://github.com/jquense/yup/issues/851#issuecomment-1049705180
 | 
			
		||||
      try {
 | 
			
		||||
        await string().required('required').validate(nwcUrl)
 | 
			
		||||
        await string().matches(/^nostr\+?walletconnect:\/\//, { message: 'must start with nostr+walletconnect://' }).validate(nwcUrl)
 | 
			
		||||
        let relayUrl, walletPubkey, secret
 | 
			
		||||
        try {
 | 
			
		||||
          ({ relayUrl, walletPubkey, secret } = parseNwcUrl(nwcUrl))
 | 
			
		||||
        } catch {
 | 
			
		||||
          // invalid URL error. handle as if pubkey validation failed to not confuse user.
 | 
			
		||||
          throw new Error('pubkey must be 64 hex chars')
 | 
			
		||||
        }
 | 
			
		||||
        await string().required('pubkey required').trim().matches(NOSTR_PUBKEY_HEX, 'pubkey must be 64 hex chars').validate(walletPubkey)
 | 
			
		||||
        await string().required('relay url required').trim().wss('relay must use wss://').validate(relayUrl)
 | 
			
		||||
        await string().required('secret required').trim().matches(/^[0-9a-fA-F]{64}$/, 'secret must be 64 hex chars').validate(secret)
 | 
			
		||||
      } catch (err) {
 | 
			
		||||
        return context.createError({ message: err.message })
 | 
			
		||||
      }
 | 
			
		||||
      return true
 | 
			
		||||
    })
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
export const lncSchema = object({
 | 
			
		||||
  pairingPhrase: array()
 | 
			
		||||
    .transform(function (value, originalValue) {
 | 
			
		||||
 | 
			
		||||
@ -149,6 +149,11 @@ function generateSchema (wallet, { me }) {
 | 
			
		||||
      return validator
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (field.validate.schema) {
 | 
			
		||||
      // complex validation
 | 
			
		||||
      return field.validate.schema
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const { type: validationType } = field.validate
 | 
			
		||||
 | 
			
		||||
    let validator
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,6 @@
 | 
			
		||||
import { nwcSchema } from '@/lib/validate'
 | 
			
		||||
import { NOSTR_PUBKEY_HEX } from '@/lib/nostr'
 | 
			
		||||
import { parseNwcUrl } from '@/lib/url'
 | 
			
		||||
import { string } from 'yup'
 | 
			
		||||
 | 
			
		||||
export const name = 'nwc'
 | 
			
		||||
 | 
			
		||||
@ -6,7 +8,32 @@ export const fields = [
 | 
			
		||||
  {
 | 
			
		||||
    name: 'nwcUrl',
 | 
			
		||||
    label: 'connection',
 | 
			
		||||
    type: 'password'
 | 
			
		||||
    type: 'password',
 | 
			
		||||
    validate: {
 | 
			
		||||
      schema: string()
 | 
			
		||||
        .required('required')
 | 
			
		||||
        .test(async (nwcUrl, context) => {
 | 
			
		||||
          // run validation in sequence to control order of errors
 | 
			
		||||
          // inspired by https://github.com/jquense/yup/issues/851#issuecomment-1049705180
 | 
			
		||||
          try {
 | 
			
		||||
            await string().required('required').validate(nwcUrl)
 | 
			
		||||
            await string().matches(/^nostr\+?walletconnect:\/\//, { message: 'must start with nostr+walletconnect://' }).validate(nwcUrl)
 | 
			
		||||
            let relayUrl, walletPubkey, secret
 | 
			
		||||
            try {
 | 
			
		||||
              ({ relayUrl, walletPubkey, secret } = parseNwcUrl(nwcUrl))
 | 
			
		||||
            } catch {
 | 
			
		||||
            // invalid URL error. handle as if pubkey validation failed to not confuse user.
 | 
			
		||||
              throw new Error('pubkey must be 64 hex chars')
 | 
			
		||||
            }
 | 
			
		||||
            await string().required('pubkey required').trim().matches(NOSTR_PUBKEY_HEX, 'pubkey must be 64 hex chars').validate(walletPubkey)
 | 
			
		||||
            await string().required('relay url required').trim().wss('relay must use wss://').validate(relayUrl)
 | 
			
		||||
            await string().required('secret required').trim().matches(/^[0-9a-fA-F]{64}$/, 'secret must be 64 hex chars').validate(secret)
 | 
			
		||||
          } catch (err) {
 | 
			
		||||
            return context.createError({ message: err.message })
 | 
			
		||||
          }
 | 
			
		||||
          return true
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
@ -15,5 +42,3 @@ export const card = {
 | 
			
		||||
  subtitle: 'use Nostr Wallet Connect for payments',
 | 
			
		||||
  badges: ['send only', 'non-custodialish']
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const schema = nwcSchema
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user