@@ -32,23 +32,27 @@ async function getCredentials(lndSettingsDir) {
3232 return grpc . credentials . createSsl ( lndCert ) ;
3333}
3434
35- function getMetadata ( lndSettingsDir ) {
36- const metadata = new grpc . Metadata ( ) ;
37- const macaroonPath = path . join ( lndSettingsDir , 'admin.macaroon' ) ;
38- const macaroonHex = fs . readFileSync ( macaroonPath ) . toString ( 'hex' ) ;
39- metadata . add ( 'macaroon' , macaroonHex ) ;
40- return metadata ;
35+ function getMacaroonCreds ( lndSettingsDir , network ) {
36+ return grpc . credentials . createFromMetadataGenerator ( ( args , callback ) => {
37+ const metadata = new grpc . Metadata ( ) ;
38+ const macaroonPath = path . join (
39+ lndSettingsDir ,
40+ `data/chain/bitcoin/${ network } /admin.macaroon`
41+ ) ;
42+ const macaroonHex = fs . readFileSync ( macaroonPath ) . toString ( 'hex' ) ;
43+ metadata . add ( 'macaroon' , macaroonHex ) ;
44+ callback ( null , metadata ) ;
45+ } ) ;
4146}
4247
4348module . exports . init = async function ( {
4449 ipcMain,
4550 lndPort,
4651 lndSettingsDir,
47- macaroonsEnabled ,
52+ network ,
4853} ) {
4954 let credentials ;
5055 let protoPath ;
51- let metadata ;
5256 let lnrpc ;
5357 let unlocker ;
5458 let lnd ;
@@ -57,9 +61,6 @@ module.exports.init = async function({
5761 credentials = await getCredentials ( lndSettingsDir ) ;
5862 protoPath = path . join ( __dirname , '..' , 'assets' , 'rpc.proto' ) ;
5963 lnrpc = grpc . load ( protoPath ) . lnrpc ;
60- if ( macaroonsEnabled ) {
61- metadata = getMetadata ( lndSettingsDir ) ;
62- }
6364 unlocker = new lnrpc . WalletUnlocker ( `localhost:${ lndPort } ` , credentials ) ;
6465 grpc . waitForClientReady ( unlocker , Infinity , err => {
6566 event . sender . send ( 'unlockReady' , { err } ) ;
@@ -72,6 +73,11 @@ module.exports.init = async function({
7273 } ) ;
7374
7475 ipcMain . on ( 'lndInit' , event => {
76+ const macaroonCreds = getMacaroonCreds ( lndSettingsDir , network ) ;
77+ credentials = grpc . credentials . combineChannelCredentials (
78+ credentials ,
79+ macaroonCreds
80+ ) ;
7581 lnd = new lnrpc . Lightning ( `localhost:${ lndPort } ` , credentials ) ;
7682 grpc . waitForClientReady ( lnd , Infinity , err => {
7783 event . sender . send ( 'lndReady' , { err } ) ;
@@ -88,33 +94,21 @@ module.exports.init = async function({
8894 const handleResponse = ( err , response ) => {
8995 event . sender . send ( `unlockResponse_${ method } ` , { err, response } ) ;
9096 } ;
91- if ( metadata ) {
92- unlocker [ method ] ( body , metadata , { deadline } , handleResponse ) ;
93- } else {
94- unlocker [ method ] ( body , { deadline } , handleResponse ) ;
95- }
97+ unlocker [ method ] ( body , { deadline } , handleResponse ) ;
9698 } ) ;
9799
98100 ipcMain . on ( 'lndRequest' , ( event , { method, body } ) => {
99101 const deadline = new Date ( new Date ( ) . getTime ( ) + GRPC_TIMEOUT ) ;
100102 const handleResponse = ( err , response ) => {
101103 event . sender . send ( `lndResponse_${ method } ` , { err, response } ) ;
102104 } ;
103- if ( metadata ) {
104- lnd [ method ] ( body , metadata , { deadline } , handleResponse ) ;
105- } else {
106- lnd [ method ] ( body , { deadline } , handleResponse ) ;
107- }
105+ lnd [ method ] ( body , { deadline } , handleResponse ) ;
108106 } ) ;
109107
110108 const streams = { } ;
111109 ipcMain . on ( 'lndStreamRequest' , ( event , { method, body } ) => {
112110 let stream ;
113- if ( metadata ) {
114- stream = lnd [ method ] ( metadata , body ) ;
115- } else {
116- stream = lnd [ method ] ( body ) ;
117- }
111+ stream = lnd [ method ] ( body ) ;
118112 const send = res => event . sender . send ( `lndStreamEvent_${ method } ` , res ) ;
119113 stream . on ( 'data' , data => send ( { event : 'data' , data } ) ) ;
120114 stream . on ( 'end' , ( ) => send ( { event : 'end' } ) ) ;
0 commit comments