-
Notifications
You must be signed in to change notification settings - Fork 15
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
Open
Forte11Cuba
wants to merge
1
commit into
cashubtc:master
Choose a base branch
from
Forte11Cuba:lnbits
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| export enum LightningType { | ||
| LND = 'lnd', | ||
| CLN = 'cln', | ||
| LNBITS = 'lnbits', | ||
| } | ||
|
|
||
| export enum LightningAddressType { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* Application Dependencies */ | ||
| import {LightningRequestType} from '@server/modules/lightning/lightning.enums'; | ||
|
|
||
| export function mapRequestDescription(description: string | null): string | null { | ||
| if (!description) return null; | ||
| if (description === '') return null; | ||
| return description; | ||
| } | ||
|
|
||
| export function mapRequestExpiry(request: any): number | null { | ||
| if (request?.expires_at) return Number(request.expires_at); | ||
| if (request?.expiry) return Number(request.expiry); | ||
| return null; | ||
| } | ||
|
|
||
| export function mapRequestType(type?: string): LightningRequestType { | ||
| return LightningRequestType.BOLT11_INVOICE; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function always returns |
||
| } | ||
|
|
||
| export function mapLnbitsError(error: any): string { | ||
| if (error?.detail) { | ||
| if (Array.isArray(error.detail)) { | ||
| return error.detail.map((d: any) => d.msg || d).join(', '); | ||
| } | ||
| return error.detail; | ||
| } | ||
| return error?.message || 'Unknown LNbits error'; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /* Core Dependencies */ | ||
| import {Module} from '@nestjs/common'; | ||
| /* Application Dependencies */ | ||
| import {CredentialModule} from '@server/modules/credential/credential.module'; | ||
| import {FetchModule} from '@server/modules/fetch/fetch.module'; | ||
| /* Local Dependencies */ | ||
| import {LnbitsService} from './lnbits.service'; | ||
|
|
||
| @Module({ | ||
| imports: [CredentialModule, FetchModule], | ||
| providers: [LnbitsService], | ||
| exports: [LnbitsService], | ||
| }) | ||
| export class LnbitsModule {} |
116 changes: 116 additions & 0 deletions
116
src/server/modules/lightning/lnbits/lnbits.service.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* Core Dependencies */ | ||
| import {Test, TestingModule} from '@nestjs/testing'; | ||
| import {expect} from '@jest/globals'; | ||
| import {ConfigService} from '@nestjs/config'; | ||
| /* Application Dependencies */ | ||
| import {CredentialService} from '@server/modules/credential/credential.service'; | ||
| import {FetchService} from '@server/modules/fetch/fetch.service'; | ||
| /* Local Dependencies */ | ||
| import {LnbitsService} from './lnbits.service'; | ||
|
|
||
| describe('LnbitsService', () => { | ||
| let lnbits_service: LnbitsService; | ||
| let config_service: jest.Mocked<ConfigService>; | ||
| let credential_service: jest.Mocked<CredentialService>; | ||
| let fetch_service: jest.Mocked<FetchService>; | ||
|
|
||
| beforeEach(async () => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| providers: [ | ||
| LnbitsService, | ||
| {provide: ConfigService, useValue: {get: jest.fn()}}, | ||
| {provide: CredentialService, useValue: {loadPemOrPath: jest.fn()}}, | ||
| {provide: FetchService, useValue: {fetchWithProxy: jest.fn()}}, | ||
| ], | ||
| }).compile(); | ||
|
|
||
| lnbits_service = module.get<LnbitsService>(LnbitsService); | ||
| config_service = module.get(ConfigService); | ||
| credential_service = module.get(CredentialService); | ||
| fetch_service = module.get(FetchService); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(lnbits_service).toBeDefined(); | ||
| }); | ||
|
|
||
| describe('initializeLightningClient', () => { | ||
| it('should initialize successfully with valid credentials', () => { | ||
| config_service.get.mockReturnValueOnce('http://localhost:5000'); // api_url | ||
| config_service.get.mockReturnValueOnce('test_api_key'); // api_key | ||
| credential_service.loadPemOrPath.mockReturnValue(Buffer.from('test_api_key')); | ||
|
|
||
| const client = lnbits_service.initializeLightningClient(); | ||
| expect(client).toBe(lnbits_service); | ||
| }); | ||
|
|
||
| it('should throw error with missing credentials', () => { | ||
| config_service.get.mockReturnValue(null); | ||
|
|
||
| expect(() => lnbits_service.initializeLightningClient()).toThrow('Failed to initialize LNbits client'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('mapLnbitsRequest', () => { | ||
| it('should map LNbits request correctly', () => { | ||
| const lnbits_request = { | ||
| type: 'bolt11', | ||
| valid: true, | ||
| description: 'Test payment', | ||
| expires_at: 1234567890, | ||
| }; | ||
|
|
||
| const result = lnbits_service.mapLnbitsRequest(lnbits_request); | ||
|
|
||
| expect(result.valid).toBe(true); | ||
| expect(result.description).toBe('Test payment'); | ||
| expect(result.expiry).toBe(1234567890); | ||
| }); | ||
| }); | ||
|
|
||
| describe('mapLnbitsInfo', () => { | ||
| it('should map LNbits info correctly', async () => { | ||
| const lnbits_info = { | ||
| version: '1.0.0', | ||
| identity_pubkey: 'test_pubkey', | ||
| alias: 'Test Node', | ||
| block_height: 800000, | ||
| testnet: false, | ||
| }; | ||
|
|
||
| const result = await lnbits_service.mapLnbitsInfo(lnbits_info); | ||
|
|
||
| expect(result.version).toBe('1.0.0'); | ||
| expect(result.identity_pubkey).toBe('test_pubkey'); | ||
| expect(result.alias).toBe('Test Node'); | ||
| expect(result.block_height).toBe(800000); | ||
| expect(result.testnet).toBe(false); | ||
| expect(result.num_active_channels).toBe(0); // LNbits no maneja canales | ||
| }); | ||
| }); | ||
|
|
||
| describe('mapLnbitsChannelBalance', () => { | ||
| it('should map LNbits balance correctly', async () => { | ||
| const balance = 100000; // 100k sats | ||
|
|
||
| const result = await lnbits_service.mapLnbitsChannelBalance(balance); | ||
|
|
||
| expect(result.balance).toBe('100000'); | ||
| expect(result.local_balance.sat).toBe('100000'); | ||
| expect(result.local_balance.msat).toBe('100000000'); | ||
| expect(result.remote_balance.sat).toBe('0'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('mapLnbitsAddresses', () => { | ||
| it('should map bitcoin address correctly', async () => { | ||
| const address = 'bc1qtest123456789abcdef'; | ||
|
|
||
| const result = await lnbits_service.mapLnbitsAddresses(address); | ||
|
|
||
| expect(result.account_with_addresses).toHaveLength(1); | ||
| expect(result.account_with_addresses[0].addresses[0].address).toBe(address); | ||
| expect(result.account_with_addresses[0].name).toBe('LNbits Onchain'); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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_clientorgrpc_client) is being used at a time, so you may want to make a singleclientvar to simplify.