import { sdk } from '../sdk' import { configFile } from '../file-models/config.json' const { InputSpec, Value } = sdk // Connects the relay to the operator's BTCPay store so users can top // up their credit balance via Lightning / on-chain payments. All // four fields must be set together — leaving any blank disables the // credit-purchase flow (the rest of the relay keeps working). // // To generate the API key in BTCPay: // 1. Account → Manage Account → API Keys // 2. "Generate Key" with scopes: // - btcpay.store.cancreateinvoice // - btcpay.store.canviewinvoices // 3. Restrict to your Recap store // // To set up the webhook in BTCPay: // 1. Open your "Recap" store → Settings → Webhooks // 2. URL: https:///relay/btcpay/webhook // 3. Subscribe to: "An invoice has been settled" // 4. Set "Automatic redelivery" on // 5. Copy the auto-generated secret into the field below const inputSpec = InputSpec.of({ relay_btcpay_base_url: Value.text({ name: 'BTCPay Base URL', description: 'Public URL of your BTCPay server. The relay POSTs invoice-create requests here, and BTCPay POSTs webhooks back to /relay/btcpay/webhook on your relay host. Example: https://btcpay.keysat.xyz', required: false, default: null, minLength: 0, maxLength: 256, patterns: [ { regex: '^(https?://.+)?$', description: 'Must be empty or start with http:// or https://', }, ], }), relay_btcpay_store_id: Value.text({ name: 'BTCPay Store ID', description: 'UUID of the BTCPay store invoices should be created against. Find it in BTCPay → Store Settings → General → Store ID.', required: false, default: null, minLength: 0, maxLength: 64, }), relay_btcpay_api_key: Value.text({ name: 'BTCPay API Key', description: 'Greenfield API token with the canCreateInvoice + canViewInvoices scopes restricted to your Recap store. Generated under Account → Manage Account → API Keys.', required: false, default: null, masked: true, minLength: 0, maxLength: 256, }), relay_btcpay_webhook_secret: Value.text({ name: 'BTCPay Webhook Secret', description: 'Shared secret BTCPay uses to HMAC-sign webhook deliveries to /relay/btcpay/webhook. Get this from BTCPay → Store Settings → Webhooks after creating the webhook entry.', required: false, default: null, masked: true, minLength: 0, maxLength: 256, }), }) export const setBtcpayConnection = sdk.Action.withInput( 'set-btcpay-connection', async ({ effects }) => ({ name: 'Set BTCPay Connection (credit purchases)', description: 'Wire the relay to your BTCPay store so users can buy credit top-ups via Lightning. Leave any field blank to disable the credit-purchase flow — the rest of the relay keeps working without it.', warning: null, allowedStatuses: 'any', group: 'Tiers', visibility: 'enabled', }), inputSpec, async ({ effects }) => { const config = await configFile.read().once() return { relay_btcpay_base_url: config?.relay_btcpay_base_url || undefined, relay_btcpay_store_id: config?.relay_btcpay_store_id || undefined, relay_btcpay_api_key: config?.relay_btcpay_api_key || undefined, relay_btcpay_webhook_secret: config?.relay_btcpay_webhook_secret || undefined, } }, async ({ effects, input }) => { await configFile.merge(effects, { relay_btcpay_base_url: (input.relay_btcpay_base_url || '').trim(), relay_btcpay_store_id: (input.relay_btcpay_store_id || '').trim(), relay_btcpay_api_key: (input.relay_btcpay_api_key || '').trim(), relay_btcpay_webhook_secret: ( input.relay_btcpay_webhook_secret || '' ).trim(), }) return null }, )