Skip to content

Commit

Permalink
Switch LN actions to use invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusd committed May 13, 2021
1 parent 2bb4946 commit 21483a1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 77 deletions.
14 changes: 6 additions & 8 deletions app/actions/LNActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { getWalletCfg } from "../config";
import { getWalletPath } from "main_dev/paths";
import { getNextAccountAttempt } from "./ControlActions";
import * as cfgConstants from "constants/config";
import { isString, isNumber } from "lodash";
import { isNumber } from "lodash";
import { invoke } from "helpers/electronRenderer";

export const CLOSETYPE_COOPERATIVE_CLOSE = 0;
export const CLOSETYPE_LOCAL_FORCE_CLOSE = 1;
Expand Down Expand Up @@ -97,7 +98,7 @@ export const startDcrlnd = (
stage: LNWALLET_STARTUPSTAGE_STARTDCRLND,
type: LNWALLET_STARTUP_CHANGEDSTAGE
});
const res = ipcRenderer.sendSync(
const res = await invoke(
"start-dcrlnd",
lnAccount,
walletPort,
Expand All @@ -106,9 +107,6 @@ export const startDcrlnd = (
isTestnet,
autopilotEnabled
);
if (isString(res) || res instanceof Error) {
throw res;
}
dcrlndCreds = res;
} catch (error) {
dispatch({ type: LNWALLET_STARTUP_FAILED });
Expand All @@ -120,7 +118,7 @@ export const startDcrlnd = (
// dcrlnd is already running so if some error occurs we need to shut it down.
const cleanup = () => {
// Force dcrlnd to stop.
ipcRenderer.send("stop-dcrlnd");
invoke("stop-dcrlnd");
dispatch({ type: LNWALLET_STARTUP_FAILED });

if (creating) {
Expand Down Expand Up @@ -223,7 +221,7 @@ export const stopDcrlnd = () => (dispatch, getState) => {
return;
}

ipcRenderer.send("stop-dcrlnd");
invoke("stop-dcrlnd");
dispatch({ type: LNWALLET_DCRLND_STOPPED });
};

Expand All @@ -240,7 +238,7 @@ export const checkLnWallet = () => async (dispatch) => {
}

// Check whether the app knows of a previously running dcrlnd instance.
const creds = ipcRenderer.sendSync("dcrlnd-creds");
const creds = await invoke("dcrlnd-creds");
if (!creds) {
return;
}
Expand Down
9 changes: 9 additions & 0 deletions app/helpers/electronRenderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ipcRenderer } from "electron";

export const invoke = async (...args) => {
const res = await ipcRenderer.invoke(...args);
if (res instanceof Error) {
throw res;
}
return res;
};
66 changes: 5 additions & 61 deletions app/main.development.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,39 +379,9 @@ handle("start-wallet", (walletPath, testnet, rpcCreds) => {
);
});

ipcMain.on(
"start-dcrlnd",
async (
event,
walletAccount,
walletPort,
rpcCreds,
walletPath,
testnet,
autopilotEnabled
) => {
try {
event.returnValue = await startDcrlnd(
walletAccount,
walletPort,
rpcCreds,
walletPath,
testnet,
autopilotEnabled
);
} catch (error) {
if (!(error instanceof Error)) {
event.returnValue = new Error(error);
} else {
event.returnValue = error;
}
}
}
);
handle("start-dcrlnd", startDcrlnd);

ipcMain.on("stop-dcrlnd", async (event) => {
event.returnValue = await stopDcrlnd();
});
handle("stop-dcrlnd", stopDcrlnd);

handle("check-init-dex", checkInitDex);

Expand Down Expand Up @@ -451,37 +421,11 @@ handle("check-btc-config", getCurrentBitcoinConfig);

handle("update-btc-config", updateDefaultBitcoinConfig);

ipcMain.on("dcrlnd-creds", (event) => {
if (GetDcrlndPID() && GetDcrlndPID() !== -1) {
event.returnValue = GetDcrlndCreds();
} else {
event.returnValue = null;
}
});
handle("dcrlnd-creds", () => (GetDcrlndPID() !== -1 ? GetDcrlndCreds() : null));

ipcMain.on("ln-scb-info", (event, walletPath, testnet) => {
try {
event.returnValue = lnScbInfo(walletPath, testnet);
} catch (error) {
if (!(error instanceof Error)) {
event.returnValue = new Error(error);
} else {
event.returnValue = error;
}
}
});
handle("ln-scb-info", lnScbInfo);

ipcMain.on("ln-remove-dir", (event, walletName, testnet) => {
try {
event.returnValue = removeDcrlnd(walletName, testnet);
} catch (error) {
if (!(error instanceof Error)) {
event.returnValue = new Error(error);
} else {
event.returnValue = error;
}
}
});
handle("ln-remove-dir", removeDcrlnd);

handle("check-daemon", getBlockChainInfo);

Expand Down
11 changes: 3 additions & 8 deletions app/wallet/ln/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { lnrpc as pb } from "middleware/ln/rpc_pb";
import { lnrpc as wupb } from "middleware/ln/walletunlocker_pb";
import { strHashToRaw } from "helpers/byteActions";
import { ipcRenderer } from "electron";
import { invoke } from "helpers/electronRenderer";

export const getLightningClient = client.getLightningClient;
export const getWatchtowerClient = client.getWatchtowerClient;
Expand Down Expand Up @@ -304,16 +305,10 @@ export const stopDaemon = (client) => {
};

export const removeDcrlnd = (walletName, testnet) =>
new Promise((resolve, reject) => {
const res = ipcRenderer.sendSync("ln-remove-dir", walletName, testnet);
res instanceof Error ? reject(res) : resolve(res);
});
invoke("ln-remove-dir", walletName, testnet);

export const scbInfo = (walletPath, testnet) =>
new Promise((resolve, reject) => {
const res = ipcRenderer.sendSync("ln-scb-info", walletPath, testnet);
res instanceof Error ? reject(res) : resolve(res);
});
invoke("ln-scb-info", walletPath, testnet);

export const exportBackup = (client, destPath) =>
new Promise((resolve, reject) => {
Expand Down

0 comments on commit 21483a1

Please sign in to comment.