@@ -5,19 +5,12 @@ import protobuf from 'protobufjs'
5
5
import grpcCredentials from 'lightning/lnd_grpc/grpc_credentials'
6
6
import { grpcSslCipherSuites } from 'lightning/grpc/index'
7
7
import { fromJSON } from '@grpc/proto-loader'
8
- import { estimateRouteFee } from '@/api/lnd'
9
8
import * as bech32b12 from '@/lib/bech32b12'
10
9
11
10
/* eslint-disable camelcase */
12
11
const { GRPC_SSL_CIPHER_SUITES } = process . env
13
12
14
- const lndkInstances = new WeakMap ( )
15
-
16
- export function enableLNDK ( lnd , { cert, macaroon, socket : lndkSocket } , withProxy ) {
17
- // already installed
18
- if ( lndkInstances . has ( lnd ) ) return
19
- console . log ( 'enabling lndk' , lndkSocket , 'withProxy' , withProxy )
20
-
13
+ export function authenticatedLndkGrpc ( { cert, macaroon, socket : lndkSocket } , withProxy ) {
21
14
// workaround to load from string
22
15
const protoArgs = { keepCase : true , longs : Number , defaults : true , oneofs : true }
23
16
const proto = protobuf . parse ( LNDK_RPC_PROTO , protoArgs ) . root
@@ -38,22 +31,13 @@ export function enableLNDK (lnd, { cert, macaroon, socket: lndkSocket }, withPro
38
31
}
39
32
40
33
const client = new OffersService ( lndkSocket , credentials , params )
41
- lndkInstances . set ( lnd , client )
42
- }
43
-
44
- export function getLNDK ( lnd ) {
45
- if ( ! lndkInstances . has ( lnd ) ) {
46
- throw new Error ( 'lndk not available, please use enableLNDK first' )
47
- }
48
- return lndkInstances . get ( lnd )
34
+ return client
49
35
}
50
36
51
37
export async function decodeBolt12Invoice ( {
52
- lnd ,
38
+ lndk ,
53
39
request
54
40
} ) {
55
- const lndk = getLNDK ( lnd )
56
-
57
41
// decode bech32 bolt12 invoice to hex string
58
42
if ( ! request . startsWith ( 'lni1' ) ) throw new Error ( 'not a valid bech32 encoded bolt12 invoice' )
59
43
const invoice_hex_str = bech32b12 . decode ( request . slice ( 4 ) ) . toString ( 'hex' )
@@ -70,9 +54,7 @@ export async function decodeBolt12Invoice ({
70
54
return { ...decodedRequest , invoice_hex_str }
71
55
}
72
56
73
- export async function fetchBolt12InvoiceFromOffer ( { lnd, offer, msats, description, timeout = 10_000 } ) {
74
- const lndk = getLNDK ( lnd )
75
-
57
+ export async function fetchBolt12InvoiceFromOffer ( { lndk, offer, msats, description, timeout = 10_000 } ) {
76
58
return new Promise ( ( resolve , reject ) => {
77
59
lndk . GetInvoice ( {
78
60
offer,
@@ -87,7 +69,7 @@ export async function fetchBolt12InvoiceFromOffer ({ lnd, offer, msats, descript
87
69
const bech32invoice = 'lni1' + bech32b12 . encode ( Buffer . from ( response . invoice_hex_str , 'hex' ) )
88
70
89
71
// sanity check
90
- const { amount_msats } = await decodeBolt12Invoice ( { lnd , request : bech32invoice } )
72
+ const { amount_msats } = await decodeBolt12Invoice ( { lndk , request : bech32invoice } )
91
73
if ( toPositiveNumber ( amount_msats ) !== toPositiveNumber ( msats ) ) {
92
74
return reject ( new Error ( 'invalid invoice response' ) )
93
75
}
@@ -101,14 +83,12 @@ export async function fetchBolt12InvoiceFromOffer ({ lnd, offer, msats, descript
101
83
}
102
84
103
85
export async function payViaBolt12PaymentRequest ( {
104
- lnd ,
86
+ lndk ,
105
87
request : invoiceBech32 ,
106
88
max_fee,
107
89
max_fee_mtokens
108
90
} ) {
109
- const lndk = getLNDK ( lnd )
110
-
111
- const { amount_msats, invoice_hex_str } = await decodeBolt12Invoice ( { lnd, request : invoiceBech32 } )
91
+ const { amount_msats, invoice_hex_str } = await decodeBolt12Invoice ( { lndk, request : invoiceBech32 } )
112
92
113
93
if ( ! max_fee_mtokens && max_fee ) {
114
94
max_fee_mtokens = toPositiveNumber ( satsToMsats ( max_fee ) )
@@ -130,16 +110,3 @@ export async function payViaBolt12PaymentRequest ({
130
110
} )
131
111
} )
132
112
}
133
-
134
- export async function estimateBolt12RouteFee ( { lnd, destination, tokens, mtokens, request, timeout } ) {
135
- const { amount_msats, node_id } = request ? await decodeBolt12Invoice ( { lnd, request } ) : { }
136
-
137
- // extract mtokens and destination from invoice if they are not provided
138
- if ( ! tokens && ! mtokens ) mtokens = toPositiveNumber ( amount_msats )
139
- destination ??= Buffer . from ( node_id . key ) . toString ( 'hex' )
140
-
141
- if ( ! destination ) throw new Error ( 'no destination provided' )
142
- if ( ! tokens && ! mtokens ) throw new Error ( 'no tokens amount provided' )
143
-
144
- return await estimateRouteFee ( { lnd, destination, tokens, mtokens, timeout } )
145
- }
0 commit comments