-
Notifications
You must be signed in to change notification settings - Fork 286
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
feat: add express endpoints for lightning invoice #5643
Open
007harshmahajan
wants to merge
1
commit into
master
Choose a base branch
from
express_lightning_apis
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.
+242
−0
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 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 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,41 @@ | ||
import * as express from 'express'; | ||
import { ApiResponseError } from '../errors'; | ||
import { getLightningWallet, InvoiceQuery, UpdateLightningWalletSignedRequest } from '@bitgo/abstract-lightning'; | ||
import { decodeOrElse } from '@bitgo/sdk-core'; | ||
import * as t from 'io-ts'; | ||
|
||
const UpdateWalletRequest = t.intersection([ | ||
UpdateLightningWalletSignedRequest, | ||
t.type({ | ||
passphrase: t.string, | ||
}), | ||
]); | ||
|
||
export async function handleListLightningInvoices(req: express.Request): Promise<unknown> { | ||
const bitgo = req.bitgo; | ||
|
||
const params = decodeOrElse(InvoiceQuery.name, InvoiceQuery, req.query, (error) => { | ||
throw new ApiResponseError(`Invalid query parameters for listing lightning invoices: ${error}`, 400); | ||
}); | ||
|
||
const coin = bitgo.coin(req.params.coin); | ||
const wallet = await coin.wallets().get({ id: req.params.id }); | ||
const lightningWallet = getLightningWallet(wallet); | ||
|
||
return await lightningWallet.listInvoices(params); | ||
} | ||
|
||
export async function handleUpdateLightningWalletCoinSpecific(req: express.Request): Promise<unknown> { | ||
const bitgo = req.bitgo; | ||
|
||
const { passphrase, ...params } = decodeOrElse('UpdateWalletRequest', UpdateWalletRequest, req.body, (_) => { | ||
// DON'T throw errors from decodeOrElse. It could leak sensitive information. | ||
throw new ApiResponseError('Invalid request body to update lightning wallet coin specific', 400); | ||
}); | ||
|
||
const coin = bitgo.coin(req.params.coin); | ||
const wallet = await coin.wallets().get({ id: req.params.id }); | ||
const lightningWallet = getLightningWallet(wallet); | ||
|
||
return await lightningWallet.updateWalletCoinSpecific(params, passphrase); | ||
} |
186 changes: 186 additions & 0 deletions
186
modules/express/test/unit/lightning/lightningWalletRoutes.test.ts
This file contains 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,186 @@ | ||
import * as sinon from 'sinon'; | ||
import * as should from 'should'; | ||
import * as express from 'express'; | ||
import { | ||
handleListLightningInvoices, | ||
handleUpdateLightningWalletCoinSpecific, | ||
} from '../../../src/lightning/lightningWalletRoutes'; | ||
import { BitGo } from 'bitgo'; | ||
import { InvoiceInfo } from '@bitgo/abstract-lightning'; | ||
|
||
describe('Lightning Wallet Routes', () => { | ||
let bitgo; | ||
const coin = 'tlnbtc'; | ||
|
||
const mockRequestObject = (params: { body?: any; params?: any; query?: any; bitgo?: any }) => { | ||
const req: Partial<express.Request> = {}; | ||
req.body = params.body || {}; | ||
req.params = params.params || {}; | ||
req.query = params.query || {}; | ||
req.bitgo = params.bitgo; | ||
return req as express.Request; | ||
}; | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
describe('List Lightning Invoices', () => { | ||
it('should successfully list lightning invoices', async () => { | ||
const queryParams = { | ||
status: 'open', | ||
limit: '10', | ||
startDate: '2024-01-01', | ||
endDate: '2024-01-31', | ||
}; | ||
|
||
const mockResponse: InvoiceInfo[] = [ | ||
{ | ||
invoice: 'lntb100u1p3h2jk3pp5...', | ||
paymentHash: 'abc123', | ||
valueMsat: 10000n, | ||
walletId: 'testWalletId', | ||
status: 'open', | ||
expiresAt: new Date(), | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}, | ||
]; | ||
|
||
const listInvoicesSpy = sinon.stub().resolves(mockResponse); | ||
const mockLightningWallet = { | ||
listInvoices: listInvoicesSpy, | ||
}; | ||
|
||
const proxyquire = require('proxyquire'); | ||
const lightningRoutes = proxyquire('../../../src/lightning/lightningWalletRoutes', { | ||
'@bitgo/abstract-lightning': { | ||
getLightningWallet: () => mockLightningWallet, | ||
}, | ||
}); | ||
|
||
const walletStub = {}; | ||
const coinStub = { | ||
wallets: () => ({ get: sinon.stub().resolves(walletStub) }), | ||
}; | ||
const stubBitgo = sinon.createStubInstance(BitGo as any, { coin: coinStub }); | ||
|
||
const req = mockRequestObject({ | ||
params: { id: 'testWalletId', coin }, | ||
query: queryParams, | ||
bitgo: stubBitgo, | ||
}); | ||
|
||
const result = await lightningRoutes.handleListLightningInvoices(req); | ||
|
||
should(result).deepEqual(mockResponse); | ||
should(listInvoicesSpy).be.calledOnce(); | ||
const [firstArg] = listInvoicesSpy.getCall(0).args; | ||
|
||
should(firstArg).have.property('status', 'open'); | ||
should(firstArg).have.property('limit', 10n); | ||
should(firstArg.startDate).be.instanceOf(Date); | ||
should(firstArg.endDate).be.instanceOf(Date); | ||
}); | ||
|
||
it('should handle invalid query parameters', async () => { | ||
const invalidQuery = { | ||
status: 'invalidStatus', | ||
limit: 'notANumber', | ||
}; | ||
|
||
const req = mockRequestObject({ | ||
params: { id: 'testWalletId', coin }, | ||
query: invalidQuery, | ||
}); | ||
req.bitgo = bitgo; | ||
|
||
await should(handleListLightningInvoices(req)).be.rejectedWith( | ||
/Invalid query parameters for listing lightning invoices: Invalid value '"invalidStatus"' supplied to InvoiceQuery\.status\.0, expected "open"/ | ||
); | ||
}); | ||
}); | ||
|
||
describe('Update Wallet Coin Specific', () => { | ||
it('should successfully update wallet coin specific data', async () => { | ||
const inputParams = { | ||
encryptedSignerMacaroon: 'encrypted-macaroon-data', | ||
signerHost: 'signer.example.com', | ||
passphrase: 'wallet-password-123', | ||
}; | ||
|
||
const expectedResponse = { | ||
coinSpecific: { | ||
updated: true, | ||
}, | ||
}; | ||
|
||
const updateStub = sinon.stub().resolves(expectedResponse); | ||
const mockLightningWallet = { | ||
updateWalletCoinSpecific: updateStub, | ||
}; | ||
|
||
const proxyquire = require('proxyquire'); | ||
const lightningRoutes = proxyquire('../../../src/lightning/lightningWalletRoutes', { | ||
'@bitgo/abstract-lightning': { | ||
getLightningWallet: () => mockLightningWallet, | ||
}, | ||
}); | ||
|
||
const walletStub = {}; | ||
const coinStub = { | ||
wallets: () => ({ get: sinon.stub().resolves(walletStub) }), | ||
}; | ||
const stubBitgo = sinon.createStubInstance(BitGo as any, { coin: coinStub }); | ||
|
||
const req = mockRequestObject({ | ||
params: { id: 'testWalletId', coin }, | ||
body: inputParams, | ||
bitgo: stubBitgo, | ||
}); | ||
|
||
const result = await lightningRoutes.handleUpdateLightningWalletCoinSpecific(req); | ||
|
||
should(result).deepEqual(expectedResponse); | ||
should(updateStub).be.calledOnce(); | ||
const [firstArg, secondArg] = updateStub.getCall(0).args; | ||
should(firstArg).have.property('encryptedSignerMacaroon', 'encrypted-macaroon-data'); | ||
should(firstArg).have.property('signerHost', 'signer.example.com'); | ||
should(secondArg).equal('wallet-password-123'); | ||
}); | ||
|
||
it('should throw error when passphrase is missing', async () => { | ||
const invalidParams = { | ||
encryptedSignerMacaroon: 'encrypted-data', | ||
signerHost: 'signer.example.com', | ||
}; | ||
|
||
const req = mockRequestObject({ | ||
params: { id: 'testWalletId', coin }, | ||
body: invalidParams, | ||
}); | ||
req.bitgo = bitgo; | ||
|
||
await should(handleUpdateLightningWalletCoinSpecific(req)).be.rejectedWith( | ||
'Invalid request body to update lightning wallet coin specific' | ||
); | ||
}); | ||
|
||
it('should handle invalid request body', async () => { | ||
const invalidParams = { | ||
signerHost: 12345, // invalid type | ||
passphrase: 'valid-pass', | ||
}; | ||
|
||
const req = mockRequestObject({ | ||
params: { id: 'testWalletId', coin }, | ||
body: invalidParams, | ||
}); | ||
req.bitgo = bitgo; | ||
|
||
await should(handleUpdateLightningWalletCoinSpecific(req)).be.rejectedWith( | ||
'Invalid request body to update lightning wallet coin specific' | ||
); | ||
}); | ||
}); | ||
}); |
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.
This should contain
signerTlsKey
instead ofencryptedSignerTlsKey
.handleUpdateLightningWalletCoinSpecific
should encryptsignerTlsKey
toencryptedSignerTlsKey
using wallet passphrase (similar tosignerAdminMacaroon
->encryptedSignerAdminMacaroon
)