|
| 1 | +import { z } from "zod"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Input for the payout command. Destination is NOT in the payload — the node-side |
| 5 | + * handler reads it from process.env.WITHDRAWAL_DESTINATION. This means mdk.com cannot |
| 6 | + * direct funds to an arbitrary destination even if the per-app key is compromised. |
| 7 | + * |
| 8 | + * amountMsat is required and positive; "drain entire balance" semantics are out of v1. |
| 9 | + * If needed later, add a separate explicit command (e.g. payout.drainAll). |
| 10 | + */ |
| 11 | +export const PayoutInputSchema = z.object({ |
| 12 | + amountMsat: z.number().int().positive(), |
| 13 | + idempotencyKey: z.string(), |
| 14 | +}); |
| 15 | +export type PayoutInput = z.infer<typeof PayoutInputSchema>; |
| 16 | + |
| 17 | +/** |
| 18 | + * Result of a payout command. Returned synchronously after the underlying |
| 19 | + * payWhileRunning(_, _, 0) fire-and-forget call. The final outcome (Sent or Failed) |
| 20 | + * arrives later as a paymentSent or paymentFailed event over the events() iterator. |
| 21 | + */ |
| 22 | +export const PayoutResultSchema = z.object({ |
| 23 | + accepted: z.literal(true), |
| 24 | + paymentId: z.string(), |
| 25 | + paymentHash: z.string().nullable(), |
| 26 | +}); |
| 27 | +export type PayoutResult = z.infer<typeof PayoutResultSchema>; |
| 28 | + |
| 29 | +/** |
| 30 | + * Input for createBolt11. amountMsat null means a variable-amount JIT invoice. |
| 31 | + */ |
| 32 | +export const InvoiceCreateBolt11InputSchema = z.object({ |
| 33 | + amountMsat: z.number().int().positive().nullable(), |
| 34 | + description: z.string(), |
| 35 | + expirySecs: z.number().int().positive(), |
| 36 | + idempotencyKey: z.string(), |
| 37 | +}); |
| 38 | +export type InvoiceCreateBolt11Input = z.infer< |
| 39 | + typeof InvoiceCreateBolt11InputSchema |
| 40 | +>; |
| 41 | + |
| 42 | +/** |
| 43 | + * Result of createBolt11. expiresAt is a unix timestamp in seconds (matches lightning-js). |
| 44 | + */ |
| 45 | +export const InvoiceBolt11ResultSchema = z.object({ |
| 46 | + bolt11: z.string(), |
| 47 | + paymentHash: z.string(), |
| 48 | + expiresAt: z.number(), |
| 49 | + scid: z.string(), |
| 50 | +}); |
| 51 | +export type InvoiceBolt11Result = z.infer<typeof InvoiceBolt11ResultSchema>; |
| 52 | + |
| 53 | +/** |
| 54 | + * Input for createBolt12Offer. amountMsat null means a variable-amount offer. |
| 55 | + */ |
| 56 | +export const InvoiceCreateBolt12OfferInputSchema = z.object({ |
| 57 | + amountMsat: z.number().int().positive().nullable(), |
| 58 | + description: z.string(), |
| 59 | + expirySecs: z.number().int().positive().optional(), |
| 60 | + idempotencyKey: z.string(), |
| 61 | +}); |
| 62 | +export type InvoiceCreateBolt12OfferInput = z.infer< |
| 63 | + typeof InvoiceCreateBolt12OfferInputSchema |
| 64 | +>; |
| 65 | + |
| 66 | +export const InvoiceBolt12OfferResultSchema = z.object({ |
| 67 | + offer: z.string(), |
| 68 | +}); |
| 69 | +export type InvoiceBolt12OfferResult = z.infer< |
| 70 | + typeof InvoiceBolt12OfferResultSchema |
| 71 | +>; |
| 72 | + |
| 73 | +/** |
| 74 | + * Events pushed from the node to mdk.com over the events() AsyncIterable. |
| 75 | + * |
| 76 | + * - ready: emitted once after node.startReceiving() + setupBolt12Receive() complete. |
| 77 | + * mdk.com SHOULD wait for this before sending command RPCs (commands are gated on |
| 78 | + * nodeReady server-side and reject with {error:'node-not-ready'} otherwise). |
| 79 | + * |
| 80 | + * - paymentSent / paymentFailed: outbound payment outcomes. Correlate by paymentId |
| 81 | + * returned from the original payout RPC. paymentId is only present for outbound |
| 82 | + * payments (per lightning-js PaymentEvent typing); inbound failures clear pending |
| 83 | + * claims locally but do not surface here. |
| 84 | + * |
| 85 | + * - draining: emitted when the node enters its drain window (15s before the |
| 86 | + * hardcoded 300s lifetime expires). New command RPCs reject after this. |
| 87 | + * |
| 88 | + * - leaseReleased: emitted right before the node initiates a graceful shutdown |
| 89 | + * (60s of quiet + no in-flight outbound + no pending claims + empty queue). |
| 90 | + * Followed by a graceful WS close. |
| 91 | + * |
| 92 | + * reason on paymentFailed is optional because lightning-js types it optional in |
| 93 | + * PaymentEvent (index.d.ts:47); forcing a default would lose signal. |
| 94 | + */ |
| 95 | +export const NodeEventSchema = z.discriminatedUnion("type", [ |
| 96 | + z.object({ type: z.literal("ready"), nodeId: z.string() }), |
| 97 | + z.object({ |
| 98 | + type: z.literal("paymentSent"), |
| 99 | + paymentId: z.string(), |
| 100 | + paymentHash: z.string(), |
| 101 | + preimage: z.string(), |
| 102 | + }), |
| 103 | + z.object({ |
| 104 | + type: z.literal("paymentFailed"), |
| 105 | + paymentId: z.string(), |
| 106 | + paymentHash: z.string(), |
| 107 | + reason: z.string().optional(), |
| 108 | + }), |
| 109 | + z.object({ type: z.literal("draining") }), |
| 110 | + z.object({ type: z.literal("leaseReleased") }), |
| 111 | +]); |
| 112 | +export type NodeEvent = z.infer<typeof NodeEventSchema>; |
0 commit comments