-
Notifications
You must be signed in to change notification settings - Fork 8
Add LNbits Lightning backend integration #102
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: master
Are you sure you want to change the base?
Conversation
kwsantiago
left a comment
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.
This integration looks solid overall. I left some comments, but at a high-level:
- Missing error handling for LNbits-specific API failures vs network failures
- No retry logic for failed HTTP requests (unlike gRPC which may have built-in retry logic)
| private fetchService: FetchService, | ||
| ) {} | ||
|
|
||
| private getApiCredentials() { |
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.
This function is called on every HTTP request, consider caching credentials in onModuleInit() instead to avoid repeated config lookups.
| } | ||
|
|
||
| private async makeHttpRequest(endpoint: string, options: RequestInit = {}): Promise<any> { | ||
| const credentials = this.getApiCredentials(); |
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.
Store credentials once during initialization instead of calling getApiCredentials() on every request.
|
|
||
| return await response.json(); | ||
| } catch (error) { | ||
| this.logger.error(`LNbits request failed: ${error.message}`); |
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.
Does error.message exist on all error types? If so, you may want to wrap in case the throw loses the original error.
| return { | ||
| balance: balanceStr, | ||
| pending_open_balance: '0', | ||
| local_balance: {sat: balanceStr, msat: (parseInt(balanceStr) * 1000).toString()}, |
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.
parseInt(balanceStr, 10)
| if (address.startsWith('bc1p') || address.startsWith('tb1p')) { | ||
| return LightningAddressType.TAPROOT_PUBKEY; | ||
| } | ||
| return LightningAddressType.UNKOWN; |
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.
UNKNOWN
| } | ||
|
|
||
| export function mapRequestType(type?: string): LightningRequestType { | ||
| return LightningRequestType.BOLT11_INVOICE; |
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.
This function always returns BOLT11_INVOICE so why do you have type in the function?
| private readonly logger = new Logger(LightningService.name); | ||
|
|
||
| private grpc_client: any = null; | ||
| private http_client: any = null; |
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.
Only one of (http_client or grpc_client) is being used at a time, so you may want to make a single client var to simplify.
| private readonly logger = new Logger(LightningWalletKitService.name); | ||
|
|
||
| private grpc_client: any = null; | ||
| private http_client: any = null; |
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.
Only one of (http_client or grpc_client) is being used at a time, so you may want to make a single client var to simplify.
Implement LNbits as third Lightning backend option alongside LND and CLN.
Add HTTP REST API client, configuration support, and integration into existing Lightning architecture.
Configuration
To use LNbits as Lightning backend, add these lines to your
.envfile:Set Lightning type to lnbits
LIGHTNING_TYPE=lnbits
LNbits server configuration
LIGHTNING_API_URL=https://your.lnbits.server
LIGHTNING_API_KEY=your_wallet_api_key_here
Replace https://your.lnbits.server with your LNbits instance URL and your_wallet_api_key_here with your wallet's API key from LNbits.
Technical Details
• Uses LNbits native /api/v1/wallet endpoint for reliable balance retrieval
• Implements proper units conversion (millisats to sats)
• Follows existing NestJS patterns for dependency injection
• Maintains compatibility with existing LND/CLN integrations