Skip to content

Commit d8ec7cf

Browse files
authored
Merge pull request #5479 from BitGo/BTC-1776-lightning-skeleton-v2
feat: add skeleton for v2 lightning
2 parents 1e542c7 + 5bd613e commit d8ec7cf

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { strict as assert } from 'assert';
2+
import { TestBitGo } from '@bitgo/sdk-test';
3+
import { BitGo } from '../../../src';
4+
import { Wallet } from '@bitgo/sdk-core';
5+
6+
describe('LightningV2 Wallet:', function () {
7+
const bitgo = TestBitGo.decorate(BitGo, { env: 'test' });
8+
bitgo.initializeTestVars();
9+
10+
it('should allow lightningV2 wallets to be created for supported coins', function () {
11+
const lnbtcWallet = new Wallet(bitgo, bitgo.coin('lnbtc'), {
12+
id: '123',
13+
coin: 'lnbtc',
14+
});
15+
16+
const tlntcWallet = new Wallet(bitgo, bitgo.coin('tlnbtc'), {
17+
id: '123',
18+
coin: 'tlntc',
19+
});
20+
21+
assert(lnbtcWallet.lightningV2(), 'lnbtc wallet should support lightningV2');
22+
assert(tlntcWallet.lightningV2(), 'tlnbtc wallet should support lightningV2');
23+
});
24+
25+
it('should throw error when creating lightningV2 wallet for unsupported coins', function () {
26+
const btcWallet = new Wallet(bitgo, bitgo.coin('btc'), {
27+
id: '123',
28+
coin: 'btc',
29+
});
30+
31+
assert.throws(() => {
32+
btcWallet.lightningV2();
33+
}, /Lightning not supported for btc/);
34+
});
35+
});

modules/sdk-core/src/bitgo/wallet/iWallet.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { ILightning } from '../lightning/custodial';
3232
import { SerializedNtilde } from '../../account-lib/mpc/tss/ecdsa/types';
3333
import { IAddressBook } from '../address-book';
3434
import { WalletUser } from '@bitgo/public-types';
35+
import { ILightningWallet } from './lightning';
3536

3637
export interface MaximumSpendableOptions {
3738
minValue?: number | string;
@@ -882,6 +883,7 @@ export interface IWallet {
882883
sendTokenEnablement(params?: PrebuildAndSignTransactionOptions): Promise<any>;
883884
sendTokenEnablements(params?: BuildTokenEnablementOptions): Promise<any>;
884885
lightning(): ILightning;
886+
lightningV2(): ILightningWallet;
885887
signMessage(params: WalletSignMessageOptions): Promise<SignedMessage>;
886888
signTypedData(params: WalletSignTypedDataOptions): Promise<SignedMessage>;
887889
fetchCrossChainUTXOs(params: FetchCrossChainUTXOsOptions): Promise<CrossChainUTXO[]>;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { IWallet } from './iWallet';
2+
3+
export interface ILightningWallet {
4+
/**
5+
* Creates a lightning invoice
6+
* @param params Invoice parameters (to be defined)
7+
*/
8+
createInvoice(params: unknown): Promise<unknown>;
9+
10+
/**
11+
* Pay a lightning invoice
12+
* @param params Payment parameters (to be defined)
13+
*/
14+
payInvoice(params: unknown): Promise<unknown>;
15+
}
16+
17+
export class SelfCustodialLightningWallet implements ILightningWallet {
18+
public wallet: IWallet;
19+
20+
constructor(wallet: IWallet) {
21+
this.wallet = wallet;
22+
}
23+
24+
async createInvoice(params: unknown): Promise<unknown> {
25+
throw new Error('Method not implemented.');
26+
}
27+
28+
async payInvoice(params: unknown): Promise<unknown> {
29+
throw new Error('Method not implemented.');
30+
}
31+
}

modules/sdk-core/src/bitgo/wallet/wallet.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ import { TxSendBody } from '@bitgo/public-types';
111111
import { AddressBook, IAddressBook } from '../address-book';
112112
import { IRequestTracer } from '../../api';
113113
import { getTxRequestApiVersion, validateTxRequestApiVersion } from '../utils/txRequest';
114+
import { ILightningWallet, SelfCustodialLightningWallet } from './lightning';
115+
import { isLightningCoinName } from '../lightning';
114116

115117
const debug = require('debug')('bitgo:v2:wallet');
116118

@@ -3075,6 +3077,16 @@ export class Wallet implements IWallet {
30753077
return new Lightning(this.bitgo, this);
30763078
}
30773079

3080+
/**
3081+
* Return a lightwallet instance if the coin supports it
3082+
*/
3083+
public lightningV2(): ILightningWallet {
3084+
if (!isLightningCoinName(this.baseCoin.getChain())) {
3085+
throw new Error(`Lightning not supported for ${this.coin()}`);
3086+
}
3087+
return new SelfCustodialLightningWallet(this);
3088+
}
3089+
30783090
/* MARK: TSS Helpers */
30793091

30803092
/**

0 commit comments

Comments
 (0)