Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit a1c9c4c

Browse files
authored
Merge pull request #552 from lightninglabs/enable-macaroons
Enable macaroons
2 parents ab50389 + 4a3409a commit a1c9c4c

File tree

6 files changed

+38
-49
lines changed

6 files changed

+38
-49
lines changed

assets/script/setup_local_cluster.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ After initiating the wallet and setting your password close the app again.
1111
```
1212
btcd --txindex --simnet --rpcuser=kek --rpcpass=kek --datadir=data/btcd/data --logdir=data/btcd/logs
1313
14-
lnd --rpclisten=localhost:10009 --listen=localhost:10019 --restlisten=localhost:8009 --lnddir=data/lnd --debuglevel=info --bitcoin.simnet --bitcoin.active --bitcoin.node=btcd --btcd.rpcuser=kek --btcd.rpcpass=kek --no-macaroons
14+
lnd --rpclisten=localhost:10009 --listen=localhost:10019 --restlisten=localhost:8009 --lnddir=data/lnd --debuglevel=info --bitcoin.simnet --bitcoin.active --bitcoin.node=btcd --btcd.rpcuser=kek --btcd.rpcpass=kek
1515
16-
lncli --rpcserver=localhost:10009 --no-macaroons --lnddir=data/lnd unlock
16+
lncli --rpcserver=localhost:10009 --lnddir=data/lnd unlock
1717
18-
lnd --rpclisten=localhost:10002 --listen=localhost:10012 --restlisten=localhost:8002 --lnddir=data/lnd2 --debuglevel=info --bitcoin.simnet --bitcoin.active --bitcoin.node=btcd --btcd.rpcuser=kek --btcd.rpcpass=kek --no-macaroons --noencryptwallet
18+
lnd --rpclisten=localhost:10002 --listen=localhost:10012 --restlisten=localhost:8002 --lnddir=data/lnd2 --debuglevel=info --bitcoin.simnet --bitcoin.active --bitcoin.node=btcd --btcd.rpcuser=kek --btcd.rpcpass=kek --noencryptwallet
1919
```
2020

2121
## Fund wallets addresses
2222

