From 5bd613e8569a399d16cf136e4291c58c550e45ce Mon Sep 17 00:00:00 2001 From: Luis Covarrubias Date: Tue, 4 Feb 2025 18:02:16 -0800 Subject: [PATCH] feat: add skeleton for v2 lightning Ticket: BTC-1776 --- modules/bitgo/test/v2/unit/lightningWallet.ts | 35 +++++++++++++++++++ modules/sdk-core/src/bitgo/wallet/iWallet.ts | 2 ++ .../sdk-core/src/bitgo/wallet/lightning.ts | 31 ++++++++++++++++ modules/sdk-core/src/bitgo/wallet/wallet.ts | 12 +++++++ 4 files changed, 80 insertions(+) create mode 100644 modules/bitgo/test/v2/unit/lightningWallet.ts create mode 100644 modules/sdk-core/src/bitgo/wallet/lightning.ts diff --git a/modules/bitgo/test/v2/unit/lightningWallet.ts b/modules/bitgo/test/v2/unit/lightningWallet.ts new file mode 100644 index 0000000000..be1eb8e3cd --- /dev/null +++ b/modules/bitgo/test/v2/unit/lightningWallet.ts @@ -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/); + }); +}); diff --git a/modules/sdk-core/src/bitgo/wallet/iWallet.ts b/modules/sdk-core/src/bitgo/wallet/iWallet.ts index e547168e37..cb4a404441 100644 --- a/modules/sdk-core/src/bitgo/wallet/iWallet.ts +++ b/modules/sdk-core/src/bitgo/wallet/iWallet.ts @@ -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; @@ -882,6 +883,7 @@ export interface IWallet { sendTokenEnablement(params?: PrebuildAndSignTransactionOptions): Promise; sendTokenEnablements(params?: BuildTokenEnablementOptions): Promise; lightning(): ILightning; + lightningV2(): ILightningWallet; signMessage(params: WalletSignMessageOptions): Promise; signTypedData(params: WalletSignTypedDataOptions): Promise; fetchCrossChainUTXOs(params: FetchCrossChainUTXOsOptions): Promise; diff --git a/modules/sdk-core/src/bitgo/wallet/lightning.ts b/modules/sdk-core/src/bitgo/wallet/lightning.ts new file mode 100644 index 0000000000..bc5788ed84 --- /dev/null +++ b/modules/sdk-core/src/bitgo/wallet/lightning.ts @@ -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; + + /** + * Pay a lightning invoice + * @param params Payment parameters (to be defined) + */ + payInvoice(params: unknown): Promise; +} + +export class SelfCustodialLightningWallet implements ILightningWallet { + public wallet: IWallet; + + constructor(wallet: IWallet) { + this.wallet = wallet; + } + + async createInvoice(params: unknown): Promise { + throw new Error('Method not implemented.'); + } + + async payInvoice(params: unknown): Promise { + throw new Error('Method not implemented.'); + } +} diff --git a/modules/sdk-core/src/bitgo/wallet/wallet.ts b/modules/sdk-core/src/bitgo/wallet/wallet.ts index b7c411ffa7..de13123da3 100644 --- a/modules/sdk-core/src/bitgo/wallet/wallet.ts +++ b/modules/sdk-core/src/bitgo/wallet/wallet.ts @@ -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'); @@ -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 */ /**