2024-07-16 22:36:25 +00:00
|
|
|
import { NOSTR_PUBKEY_HEX } from '@/lib/nostr'
|
|
|
|
import { parseNwcUrl } from '@/lib/url'
|
|
|
|
import { string } from 'yup'
|
2024-07-16 05:54:27 +00:00
|
|
|
|
|
|
|
export const name = 'nwc'
|
|
|
|
|
|
|
|
export const fields = [
|
|
|
|
{
|
|
|
|
name: 'nwcUrl',
|
|
|
|
label: 'connection',
|
2024-07-16 22:36:25 +00:00
|
|
|
type: 'password',
|
|
|
|
validate: {
|
2024-07-17 01:32:00 +00:00
|
|
|
type: 'string',
|
|
|
|
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
|
2024-07-16 22:36:25 +00:00
|
|
|
try {
|
2024-07-17 01:32:00 +00:00
|
|
|
({ relayUrl, walletPubkey, secret } = parseNwcUrl(nwcUrl))
|
|
|
|
} catch {
|
2024-07-16 22:36:25 +00:00
|
|
|
// invalid URL error. handle as if pubkey validation failed to not confuse user.
|
2024-07-17 01:32:00 +00:00
|
|
|
throw new Error('pubkey must be 64 hex chars')
|
2024-07-16 22:36:25 +00:00
|
|
|
}
|
2024-07-17 01:32:00 +00:00
|
|
|
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
|
|
|
|
}
|
2024-07-16 22:36:25 +00:00
|
|
|
}
|
2024-07-16 05:54:27 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
export const card = {
|
|
|
|
title: 'NWC',
|
|
|
|
subtitle: 'use Nostr Wallet Connect for payments',
|
|
|
|
badges: ['send only', 'non-custodialish']
|
|
|
|
}
|