-
Notifications
You must be signed in to change notification settings - Fork 1
Payout address env var #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d9dc3b7
14bab00
cbdc99b
77a5350
227b351
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { z } from 'zod' | ||
|
|
||
| import { log } from '../logging' | ||
| import { createMoneyDevKitNode } from '../mdk' | ||
| import { getPayoutConfig, PayoutAddressType } from '../payout-address' | ||
|
|
||
| const payoutSchema = z.object({ | ||
| amount: z.number().positive(), | ||
| }) | ||
|
|
||
| function jsonResponse(status: number, body: Record<string, unknown>) { | ||
| return new Response(JSON.stringify(body), { | ||
| status, | ||
| headers: { 'content-type': 'application/json' }, | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Unified payout handler that automatically detects the payout address type | ||
| * from PAYOUT_ADDRESS env var and routes to the appropriate payment method. | ||
| * | ||
| * Supports: LNURL, Lightning Address, Bolt12 Offer, BIP-353 | ||
| * | ||
| * Request body: | ||
| * - amount: number (required) - amount in millisatoshis | ||
| */ | ||
| export async function handlePayout(request: Request): Promise<Response> { | ||
| try { | ||
| const body = await request.json() | ||
| const parsed = payoutSchema.safeParse(body) | ||
|
|
||
| if (!parsed.success) { | ||
| return jsonResponse(400, { | ||
| error: 'Invalid payout request', | ||
| details: parsed.error.issues, | ||
| }) | ||
| } | ||
|
|
||
| const config = getPayoutConfig() | ||
|
|
||
| if (!config.address) { | ||
| return jsonResponse(500, { | ||
| error: 'Payout address not configured. Set PAYOUT_ADDRESS environment variable.', | ||
| }) | ||
| } | ||
|
|
||
| const { address, type } = config.address | ||
| const { amount } = parsed.data | ||
|
|
||
| log(`Initiating payout flow with ${type} address`) | ||
|
|
||
| const node = createMoneyDevKitNode() | ||
|
|
||
| switch (type) { | ||
| case 'bolt12': | ||
| await node.payBolt12Offer(address, amount) | ||
| break | ||
|
|
||
| case 'lnurl': | ||
| case 'lightning_address': | ||
| case 'bip353': | ||
| // LNURL, Lightning Address, and BIP-353 all use the same payment method | ||
| await node.payLNUrl(address, amount) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we need to parse the payment instructions at the bip353 address to figure out what to use? Is it common for people to put an lnurl address behind bip-353? Thought sticking a Bolt12 offer at the bip-353 address would be the most common case (onchain aside)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is leveraging the fact that
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Confusing method name |
||
| break | ||
|
|
||
| case 'bolt11': | ||
| // Bolt11 invoices are typically one-time use with a fixed amount | ||
| // The amount parameter is ignored for bolt11 | ||
| await node.payBolt11(address) | ||
| break | ||
|
|
||
| default: | ||
| return jsonResponse(500, { | ||
| error: `Unsupported payout address type: ${type satisfies never}`, | ||
| }) | ||
| } | ||
|
|
||
| return jsonResponse(200, { | ||
| success: true, | ||
| type, | ||
| amount, | ||
| }) | ||
| } catch (error) { | ||
| console.error('Payout error:', error) | ||
| return jsonResponse(500, { error: 'Internal Server Error' }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For
lightning_addressas it is implemented now I think the payment type is still ambiguous. I think we have to try fetching an invoice first from an LNURL server then fallback to fetching payment instructions from a DNS server. could also it it the other way around but LNUrl is more common right now.