|
| 1 | +# Cursor Rules for ldk_node |
| 2 | + |
| 3 | +## Project Type |
| 4 | +Flutter package for Lightning Network integration using LDK and BDK. |
| 5 | + |
| 6 | +## Code Style |
| 7 | +- Dart 3.0+ with null safety |
| 8 | +- Follow Flutter/Dart style guide |
| 9 | +- Use async/await for all node operations |
| 10 | +- Use BigInt for all amount values (millisats, sats) |
| 11 | + |
| 12 | +## When generating code involving this package: |
| 13 | + |
| 14 | +### 1. Always import both: |
| 15 | +```dart |
| 16 | +import 'package:ldk_node/ldk_node.dart'; |
| 17 | +import 'package:path_provider/path_provider.dart'; |
| 18 | +``` |
| 19 | + |
| 20 | +### 2. Initialize in this order: |
| 21 | +```dart |
| 22 | +// 1. Get storage directory |
| 23 | +final dir = await getApplicationDocumentsDirectory(); |
| 24 | +
|
| 25 | +// 2. Generate or load mnemonic (ASYNC!) |
| 26 | +final mnemonic = await Mnemonic.generate(); |
| 27 | +// OR: Mnemonic(seedPhrase: 'existing words...') |
| 28 | +
|
| 29 | +// 3. Build node with chained methods |
| 30 | +final node = await Builder.testnet() |
| 31 | + .setStorageDirPath('${dir.path}/ldk_node') |
| 32 | + .setEntropyBip39Mnemonic(mnemonic: mnemonic) |
| 33 | + .build(); |
| 34 | +
|
| 35 | +// 4. Start node (REQUIRED) |
| 36 | +await node.start(); |
| 37 | +
|
| 38 | +// 5. Sync wallets (REQUIRED for balances) |
| 39 | +await node.syncWallets(); |
| 40 | +``` |
| 41 | + |
| 42 | +### 3. Use testnet by default: |
| 43 | +```dart |
| 44 | +Builder.testnet() // Testnet with Esplora + RGS preconfigured |
| 45 | +Builder.mutinynet() // Signet with LSPS2 liquidity |
| 46 | +``` |
| 47 | + |
| 48 | +### 4. Payment handlers are ASYNC getters: |
| 49 | +```dart |
| 50 | +// ✅ CORRECT - use await |
| 51 | +final bolt11 = await node.bolt11Payment(); |
| 52 | +final bolt12 = await node.bolt12Payment(); |
| 53 | +final onChain = await node.onChainPayment(); |
| 54 | +final spontaneous = await node.spontaneousPayment(); |
| 55 | +
|
| 56 | +// ❌ WRONG - property access doesn't exist |
| 57 | +node.bolt11Payment.receive(...) |
| 58 | +``` |
| 59 | + |
| 60 | +### 5. Method names have "Unsafe" suffix: |
| 61 | +```dart |
| 62 | +await bolt11.receiveUnsafe(...) |
| 63 | +await bolt11.sendUnsafe(...) |
| 64 | +await bolt12.receiveUnsafe(...) |
| 65 | +await onChain.sendToAddress(...) |
| 66 | +``` |
| 67 | + |
| 68 | +### 6. Convert amounts with BigInt: |
| 69 | +```dart |
| 70 | +// Sats to millisats |
| 71 | +amountMsat: BigInt.from(sats * 1000) |
| 72 | +
|
| 73 | +// Display millisats as sats |
| 74 | +final sats = amountMsat ~/ BigInt.from(1000); |
| 75 | +``` |
| 76 | + |
| 77 | +### 7. Bolt11Invoice constructor: |
| 78 | +```dart |
| 79 | +// ✅ CORRECT |
| 80 | +Bolt11Invoice(signedRawInvoice: 'lnbc...') |
| 81 | +
|
| 82 | +// ❌ WRONG |
| 83 | +Bolt11Invoice('lnbc...') |
| 84 | +``` |
| 85 | + |
| 86 | +### 8. Handle errors with specific exceptions: |
| 87 | +```dart |
| 88 | +try { |
| 89 | + final bolt11 = await node.bolt11Payment(); |
| 90 | + await bolt11.sendUnsafe(invoice: invoice); |
| 91 | +} on NodeException catch (e) { |
| 92 | + print('Node error: ${e.code} - ${e.errorMessage}'); |
| 93 | +} on PaymentException catch (e) { |
| 94 | + print('Payment error: ${e.code} - ${e.errorMessage}'); |
| 95 | +} on ChannelException catch (e) { |
| 96 | + print('Channel error: ${e.code} - ${e.errorMessage}'); |
| 97 | +} on LdkFfiException catch (e) { |
| 98 | + print('LDK error: ${e.code} - ${e.errorMessage}'); |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +### 9. Event handling requires acknowledgment: |
| 103 | +```dart |
| 104 | +final event = await node.nextEventAsync(); |
| 105 | +// ... handle event ... |
| 106 | +await node.eventHandled(); // MUST call this! |
| 107 | +``` |
| 108 | + |
| 109 | +### 10. Always stop node on dispose: |
| 110 | +```dart |
| 111 | +await node.stop(); |
| 112 | +``` |
| 113 | + |
| 114 | +## Common Patterns |
| 115 | + |
| 116 | +### Receive Lightning Payment |
| 117 | +```dart |
| 118 | +final bolt11 = await node.bolt11Payment(); |
| 119 | +final invoice = await bolt11.receiveUnsafe( |
| 120 | + amountMsat: BigInt.from(10000 * 1000), // 10,000 sats |
| 121 | + description: 'Payment for service', |
| 122 | + expirySecs: 3600, |
| 123 | +); |
| 124 | +return invoice.signedRawInvoice; |
| 125 | +``` |
| 126 | + |
| 127 | +### Send Lightning Payment |
| 128 | +```dart |
| 129 | +final bolt11 = await node.bolt11Payment(); |
| 130 | +await bolt11.sendUnsafe( |
| 131 | + invoice: Bolt11Invoice(signedRawInvoice: invoiceString), |
| 132 | +); |
| 133 | +``` |
| 134 | + |
| 135 | +### Get Balances |
| 136 | +```dart |
| 137 | +final balances = await node.listBalances(); |
| 138 | +print('On-chain: ${balances.spendableOnchainBalanceSats} sats'); |
| 139 | +print('Lightning: ${balances.totalLightningBalanceSats} sats'); |
| 140 | +``` |
| 141 | + |
| 142 | +### Generate On-Chain Address |
| 143 | +```dart |
| 144 | +final onChain = await node.onChainPayment(); |
| 145 | +final address = await onChain.newAddress(); |
| 146 | +return address.s; |
| 147 | +``` |
| 148 | + |
| 149 | +## File Organization (Example App) |
| 150 | +- Services: `/lib/services/` - Lightning service wrapper |
| 151 | +- Models: `/lib/models/` - App-specific data models |
| 152 | +- Screens: `/lib/screens/` - UI screens |
| 153 | +- Widgets: `/lib/widgets/` - Reusable widgets |
| 154 | +- Providers: `/lib/providers/` - State management |
| 155 | + |
| 156 | +## Don't Forget |
| 157 | +- `Mnemonic.generate()` is async - use `await` |
| 158 | +- Store mnemonics securely with `flutter_secure_storage` |
| 159 | +- Never use mainnet without explicit user request |
| 160 | +- Call `node.stop()` when disposing |
| 161 | +- Call `node.eventHandled()` after processing each event |
0 commit comments