Skip to content

Commit

Permalink
[multi] Introduce preload script (#3397)
Browse files Browse the repository at this point in the history
This simplifies some code that runs in the main process and introduces a
preload script that runs before the main wallet interface. This starts the
work towards moving all node-related calls to the preload script, so that
node integration can be turned off for the main UI code.
  • Loading branch information
matheusd committed May 25, 2021
1 parent 6f0f513 commit fce4fb9
Show file tree
Hide file tree
Showing 23 changed files with 483 additions and 530 deletions.
7 changes: 5 additions & 2 deletions app/actions/DaemonActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ export const checkDecreditonVersion = () => (dispatch, getState) => {
}
})
.catch(function (error) {
console.log("Unable to check latest decrediton release version.", error);
console.error(
"Unable to check latest decrediton release version.",
error
);
});
};

Expand Down Expand Up @@ -260,7 +263,7 @@ export const shutdownApp = () => async (dispatch, getState) => {
return dispatch(finalShutdown());
} catch (error) {
const openOrder =
error.indexOf("cannot log out with active orders", 0) > -1;
String(error).indexOf("cannot log out with active orders", 0) > -1;
dispatch({ type: DEX_LOGOUT_FAILED, error, openOrder });
dispatch(showCantCloseModal());
}
Expand Down
71 changes: 32 additions & 39 deletions app/actions/DexActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { EXTERNALREQUEST_DEX } from "main_dev/externalRequests";
import * as configConstants from "constants/config";
import { makeRandomString } from "helpers";

