|
| 1 | +import { createSettings } from '../settings-factory' |
| 2 | +import { NwcPaymentsProcessor } from '../../payments-processors/nwc-payments-processor' |
| 3 | +import { createLogger } from '../logger-factory' |
| 4 | +import { IPaymentsProcessor } from '../../@types/clients' |
| 5 | +import { Settings } from '../../@types/settings' |
| 6 | + |
| 7 | +const logger = createLogger('nwc-payments-processor-factory') |
| 8 | + |
| 9 | +const getNwcConfig = (settings: Settings): { nwcUrl: string; replyTimeoutMs: number } => { |
| 10 | + const nwcUrl = process.env.NWC_URL |
| 11 | + |
| 12 | + if (!nwcUrl) { |
| 13 | + const error = new Error('NWC_URL must be set.') |
| 14 | + logger('Unable to create NWC payments processor. %o', error) |
| 15 | + throw error |
| 16 | + } |
| 17 | + |
| 18 | + if (!nwcUrl.startsWith('nostr+walletconnect://') && !nwcUrl.startsWith('nostrwalletconnect://')) { |
| 19 | + const error = new Error('NWC_URL must be a valid nostr+walletconnect:// or nostrwalletconnect:// URI.') |
| 20 | + logger('Unable to create NWC payments processor. %o', error) |
| 21 | + throw error |
| 22 | + } |
| 23 | + |
| 24 | + try { |
| 25 | + new URL(nwcUrl) |
| 26 | + } catch { |
| 27 | + const error = new Error('NWC_URL is not parseable as a URL.') |
| 28 | + logger('Unable to create NWC payments processor. %o', error) |
| 29 | + throw error |
| 30 | + } |
| 31 | + |
| 32 | + const replyTimeoutMs = settings.paymentsProcessors?.nwc?.replyTimeoutMs |
| 33 | + if (typeof replyTimeoutMs !== 'number' || replyTimeoutMs <= 0) { |
| 34 | + const error = new Error('Setting paymentsProcessors.nwc.replyTimeoutMs must be a positive number.') |
| 35 | + logger('Unable to create NWC payments processor. %o', error) |
| 36 | + throw error |
| 37 | + } |
| 38 | + |
| 39 | + const invoiceExpirySeconds = settings.paymentsProcessors?.nwc?.invoiceExpirySeconds |
| 40 | + if (typeof invoiceExpirySeconds !== 'number' || !Number.isInteger(invoiceExpirySeconds) || invoiceExpirySeconds <= 0) { |
| 41 | + const error = new Error('Setting paymentsProcessors.nwc.invoiceExpirySeconds must be a positive integer.') |
| 42 | + logger('Unable to create NWC payments processor. %o', error) |
| 43 | + throw error |
| 44 | + } |
| 45 | + |
| 46 | + return { nwcUrl, replyTimeoutMs } |
| 47 | +} |
| 48 | + |
| 49 | +export const createNwcPaymentsProcessor = (settings: Settings): IPaymentsProcessor => { |
| 50 | + const { nwcUrl, replyTimeoutMs } = getNwcConfig(settings) |
| 51 | + |
| 52 | + return new NwcPaymentsProcessor(nwcUrl, replyTimeoutMs, createSettings) |
| 53 | +} |
0 commit comments