Skip to content

Commit

Permalink
[multi] Disable Node Integration in the UI code (#3486)
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusd authored Jun 7, 2021
1 parent bb7b822 commit ed27624
Show file tree
Hide file tree
Showing 62 changed files with 1,392 additions and 422 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module.exports = {
globals: {
Uint8Array: true,
React: "readonly",
PropTypes: "readonly"
PropTypes: "readonly",
globalThis: "readonly"
}
};
2 changes: 1 addition & 1 deletion app/actions/AccountMixerActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Promise from "promise";
import * as sel from "selectors";
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import {
getAcctSpendableBalance,
getAccountsAttempt,
Expand Down
2 changes: 1 addition & 1 deletion app/actions/ClientActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import * as sel from "selectors";
import eq from "lodash/fp/eq";
import isUndefined from "lodash/fp/isUndefined";
Expand Down
165 changes: 64 additions & 101 deletions app/actions/ControlActions.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
// @flow
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import * as sel from "selectors";
import { isValidAddress, isValidMasterPubKey } from "helpers";
import {
getStakeInfoAttempt,
startWalletServices,
getStartupWalletInfo
} from "./ClientActions";
import { walletrpc as api } from "../middleware/walletrpc/api_pb";
import { reverseRawHash, rawToHex } from "helpers/byteActions";
import { listUnspentOutputs } from "./TransactionActions";
import { updateUsedVSPs, getVSPTrackedTickets } from "./VSPActions";
import { isNumber } from "fp";
import { setNeedsVSPdProcessTickets } from "./SettingsActions";

const { ConstructTransactionRequest } = api;

export const GETNEXTADDRESS_ATTEMPT = "GETNEXTADDRESS_ATTEMPT";
export const GETNEXTADDRESS_FAILED = "GETNEXTADDRESS_FAILED";
export const GETNEXTADDRESS_SUCCESS = "GETNEXTADDRESS_SUCCESS";
Expand Down Expand Up @@ -588,33 +585,22 @@ export const constructTransactionAttempt = (
confirmations,
outputs,
all
) => (dispatch, getState) => {
const request = new ConstructTransactionRequest();
let totalAmount;
request.setSourceAccount(parseInt(account));
request.setRequiredConfirmations(parseInt(parseInt(confirmations)));
if (!all) {
request.setOutputSelectionAlgorithm(0);
totalAmount = 0;
outputs.forEach((output) => {
const outputDest = new ConstructTransactionRequest.OutputDestination();
outputDest.setAddress(output.destination);
const newOutput = new ConstructTransactionRequest.Output();
newOutput.setDestination(outputDest);
newOutput.setAmount(parseInt(output.amount));
request.addNonChangeOutputs(newOutput);
totalAmount += output.amount;
});
) => async (dispatch, getState) => {
const { constructTxRequestAttempt } = getState().control;
if (constructTxRequestAttempt) {
return;
}

// Determine the change address.
let change;
if (!all) {
// If there's a previously stored change address for this account, use it.
// This alleviates a possible gap limit address exhaustion. See
// issue dcrwallet#1622.
const changeScript =
getState().control.changeScriptByAccount[account] || null;
if (changeScript) {
const changeDest = new ConstructTransactionRequest.OutputDestination();
changeDest.setScript(changeScript);
request.setChangeDestination(changeDest);
change = { script: changeScript };
} else {
// set change destination. If it is a privacy wallet we get the
// next unmixed address.
Expand All @@ -629,94 +615,71 @@ export const constructTransactionAttempt = (
};
}
const newChangeAddress = sel.nextAddress(getState());
const outputDest = new ConstructTransactionRequest.OutputDestination();
outputDest.setAddress(newChangeAddress);
request.setChangeDestination(outputDest);
change = { address: newChangeAddress };
}
}
} else {
if (outputs.length > 1) {
return (dispatch) => {
const error = "Too many outputs provided for a send all request.";
dispatch({ error, type: CONSTRUCTTX_FAILED });
};
}
if (outputs.length == 0) {
return (dispatch) => {
const error = "No destination specified for send all request.";
dispatch({ error, type: CONSTRUCTTX_FAILED });
};
}
// set change to same destination as it is a send all tx.
request.setOutputSelectionAlgorithm(1);
const outputDest = new ConstructTransactionRequest.OutputDestination();
outputDest.setAddress(outputs[0].data.destination);
request.setChangeDestination(outputDest);
}

let { constructTxRequestAttempt } = getState().control;
if (constructTxRequestAttempt) {
constructTxRequestAttempt.cancel();
}
const chainParams = sel.chainParams(getState());
const { walletService } = getState().grpc;
constructTxRequestAttempt = walletService.constructTransaction(
request,
function (error, constructTxResponse) {
if (error) {
if (String(error).indexOf("insufficient balance") > 0) {
dispatch({ error, type: CONSTRUCTTX_FAILED_LOW_BALANCE });
} else if (
String(error).indexOf(
"violates the unused address gap limit policy"
) > 0
) {
// Work around dcrwallet#1622: generate a new address in the internal
// branch using the wrap gap policy so that change addresses can be
// regenerated again.
// We'll still error out to let the user know wrapping has occurred.
wallet.getNextAddress(
sel.walletService(getState()),
parseInt(account),
1
);
dispatch({ error, type: CONSTRUCTTX_FAILED });
} else if (String(error).indexOf("Cancelled") == 0) {
dispatch({ error, type: CONSTRUCTTX_FAILED });
}
return;
}

const changeScriptByAccount =
getState().control.changeScriptByAccount || {};
if (!all) {
// Store the change address we just generated so that future changes to
// the tx being constructed will use the same address and prevent gap
// limit exhaustion (see above note on issue dcrwallet#1622).
const changeIndex = constructTxResponse.getChangeIndex();
if (changeIndex > -1) {
const rawTx = Buffer.from(
constructTxResponse.getUnsignedTransaction()
);
const decoded = wallet.decodeRawTransaction(rawTx, chainParams);
changeScriptByAccount[account] = decoded.outputs[changeIndex].script;
}
dispatch({ type: CONSTRUCTTX_ATTEMPT, constructTxRequestAttempt: true });
try {
const constructFunc = all
? wallet.constructSendAllTransaction
: wallet.constructTransaction;
const constructTxResponse = await constructFunc(
walletService,
account,
confirmations,
outputs,
change
);

constructTxResponse.totalAmount = totalAmount;
} else {
constructTxResponse.totalAmount = constructTxResponse.getTotalOutputAmount();
const changeScriptByAccount =
getState().control.changeScriptByAccount || {};

if (!all) {
// Store the change address we just generated so that future changes to
// the tx being constructed will use the same address and prevent gap
// limit exhaustion (see above note on issue dcrwallet#1622).
const changeIndex = constructTxResponse.getChangeIndex();
if (changeIndex > -1) {
const rawTx = Buffer.from(constructTxResponse.getUnsignedTransaction());
const decoded = wallet.decodeRawTransaction(rawTx, chainParams);
changeScriptByAccount[account] = decoded.outputs[changeIndex].script;
}
constructTxResponse.rawTx = rawToHex(
constructTxResponse.getUnsignedTransaction()
}

constructTxResponse.rawTx = rawToHex(
constructTxResponse.getUnsignedTransaction()
);

dispatch({
constructTxResponse: constructTxResponse,
changeScriptByAccount,
type: CONSTRUCTTX_SUCCESS
});
} catch (error) {
if (String(error).indexOf("insufficient balance") > 0) {
dispatch({ error, type: CONSTRUCTTX_FAILED_LOW_BALANCE });
} else if (
String(error).indexOf("violates the unused address gap limit policy") > 0
) {
// Work around dcrwallet#1622: generate a new address in the internal
// branch using the wrap gap policy so that change addresses can be
// regenerated again.
// We'll still error out to let the user know wrapping has occurred.
wallet.getNextAddress(
sel.walletService(getState()),
parseInt(account),
1
);
dispatch({
constructTxResponse: constructTxResponse,
changeScriptByAccount,
type: CONSTRUCTTX_SUCCESS
});
dispatch({ error, type: CONSTRUCTTX_FAILED });
} else {
dispatch({ error, type: CONSTRUCTTX_FAILED });
}
);
dispatch({ type: CONSTRUCTTX_ATTEMPT, constructTxRequestAttempt });
}
};

export const VALIDATEADDRESS_CLEANSTORE = "VALIDATEADDRESS_CLEANSTORE";
Expand Down
8 changes: 3 additions & 5 deletions app/actions/DaemonActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ import {
SET_REMEMBERED_VSP_HOST,
SET_AUTOBUYER_SETTINGS
} from "./VSPActions";
import * as fs from "wallet/fs";
import * as wallet from "wallet";
import { wallet, fs } from "wallet-preload-shim";
import { push as pushHistory, goBack } from "connected-react-router";
import { isTestNet } from "selectors";
import axios from "axios";
import { getJSON } from "helpers/fetch";
import { STANDARD_EXTERNAL_REQUESTS } from "constants";
import { DIFF_CONNECTION_ERROR, LOCALE, TESTNET } from "constants";
import * as cfgConstants from "constants/config";
Expand Down Expand Up @@ -75,8 +74,7 @@ export const checkDecreditonVersion = () => (dispatch, getState) => {
const detectedVersion = getState().daemon.appVersion;
const releaseApiURL =
"https://api.github.com/repos/decred/decrediton/releases";
axios
.get(releaseApiURL, { timeout: 5000 })
getJSON(releaseApiURL)
.then(function (response) {
const currentVersion = response.data[0].tag_name.split("v")[1];
if (semverCompatible(currentVersion, detectedVersion)) {
Expand Down
3 changes: 1 addition & 2 deletions app/actions/DexActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as sel from "selectors";
import * as dex from "wallet/dex";
import * as wallet from "wallet";
import { wallet, dex } from "wallet-preload-shim";
import { addAllowedExternalRequest } from "./SettingsActions";
import { getNextAccountAttempt } from "./ControlActions";
import { closeWalletRequest } from "./WalletLoaderActions";
Expand Down
3 changes: 1 addition & 2 deletions app/actions/GovernanceActions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { push as pushHistory } from "connected-react-router";
import * as sel from "selectors";
import * as pi from "wallet/politeia";
import * as wallet from "wallet";
import { wallet, politeia as pi } from "wallet-preload-shim";
import {
hexReversedHashToArray,
reverseRawHash,
Expand Down
3 changes: 1 addition & 2 deletions app/actions/LNActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as ln from "wallet/ln";
import * as sel from "selectors";
import * as wallet from "wallet";
import { wallet, ln } from "wallet-preload-shim";
import { getNextAccountAttempt } from "./ControlActions";
import * as cfgConstants from "constants/config";
import { isNumber } from "lodash";
Expand Down
2 changes: 1 addition & 1 deletion app/actions/NotificationActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import {
getTicketPriceAttempt,
updateAccount,
Expand Down
2 changes: 1 addition & 1 deletion app/actions/SettingsActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isTestNet } from "selectors";
import { equalElements } from "helpers";
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import * as sel from "selectors";
import { closeWalletRequest } from "actions/WalletLoaderActions";
import { closeDaemonRequest, backToCredentials } from "actions/DaemonActions";
Expand Down
2 changes: 1 addition & 1 deletion app/actions/StatisticsActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import * as sel from "selectors";
import { fs } from "wallet-preload-shim";
import { isNumber, isNil, isUndefined } from "lodash";
Expand Down
2 changes: 1 addition & 1 deletion app/actions/TransactionActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import * as sel from "selectors";
import eq from "lodash/fp/eq";
import { checkUnmixedAccountBalance } from "./AccountMixerActions";
Expand Down
3 changes: 1 addition & 2 deletions app/actions/TrezorActions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as wallet from "wallet";
import * as fs from "wallet/fs";
import { wallet, fs } from "wallet-preload-shim";
import * as selectors from "selectors";
import { hexToBytes, str2utf8hex, rawToHex } from "helpers";
import {
Expand Down
2 changes: 1 addition & 1 deletion app/actions/VSPActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from "./ControlActions";
import { SETVOTECHOICES_SUCCESS } from "./ClientActions";
import * as sel from "../selectors";
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import {
TESTNET,
MAINNET,
Expand Down
2 changes: 1 addition & 1 deletion app/actions/VersionActions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { loaderRequest, getWalletSeedService } from "./WalletLoaderActions";
import { push as pushHistory } from "connected-react-router";
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import { isTestNet } from "selectors";

export const GETVERSIONSERVICE_ATTEMPT = "GETVERSIONSERVICE_ATTEMPT";
Expand Down
2 changes: 1 addition & 1 deletion app/actions/WalletLoaderActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import {
DEX_LOGOUT_ATTEMPT,
DEX_LOGOUT_SUCCESS,
Expand Down
2 changes: 1 addition & 1 deletion app/components/buttons/HelpLink/HelpLink.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import { classNames } from "pi-ui";
import styles from "./HelpLink.module.css";

Expand Down
2 changes: 1 addition & 1 deletion app/components/inputs/PathBrowseInput/PathBrowseInput.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import PathInput from "../PathInput/PathInput";
import style from "./PathBrowseInput.module.css";
import { PathButton } from "buttons";
Expand Down
6 changes: 3 additions & 3 deletions app/components/shared/Documentation/Documentation.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import { classNames } from "pi-ui";
import Docs from "i18n/docs";
import { default as ReactMarkdown, uriTransformer } from "react-markdown";
Expand Down Expand Up @@ -47,9 +47,9 @@ const Documentation = ({ name, className }) => {
return (
<>
<ReactMarkdown
source={content}
children={content}
className={classNames(styles.documentation, className)}
renderers={{ link: renderDocLink }}
components={{ a: renderDocLink }}
/>
{unavailable}
</>
Expand Down
2 changes: 1 addition & 1 deletion app/components/shared/ExternalLink.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import { useNetwork } from "hooks";

const clicker = (isTestNet, href, hrefTestNet) => () => {
Expand Down
2 changes: 1 addition & 1 deletion app/components/shared/PoliteiaLink.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMemo, useCallback } from "react";
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import { Link } from "pi-ui";

const PoliteiaLink = ({
Expand Down
2 changes: 1 addition & 1 deletion app/components/views/FatalErrorPage/FatalErrorPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { KeyBlueButton, RemoveDaemonButton } from "buttons";
import { PageBody } from "layout";
import { CopyToClipboard, ExternalLink } from "shared";
import { DIFF_CONNECTION_ERROR } from "constants";
import * as wallet from "wallet";
import { wallet } from "wallet-preload-shim";
import { useFatalErrorPage } from "./hooks";
import styles from "./FatalErrorPage.module.css";

Expand Down
Loading

0 comments on commit ed27624

Please sign in to comment.