Skip to content

Commit 916d430

Browse files
committed
feat(express): migrate getlightningstate to typed routes
TICKET: WP-5447
1 parent cb1d499 commit 916d430

File tree

6 files changed

+57
-8
lines changed

6 files changed

+57
-8
lines changed

modules/express/src/clientRoutes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,7 @@ export function setupAPIRoutes(app: express.Application, config: Config): void {
15901590
);
15911591

15921592
router.post('express.v1.wallet.signTransaction', [prepareBitGo(config), typedPromiseWrapper(handleSignTransaction)]);
1593+
router.get('express.lightning.getState', [prepareBitGo(config), typedPromiseWrapper(handleGetLightningWalletState)]);
15931594

15941595
app.post('/api/v1/wallet/:id/simpleshare', parseBody, prepareBitGo(config), promiseWrapper(handleShareWallet));
15951596
router.post('express.v1.wallet.acceptShare', [prepareBitGo(config), typedPromiseWrapper(handleAcceptShare)]);
@@ -1808,5 +1809,4 @@ export function setupLightningSignerNodeRoutes(app: express.Application, config:
18081809
prepareBitGo(config),
18091810
promiseWrapper(handleUnlockLightningWallet)
18101811
);
1811-
app.get('/api/v2/:coin/wallet/:id/state', prepareBitGo(config), promiseWrapper(handleGetLightningWalletState));
18121812
}

modules/express/src/lightning/lightningSignerRoutes.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,13 @@ export async function handleCreateSignerMacaroon(req: express.Request): Promise<
187187
/**
188188
* Handle the request to get the state of a wallet from the signer.
189189
*/
190-
export async function handleGetLightningWalletState(req: express.Request): Promise<GetWalletStateResponse> {
191-
const coinName = req.params.coin;
190+
export async function handleGetLightningWalletState(
191+
req: import('../typedRoutes/api').ExpressApiRouteRequest<'express.lightning.getState', 'get'>
192+
): Promise<GetWalletStateResponse> {
193+
const { coin: coinName, walletId } = req.decoded;
192194
if (!isLightningCoinName(coinName)) {
193195
throw new ApiResponseError(`Invalid coin to get lightning wallet state: ${coinName}`, 400);
194196
}
195-
const walletId = req.params.id;
196-
if (typeof walletId !== 'string') {
197-
throw new ApiResponseError(`Invalid wallet id: ${walletId}`, 400);
198-
}
199197

200198
const lndSignerClient = await LndSignerClient.create(walletId, req.config);
201199
return await lndSignerClient.getWalletState();

modules/express/src/typedRoutes/api/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { PostAcceptShare } from './v1/acceptShare';
1212
import { PostSimpleCreate } from './v1/simpleCreate';
1313
import { PutPendingApproval } from './v1/pendingApproval';
1414
import { PostSignTransaction } from './v1/signTransaction';
15+
import { GetLightningState } from './v2/lightningState';
1516

1617
export const ExpressApi = apiSpec({
1718
'express.ping': {
@@ -44,6 +45,9 @@ export const ExpressApi = apiSpec({
4445
'express.v1.wallet.signTransaction': {
4546
post: PostSignTransaction,
4647
},
48+
'express.lightning.getState': {
49+
get: GetLightningState,
50+
},
4751
});
4852

4953
export type ExpressApi = typeof ExpressApi;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as t from 'io-ts';
2+
import { httpRoute, httpRequest } from '@api-ts/io-ts-http';
3+
import { BitgoExpressError } from '../../schemas/error';
4+
import { WalletState } from '../../../lightning/codecs';
5+
6+
/**
7+
* Path parameters for getting lightning node state
8+
* @property {string} coin - A lightning coin name (e.g., lnbtc or tlnbtc)
9+
* @property {string} walletId - The ID of the lightning self-custody wallet
10+
*/
11+
export const LightningStateParams = {
12+
coin: t.string,
13+
walletId: t.string,
14+
};
15+
16+
/**
17+
* Lightning - Get node state
18+
*
19+
* This is only used for self-custody lightning. Get the current state of the lightning node.
20+
*
21+
* @operationId express.lightning.getState
22+
*/
23+
export const GetLightningState = httpRoute({
24+
method: 'GET',
25+
path: '/api/v2/{coin}/wallet/{walletId}/state',
26+
request: httpRequest({
27+
params: LightningStateParams,
28+
}),
29+
response: {
30+
200: t.type({
31+
state: WalletState,
32+
}),
33+
400: BitgoExpressError,
34+
},
35+
});

modules/express/test/unit/clientRoutes/lightning/lightningSignerRoutes.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,16 @@ describe('Lightning signer routes', () => {
153153
params: {
154154
coin: 'tlnbtc',
155155
id: apiData.wallet.id,
156+
walletId: apiData.wallet.id,
157+
},
158+
decoded: {
159+
coin: 'tlnbtc',
160+
walletId: apiData.wallet.id,
156161
},
157162
config: {
158163
lightningSignerFileSystemPath: 'lightningSignerFileSystemPath',
159164
},
160-
} as unknown as express.Request;
165+
} as any;
161166

162167
await handleGetLightningWalletState(req);
163168

modules/express/test/unit/typedRoutes/decode.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { EncryptRequestBody } from '../../../src/typedRoutes/api/common/encrypt'
55
import { LoginRequest } from '../../../src/typedRoutes/api/common/login';
66
import { VerifyAddressBody } from '../../../src/typedRoutes/api/common/verifyAddress';
77
import { SimpleCreateRequestBody } from '../../../src/typedRoutes/api/v1/simpleCreate';
8+
import { LightningStateParams } from '../../../src/typedRoutes/api/v2/lightningState';
89

910
export function assertDecode<T>(codec: t.Type<T, unknown>, input: unknown): T {
1011
const result = codec.decode(input);
@@ -100,4 +101,10 @@ describe('io-ts decode tests', function () {
100101
passphrase: 'pass',
101102
});
102103
});
104+
it('express.lightning.getState params valid', function () {
105+
assertDecode(t.type(LightningStateParams), { coin: 'lnbtc', walletId: 'wallet123' });
106+
});
107+
it('express.lightning.getState params invalid', function () {
108+
assert.throws(() => assertDecode(t.type(LightningStateParams), { coin: 'lnbtc' }));
109+
});
103110
});

0 commit comments

Comments
 (0)