|
| 1 | +# Lightning Node Interface |
| 2 | + |
| 3 | +Remote connect to major Lightning node implementations with one TypeScript interface. |
| 4 | + |
| 5 | +- Supports major nodes: CLN, LND, Phoenixd |
| 6 | +- Supports protocols: BOLT11, BOLT12, NWC |
| 7 | +- Includes custodial APIs: Strike, Speed, Blink |
| 8 | +- LNURL + Lightning Address support (`user@domain.com`, `lnurl1...`) |
| 9 | +- Frontend-capable TypeScript runtime (`fetch`-based) |
| 10 | + |
| 11 | +## Install |
| 12 | + |
| 13 | +```bash |
| 14 | +npm install @sunnyln/lni |
| 15 | +``` |
| 16 | + |
| 17 | +## TypeScript Examples |
| 18 | + |
| 19 | +### Node API |
| 20 | + |
| 21 | +```ts |
| 22 | +import { |
| 23 | + createNode, |
| 24 | + InvoiceType, |
| 25 | + type BackendNodeConfig, |
| 26 | +} from '@sunnyln/lni'; |
| 27 | + |
| 28 | +const backend: BackendNodeConfig = { |
| 29 | + kind: 'lnd', |
| 30 | + config: { |
| 31 | + url: 'https://lnd.example.com', |
| 32 | + macaroon: '...', |
| 33 | + }, |
| 34 | +}; |
| 35 | + |
| 36 | +const node = createNode(backend); |
| 37 | + |
| 38 | +const info = await node.getInfo(); |
| 39 | + |
| 40 | +const invoiceParams = { |
| 41 | + invoiceType: InvoiceType.Bolt11, |
| 42 | + amountMsats: 2000, |
| 43 | + description: 'your memo', |
| 44 | + expiry: 3600, |
| 45 | +}; |
| 46 | + |
| 47 | +const invoice = await node.createInvoice(invoiceParams); |
| 48 | + |
| 49 | +const payInvoiceParams = { |
| 50 | + invoice: invoice.invoice, |
| 51 | + feeLimitPercentage: 1, |
| 52 | + allowSelfPayment: true, |
| 53 | +}; |
| 54 | + |
| 55 | +const payment = await node.payInvoice(payInvoiceParams); |
| 56 | + |
| 57 | +const status = await node.lookupInvoice({ paymentHash: invoice.paymentHash }); |
| 58 | + |
| 59 | +const txs = await node.listTransactions({ from: 0, limit: 10 }); |
| 60 | +``` |
| 61 | + |
| 62 | +For NWC specifically, `createNode` returns `NwcNode` when `kind: 'nwc'`, so you can close it: |
| 63 | + |
| 64 | +```ts |
| 65 | +const nwcNode = createNode({ kind: 'nwc', config: { nwcUri: 'nostr+walletconnect://...' } }); |
| 66 | +// ... use node |
| 67 | +nwcNode.close(); |
| 68 | +``` |
| 69 | + |
| 70 | +### LNURL + Lightning Address |
| 71 | + |
| 72 | +```ts |
| 73 | +import { detectPaymentType, needsResolution, getPaymentInfo, resolveToBolt11 } from '@sunnyln/lni'; |
| 74 | + |
| 75 | +const destination = 'user@domain.com'; |
| 76 | + |
| 77 | +const type = detectPaymentType(destination); |
| 78 | +const requiresResolution = needsResolution(destination); |
| 79 | +const info = await getPaymentInfo(destination, 100_000); |
| 80 | +const bolt11 = await resolveToBolt11(destination, 100_000); |
| 81 | +``` |
| 82 | + |
| 83 | +### Invoice Event Polling |
| 84 | + |
| 85 | +```ts |
| 86 | +await node.onInvoiceEvents( |
| 87 | + { |
| 88 | + paymentHash: invoice.paymentHash, |
| 89 | + pollingDelaySec: 3, |
| 90 | + maxPollingSec: 60, |
| 91 | + }, |
| 92 | + (status, tx) => { |
| 93 | + console.log('Invoice event:', status, tx); |
| 94 | + }, |
| 95 | +); |
| 96 | +``` |
| 97 | + |
| 98 | +## Implemented in this package |
| 99 | + |
| 100 | +- `PhoenixdNode` |
| 101 | +- `ClnNode` |
| 102 | +- `LndNode` |
| 103 | +- `NwcNode` |
| 104 | +- `StrikeNode` |
| 105 | +- `SpeedNode` |
| 106 | +- `BlinkNode` |
| 107 | +- LNURL helpers (`detectPaymentType`, `needsResolution`, `resolveToBolt11`, `getPaymentInfo`) |
| 108 | + |
| 109 | +Not included yet: |
| 110 | +- `SparkNode` (planned) |
| 111 | + |
| 112 | +## Frontend Runtime Notes |
| 113 | + |
| 114 | +- Uses `fetch`, no Node-native runtime dependency required. |
| 115 | +- You can inject custom fetch via constructor options: |
| 116 | + - `new LndNode(config, { fetch: customFetch })` |
| 117 | +- Most backends require secrets (API keys, macaroons, runes, passwords). For production web apps, use a backend proxy/BFF to protect credentials. |
| 118 | + |
| 119 | +## Build and Publish (package maintainers) |
| 120 | + |
| 121 | +```bash |
| 122 | +npm run prepack |
| 123 | +npm run pack:dry-run |
| 124 | +npm run publish:public |
| 125 | +``` |
| 126 | + |
| 127 | +## Integration tests |
| 128 | + |
| 129 | +```bash |
| 130 | +npm run test:integration |
| 131 | +``` |
| 132 | + |
| 133 | +These scripts set `NODE_TLS_REJECT_UNAUTHORIZED=0` because many local Lightning nodes use self-signed certs in test environments. Do not use this in production. |
0 commit comments