Skip to content

Commit 3356783

Browse files
feat(express): remove unneeded express routes and use subtype on wallet
BTC-1922 TICKET: BTC-0
1 parent b5b9427 commit 3356783

File tree

18 files changed

+52
-933
lines changed

18 files changed

+52
-933
lines changed

modules/abstract-lightning/src/wallet/custodialLightning.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export type ICustodialLightningWallet = ILightningWallet;
66
export class CustodialLightningWallet extends LightningWallet implements ICustodialLightningWallet {
77
constructor(wallet: sdkcore.IWallet) {
88
super(wallet);
9-
if (wallet.type() !== 'custodial') {
10-
throw new Error(`Invalid lightning wallet type for custodial lightning: ${wallet.type()}`);
9+
if (wallet.subType() !== 'lightningCustody') {
10+
throw new Error(`Invalid lightning wallet type for custodial lightning: ${wallet.subType()}`);
1111
}
1212
}
1313
}

modules/abstract-lightning/src/wallet/selfCustodialLightning.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ export async function updateWalletCoinSpecific(
7070
params: UpdateLightningWalletClientRequest
7171
): Promise<unknown> {
7272
if (!isLightningCoinName(wallet.coin())) {
73-
throw new Error(`cant update lightning wallet coin specific for coin ${wallet.type()}`);
73+
throw new Error(`cant update lightning wallet coin specific for coin ${wallet.subType()}`);
7474
}
75-
if (wallet.type() !== 'hot') {
76-
throw new Error(`cant update lightning wallet coin specific for wallet type ${wallet.type()}`);
75+
if (wallet.subType() !== 'lightningSelfCustody') {
76+
throw new Error(`cant update lightning wallet coin specific for wallet type ${wallet.subType()}`);
7777
}
7878

7979
sdkcore.decodeOrElse(
@@ -112,8 +112,8 @@ export interface ISelfCustodialLightningWallet extends ILightningWallet {
112112
export class SelfCustodialLightningWallet extends LightningWallet implements ISelfCustodialLightningWallet {
113113
constructor(wallet: sdkcore.IWallet) {
114114
super(wallet);
115-
if (wallet.type() !== 'hot') {
116-
throw new Error(`Invalid lightning wallet type for self custodial lightning: ${wallet.type()}`);
115+
if (wallet.subType() !== 'lightningSelfCustody') {
116+
throw new Error(`Invalid lightning wallet type for self custodial lightning: ${wallet.subType()}`);
117117
}
118118
}
119119

modules/abstract-lightning/src/wallet/wallet.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ export function getLightningWallet(wallet: sdkcore.IWallet): ILightningWallet {
1111
throw new Error(`invalid coin for lightning wallet: ${wallet.baseCoin.getFamily()}`);
1212
}
1313

14-
switch (wallet.type()) {
15-
case 'custodial':
14+
switch (wallet.subType()) {
15+
case 'lightningCustody':
1616
return new CustodialLightningWallet(wallet);
17-
case 'hot':
17+
case 'lightningSelfCustody':
1818
return new SelfCustodialLightningWallet(wallet);
1919
default:
20-
throw new Error(`invalid wallet type ${wallet.type()} for lightning coin`);
20+
throw new Error(`invalid wallet type ${wallet.subType()} for lightning coin`);
2121
}
2222
}

modules/bitgo/test/v2/unit/lightning/lightningWallets.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('Lightning wallets', function () {
7272
passphrase: 'pass123',
7373
enterprise: 'ent123',
7474
passcodeEncryptionCode: 'code123',
75-
type: 'custodial',
75+
subType: 'lightningCustody',
7676
})
7777
.should.be.rejectedWith(
7878
"error(s) parsing generate lightning wallet request params: Invalid value 'undefined' supplied to GenerateLightningWalletOptions.label, expected string."
@@ -83,7 +83,7 @@ describe('Lightning wallets', function () {
8383
label: 'my ln wallet',
8484
enterprise: 'ent123',
8585
passcodeEncryptionCode: 'code123',
86-
type: 'custodial',
86+
subType: 'lightningCustody',
8787
})
8888
.should.be.rejectedWith(
8989
"error(s) parsing generate lightning wallet request params: Invalid value 'undefined' supplied to GenerateLightningWalletOptions.passphrase, expected string."
@@ -94,7 +94,7 @@ describe('Lightning wallets', function () {
9494
label: 'my ln wallet',
9595
passphrase: 'pass123',
9696
passcodeEncryptionCode: 'code123',
97-
type: 'custodial',
97+
subType: 'lightningCustody',
9898
})
9999
.should.be.rejectedWith(
100100
"error(s) parsing generate lightning wallet request params: Invalid value 'undefined' supplied to GenerateLightningWalletOptions.enterprise, expected string."
@@ -105,7 +105,7 @@ describe('Lightning wallets', function () {
105105
label: 'my ln wallet',
106106
passphrase: 'pass123',
107107
enterprise: 'ent123',
108-
type: 'custodial',
108+
subType: 'lightningCustody',
109109
})
110110
.should.be.rejectedWith(
111111
"error(s) parsing generate lightning wallet request params: Invalid value 'undefined' supplied to GenerateLightningWalletOptions.passcodeEncryptionCode, expected string."
@@ -117,7 +117,7 @@ describe('Lightning wallets', function () {
117117
passphrase: 'pass123',
118118
enterprise: 'ent123',
119119
passcodeEncryptionCode: 'code123',
120-
type: 'custodial',
120+
subType: 'lightningCustody',
121121
})
122122
.should.be.rejectedWith(
123123
"error(s) parsing generate lightning wallet request params: Invalid value '123' supplied to GenerateLightningWalletOptions.label, expected string."
@@ -129,7 +129,7 @@ describe('Lightning wallets', function () {
129129
passphrase: 123 as any,
130130
enterprise: 'ent123',
131131
passcodeEncryptionCode: 'code123',
132-
type: 'custodial',
132+
subType: 'lightningCustody',
133133
})
134134
.should.be.rejectedWith(
135135
"error(s) parsing generate lightning wallet request params: Invalid value '123' supplied to GenerateLightningWalletOptions.passphrase, expected string."
@@ -141,7 +141,7 @@ describe('Lightning wallets', function () {
141141
passphrase: 'pass123',
142142
enterprise: 123 as any,
143143
passcodeEncryptionCode: 'code123',
144-
type: 'custodial',
144+
subType: 'lightningCustody',
145145
})
146146
.should.be.rejectedWith(
147147
"error(s) parsing generate lightning wallet request params: Invalid value '123' supplied to GenerateLightningWalletOptions.enterprise, expected string."
@@ -153,34 +153,21 @@ describe('Lightning wallets', function () {
153153
passphrase: 'pass123',
154154
enterprise: 'ent123',
155155
passcodeEncryptionCode: 123 as any,
156-
type: 'custodial',
156+
subType: 'lightningCustody',
157157
})
158158
.should.be.rejectedWith(
159159
"error(s) parsing generate lightning wallet request params: Invalid value '123' supplied to GenerateLightningWalletOptions.passcodeEncryptionCode, expected string."
160160
);
161-
162-
await wallets
163-
.generateWallet({
164-
label: 'my ln wallet',
165-
passphrase: 'pass123',
166-
enterprise: 'ent123',
167-
passcodeEncryptionCode: 'code123',
168-
type: 'cold',
169-
})
170-
.should.be.rejectedWith(
171-
'error(s) parsing generate lightning wallet request params: Invalid value \'"cold"\' supplied to GenerateLightningWalletOptions.type.0, expected "custodial".\n' +
172-
'Invalid value \'"cold"\' supplied to GenerateLightningWalletOptions.type.1, expected "hot".'
173-
);
174161
});
175162

176-
for (const type of ['hot', 'custodial'] as const) {
177-
it(`should generate ${type} lightning wallet`, async function () {
163+
for (const subType of ['lightningCustody', 'lightningSelfCustody'] as const) {
164+
it(`should generate ${subType} lightning wallet`, async function () {
178165
const params: GenerateLightningWalletOptions = {
179166
label: 'my ln wallet',
180167
passphrase: 'pass123',
181168
enterprise: 'ent123',
182169
passcodeEncryptionCode: 'code123',
183-
type,
170+
subType: subType as 'lightningCustody' | 'lightningSelfCustody',
184171
};
185172

186173
const validateKeyRequest = (body) => {
@@ -203,7 +190,8 @@ describe('Lightning wallets', function () {
203190
body.label === 'my ln wallet' &&
204191
body.m === 1 &&
205192
body.n === 1 &&
206-
body.type === type &&
193+
body.type === 'hot' &&
194+
body.subType === subType &&
207195
body.enterprise === 'ent123' &&
208196
Array.isArray(body.keys) &&
209197
body.keys.length === 1 &&
@@ -247,7 +235,12 @@ describe('Lightning wallets', function () {
247235
let wallet: LightningWallet;
248236
beforeEach(function () {
249237
wallet = getLightningWallet(
250-
new Wallet(bitgo, basecoin, { id: 'walletId', coin: 'tlnbtc', coinSpecific: { keys: ['def', 'ghi'] } })
238+
new Wallet(bitgo, basecoin, {
239+
id: 'walletId',
240+
coin: 'tlnbtc',
241+
subType: 'lightningCustody',
242+
coinSpecific: { keys: ['def', 'ghi'] },
243+
})
251244
) as LightningWallet;
252245
});
253246

@@ -476,6 +469,7 @@ describe('Lightning wallets', function () {
476469
coin: coinName,
477470
keys: ['abc'],
478471
coinSpecific: { keys: ['def', 'ghi'] },
472+
subType: 'lightningCustody',
479473
};
480474

481475
const userKeyData = {
@@ -588,6 +582,7 @@ describe('Lightning wallets', function () {
588582
coin: coinName,
589583
keys: ['abc'],
590584
coinSpecific: { keys: ['def', 'ghi'] },
585+
subType: 'lightningSelfCustody',
591586
};
592587

593588
const watchOnlyAccounts = {

modules/bitgo/test/v2/unit/lightningWallet.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ describe('LightningV2 Wallet:', function () {
1212
const lnbtcWallet = new Wallet(bitgo, bitgo.coin('lnbtc'), {
1313
id: '123',
1414
coin: 'lnbtc',
15+
subType: 'lightningCustody',
1516
});
1617

1718
const tlntcWallet = new Wallet(bitgo, bitgo.coin('tlnbtc'), {
1819
id: '123',
1920
coin: 'tlntc',
21+
subType: 'lightningCustody',
2022
});
2123

2224
assert(getLightningWallet(lnbtcWallet), 'lnbtc wallet should support lightningV2');

modules/express/src/clientRoutes.ts

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,10 @@ import {
5353
handleInitLightningWallet,
5454
handleUnlockLightningWallet,
5555
} from './lightning/lightningSignerRoutes';
56-
import { handleCreateLightningInvoice, handlePayLightningInvoice } from './lightning/lightningInvoiceRoutes';
57-
import {
58-
handleListLightningInvoices,
59-
handleUpdateLightningWalletCoinSpecific,
60-
handleListLightningTransactions,
61-
handleGetLightningTransaction,
62-
handleGetLightningInvoice,
63-
handleGetLightningPayment,
64-
handleListLightningPayments,
65-
} from './lightning/lightningWalletRoutes';
56+
import { handlePayLightningInvoice } from './lightning/lightningInvoiceRoutes';
57+
import { handleUpdateLightningWalletCoinSpecific } from './lightning/lightningWalletRoutes';
6658
import { ProxyAgent } from 'proxy-agent';
6759
import { isLightningCoinName } from '@bitgo/abstract-lightning';
68-
import { handleGetChannelBackup } from './lightning/lightningBackupRoutes';
6960

7061
const { version } = require('bitgo/package.json');
7162
const pjson = require('../package.json');
@@ -1677,63 +1668,14 @@ export function setupAPIRoutes(app: express.Application, config: Config): void {
16771668
promiseWrapper(handleV2PendingApproval)
16781669
);
16791670

1680-
// lightning - create invoice
1681-
app.post(
1682-
'/api/v2/:coin/wallet/:id/lightning/invoice',
1683-
parseBody,
1684-
prepareBitGo(config),
1685-
promiseWrapper(handleCreateLightningInvoice)
1686-
);
1687-
1688-
// lightning - get invoice
1689-
app.get(
1690-
'/api/v2/:coin/wallet/:id/lightning/invoice/:paymentHash',
1691-
prepareBitGo(config),
1692-
promiseWrapper(handleGetLightningInvoice)
1693-
);
1694-
1695-
// lightning - list invoices
1696-
app.get(
1697-
'/api/v2/:coin/wallet/:id/lightning/invoices',
1698-
prepareBitGo(config),
1699-
promiseWrapper(handleListLightningInvoices)
1700-
);
1701-
17021671
// lightning - pay invoice
17031672
app.post(
1704-
'/api/v2/:coin/wallet/:id/lightning/pay',
1673+
'/api/v2/:coin/wallet/:id/lightning/payment',
17051674
parseBody,
17061675
prepareBitGo(config),
17071676
promiseWrapper(handlePayLightningInvoice)
17081677
);
17091678

1710-
// lightning - get payment
1711-
app.get(
1712-
'/api/v2/:coin/wallet/:id/lightning/payment/:paymentHash',
1713-
prepareBitGo(config),
1714-
promiseWrapper(handleGetLightningPayment)
1715-
);
1716-
1717-
//lightning - list payments
1718-
app.get('/api/v2/:coin/wallet/:id/lightning/payment', promiseWrapper(handleListLightningPayments));
1719-
1720-
// lightning - get transaction
1721-
app.get(
1722-
'/api/v2/:coin/wallet/:id/lightning/transaction/:txid',
1723-
prepareBitGo(config),
1724-
promiseWrapper(handleGetLightningTransaction)
1725-
);
1726-
1727-
// lightning - list transactions
1728-
app.get(
1729-
'/api/v2/:coin/wallet/:id/lightning/transaction',
1730-
prepareBitGo(config),
1731-
promiseWrapper(handleListLightningTransactions)
1732-
);
1733-
1734-
// lightning - backup
1735-
app.get('/api/v2/:coin/wallet/:id/lightning/backup', prepareBitGo(config), promiseWrapper(handleGetChannelBackup));
1736-
17371679
// any other API v2 call
17381680
app.use('/api/v2/user/*', parseBody, prepareBitGo(config), promiseWrapper(handleV2UserREST));
17391681
app.use('/api/v2/:coin/*', parseBody, prepareBitGo(config), promiseWrapper(handleV2CoinSpecificREST));

modules/express/src/lightning/lightningBackupRoutes.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

modules/express/src/lightning/lightningInvoiceRoutes.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
import * as express from 'express';
22
import { ApiResponseError } from '../errors';
3-
import { CreateInvoiceBody, getLightningWallet, SubmitPaymentParams } from '@bitgo/abstract-lightning';
3+
import { getLightningWallet, SubmitPaymentParams } from '@bitgo/abstract-lightning';
44
import { decodeOrElse } from '@bitgo/sdk-core';
55

6-
export async function handleCreateLightningInvoice(req: express.Request): Promise<any> {
7-
const bitgo = req.bitgo;
8-
9-
const params = decodeOrElse(CreateInvoiceBody.name, CreateInvoiceBody, req.body, (error) => {
10-
throw new ApiResponseError(`Invalid request body to create lightning invoice: ${error}`, 400);
11-
});
12-
13-
const coin = bitgo.coin(req.params.coin);
14-
const wallet = await coin.wallets().get({ id: req.params.id });
15-
const lightningWallet = getLightningWallet(wallet);
16-
17-
return await lightningWallet.createInvoice(params);
18-
}
19-
206
export async function handlePayLightningInvoice(req: express.Request): Promise<any> {
217
const bitgo = req.bitgo;
228
const params = decodeOrElse(SubmitPaymentParams.name, SubmitPaymentParams, req.body, (error) => {

modules/express/src/lightning/lightningSignerRoutes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export async function handleInitLightningWallet(req: express.Request): Promise<u
8585
);
8686

8787
const wallet = await coin.wallets().get({ id: walletId });
88-
if (wallet.type() !== 'hot') {
88+
if (wallet.subType() !== 'lightningSelfCustody') {
8989
throw new ApiResponseError(`not a self custodial lighting wallet ${walletId}`, 400);
9090
}
9191
const lndSignerClient = await LndSignerClient.create(walletId, req.config);
@@ -139,7 +139,7 @@ export async function handleCreateSignerMacaroon(req: express.Request): Promise<
139139
);
140140

141141
const wallet = await coin.wallets().get({ id: walletId });
142-
if (wallet.type() !== 'hot') {
142+
if (wallet.subType() !== 'lightningSelfCustody') {
143143
throw new ApiResponseError(`not a self custodial lighting wallet ${walletId}`, 400);
144144
}
145145
const watchOnlyIp = wallet.coinSpecific()?.watchOnlyExternalIp;

0 commit comments

Comments
 (0)