@@ -16,6 +16,8 @@ import {
16
16
getLightningAuthKeychains ,
17
17
updateWalletCoinSpecific ,
18
18
LightningOnchainWithdrawParams ,
19
+ PaymentInfo ,
20
+ PaymentQuery ,
19
21
} from '@bitgo/abstract-lightning' ;
20
22
21
23
import { BitGo , common , GenerateLightningWalletOptions , Wallet , Wallets } from '../../../../src' ;
@@ -264,17 +266,56 @@ describe('Lightning wallets', function () {
264
266
const listInvoicesNock = nock ( bgUrl )
265
267
. get ( `/api/v2/wallet/${ wallet . wallet . id ( ) } /lightning/invoice` )
266
268
. query ( InvoiceQuery . encode ( query ) )
267
- . reply ( 200 , [ InvoiceInfo . encode ( invoice ) ] ) ;
269
+ . reply ( 200 , { invoices : [ InvoiceInfo . encode ( invoice ) ] } ) ;
268
270
const invoiceResponse = await wallet . listInvoices ( query ) ;
269
- assert . strictEqual ( invoiceResponse . length , 1 ) ;
270
- assert . deepStrictEqual ( invoiceResponse [ 0 ] , invoice ) ;
271
+ assert . strictEqual ( invoiceResponse . invoices . length , 1 ) ;
272
+ assert . deepStrictEqual ( invoiceResponse . invoices [ 0 ] , invoice ) ;
273
+ listInvoicesNock . done ( ) ;
274
+ } ) ;
275
+
276
+ it ( 'should work properly with pagination while listing invoices' , async function ( ) {
277
+ const invoice1 : InvoiceInfo = {
278
+ valueMsat : 1000n ,
279
+ paymentHash : 'foo1' ,
280
+ invoice : 'tlnfoobar1' ,
281
+ walletId : wallet . wallet . id ( ) ,
282
+ status : 'open' ,
283
+ expiresAt : new Date ( ) ,
284
+ createdAt : new Date ( ) ,
285
+ updatedAt : new Date ( ) ,
286
+ } ;
287
+ const invoice2 : InvoiceInfo = {
288
+ ...invoice1 ,
289
+ paymentHash : 'foo2' ,
290
+ invoice : 'tlnfoobar2' ,
291
+ } ;
292
+ const invoice3 : InvoiceInfo = {
293
+ ...invoice1 ,
294
+ paymentHash : 'foo3' ,
295
+ invoice : 'tlnfoobar3' ,
296
+ } ;
297
+ const allInvoices = [ InvoiceInfo . encode ( invoice1 ) , InvoiceInfo . encode ( invoice2 ) , InvoiceInfo . encode ( invoice3 ) ] ;
298
+ const query = {
299
+ status : 'open' ,
300
+ startDate : new Date ( ) ,
301
+ limit : 2n ,
302
+ } as InvoiceQuery ;
303
+ const listInvoicesNock = nock ( bgUrl )
304
+ . get ( `/api/v2/wallet/${ wallet . wallet . id ( ) } /lightning/invoice` )
305
+ . query ( InvoiceQuery . encode ( query ) )
306
+ . reply ( 200 , { invoices : allInvoices . slice ( 0 , 2 ) , nextBatchPrevId : invoice2 . paymentHash } ) ;
307
+ const invoiceResponse = await wallet . listInvoices ( query ) ;
308
+ assert . strictEqual ( invoiceResponse . invoices . length , 2 ) ;
309
+ assert . deepStrictEqual ( invoiceResponse . invoices [ 0 ] , invoice1 ) ;
310
+ assert . deepStrictEqual ( invoiceResponse . invoices [ 1 ] , invoice2 ) ;
311
+ assert . strictEqual ( invoiceResponse . nextBatchPrevId , invoice2 . paymentHash ) ;
271
312
listInvoicesNock . done ( ) ;
272
313
} ) ;
273
314
274
315
it ( 'listInvoices should throw error if wp response is invalid' , async function ( ) {
275
316
const listInvoicesNock = nock ( bgUrl )
276
317
. get ( `/api/v2/wallet/${ wallet . wallet . id ( ) } /lightning/invoice` )
277
- . reply ( 200 , [ { valueMsat : '1000' } ] ) ;
318
+ . reply ( 200 , { invoices : [ { valueMsat : '1000' } ] } ) ;
278
319
await assert . rejects ( async ( ) => await wallet . listInvoices ( { } ) , / I n v a l i d l i s t i n v o i c e s r e s p o n s e / ) ;
279
320
listInvoicesNock . done ( ) ;
280
321
} ) ;
@@ -464,6 +505,99 @@ describe('Lightning wallets', function () {
464
505
} ) ;
465
506
} ) ;
466
507
508
+ describe ( 'payments' , function ( ) {
509
+ let wallet : LightningWallet ;
510
+ beforeEach ( function ( ) {
511
+ wallet = getLightningWallet (
512
+ new Wallet ( bitgo , basecoin , {
513
+ id : 'walletId' ,
514
+ coin : 'tlnbtc' ,
515
+ subType : 'lightningCustody' ,
516
+ coinSpecific : { keys : [ 'def' , 'ghi' ] } ,
517
+ } )
518
+ ) as LightningWallet ;
519
+ } ) ;
520
+
521
+ it ( 'should get payments' , async function ( ) {
522
+ const payment : PaymentInfo = {
523
+ paymentHash : 'foo' ,
524
+ walletId : wallet . wallet . id ( ) ,
525
+ txRequestId : 'txReqId' ,
526
+ status : 'settled' ,
527
+ invoice : 'tlnfoobar' ,
528
+ destination : 'destination' ,
529
+ feeLimitMsat : 100n ,
530
+ amountMsat : 1000n ,
531
+ createdAt : new Date ( ) ,
532
+ updatedAt : new Date ( ) ,
533
+ } ;
534
+ const query = {
535
+ status : 'settled' ,
536
+ startDate : new Date ( ) ,
537
+ limit : 100n ,
538
+ } as PaymentQuery ;
539
+ const listPaymentsNock = nock ( bgUrl )
540
+ . get ( `/api/v2/wallet/${ wallet . wallet . id ( ) } /lightning/payment` )
541
+ . query ( PaymentQuery . encode ( query ) )
542
+ . reply ( 200 , { payments : [ PaymentInfo . encode ( payment ) ] } ) ;
543
+ const listPaymentsResponse = await wallet . listPayments ( query ) ;
544
+ assert . strictEqual ( listPaymentsResponse . payments . length , 1 ) ;
545
+ assert . deepStrictEqual ( listPaymentsResponse . payments [ 0 ] , payment ) ;
546
+ listPaymentsNock . done ( ) ;
547
+ } ) ;
548
+
549
+ it ( 'should work properly with pagination while listing payments' , async function ( ) {
550
+ const payment1 : PaymentInfo = {
551
+ paymentHash : 'foo1' ,
552
+ walletId : wallet . wallet . id ( ) ,
553
+ txRequestId : 'txReqId1' ,
554
+ status : 'settled' ,
555
+ invoice : 'tlnfoobar1' ,
556
+ destination : 'destination' ,
557
+ feeLimitMsat : 100n ,
558
+ amountMsat : 1000n ,
559
+ createdAt : new Date ( ) ,
560
+ updatedAt : new Date ( ) ,
561
+ } ;
562
+ const payment2 : PaymentInfo = {
563
+ ...payment1 ,
564
+ paymentHash : 'foo2' ,
565
+ txRequestId : 'txReqId2' ,
566
+ invoice : 'tlnfoobar2' ,
567
+ } ;
568
+ const payment3 : PaymentInfo = {
569
+ ...payment1 ,
570
+ paymentHash : 'foo3' ,
571
+ txRequestId : 'txReqId3' ,
572
+ invoice : 'tlnfoobar3' ,
573
+ } ;
574
+ const allPayments = [ PaymentInfo . encode ( payment1 ) , PaymentInfo . encode ( payment2 ) , PaymentInfo . encode ( payment3 ) ] ;
575
+ const query = {
576
+ status : 'settled' ,
577
+ startDate : new Date ( ) ,
578
+ limit : 2n ,
579
+ } as PaymentQuery ;
580
+ const listPaymentsNock = nock ( bgUrl )
581
+ . get ( `/api/v2/wallet/${ wallet . wallet . id ( ) } /lightning/payment` )
582
+ . query ( PaymentQuery . encode ( query ) )
583
+ . reply ( 200 , { payments : allPayments . slice ( 0 , 2 ) , nextBatchPrevId : payment2 . paymentHash } ) ;
584
+ const listPaymentsResponse = await wallet . listPayments ( query ) ;
585
+ assert . strictEqual ( listPaymentsResponse . payments . length , 2 ) ;
586
+ assert . deepStrictEqual ( listPaymentsResponse . payments [ 0 ] , payment1 ) ;
587
+ assert . deepStrictEqual ( listPaymentsResponse . payments [ 1 ] , payment2 ) ;
588
+ assert . strictEqual ( listPaymentsResponse . nextBatchPrevId , payment2 . paymentHash ) ;
589
+ listPaymentsNock . done ( ) ;
590
+ } ) ;
591
+
592
+ it ( 'listPayments should throw error if wp response is invalid' , async function ( ) {
593
+ const listPaymentsNock = nock ( bgUrl )
594
+ . get ( `/api/v2/wallet/${ wallet . wallet . id ( ) } /lightning/payment` )
595
+ . reply ( 200 , { payments : [ { amountMsat : '1000' } ] } ) ;
596
+ await assert . rejects ( async ( ) => await wallet . listPayments ( { } ) , / I n v a l i d p a y m e n t l i s t r e s p o n s e / ) ;
597
+ listPaymentsNock . done ( ) ;
598
+ } ) ;
599
+ } ) ;
600
+
467
601
describe ( 'Get lightning key(s)' , function ( ) {
468
602
const walletData = {
469
603
id : 'fakeid' ,
0 commit comments