|
| 1 | +import { Cards } from 'vocs' |
| 2 | +import { SpecCard } from '../../../components/SpecCard' |
| 3 | + |
| 4 | +# Lightning charge [One-time payments using BOLT11 invoices] |
| 5 | + |
| 6 | +The Lightning implementation of the [charge](/intents/charge) intent. |
| 7 | + |
| 8 | +The server generates a fresh [BOLT11](https://github.com/lightning/bolts/blob/master/11-payment-encoding.md) invoice for each request. The client pays it over the [Lightning Network](https://lightning.network) and presents the payment preimage as a credential. The server verifies `sha256(preimage) == paymentHash` locally and returns the resource with a receipt. |
| 9 | + |
| 10 | +This method is best for single API calls, content access, or one-off purchases. |
| 11 | + |
| 12 | +## Server |
| 13 | + |
| 14 | +The `spark` namespace is the reference implementation using [Spark](https://spark.money) wallets. The protocol works with any Lightning node—you can build your own method handler using [LND](https://github.com/lightningnetwork/lnd), [LDK](https://lightningdevkit.org), or any stack that can create BOLT11 invoices and verify preimages. |
| 15 | + |
| 16 | +Use `spark.charge` to gate any endpoint behind a one-time Lightning payment. The method handles invoice generation, Challenge creation, preimage verification, and Receipt generation. |
| 17 | + |
| 18 | +```ts |
| 19 | +import { Mppx, spark } from '@buildonspark/lightning-mpp-sdk/server' |
| 20 | + |
| 21 | +const mppx = Mppx.create({ |
| 22 | + methods: [spark.charge({ mnemonic: process.env.MNEMONIC! })], |
| 23 | + secretKey: process.env.MPP_SECRET_KEY!, |
| 24 | +}) |
| 25 | + |
| 26 | +export async function handler(request: Request) { |
| 27 | + const result = await mppx.charge({ |
| 28 | + amount: '100', |
| 29 | + currency: 'BTC', |
| 30 | + description: 'Premium API access', |
| 31 | + })(request) |
| 32 | + |
| 33 | + if (result.status === 402) return result.challenge |
| 34 | + |
| 35 | + return result.withReceipt(Response.json({ data: '...' })) |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### With expiry |
| 40 | + |
| 41 | +```ts |
| 42 | +import { Mppx, Expires, spark } from '@buildonspark/lightning-mpp-sdk/server' |
| 43 | + |
| 44 | +const mppx = Mppx.create({ |
| 45 | + methods: [spark.charge({ mnemonic: process.env.MNEMONIC! })], |
| 46 | + secretKey: process.env.MPP_SECRET_KEY!, |
| 47 | +}) |
| 48 | + |
| 49 | +export async function handler(request: Request) { |
| 50 | + const result = await mppx.charge({ |
| 51 | + amount: '100', |
| 52 | + currency: 'BTC', |
| 53 | + expires: Expires.minutes(10), // [!code hl] |
| 54 | + })(request) |
| 55 | + |
| 56 | + if (result.status === 402) return result.challenge |
| 57 | + return result.withReceipt(Response.json({ data: '...' })) |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### With regtest |
| 62 | + |
| 63 | +For local development and testing, set `network` to `'regtest'` and use the [Spark faucet](https://docs.spark.money/tools/faucet) to fund wallets. |
| 64 | + |
| 65 | +```ts |
| 66 | +import { Mppx, spark } from '@buildonspark/lightning-mpp-sdk/server' |
| 67 | + |
| 68 | +const mppx = Mppx.create({ |
| 69 | + methods: [spark.charge({ |
| 70 | + mnemonic: process.env.MNEMONIC!, |
| 71 | + network: 'regtest', // [!code hl] |
| 72 | + })], |
| 73 | + secretKey: process.env.MPP_SECRET_KEY!, |
| 74 | +}) |
| 75 | +``` |
| 76 | + |
| 77 | +### Server parameters |
| 78 | + |
| 79 | +| Parameter | Type | Required | Default | |
| 80 | +| --- | --- | --- | --- | |
| 81 | +| `mnemonic` | `string` | Required | | |
| 82 | +| `network` | `'mainnet'` \| `'regtest'` \| `'signet'` | Optional | `'mainnet'` | |
| 83 | + |
| 84 | +## Client |
| 85 | + |
| 86 | +Use `spark.charge` with `Mppx.create` to automatically handle `402` responses. The client parses the Challenge, pays the BOLT11 invoice, and retries with the preimage as a Credential. |
| 87 | + |
| 88 | +```ts |
| 89 | +import { Mppx, spark } from '@buildonspark/lightning-mpp-sdk/client' |
| 90 | + |
| 91 | +Mppx.create({ |
| 92 | + methods: [spark.charge({ mnemonic: process.env.MNEMONIC! })], |
| 93 | +}) |
| 94 | + |
| 95 | +const response = await fetch('https://api.example.com/resource') |
| 96 | +``` |
| 97 | + |
| 98 | +### Without polyfill |
| 99 | + |
| 100 | +If you don't want to patch `globalThis.fetch`, use `mppx.fetch` directly: |
| 101 | + |
| 102 | +```ts |
| 103 | +import { Mppx, spark } from '@buildonspark/lightning-mpp-sdk/client' |
| 104 | + |
| 105 | +const method = spark.charge({ mnemonic: process.env.MNEMONIC! }) |
| 106 | + |
| 107 | +const mppx = Mppx.create({ |
| 108 | + polyfill: false, |
| 109 | + methods: [method], |
| 110 | +}) |
| 111 | + |
| 112 | +try { |
| 113 | + const response = await mppx.fetch('https://api.example.com/resource') |
| 114 | + console.log(await response.json()) |
| 115 | +} finally { |
| 116 | + await method.cleanup() |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +:::info |
| 121 | +The Spark SDK maintains WebSocket connections for Lightning payments. Call `method.cleanup()` when done to close connections and allow the process to exit. |
| 122 | +::: |
| 123 | + |
| 124 | +### Client parameters |
| 125 | + |
| 126 | +| Parameter | Type | Required | Default | |
| 127 | +| --- | --- | --- | --- | |
| 128 | +| `mnemonic` | `string` | Required | | |
| 129 | +| `network` | `'mainnet'` \| `'regtest'` \| `'signet'` | Optional | `'mainnet'` | |
| 130 | +| `maxFeeSats` | `number` | Optional | `100` | |
| 131 | + |
| 132 | +## Request fields |
| 133 | + |
| 134 | +The Challenge request includes the base charge fields plus Lightning method details. |
| 135 | + |
| 136 | +| Field | Type | Required | Description | |
| 137 | +| --- | --- | --- | --- | |
| 138 | +| `amount` | `string` | Required | Invoice amount in satoshis | |
| 139 | +| `currency` | `string` | Optional | Must be `'BTC'` if present. Defaults to `'BTC'` | |
| 140 | +| `description` | `string` | Optional | Human-readable memo. Maps to the BOLT11 description field | |
| 141 | +| `methodDetails.invoice` | `string` | Required | Full BOLT11-encoded payment request (`lnbc...`). Authoritative source for all payment parameters | |
| 142 | +| `methodDetails.paymentHash` | `string` | Optional | SHA-256 hash of the preimage, lowercase hex. Convenience field—must match the hash decoded from `invoice` | |
| 143 | +| `methodDetails.network` | `string` | Optional | `'mainnet'`, `'regtest'`, or `'signet'`. Convenience field—must match `invoice`'s human-readable prefix. Defaults to `'mainnet'` | |
| 144 | + |
| 145 | +## Credential payload |
| 146 | + |
| 147 | +The Credential payload contains the payment preimage revealed by Lightning HTLC settlement. |
| 148 | + |
| 149 | +| Field | Type | Required | Description | |
| 150 | +| --- | --- | --- | --- | |
| 151 | +| `preimage` | `string` | Required | 32-byte payment preimage, lowercase hex (64 characters) | |
| 152 | + |
| 153 | +## Verification |
| 154 | + |
| 155 | +The server verifies payment with a single hash operation: |
| 156 | + |
| 157 | +1. Decode the Credential and extract `preimage`. |
| 158 | +2. Compute `sha256(hex_to_bytes(preimage))`. |
| 159 | +3. Compare against the `paymentHash` from the original Challenge. |
| 160 | +4. If equal, payment is verified. Return the resource with a Receipt. |
| 161 | + |
| 162 | +The entire verification path is local and self-contained. |
| 163 | + |
| 164 | +## Specification |
| 165 | + |
| 166 | +<Cards> |
| 167 | + <SpecCard to="https://tempoxyz.github.io/mpp-specs/draft-lightning-charge-00" /> |
| 168 | +</Cards> |
0 commit comments