@@ -5,19 +5,12 @@ import protobuf from 'protobufjs'
55import grpcCredentials from 'lightning/lnd_grpc/grpc_credentials'
66import { grpcSslCipherSuites } from 'lightning/grpc/index'
77import { fromJSON } from '@grpc/proto-loader'
8- import { estimateRouteFee } from '@/api/lnd'
98import * as bech32b12 from '@/lib/bech32b12'
109
1110/* eslint-disable camelcase */
1211const { GRPC_SSL_CIPHER_SUITES } = process . env
1312
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 ) {
2114 // workaround to load from string
2215 const protoArgs = { keepCase : true , longs : Number , defaults : true , oneofs : true }
2316 const proto = protobuf . parse ( LNDK_RPC_PROTO , protoArgs ) . root
@@ -38,22 +31,13 @@ export function enableLNDK (lnd, { cert, macaroon, socket: lndkSocket }, withPro
3831 }
3932
4033 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
4935}
5036
5137export async function decodeBolt12Invoice ( {
52- lnd ,
38+ lndk ,
5339 request
5440} ) {
55- const lndk = getLNDK ( lnd )
56-
5741 // decode bech32 bolt12 invoice to hex string
5842 if ( ! request . startsWith ( 'lni1' ) ) throw new Error ( 'not a valid bech32 encoded bolt12 invoice' )
5943 const invoice_hex_str = bech32b12 . decode ( request . slice ( 4 ) ) . toString ( 'hex' )
@@ -70,9 +54,7 @@ export async function decodeBolt12Invoice ({
7054 return { ...decodedRequest , invoice_hex_str }
7155}
7256
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 } ) {
7658 return new Promise ( ( resolve , reject ) => {
7759 lndk . GetInvoice ( {
7860 offer,
@@ -87,7 +69,7 @@ export async function fetchBolt12InvoiceFromOffer ({ lnd, offer, msats, descript
8769 const bech32invoice = 'lni1' + bech32b12 . encode ( Buffer . from ( response . invoice_hex_str , 'hex' ) )
8870
8971 // sanity check
90- const { amount_msats } = await decodeBolt12Invoice ( { lnd , request : bech32invoice } )
72+ const { amount_msats } = await decodeBolt12Invoice ( { lndk , request : bech32invoice } )
9173 if ( toPositiveNumber ( amount_msats ) !== toPositiveNumber ( msats ) ) {
9274 return reject ( new Error ( 'invalid invoice response' ) )
9375 }
@@ -101,14 +83,12 @@ export async function fetchBolt12InvoiceFromOffer ({ lnd, offer, msats, descript
10183}
10284
10385export async function payViaBolt12PaymentRequest ( {
104- lnd ,
86+ lndk ,
10587 request : invoiceBech32 ,
10688 max_fee,
10789 max_fee_mtokens
10890} ) {
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 } )
11292
11393 if ( ! max_fee_mtokens && max_fee ) {
11494 max_fee_mtokens = toPositiveNumber ( satsToMsats ( max_fee ) )
@@ -130,16 +110,3 @@ export async function payViaBolt12PaymentRequest ({
130110 } )
131111 } )
132112}
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