Skip to content

Conversation

@Forte11Cuba
Copy link

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 .env file:

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

Copy link
Contributor

@kwsantiago kwsantiago left a 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() {
Copy link
Contributor

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();
Copy link
Contributor

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}`);
Copy link
Contributor

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()},
Copy link
Contributor

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;
Copy link
Contributor

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;
Copy link
Contributor

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;
Copy link
Contributor

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;
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants