Skip to content

Commit

Permalink
Merge branch 'develop' into fix-872-a
Browse files Browse the repository at this point in the history
  • Loading branch information
NidhiKJha authored Aug 1, 2023
2 parents dfa2f83 + 10ffc1e commit 6457684
Show file tree
Hide file tree
Showing 54 changed files with 2,456 additions and 448 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ version: 2.1
executors:
node-browsers:
docker:
- image: cimg/node:16.20-browsers
- image: cimg/node:18.17-browsers
node-browsers-medium-plus:
docker:
- image: cimg/node:16.20-browsers
- image: cimg/node:18.17-browsers
resource_class: medium+
environment:
NODE_OPTIONS: --max_old_space_size=2048
node-browsers-large:
docker:
- image: cimg/node:16.20-browsers
- image: cimg/node:18.17-browsers
resource_class: large
environment:
NODE_OPTIONS: --max_old_space_size=2048
Expand Down
16 changes: 9 additions & 7 deletions .circleci/scripts/install-dependencies.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/usr/bin/env bash

set -e
set -u
set -o pipefail

PR_NUMBER="${CIRCLE_PULL_REQUEST##*/}"
if [ -n "$PR_NUMBER" ]
IS_NON_FORK_DRAFT='false'

if [[ -n $CIRCLE_PULL_REQUEST ]] && gh auth status
then
IS_DRAFT="$(gh pr view --json isDraft --jq '.isDraft' "$PR_NUMBER")"
else
IS_DRAFT='false'
PR_NUMBER="${CIRCLE_PULL_REQUEST##*/}"
if [ -n "$PR_NUMBER" ]
then
IS_NON_FORK_DRAFT="$(gh pr view --json isDraft --jq '.isDraft' "$PR_NUMBER")"
fi
fi

# Build query to see whether there are any "preview-like" packages in the manifest
Expand All @@ -29,7 +31,7 @@ else
HAS_PREVIEW_BUILDS='false'
fi

if [[ $IS_DRAFT == 'true' && $HAS_PREVIEW_BUILDS == 'true' ]]
if [[ $IS_NON_FORK_DRAFT == 'true' && $HAS_PREVIEW_BUILDS == 'true' ]]
then
# Use GitHub registry on draft PRs, allowing the use of preview builds
echo "Installing with preview builds"
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16
v18
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ To learn how to contribute to the MetaMask project itself, visit our [Internal D

## Building locally

- Install [Node.js](https://nodejs.org) version 16
- Install [Node.js](https://nodejs.org) version 18
- If you are using [nvm](https://github.com/nvm-sh/nvm#installing-and-updating) (recommended) running `nvm use` will automatically choose the right node version for you.
- Install [Yarn v3](https://yarnpkg.com/getting-started/install)
- ONLY follow the steps in the "Install Corepack" and "Updating the global Yarn version" sections
- DO NOT take any of the steps in the "Initializing your project", "Updating to the latest versions" or "Installing the latest build fresh from master" sections. These steps could result in your repo being reset or installing the wrong yarn version, which can break your build.
- ONLY follow the steps in the "Install Corepack" and "Updating the global Yarn version" sections
- DO NOT take any of the steps in the "Initializing your project", "Updating to the latest versions" or "Installing the latest build fresh from master" sections. These steps could result in your repo being reset or installing the wrong yarn version, which can break your build.
- Duplicate `.metamaskrc.dist` within the root and rename it to `.metamaskrc`
- Replace the `INFURA_PROJECT_ID` value with your own personal [Infura Project ID](https://infura.io/docs).
- If debugging MetaMetrics, you'll need to add a value for `SEGMENT_WRITE_KEY` [Segment write key](https://segment.com/docs/connections/find-writekey/), see [Developing on MetaMask - Segment](./development/README.md#segment).
Expand Down
4 changes: 4 additions & 0 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
* @file The entry point for the web extension singleton process.
*/

// This import sets up a global function required for Sentry to function.
// It must be run first in case an error is thrown later during initialization.
import './lib/setup-persisted-state-hook';

import EventEmitter from 'events';
import endOfStream from 'end-of-stream';
import pump from 'pump';
Expand Down
6 changes: 6 additions & 0 deletions app/scripts/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,12 @@ function redirectToPhishingWarning() {

const querystring = new URLSearchParams({ hostname, href });
window.location.href = `${baseUrl}#${querystring}`;
// eslint-disable-next-line no-constant-condition
while (1) {
console.log(
'MetaMask: Locking js execution, redirection will complete shortly',
);
}
}

const start = () => {
Expand Down
8 changes: 4 additions & 4 deletions app/scripts/controllers/metametrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,18 +419,18 @@ export default class MetaMetricsController {
*
* @param {boolean} participateInMetaMetrics - Whether or not the user wants
* to participate in MetaMetrics
* @returns {string|null} the string of the new metametrics id, or null
* @returns {Promise<string|null>} the string of the new metametrics id, or null
* if not set
*/
setParticipateInMetaMetrics(participateInMetaMetrics) {
async setParticipateInMetaMetrics(participateInMetaMetrics) {
let { metaMetricsId } = this.state;
if (participateInMetaMetrics && !metaMetricsId) {
// We also need to start sentry automatic session tracking at this point
globalThis.sentry?.startSession();
await globalThis.sentry?.startSession();
metaMetricsId = this.generateMetaMetricsId();
} else if (participateInMetaMetrics === false) {
// We also need to stop sentry automatic session tracking at this point
globalThis.sentry?.endSession();
await globalThis.sentry?.endSession();
metaMetricsId = null;
}
this.store.updateState({ participateInMetaMetrics, metaMetricsId });
Expand Down
14 changes: 7 additions & 7 deletions app/scripts/controllers/metametrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,30 +313,30 @@ describe('MetaMetricsController', function () {
});

describe('setParticipateInMetaMetrics', function () {
it('should update the value of participateInMetaMetrics', function () {
it('should update the value of participateInMetaMetrics', async function () {
const metaMetricsController = getMetaMetricsController({
participateInMetaMetrics: null,
metaMetricsId: null,
});
assert.equal(metaMetricsController.state.participateInMetaMetrics, null);
metaMetricsController.setParticipateInMetaMetrics(true);
await metaMetricsController.setParticipateInMetaMetrics(true);
assert.ok(globalThis.sentry.startSession.calledOnce);
assert.equal(metaMetricsController.state.participateInMetaMetrics, true);
metaMetricsController.setParticipateInMetaMetrics(false);
await metaMetricsController.setParticipateInMetaMetrics(false);
assert.equal(metaMetricsController.state.participateInMetaMetrics, false);
});
it('should generate and update the metaMetricsId when set to true', function () {
it('should generate and update the metaMetricsId when set to true', async function () {
const metaMetricsController = getMetaMetricsController({
participateInMetaMetrics: null,
metaMetricsId: null,
});
assert.equal(metaMetricsController.state.metaMetricsId, null);
metaMetricsController.setParticipateInMetaMetrics(true);
await metaMetricsController.setParticipateInMetaMetrics(true);
assert.equal(typeof metaMetricsController.state.metaMetricsId, 'string');
});
it('should nullify the metaMetricsId when set to false', function () {
it('should nullify the metaMetricsId when set to false', async function () {
const metaMetricsController = getMetaMetricsController();
metaMetricsController.setParticipateInMetaMetrics(false);
await metaMetricsController.setParticipateInMetaMetrics(false);
assert.ok(globalThis.sentry.endSession.calledOnce);
assert.equal(metaMetricsController.state.metaMetricsId, null);
});
Expand Down
39 changes: 37 additions & 2 deletions app/scripts/controllers/transactions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ import {
isEIP1559Transaction,
} from '../../../../shared/modules/transaction.utils';
import { ORIGIN_METAMASK } from '../../../../shared/constants/app';
///: BEGIN:ONLY_INCLUDE_IN(blockaid)
import {
BlockaidReason,
BlockaidResultType,
} from '../../../../shared/constants/security-provider';
///: END:ONLY_INCLUDE_IN
import {
calcGasTotal,
getSwapsTokensReceivedFromTxMeta,
Expand Down Expand Up @@ -327,6 +333,7 @@ export default class TransactionController extends EventEmitter {
this.txStateManager.wipeTransactions(address);
}

/* eslint-disable jsdoc/require-param, jsdoc/check-param-names */
/**
* Add a new unapproved transaction
*
Expand All @@ -342,7 +349,7 @@ export default class TransactionController extends EventEmitter {
* @param {boolean} opts.swaps.meta - Additional metadata to store for the transaction
* @param {TransactionType} opts.type - Type of transaction to add, such as 'cancel' or 'swap'
* @returns {Promise<{transactionMeta: TransactionMeta, result: Promise<string>}>} An object containing the transaction metadata, and a promise that resolves to the transaction hash after being submitted to the network
*/
*/ /* eslint-enable jsdoc/require-param, jsdoc/check-param-names */
async addTransaction(
txParams,
{
Expand All @@ -353,6 +360,9 @@ export default class TransactionController extends EventEmitter {
sendFlowHistory,
swaps: { hasApproveTx, meta } = {},
type,
///: BEGIN:ONLY_INCLUDE_IN(blockaid)
securityAlertResponse,
///: END:ONLY_INCLUDE_IN
} = {},
) {
log.debug(`MetaMaskController addTransaction ${JSON.stringify(txParams)}`);
Expand All @@ -364,6 +374,9 @@ export default class TransactionController extends EventEmitter {
sendFlowHistory,
swaps: { hasApproveTx, meta },
type,
///: BEGIN:ONLY_INCLUDE_IN(blockaid)
securityAlertResponse,
///: END:ONLY_INCLUDE_IN
});

return {
Expand Down Expand Up @@ -1469,7 +1482,17 @@ export default class TransactionController extends EventEmitter {

async _createTransaction(
txParams,
{ actionId, method, origin, sendFlowHistory = [], swaps, type },
{
actionId,
method,
origin,
sendFlowHistory = [],
swaps,
type,
///: BEGIN:ONLY_INCLUDE_IN(blockaid)
securityAlertResponse,
///: END:ONLY_INCLUDE_IN
},
) {
if (
type !== undefined &&
Expand Down Expand Up @@ -1504,6 +1527,9 @@ export default class TransactionController extends EventEmitter {
txParams: normalizedTxParams,
origin,
sendFlowHistory,
///: BEGIN:ONLY_INCLUDE_IN(blockaid)
securityAlertResponse,
///: END:ONLY_INCLUDE_IN
});

// Add actionId to txMeta to check if same actionId is seen again
Expand Down Expand Up @@ -2233,6 +2259,9 @@ export default class TransactionController extends EventEmitter {
finalApprovalAmount,
contractMethodName,
securityProviderResponse,
///: BEGIN:ONLY_INCLUDE_IN(blockaid)
securityAlertResponse,
///: END:ONLY_INCLUDE_IN
} = txMeta;

const source = referrer === ORIGIN_METAMASK ? 'user' : 'dapp';
Expand Down Expand Up @@ -2411,6 +2440,12 @@ export default class TransactionController extends EventEmitter {
transaction_type: transactionType,
transaction_speed_up: type === TransactionType.retry,
ui_customizations: uiCustomizations,
///: BEGIN:ONLY_INCLUDE_IN(blockaid)
security_alert_response:
securityAlertResponse?.result_type ?? BlockaidResultType.NotApplicable,
security_alert_reason:
securityAlertResponse?.reason ?? BlockaidReason.notApplicable,
///: END:ONLY_INCLUDE_IN
};

if (transactionContractMethod === contractMethodNames.APPROVE) {
Expand Down
26 changes: 26 additions & 0 deletions app/scripts/controllers/transactions/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { ApprovalType } from '@metamask/controller-utils';
import sinon from 'sinon';

import { errorCodes, ethErrors } from 'eth-rpc-errors';
import {
BlockaidReason,
BlockaidResultType,
} from '../../../../shared/constants/security-provider';
import {
createTestProviderTools,
getTestAccounts,
Expand Down Expand Up @@ -2163,6 +2167,8 @@ describe('Transaction Controller', function () {
device_model: 'N/A',
transaction_speed_up: false,
ui_customizations: null,
security_alert_reason: BlockaidReason.notApplicable,
security_alert_response: BlockaidResultType.NotApplicable,
status: 'unapproved',
},
sensitiveProperties: {
Expand Down Expand Up @@ -2250,6 +2256,8 @@ describe('Transaction Controller', function () {
device_model: 'N/A',
transaction_speed_up: false,
ui_customizations: null,
security_alert_reason: BlockaidReason.notApplicable,
security_alert_response: BlockaidResultType.NotApplicable,
status: 'unapproved',
},
sensitiveProperties: {
Expand Down Expand Up @@ -2349,6 +2357,8 @@ describe('Transaction Controller', function () {
device_model: 'N/A',
transaction_speed_up: false,
ui_customizations: null,
security_alert_reason: BlockaidReason.notApplicable,
security_alert_response: BlockaidResultType.NotApplicable,
status: 'unapproved',
},
sensitiveProperties: {
Expand Down Expand Up @@ -2438,6 +2448,8 @@ describe('Transaction Controller', function () {
device_model: 'N/A',
transaction_speed_up: false,
ui_customizations: null,
security_alert_reason: BlockaidReason.notApplicable,
security_alert_response: BlockaidResultType.NotApplicable,
status: 'unapproved',
},
sensitiveProperties: {
Expand Down Expand Up @@ -2505,6 +2517,10 @@ describe('Transaction Controller', function () {
securityProviderResponse: {
flagAsDangerous: 0,
},
securityAlertResponse: {
security_alert_reason: BlockaidReason.notApplicable,
security_alert_response: BlockaidResultType.NotApplicable,
},
};

const expectedPayload = {
Expand All @@ -2529,6 +2545,8 @@ describe('Transaction Controller', function () {
device_model: 'N/A',
transaction_speed_up: false,
ui_customizations: null,
security_alert_reason: BlockaidReason.notApplicable,
security_alert_response: BlockaidResultType.NotApplicable,
status: 'unapproved',
},
sensitiveProperties: {
Expand Down Expand Up @@ -2601,6 +2619,8 @@ describe('Transaction Controller', function () {
device_model: 'N/A',
transaction_speed_up: false,
ui_customizations: null,
security_alert_reason: BlockaidReason.notApplicable,
security_alert_response: BlockaidResultType.NotApplicable,
status: 'unapproved',
},
sensitiveProperties: {
Expand Down Expand Up @@ -2675,6 +2695,8 @@ describe('Transaction Controller', function () {
device_model: 'N/A',
transaction_speed_up: false,
ui_customizations: ['flagged_as_malicious'],
security_alert_reason: BlockaidReason.notApplicable,
security_alert_response: BlockaidResultType.NotApplicable,
status: 'unapproved',
},
sensitiveProperties: {
Expand Down Expand Up @@ -2749,6 +2771,8 @@ describe('Transaction Controller', function () {
device_model: 'N/A',
transaction_speed_up: false,
ui_customizations: ['flagged_as_safety_unknown'],
security_alert_reason: BlockaidReason.notApplicable,
security_alert_response: BlockaidResultType.NotApplicable,
status: 'unapproved',
},
sensitiveProperties: {
Expand Down Expand Up @@ -2831,6 +2855,8 @@ describe('Transaction Controller', function () {
device_model: 'N/A',
transaction_speed_up: false,
ui_customizations: null,
security_alert_reason: BlockaidReason.notApplicable,
security_alert_response: BlockaidResultType.NotApplicable,
status: 'unapproved',
},
sensitiveProperties: {
Expand Down
8 changes: 4 additions & 4 deletions app/scripts/lib/sentry-filter-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class FilterEvents implements Integration {
* @returns `true` if MetaMask's state has been initialized, and MetaMetrics
* is enabled, `false` otherwise.
*/
private getMetaMetricsEnabled: () => boolean;
private getMetaMetricsEnabled: () => Promise<boolean>;

/**
* @param options - Constructor options.
Expand All @@ -40,7 +40,7 @@ export class FilterEvents implements Integration {
constructor({
getMetaMetricsEnabled,
}: {
getMetaMetricsEnabled: () => boolean;
getMetaMetricsEnabled: () => Promise<boolean>;
}) {
this.getMetaMetricsEnabled = getMetaMetricsEnabled;
}
Expand All @@ -56,13 +56,13 @@ export class FilterEvents implements Integration {
addGlobalEventProcessor: (callback: EventProcessor) => void,
getCurrentHub: () => Hub,
): void {
addGlobalEventProcessor((currentEvent: SentryEvent) => {
addGlobalEventProcessor(async (currentEvent: SentryEvent) => {
// Sentry integrations use the Sentry hub to get "this" references, for
// reasons I don't fully understand.
// eslint-disable-next-line consistent-this
const self = getCurrentHub().getIntegration(FilterEvents);
if (self) {
if (!self.getMetaMetricsEnabled()) {
if (!(await self.getMetaMetricsEnabled())) {
logger.warn(`Event dropped due to MetaMetrics setting.`);
return null;
}
Expand Down
Loading

0 comments on commit 6457684

Please sign in to comment.