Skip to content

Commit

Permalink
feat: add skeleton for v2 lightning
Browse files Browse the repository at this point in the history
Ticket: BTC-1776
  • Loading branch information
lcovar committed Feb 5, 2025
1 parent 4eebcaa commit 5bd613e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
35 changes: 35 additions & 0 deletions modules/bitgo/test/v2/unit/lightningWallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { strict as assert } from 'assert';
import { TestBitGo } from '@bitgo/sdk-test';
import { BitGo } from '../../../src';
import { Wallet } from '@bitgo/sdk-core';

describe('LightningV2 Wallet:', function () {
const bitgo = TestBitGo.decorate(BitGo, { env: 'test' });
bitgo.initializeTestVars();

it('should allow lightningV2 wallets to be created for supported coins', function () {
const lnbtcWallet = new Wallet(bitgo, bitgo.coin('lnbtc'), {
id: '123',
coin: 'lnbtc',
});

const tlntcWallet = new Wallet(bitgo, bitgo.coin('tlnbtc'), {
id: '123',
coin: 'tlntc',
});

assert(lnbtcWallet.lightningV2(), 'lnbtc wallet should support lightningV2');
assert(tlntcWallet.lightningV2(), 'tlnbtc wallet should support lightningV2');
});

it('should throw error when creating lightningV2 wallet for unsupported coins', function () {
const btcWallet = new Wallet(bitgo, bitgo.coin('btc'), {
id: '123',
coin: 'btc',
});

assert.throws(() => {
btcWallet.lightningV2();
}, /Lightning not supported for btc/);
});
});
2 changes: 2 additions & 0 deletions modules/sdk-core/src/bitgo/wallet/iWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { ILightning } from '../lightning/custodial';
import { SerializedNtilde } from '../../account-lib/mpc/tss/ecdsa/types';
import { IAddressBook } from '../address-book';
import { WalletUser } from '@bitgo/public-types';
import { ILightningWallet } from './lightning';

export interface MaximumSpendableOptions {
minValue?: number | string;
Expand Down Expand Up @@ -882,6 +883,7 @@ export interface IWallet {
sendTokenEnablement(params?: PrebuildAndSignTransactionOptions): Promise<any>;
sendTokenEnablements(params?: BuildTokenEnablementOptions): Promise<any>;
lightning(): ILightning;
lightningV2(): ILightningWallet;
signMessage(params: WalletSignMessageOptions): Promise<SignedMessage>;
signTypedData(params: WalletSignTypedDataOptions): Promise<SignedMessage>;
fetchCrossChainUTXOs(params: FetchCrossChainUTXOsOptions): Promise<CrossChainUTXO[]>;
Expand Down
31 changes: 31 additions & 0 deletions modules/sdk-core/src/bitgo/wallet/lightning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { IWallet } from './iWallet';

export interface ILightningWallet {
/**
* Creates a lightning invoice
* @param params Invoice parameters (to be defined)
*/
createInvoice(params: unknown): Promise<unknown>;

/**
* Pay a lightning invoice
* @param params Payment parameters (to be defined)
*/
payInvoice(params: unknown): Promise<unknown>;
}

export class SelfCustodialLightningWallet implements ILightningWallet {
public wallet: IWallet;

constructor(wallet: IWallet) {
this.wallet = wallet;
}

async createInvoice(params: unknown): Promise<unknown> {
throw new Error('Method not implemented.');
}

async payInvoice(params: unknown): Promise<unknown> {
throw new Error('Method not implemented.');
}
}
12 changes: 12 additions & 0 deletions modules/sdk-core/src/bitgo/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ import { TxSendBody } from '@bitgo/public-types';
import { AddressBook, IAddressBook } from '../address-book';
import { IRequestTracer } from '../../api';
import { getTxRequestApiVersion, validateTxRequestApiVersion } from '../utils/txRequest';
import { ILightningWallet, SelfCustodialLightningWallet } from './lightning';
import { isLightningCoinName } from '../lightning';

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

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

/**
* Return a lightwallet instance if the coin supports it
*/
public lightningV2(): ILightningWallet {
if (!isLightningCoinName(this.baseCoin.getChain())) {
throw new Error(`Lightning not supported for ${this.coin()}`);
}
return new SelfCustodialLightningWallet(this);
}

/* MARK: TSS Helpers */

/**
Expand Down

0 comments on commit 5bd613e

Please sign in to comment.