const sendSync = (...args) => {
const res = ipcRenderer.sendSync(...args);
const invoke = async (...args) => {
const res = await ipcRenderer.invoke(...args);
if (res instanceof Error) {
throw res;
}
Expand Down Expand Up @@ -56,7 +56,7 @@ export const DEX_STARTUP_ATTEMPT = "DEX_STARTUP_ATTEMPT";
export const DEX_STARTUP_FAILED = "DEX_STARTUP_FAILED";
export const DEX_STARTUP_SUCCESS = "DEX_STARTUP_SUCCESS";

export const startDex = () => (dispatch, getState) => {
export const startDex = () => async (dispatch, getState) => {
dispatch({ type: DEX_STARTUP_ATTEMPT });
const isTestnet = sel.isTestNet(getState());
const {
Expand All @@ -65,7 +65,7 @@ export const startDex = () => (dispatch, getState) => {
const walletPath = getWalletPath(isTestnet, walletName);

try {
const res = sendSync("start-dex", walletPath, isTestnet);
const res = await invoke("start-dex", walletPath, isTestnet);
dispatch({ type: DEX_STARTUP_SUCCESS, serverAddress: res });
dispatch(dexCheckInit());
} catch (error) {
Expand All @@ -78,10 +78,10 @@ export const DEX_CHECKINIT_ATTEMPT = "DEX_CHECKINIT_ATTEMPT";
export const DEX_CHECKINIT_FAILED = "DEX_CHECKINIT_FAILED";
export const DEX_CHECKINIT_SUCCESS = "DEX_CHECKINIT_SUCCESS";

export const dexCheckInit = () => (dispatch) => {
export const dexCheckInit = () => async (dispatch) => {
dispatch({ type: DEX_CHECKINIT_ATTEMPT });
try {
const res = sendSync("check-init-dex");
const res = await invoke("check-init-dex");
dispatch({ type: DEX_CHECKINIT_SUCCESS, res });
} catch (error) {
dispatch({ type: DEX_CHECKINIT_FAILED, error });
Expand All @@ -96,22 +96,22 @@ export const stopDex = () => (dispatch, getState) => {
return;
}

ipcRenderer.send("stop-dex");
invoke("stop-dex");
dispatch({ type: DEX_STOPPED });
};

export const DEX_INIT_ATTEMPT = "DEX_INIT_ATTEMPT";
export const DEX_INIT_SUCCESS = "DEX_INIT_SUCCESS";
export const DEX_INIT_FAILED = "DEX_INIT_FAILED";

export const initDex = (passphrase) => (dispatch, getState) => {
export const initDex = (passphrase) => async (dispatch, getState) => {
dispatch({ type: DEX_INIT_ATTEMPT });
if (!sel.dexActive(getState())) {
dispatch({ type: DEX_INIT_FAILED, error: "Dex isn't active" });
return;
}
try {
sendSync("init-dex", passphrase);
await invoke("init-dex", passphrase);
dispatch({ type: DEX_INIT_SUCCESS });
// Request current user information
dispatch(userDex());
Expand All @@ -125,14 +125,14 @@ export const DEX_LOGIN_ATTEMPT = "DEX_LOGIN_ATTEMPT";
export const DEX_LOGIN_SUCCESS = "DEX_LOGIN_SUCCESS";
export const DEX_LOGIN_FAILED = "DEX_LOGIN_FAILED";

export const loginDex = (passphrase) => (dispatch, getState) => {
export const loginDex = (passphrase) => async (dispatch, getState) => {
dispatch({ type: DEX_LOGIN_ATTEMPT });
if (!sel.dexActive(getState())) {
dispatch({ type: DEX_LOGIN_FAILED, error: "Dex isn't active" });
return;
}
try {
sendSync("login-dex", passphrase);
await invoke("login-dex", passphrase);
dispatch({ type: DEX_LOGIN_SUCCESS });
// Request current user information
dispatch(userDex());
Expand All @@ -146,24 +146,17 @@ export const DEX_LOGOUT_ATTEMPT = "DEX_LOGOUT_ATTEMPT";
export const DEX_LOGOUT_SUCCESS = "DEX_LOGOUT_SUCCESS";
export const DEX_LOGOUT_FAILED = "DEX_LOGOUT_FAILED";

export const logoutDex = () =>
new Promise((resolve, reject) => {
try {
sendSync("logout-dex");
return resolve(true);
} catch (error) {
return reject(error);
}
});
export const logoutDex = () => invoke("logout-dex");

export const DEX_CREATEWALLET_ATTEMPT = "DEX_CREATEWALLET_ATTEMPT";
export const DEX_CREATEWALLET_SUCCESS = "DEX_CREATEWALLET_SUCCESS";
export const DEX_CREATEWALLET_FAILED = "DEX_CREATEWALLET_FAILED";

export const createWalletDex = (passphrase, appPassphrase, accountName) => (
dispatch,
getState
) => {
export const createWalletDex = (
passphrase,
appPassphrase,
accountName
) => async (dispatch, getState) => {
dispatch({ type: DEX_CREATEWALLET_ATTEMPT });
if (!sel.dexActive(getState())) {
dispatch({ type: DEX_CREATEWALLET_FAILED, error: "Dex isn't active" });
Expand All @@ -180,7 +173,7 @@ export const createWalletDex = (passphrase, appPassphrase, accountName) => (
const rpclisten = rpcCreds.rpcListen;
const rpccert = rpcCreds.rpcCert;
const assetID = 42;
sendSync(
await invoke(
"create-wallet-dex",
assetID,
passphrase,
Expand Down Expand Up @@ -208,7 +201,7 @@ export const btcCreateWalletDex = (
passphrase,
appPassphrase,
btcWalletName
) => (dispatch, getState) => {
) => async (dispatch, getState) => {
dispatch({ type: BTC_CREATEWALLET_ATTEMPT });
if (!sel.dexActive(getState())) {
dispatch({ type: BTC_CREATEWALLET_FAILED, error: "Dex isn't active" });
Expand All @@ -226,7 +219,7 @@ export const btcCreateWalletDex = (
? btcConfig.test.rpcbind + ":" + btcConfig.test.rpcport
: btcConfig.rpcbind + ":" + btcConfig.rpcport;
const assetID = 0;
sendSync(
await invoke(
"create-wallet-dex",
assetID,
passphrase,
Expand Down Expand Up @@ -257,14 +250,14 @@ export const DEX_USER_ATTEMPT = "DEX_USER_ATTEMPT";
export const DEX_USER_SUCCESS = "DEX_USER_SUCCESS";
export const DEX_USER_FAILED = "DEX_USER_FAILED";

export const userDex = () => (dispatch, getState) => {
export const userDex = () => async (dispatch, getState) => {
dispatch({ type: DEX_USER_ATTEMPT });
if (!sel.dexActive(getState())) {
dispatch({ type: DEX_USER_FAILED, error: "Dex isn't active" });
return;
}
try {
const user = sendSync("user-dex");
const user = await invoke("user-dex");
dispatch({ type: DEX_USER_SUCCESS, user });
} catch (error) {
dispatch({ type: DEX_USER_FAILED, error });
Expand All @@ -276,14 +269,14 @@ export const DEX_GETCONFIG_ATTEMPT = "DEX_GETCONFIG_ATTEMPT";
export const DEX_GETCONFIG_SUCCESS = "DEX_GETCONFIG_SUCCESS";
export const DEX_GETCONFIG_FAILED = "DEX_GETCONFIG_FAILED";

export const getConfigDex = (addr) => (dispatch, getState) => {
export const getConfigDex = (addr) => async (dispatch, getState) => {
dispatch({ type: DEX_GETCONFIG_ATTEMPT });
if (!sel.dexActive(getState())) {
dispatch({ type: DEX_GETCONFIG_FAILED, error: "Dex isn't active" });
return;
}
try {
const config = sendSync("get-config-dex", addr);
const config = await invoke("get-config-dex", addr);
dispatch({ type: DEX_GETCONFIG_SUCCESS, config, addr });
} catch (error) {
dispatch({ type: DEX_GETCONFIG_FAILED, error });
Expand All @@ -295,7 +288,7 @@ export const DEX_REGISTER_ATTEMPT = "DEX_REGISTER_ATTEMPT";
export const DEX_REGISTER_SUCCESS = "DEX_REGISTER_SUCCESS";
export const DEX_REGISTER_FAILED = "DEX_REGISTER_FAILED";

export const registerDex = (appPass) => (dispatch, getState) => {
export const registerDex = (appPass) => async (dispatch, getState) => {
dispatch({ type: DEX_REGISTER_ATTEMPT });
if (!sel.dexActive(getState())) {
dispatch({ type: DEX_REGISTER_FAILED, error: "Dex isn't acteive" });
Expand All @@ -309,7 +302,7 @@ export const registerDex = (appPass) => (dispatch, getState) => {
}
const fee = config.feeAsset.amount;
try {
sendSync("register-dex", appPass, addr, fee);
await invoke("register-dex", appPass, addr, fee);
dispatch({ type: DEX_REGISTER_SUCCESS });
// Request current user information
dispatch(userDex());
Expand All @@ -331,7 +324,7 @@ export const DEX_LAUNCH_WINDOW_ATTEMPT = "DEX_LAUNCH_WINDOW_ATTEMPT";
export const DEX_LAUNCH_WINDOW_SUCCESS = "DEX_LAUNCH_WINDOW_SUCCESS";
export const DEX_LAUNCH_WINDOW_FAILED = "DEX_LAUNCH_WINDOW_FAILED";

export const launchDexWindow = () => (dispatch, getState) => {
export const launchDexWindow = () => async (dispatch, getState) => {
const {
dex: { dexServerAddress }
} = getState();
Expand All @@ -342,7 +335,7 @@ export const launchDexWindow = () => (dispatch, getState) => {
}
try {
const serverAddress = dexServerAddress;
sendSync("launch-dex-window", serverAddress);
await invoke("launch-dex-window", serverAddress);
dispatch({ type: DEX_LAUNCH_WINDOW_SUCCESS });
// Request current user information
dispatch(userDex());
Expand All @@ -360,10 +353,10 @@ export const CHECK_BTC_CONFIG_SUCCESS_UPDATE_NEEDED =
export const CHECK_BTC_CONFIG_SUCCESS_NEED_INSTALL =
"CHECK_BTC_CONFIG_SUCCESS_NEED_INSTALL";

export const checkBTCConfig = () => (dispatch, getState) => {
export const checkBTCConfig = () => async (dispatch, getState) => {
dispatch({ type: CHECK_BTC_CONFIG_ATTEMPT });
try {
const res = sendSync("check-btc-config");
const res = await invoke("check-btc-config");
if (
res.rpcuser &&
res.rpcpassword &&
Expand Down Expand Up @@ -397,15 +390,15 @@ export const UPDATE_BTC_CONFIG_ATTEMPT = "UPDATE_BTC_CONFIG_ATTEMPT";
export const UPDATE_BTC_CONFIG_SUCCESS = "UPDATE_BTC_CONFIG_SUCCESS";
export const UPDATE_BTC_CONFIG_FAILED = "UPDATE_BTC_CONFIG_FAILED";

export const updateBTCConfig = () => (dispatch, getState) => {
export const updateBTCConfig = () => async (dispatch, getState) => {
dispatch({ type: UPDATE_BTC_CONFIG_ATTEMPT });
try {
const rpcuser = makeRandomString(12);
const rpcpassword = makeRandomString(12);
const rpcbind = "127.0.0.1";
const rpcport = sel.isTestNet(getState()) ? "18332" : "8332";
const testnet = sel.isTestNet(getState());
const res = sendSync(
const res = await invoke(
"update-btc-config",
rpcuser,
rpcpassword,
Expand Down
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
4 changes: 2 additions & 2 deletions app/actions/StatisticsActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as wallet from "wallet";
import * as sel from "selectors";
import fs from "fs";
import { fs } from "wallet-preload-shim";
import { isNumber, isNil, isUndefined } from "lodash";
import { endOfDay, formatLocalISODate, isSameDate } from "helpers";
import {
Expand Down Expand Up @@ -380,7 +380,7 @@ export const exportStatToCSV = (opts) => (dispatch, getState) => {
const seriesNames = allSeries.map((s) => s.name);
const headerLine = csvLine(["time", ...seriesNames]);

fd = fs.openSync(csvFilename, "w", 0o600);
fd = fs.openWritable(csvFilename);
fs.writeSync(fd, headerLine);
fs.writeSync(fd, ln);
} catch (err) {
Expand Down
12 changes: 10 additions & 2 deletions app/components/views/GetStartedPage/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,24 @@ export const useGetStarted = () => {
});
const getError = useCallback((serviceError) => {
if (!serviceError) return;

// We can return errors in the form of react component, which are objects.
// So we handle them first.
if (React.isValidElement(serviceError)) {
return serviceError;
}
// If the errors is an object but not a react component, we strigfy it so we can
// render.

// If the error is an instance of the Error class, extract the message.
if (serviceError instanceof Error) {
return serviceError.message;
}

// If the error is an object but not a react component, we stringify it so
// we can render it.
if (isObject(serviceError)) {
return JSON.stringify(serviceError);
}

return serviceError;
}, []);
const error = useMemo(
Expand Down
Loading

0 comments on commit fce4fb9

Please sign in to comment.