2323
```
24-
lncli --rpcserver=localhost:10009 --no-macaroons --lnddir=data/lnd newaddress np2wkh
24+
lncli --rpcserver=localhost:10009 --lnddir=data/lnd newaddress np2wkh
2525
26-
lncli --rpcserver=localhost:10002 --no-macaroons --lnddir=data/lnd2 newaddress np2wkh
26+
lncli --rpcserver=localhost:10002 --lnddir=data/lnd2 newaddress np2wkh
2727
2828
btcd --txindex --simnet --rpcuser=kek --rpcpass=kek --datadir=data/btcd/data --logdir=data/btcd/logs --miningaddr=NEW_ADDRESS
2929
@@ -33,19 +33,19 @@ btcctl --simnet --rpcuser=kek --rpcpass=kek generate 400
3333
## Open channel and send payment
3434

3535
```
36-
lncli --rpcserver=localhost:10009 --no-macaroons --lnddir=data/lnd getinfo
36+
lncli --rpcserver=localhost:10009 --lnddir=data/lnd getinfo
3737
38-
lncli --rpcserver=localhost:10002 --no-macaroons --lnddir=data/lnd2 getinfo
38+
lncli --rpcserver=localhost:10002 --lnddir=data/lnd2 getinfo
3939
40-
lncli --rpcserver=localhost:10009 --no-macaroons --lnddir=data/lnd connect PUB_KEY@localhost:10012
40+
lncli --rpcserver=localhost:10009 --lnddir=data/lnd connect PUB_KEY@localhost:10012
4141
42-
lncli --rpcserver=localhost:10009 --no-macaroons --lnddir=data/lnd openchannel --node_key=PUB_KEY --local_amt=16000000
42+
lncli --rpcserver=localhost:10009 --lnddir=data/lnd openchannel --node_key=PUB_KEY --local_amt=16000000
4343
4444
btcctl --simnet --rpcuser=kek --rpcpass=kek generate 6
4545
46-
lncli --rpcserver=localhost:10009 --no-macaroons --lnddir=data/lnd listchannels
46+
lncli --rpcserver=localhost:10009 --lnddir=data/lnd listchannels
4747
48-
lncli --rpcserver=localhost:10002 --no-macaroons --lnddir=data/lnd2 addinvoice --amt=10000
48+
lncli --rpcserver=localhost:10002 --lnddir=data/lnd2 addinvoice --amt=10000
4949
50-
lncli --rpcserver=localhost:10009 --no-macaroons --lnddir=data/lnd sendpayment --pay_req=ENCODED_INVOICE
50+
lncli --rpcserver=localhost:10009 --lnddir=data/lnd sendpayment --pay_req=ENCODED_INVOICE
5151
```

public/electron.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { startLndProcess, startBtcdProcess } = require('./lnd-child-process');
99
const grcpClient = require('./grpc-client');
1010
const {
1111
PREFIX_NAME,
12-
MACAROONS_ENABLED,
12+
NETWORK,
1313
LND_PORT,
1414
LND_PEER_PORT,
1515
LND_INIT_DELAY,
@@ -123,7 +123,7 @@ function createWindow() {
123123
ipcMain,
124124
lndSettingsDir,
125125
lndPort: LND_PORT,
126-
macaroonsEnabled: MACAROONS_ENABLED,
126+
network: isDev ? 'simnet' : NETWORK,
127127
});
128128
}
129129

@@ -138,7 +138,6 @@ const startLnd = async () => {
138138
lndProcess = await startLndProcess({
139139
isDev,
140140
lndSettingsDir,
141-
macaroonsEnabled: MACAROONS_ENABLED,
142141
lndPort: LND_PORT,
143142
lndPeerPort: LND_PEER_PORT,
144143
logger: Logger,

public/grpc-client.js

+20-26
Original file line numberDiff line numberDiff line change
@@ -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

4348
module.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' }));

public/lnd-child-process.js

-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ function startBlockingProcess(name, args, logger) {
4747

4848
module.exports.startLndProcess = async function({
4949
isDev,
50-
macaroonsEnabled,
5150
lndSettingsDir,
5251
lndPort,
5352
lndPeerPort,
@@ -61,7 +60,6 @@ module.exports.startLndProcess = async function({
6160
'--bitcoin.active',
6261
'--debuglevel=info',
6362
`--lnddir=${lndSettingsDir}`,
64-
macaroonsEnabled ? '' : '--no-macaroons',
6563
lndPort ? `--rpclisten=localhost:${lndPort}` : '',
6664
lndPeerPort ? `--listen=localhost:${lndPeerPort}` : '',
6765
lndRestPort ? `--restlisten=localhost:${lndRestPort}` : '',

src/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports.RATE_DELAY = 15 * 60 * 1000;
99

1010
module.exports.LND_PORT = 10009;
1111
module.exports.LND_PEER_PORT = 10019;
12-
module.exports.MACAROONS_ENABLED = false;
12+
module.exports.NETWORK = 'testnet';
1313
module.exports.BTCD_MINING_ADDRESS = 'rfu4i1Mo2NF7TQsN9bMVLFSojSzcyQCEH5';
1414

1515
const prefixName = 'lightning';

test/integration/action/action-integration.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const grcpClient = require('../../../public/grpc-client');
2525
/* eslint-disable no-unused-vars */
2626

2727
const isDev = true;
28+
const NETWORK = 'simnet';
2829
const BTCD_PORT = 18555;
2930
const BTCD_SETTINGS_DIR = 'test/data/btcd';
3031
const LND_SETTINGS_DIR_1 = 'test/data/lnd_1';
@@ -37,7 +38,6 @@ const LND_REST_PORT_1 = 8001;
3738
const LND_REST_PORT_2 = 8002;
3839
const HOST_1 = `localhost:${LND_PEER_PORT_1}`;
3940
const HOST_2 = `localhost:${LND_PEER_PORT_2}`;
40-
const MACAROONS_ENABLED = false;
4141
const NAP_TIME = process.env.NAP_TIME || 5000;
4242
const walletPassword = 'bitconeeeeeect';
4343

@@ -111,7 +111,6 @@ describe('Action Integration Tests', function() {
111111
await poll(() => isPortOpen(BTCD_PORT));
112112
const lndProcess1Promise = startLndProcess({
113113
isDev,
114-
macaroonsEnabled: MACAROONS_ENABLED,
115114
lndSettingsDir: LND_SETTINGS_DIR_1,
116115
lndPort: LND_PORT_1,
117116
lndPeerPort: LND_PEER_PORT_1,
@@ -120,7 +119,6 @@ describe('Action Integration Tests', function() {
120119
});
121120
const lndProcess2Promise = startLndProcess({
122121
isDev,
123-
macaroonsEnabled: MACAROONS_ENABLED,
124122
lndSettingsDir: LND_SETTINGS_DIR_2,
125123
lndPort: LND_PORT_2,
126124
lndPeerPort: LND_PEER_PORT_2,
@@ -135,13 +133,13 @@ describe('Action Integration Tests', function() {
135133
ipcMain: ipcMainStub1,
136134
lndPort: LND_PORT_1,
137135
lndSettingsDir: LND_SETTINGS_DIR_1,
138-
macaroonsEnabled: MACAROONS_ENABLED,
136+
network: NETWORK,
139137
});
140138
await grcpClient.init({
141139
ipcMain: ipcMainStub2,
142140
lndPort: LND_PORT_2,
143141
lndSettingsDir: LND_SETTINGS_DIR_2,
144-
macaroonsEnabled: MACAROONS_ENABLED,
142+
network: NETWORK,
145143
});
146144

147145
db1 = sinon.createStubInstance(AppStorage);

0 commit comments

Comments
 (0)