1+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2+ // @ts -nocheck
3+
14import * as assert from 'assert' ;
25import { TestBitGo } from '@bitgo/sdk-test' ;
36import * as nock from 'nock' ;
4- import { BaseCoin } from '@bitgo/sdk-core' ;
7+ import { BaseCoin , PendingApprovalData , State , Type } from '@bitgo/sdk-core' ;
58import {
69 CreateInvoiceBody ,
710 getLightningWallet ,
811 Invoice ,
912 InvoiceInfo ,
1013 InvoiceQuery ,
14+ LndCreatePaymentResponse ,
1115 SelfCustodialLightningWallet ,
16+ SubmitPaymentParams ,
1217} from '@bitgo/abstract-lightning' ;
1318
1419import { BitGo , common , GenerateLightningWalletOptions , Wallet , Wallets } from '../../../../src' ;
@@ -20,6 +25,32 @@ describe('Lightning wallets', function () {
2025 let wallets : Wallets ;
2126 let bgUrl : string ;
2227
28+ const userAuthKey = {
29+ id : 'def' ,
30+ pub : 'xpub661MyMwAqRbcGYjYsnsDj1SHdiXynWEXNnfNgMSpokN54FKyMqbu7rWEfVNDs6uAJmz86UVFtq4sefhQpXZhSAzQcL9zrEPtiLNNZoeSxCG' ,
31+ encryptedPrv :
32+ '{"iv":"zYhhaNdW0wPfJEoBjZ4pvg==","v":1,"iter":10000,"ks":256,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"tgAMua9jjhw=","ct":"HcrbxQvNlWG5tLMndYzdNCYa1l+1h7o+vSsweA0+q1le3tWt6jLUJSEjZN+JI8lTZ2KPFQgLulQQhsUa+ytUCBi0vSgjF7x7CprT7l2Cfjkew00XsEd7wnmtJUsrQk8m69Co7tIRA3oEgzrnYwy4qOM81lbNNyQ="}' ,
33+ source : 'user' ,
34+ coinSpecific : {
35+ tlnbtc : {
36+ purpose : 'userAuth' ,
37+ } ,
38+ } ,
39+ } ;
40+
41+ const nodeAuthKey = {
42+ id : 'ghi' ,
43+ pub : 'xpub661MyMwAqRbcG9xnTnAnRbJPo3MAHyRtH4zeehN8exYk4VFz5buepUzebhix33BKhS5Eb4V3LEfW5pYiSR8qmaEnyrpeghhKY8JfzAsUDpq' ,
44+ encryptedPrv :
45+ '{"iv":"bH6eGbnl9x8PZECPrgvcng==","v":1,"iter":10000,"ks":256,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"o8yknV6nTI8=","ct":"nGyzAToIzYkQeIdcVafoWHtMx7+Fgj0YldCme3WA1yxJAA0QulZVhblMZN/7efCRIumA0NNmpH7dxH6n8cVlz/Z+RUgC2q9lgvZKUoJcYNTjWUfkmkJutXX2tr8yVxm+eC/hnRiyfVLZ2qPxctvDlBVBfgLuPyc="}' ,
46+ source : 'user' ,
47+ coinSpecific : {
48+ tlnbtc : {
49+ purpose : 'nodeAuth' ,
50+ } ,
51+ } ,
52+ } ;
53+
2354 before ( function ( ) {
2455 bitgo . initializeTestVars ( ) ;
2556
@@ -191,7 +222,7 @@ describe('Lightning wallets', function () {
191222 let wallet : SelfCustodialLightningWallet ;
192223 beforeEach ( function ( ) {
193224 wallet = getLightningWallet (
194- new Wallet ( bitgo , basecoin , { id : 'walletId' , coin : 'tlnbtc' } )
225+ new Wallet ( bitgo , basecoin , { id : 'walletId' , coin : 'tlnbtc' , coinSpecific : { keys : [ 'def' , 'ghi' ] } } )
195226 ) as SelfCustodialLightningWallet ;
196227 } ) ;
197228
@@ -262,6 +293,140 @@ describe('Lightning wallets', function () {
262293 await assert . rejects ( async ( ) => await wallet . createInvoice ( createInvoice ) , / I n v a l i d c r e a t e i n v o i c e r e s p o n s e / ) ;
263294 createInvoiceNock . done ( ) ;
264295 } ) ;
296+
297+ it ( 'should pay invoice' , async function ( ) {
298+ const params : SubmitPaymentParams = {
299+ invoice : 'lnbc1...' ,
300+ amountMsat : 1000n ,
301+ feeLimitMsat : 100n ,
302+ feeLimitRatio : 0.1 ,
303+ sequenceId : '123' ,
304+ comment : 'test payment' ,
305+ } ;
306+
307+ const txRequestResponse = {
308+ txRequestId : 'txReq123' ,
309+ state : 'delivered' ,
310+ } ;
311+
312+ const lndResponse : LndCreatePaymentResponse = {
313+ status : 'settled' ,
314+ paymentHash : 'paymentHash123' ,
315+ amountMsat : params . amountMsat . toString ( ) ,
316+ feeMsat : params . feeLimitMsat . toString ( ) ,
317+ paymentPreimage : 'preimage123' ,
318+ } ;
319+
320+ const finalPaymentResponse = {
321+ txRequestId : 'txReq123' ,
322+ state : 'delivered' ,
323+ transactions : [
324+ {
325+ unsignedTx : {
326+ coinSpecific : {
327+ ...lndResponse ,
328+ } ,
329+ } ,
330+ } ,
331+ ] ,
332+ } ;
333+
334+ const createTxRequestNock = nock ( bgUrl )
335+ . post ( `/api/v2/wallet/${ wallet . wallet . id ( ) } /txrequests` )
336+ . reply ( 200 , txRequestResponse ) ;
337+
338+ const sendTxRequestNock = nock ( bgUrl )
339+ . post ( `/api/v2/wallet/${ wallet . wallet . id ( ) } /txrequests/${ txRequestResponse . txRequestId } /transactions/0/send` )
340+ . reply ( 200 , finalPaymentResponse ) ;
341+
342+ const userAuthKeyNock = nock ( bgUrl )
343+ . get ( '/api/v2/' + coinName + '/key/def' )
344+ . reply ( 200 , userAuthKey ) ;
345+ const nodeAuthKeyNock = nock ( bgUrl )
346+ . get ( '/api/v2/' + coinName + '/key/ghi' )
347+ . reply ( 200 , nodeAuthKey ) ;
348+
349+ const response = await wallet . payInvoice ( params , 'password123' ) ;
350+ assert . strictEqual ( response . txRequestId , 'txReq123' ) ;
351+ assert . strictEqual ( response . txRequestState , 'delivered' ) ;
352+ assert . strictEqual (
353+ response . paymentStatus . status ,
354+ finalPaymentResponse . transactions [ 0 ] . unsignedTx . coinSpecific . status
355+ ) ;
356+ assert . strictEqual (
357+ response . paymentStatus . paymentHash ,
358+ finalPaymentResponse . transactions [ 0 ] . unsignedTx . coinSpecific . paymentHash
359+ ) ;
360+ assert . strictEqual (
361+ response . paymentStatus . amountMsat ,
362+ finalPaymentResponse . transactions [ 0 ] . unsignedTx . coinSpecific . amountMsat
363+ ) ;
364+ assert . strictEqual (
365+ response . paymentStatus . feeMsat ,
366+ finalPaymentResponse . transactions [ 0 ] . unsignedTx . coinSpecific . feeMsat
367+ ) ;
368+ assert . strictEqual (
369+ response . paymentStatus . paymentPreimage ,
370+ finalPaymentResponse . transactions [ 0 ] . unsignedTx . coinSpecific . paymentPreimage
371+ ) ;
372+
373+ createTxRequestNock . done ( ) ;
374+ sendTxRequestNock . done ( ) ;
375+ userAuthKeyNock . done ( ) ;
376+ nodeAuthKeyNock . done ( ) ;
377+ } ) ;
378+
379+ it ( 'should handle pending approval when paying invoice' , async function ( ) {
380+ const params : SubmitPaymentParams = {
381+ invoice : 'lnbc1...' ,
382+ amountMsat : 1000n ,
383+ feeLimitMsat : 100n ,
384+ feeLimitRatio : 0.1 ,
385+ sequenceId : '123' ,
386+ comment : 'test payment' ,
387+ } ;
388+
389+ const txRequestResponse = {
390+ txRequestId : 'txReq123' ,
391+ state : 'pendingApproval' ,
392+ pendingApprovalId : 'approval123' ,
393+ } ;
394+
395+ const pendingApprovalData : PendingApprovalData = {
396+ id : 'approval123' ,
397+ state : State . PENDING ,
398+ creator : 'user123' ,
399+ info : {
400+ type : Type . TRANSACTION_REQUEST ,
401+ } ,
402+ } ;
403+
404+ const createTxRequestNock = nock ( bgUrl )
405+ . post ( `/api/v2/wallet/${ wallet . wallet . id ( ) } /txrequests` )
406+ . reply ( 200 , txRequestResponse ) ;
407+
408+ const getPendingApprovalNock = nock ( bgUrl )
409+ . get ( `/api/v2/${ coinName } /pendingapprovals/${ txRequestResponse . pendingApprovalId } ` )
410+ . reply ( 200 , pendingApprovalData ) ;
411+
412+ const userAuthKeyNock = nock ( bgUrl )
413+ . get ( '/api/v2/' + coinName + '/key/def' )
414+ . reply ( 200 , userAuthKey ) ;
415+ const nodeAuthKeyNock = nock ( bgUrl )
416+ . get ( '/api/v2/' + coinName + '/key/ghi' )
417+ . reply ( 200 , nodeAuthKey ) ;
418+
419+ const response = await wallet . payInvoice ( params , 'password123' ) ;
420+ assert . strictEqual ( response . txRequestId , 'txReq123' ) ;
421+ assert . strictEqual ( response . txRequestState , 'pendingApproval' ) ;
422+ assert ( response . pendingApproval ) ;
423+ assert . strictEqual ( response . status , undefined ) ;
424+
425+ createTxRequestNock . done ( ) ;
426+ getPendingApprovalNock . done ( ) ;
427+ userAuthKeyNock . done ( ) ;
428+ nodeAuthKeyNock . done ( ) ;
429+ } ) ;
265430 } ) ;
266431
267432 describe ( 'Get lightning key(s)' , function ( ) {
@@ -386,32 +551,6 @@ describe('Lightning wallets', function () {
386551 coinSpecific : { keys : [ 'def' , 'ghi' ] } ,
387552 } ;
388553
389- const userAuthKey = {
390- id : 'def' ,
391- pub : 'xpub661MyMwAqRbcGYjYsnsDj1SHdiXynWEXNnfNgMSpokN54FKyMqbu7rWEfVNDs6uAJmz86UVFtq4sefhQpXZhSAzQcL9zrEPtiLNNZoeSxCG' ,
392- encryptedPrv :
393- '{"iv":"zYhhaNdW0wPfJEoBjZ4pvg==","v":1,"iter":10000,"ks":256,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"tgAMua9jjhw=","ct":"HcrbxQvNlWG5tLMndYzdNCYa1l+1h7o+vSsweA0+q1le3tWt6jLUJSEjZN+JI8lTZ2KPFQgLulQQhsUa+ytUCBi0vSgjF7x7CprT7l2Cfjkew00XsEd7wnmtJUsrQk8m69Co7tIRA3oEgzrnYwy4qOM81lbNNyQ="}' ,
394- source : 'user' ,
395- coinSpecific : {
396- tlnbtc : {
397- purpose : 'userAuth' ,
398- } ,
399- } ,
400- } ;
401-
402- const nodeAuthKey = {
403- id : 'ghi' ,
404- pub : 'xpub661MyMwAqRbcG9xnTnAnRbJPo3MAHyRtH4zeehN8exYk4VFz5buepUzebhix33BKhS5Eb4V3LEfW5pYiSR8qmaEnyrpeghhKY8JfzAsUDpq' ,
405- encryptedPrv :
406- '{"iv":"bH6eGbnl9x8PZECPrgvcng==","v":1,"iter":10000,"ks":256,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"o8yknV6nTI8=","ct":"nGyzAToIzYkQeIdcVafoWHtMx7+Fgj0YldCme3WA1yxJAA0QulZVhblMZN/7efCRIumA0NNmpH7dxH6n8cVlz/Z+RUgC2q9lgvZKUoJcYNTjWUfkmkJutXX2tr8yVxm+eC/hnRiyfVLZ2qPxctvDlBVBfgLuPyc="}' ,
407- source : 'user' ,
408- coinSpecific : {
409- tlnbtc : {
410- purpose : 'nodeAuth' ,
411- } ,
412- } ,
413- } ;
414-
415554 const watchOnlyAccounts = {
416555 master_key_birthday_timestamp : 'dummy' ,
417556 master_key_fingerprint : 'dummy' ,
0 commit comments