Skip to content

Commit

Permalink
Merge branch 'main' into bump-all-packages-to-node-18
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed Feb 28, 2024
2 parents 9933f8e + 17c52d9 commit 2126c85
Show file tree
Hide file tree
Showing 136 changed files with 10,733 additions and 3,789 deletions.
8 changes: 4 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ module.exports = {
'!.eslintrc.js',
'!jest.config.js',
'node_modules',
'dist',
'docs',
'coverage',
'**/dist',
'**/docs',
'**/coverage',
'merged-packages',
'package-template',
'scripts/create-package/package-template',
],
overrides: [
{
Expand Down
30 changes: 30 additions & 0 deletions .github/workflows/ensure-blocking-pr-labels-absent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: 'Check for PR labels that block merging'
on:
pull_request:
types:
- opened
- labeled
- unlabeled

jobs:
ensure-blocking-pr-labels-absent:
runs-on: ubuntu-latest
permissions:
pull-requests: read
steps:
- uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
cache: yarn
- name: Install dependencies
run: yarn --immutable
- name: Run command
uses: actions/github-script@v7
with:
script: |
if (context.payload.pull_request.labels.some((label) => label.name === 'DO-NOT-MERGE')) {
core.setFailed(
"PR cannot be merged because it contains the label 'DO-NOT-MERGE'."
);
}
30 changes: 30 additions & 0 deletions .github/workflows/security-code-scanner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: 'MetaMask Security Code Scanner'

on:
push:
branches: ['main']
pull_request:
branches: ['main']

jobs:
run-security-scan:
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
steps:
- name: MetaMask Security Code Scanner
uses: MetaMask/Security-Code-Scanner@main
with:
repo: ${{ github.repository }}
paths_ignored: |
'**/test*/'
docs/
'**/*.test.js'
'**/*.test.ts'
node_modules
merged-packages/
'**/jest.environment.js'
mixpanel_project_token: ${{secrets.SECURITY_CODE_SCANNER_MIXPANEL_TOKEN}}
slack_webhook: ${{ secrets.APPSEC_BOT_SLACK_WEBHOOK }}
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metamask/core-monorepo",
"version": "115.0.0",
"version": "120.0.0",
"private": true,
"description": "Monorepo for packages shared between MetaMask clients",
"repository": {
Expand Down Expand Up @@ -44,7 +44,7 @@
"@babel/core": "^7.23.5",
"@babel/plugin-transform-modules-commonjs": "^7.23.3",
"@babel/preset-typescript": "^7.23.3",
"@lavamoat/allow-scripts": "^2.3.1",
"@lavamoat/allow-scripts": "^3.0.2",
"@metamask/create-release-branch": "^3.0.0",
"@metamask/eslint-config": "^12.2.0",
"@metamask/eslint-config-jest": "^12.1.0",
Expand Down Expand Up @@ -92,11 +92,6 @@
"@lavamoat/preinstall-always-fail": false,
"@keystonehq/bc-ur-registry-eth>hdkey>secp256k1": true,
"babel-runtime>core-js": false,
"eth-sig-util>ethereumjs-abi>ethereumjs-util>keccakjs>sha3": true,
"eth-sig-util>ethereumjs-util>keccak": true,
"eth-sig-util>ethereumjs-util>secp256k1": true,
"ethereumjs-util>ethereum-cryptography>keccak": true,
"ethereumjs-util>ethereum-cryptography>secp256k1": true,
"simple-git-hooks": false
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/accounts-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@ethereumjs/util": "^8.1.0",
"@metamask/base-controller": "^4.1.1",
"@metamask/eth-snap-keyring": "^2.1.1",
"@metamask/keyring-api": "^3.0.0",
"@metamask/snaps-sdk": "^1.3.2",
"@metamask/snaps-utils": "^5.1.2",
"@metamask/utils": "^8.3.0",
"deepmerge": "^4.2.2",
"ethereumjs-util": "^7.0.10",
"ethereum-cryptography": "^2.1.2",
"immer": "^9.0.6",
"uuid": "^8.3.2"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ describe('AccountsController', () => {
KeyringTypes.ledger,
KeyringTypes.lattice,
KeyringTypes.qr,
KeyringTypes.custody,
'Custody - JSON - RPC',
])('should add accounts for %s type', async (keyringType) => {
mockUUID.mockReturnValue('mock-id');

Expand Down
5 changes: 3 additions & 2 deletions packages/accounts-controller/src/AccountsController.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { toBuffer } from '@ethereumjs/util';
import type {
ControllerGetStateAction,
ControllerStateChangeEvent,
Expand All @@ -22,7 +23,7 @@ import type {
import type { SnapId } from '@metamask/snaps-sdk';
import type { Snap } from '@metamask/snaps-utils';
import type { Keyring, Json } from '@metamask/utils';
import { sha256FromString } from 'ethereumjs-util';
import { sha256 } from 'ethereum-cryptography/sha256';
import type { Draft } from 'immer';
import { v4 as uuid } from 'uuid';

Expand Down Expand Up @@ -455,7 +456,7 @@ export class AccountsController extends BaseController<
address,
);
const v4options = {
random: sha256FromString(address).slice(0, 16),
random: sha256(toBuffer(address)).slice(0, 16),
};

internalAccounts.push({
Expand Down
16 changes: 10 additions & 6 deletions packages/accounts-controller/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { KeyringTypes } from '@metamask/keyring-controller';
import { sha256FromString } from 'ethereumjs-util';
import { toBuffer } from '@ethereumjs/util';
import { isCustodyKeyring, KeyringTypes } from '@metamask/keyring-controller';
import { sha256 } from 'ethereum-cryptography/sha256';
import { v4 as uuid } from 'uuid';

/**
Expand All @@ -9,6 +10,12 @@ import { v4 as uuid } from 'uuid';
* @returns The name of the keyring type.
*/
export function keyringTypeToName(keyringType: string): string {
// Custody keyrings are a special case, as they are not a single type
// they just start with the prefix `Custody`
if (isCustodyKeyring(keyringType)) {
return 'Custody';
}

switch (keyringType) {
case KeyringTypes.simple: {
return 'Account';
Expand All @@ -31,9 +38,6 @@ export function keyringTypeToName(keyringType: string): string {
case KeyringTypes.snap: {
return 'Snap Account';
}
case KeyringTypes.custody: {
return 'Custody';
}
default: {
throw new Error(`Unknown keyring ${keyringType}`);
}
Expand All @@ -47,7 +51,7 @@ export function keyringTypeToName(keyringType: string): string {
*/
export function getUUIDFromAddressOfNormalAccount(address: string): string {
const v4options = {
random: sha256FromString(address).slice(0, 16),
random: sha256(toBuffer(address)).slice(0, 16),
};

return uuid(v4options);
Expand Down
2 changes: 1 addition & 1 deletion packages/address-book-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"dependencies": {
"@metamask/base-controller": "^4.1.1",
"@metamask/controller-utils": "^8.0.2",
"@metamask/controller-utils": "^8.0.3",
"@metamask/utils": "^8.3.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/approval-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"dependencies": {
"@metamask/base-controller": "^4.1.1",
"@metamask/rpc-errors": "^6.1.0",
"@metamask/rpc-errors": "^6.2.1",
"@metamask/utils": "^8.3.0",
"nanoid": "^3.1.31"
},
Expand Down
6 changes: 4 additions & 2 deletions packages/assets-controllers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- **BREAKING:** Adds `@metamask/accounts-controller` ^8.0.0 and `@metamask/keyring-controller` ^12.0.0 as dependencies and peer dependencies. ([#3775](https://github.com/MetaMask/core/pull/3775/)).
- **BREAKING:** `TokenDetectionController` newly subscribes to the `PreferencesController:stateChange`, `AccountsController:selectedAccountChange`, `KeyringController:lock`, `KeyringController:unlock` events, and allows the `PreferencesController:getState` messenger action. ([#3775](https://github.com/MetaMask/core/pull/3775/))
- **BREAKING:** `TokenDetectionController` newly subscribes to the `PreferencesController:stateChange`, `AccountsController:selectedAccountChange`, `KeyringController:lock`, `KeyringController:unlock` events, and allows messenger actions `AccountsController:getSelectedAccount`, `NetworkController:findNetworkClientIdByChainId`, `NetworkController:getNetworkConfigurationByNetworkClientId`, `NetworkController:getProviderConfig`, `KeyringController:getState`, `PreferencesController:getState`, `TokenListController:getState`, `TokensController:getState`, `TokensController:addDetectedTokens`. ([#3775](https://github.com/MetaMask/core/pull/3775/)), ([#3923](https://github.com/MetaMask/core/pull/3923/))
- `TokensController` now exports `TokensControllerActions`, `TokensControllerGetStateAction`, `TokensControllerAddDetectedTokensAction`, `TokensControllerEvents`, `TokensControllerStateChangeEvent`. ([#3690](https://github.com/MetaMask/core/pull/3690/))

### Changed

- **BREAKING:** `TokenDetectionController` is merged with `DetectTokensController` from the `metamask-extension` repo. ([#3775](https://github.com/MetaMask/core/pull/3775/))
- **BREAKING:** `TokenDetectionController` is merged with `DetectTokensController` from the `metamask-extension` repo. ([#3775](https://github.com/MetaMask/core/pull/3775/), [#3923](https://github.com/MetaMask/core/pull/3923)), ([#3938](https://github.com/MetaMask/core/pull/3938))
- **BREAKING:** `TokenDetectionController` now resets its polling interval to the default value of 3 minutes when token detection is triggered by external controller events `KeyringController:unlock`, `TokenListController:stateChange`, `PreferencesController:stateChange`, `AccountsController:selectedAccountChange`.
- **BREAKING:** `TokenDetectionController` now refetches tokens on `NetworkController:networkDidChange` if the `networkClientId` is changed instead of `chainId`.
- **BREAKING:** `TokenDetectionController` cannot initiate polling or token detection if `KeyringController` state is locked.
- **BREAKING:** The `detectTokens` method input option `accountAddress` has been renamed to `selectedAddress`.
- **BREAKING:** The `detectTokens` method now excludes tokens that are already included in the `TokensController`'s `detectedTokens` list from the batch of incoming tokens it sends to the `TokensController` `addDetectedTokens` method.
- **BREAKING:** The constructor for `TokenDetectionController` expects a new required proprerty `trackMetaMetricsEvent`, which defines the callback that is called in the `detectTokens` method.
- The constructor option `selectedAddress` no longer defaults to `''` if omitted. Instead, the correct address is assigned using the `AccountsController:getSelectedAccount` messenger action.
- **BREAKING:** In Mainnet, even if the `PreferenceController`'s `useTokenDetection` option is set to false, automatic token detection is performed on the legacy token list (token data from the contract-metadata repo).
- **BREAKING:** The `TokensState` type is now defined as a type alias rather than an interface. ([#3690](https://github.com/MetaMask/core/pull/3690/))
- This is breaking because it could affect how this type is used with other types, such as `Json`, which does not support TypeScript interfaces.
Expand Down
8 changes: 4 additions & 4 deletions packages/assets-controllers/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 88.47,
functions: 95.89,
lines: 96.87,
statements: 96.87,
branches: 88.58,
functions: 96.98,
lines: 97.35,
statements: 97.4,
},
},

Expand Down
8 changes: 5 additions & 3 deletions packages/assets-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@ethereumjs/util": "^8.1.0",
"@ethersproject/address": "^5.7.0",
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
Expand All @@ -40,19 +41,20 @@
"@metamask/approval-controller": "^5.1.2",
"@metamask/base-controller": "^4.1.1",
"@metamask/contract-metadata": "^2.4.0",
"@metamask/controller-utils": "^8.0.2",
"@metamask/controller-utils": "^8.0.3",
"@metamask/eth-query": "^4.0.0",
"@metamask/keyring-controller": "^12.2.0",
"@metamask/metamask-eth-abis": "3.0.0",
"@metamask/network-controller": "^17.2.0",
"@metamask/polling-controller": "^5.0.0",
"@metamask/preferences-controller": "^7.0.0",
"@metamask/rpc-errors": "^6.1.0",
"@metamask/rpc-errors": "^6.2.1",
"@metamask/utils": "^8.3.0",
"@types/bn.js": "^5.1.5",
"@types/uuid": "^8.3.0",
"async-mutex": "^0.2.6",
"bn.js": "^5.2.1",
"cockatiel": "^3.1.2",
"ethereumjs-util": "^7.0.10",
"lodash": "^4.17.21",
"multiformats": "^9.5.2",
"single-call-balance-checker-abi": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
} from '@metamask/network-controller';
import type { PreferencesState } from '@metamask/preferences-controller';
import type { Hex } from '@metamask/utils';
import type { BN } from 'ethereumjs-util';
import type BN from 'bn.js';
import abiSingleCallBalancesContract from 'single-call-balance-checker-abi';

import { SupportedTokenDetectionNetworks } from './assetsUtil';
Expand Down
2 changes: 1 addition & 1 deletion packages/assets-controllers/src/NftController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
getDefaultPreferencesState,
type PreferencesState,
} from '@metamask/preferences-controller';
import { BN } from 'ethereumjs-util';
import BN from 'bn.js';
import nock from 'nock';
import * as sinon from 'sinon';
import { v4 } from 'uuid';
Expand Down
5 changes: 3 additions & 2 deletions packages/assets-controllers/src/NftController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import type {
import type { PreferencesState } from '@metamask/preferences-controller';
import { rpcErrors } from '@metamask/rpc-errors';
import type { Hex } from '@metamask/utils';
import { remove0x } from '@metamask/utils';
import { Mutex } from 'async-mutex';
import { BN, stripHexPrefix } from 'ethereumjs-util';
import BN from 'bn.js';
import { EventEmitter } from 'events';
import { v4 as random } from 'uuid';

Expand Down Expand Up @@ -568,7 +569,7 @@ export class NftController extends BaseControllerV1<NftConfig, NftState> {
return [tokenURI, ERC1155];
}

const hexTokenId = stripHexPrefix(BNToHex(new BN(tokenId)))
const hexTokenId = remove0x(BNToHex(new BN(tokenId)))
.padStart(64, '0')
.toLowerCase();
return [tokenURI.replace('{id}', hexTokenId), ERC1155];
Expand Down
28 changes: 28 additions & 0 deletions packages/assets-controllers/src/NftDetectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,34 @@ describe('NftDetectionController', () => {
},
);
});

it('should only re-detect when relevant settings change', async () => {
await withController(
{},
async ({ controller, triggerPreferencesStateChange }) => {
const detectNfts = sinon.stub(controller, 'detectNfts');

// Repeated preference changes should only trigger 1 detection
for (let i = 0; i < 5; i++) {
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
useNftDetection: true,
});
}
await advanceTime({ clock, duration: 1 });
expect(detectNfts.callCount).toBe(1);

// Irrelevant preference changes shouldn't trigger a detection
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
useNftDetection: true,
securityAlertsEnabled: true,
});
await advanceTime({ clock, duration: 1 });
expect(detectNfts.callCount).toBe(1);
},
);
});
});

type WithControllerCallback<ReturnValue> = ({
Expand Down
3 changes: 0 additions & 3 deletions packages/assets-controllers/src/NftDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,6 @@ export class NftDetectionController extends StaticIntervalPollingControllerV1<
!useNftDetection !== disabled
) {
this.configure({ selectedAddress, disabled: !useNftDetection });
}

if (useNftDetection !== undefined) {
if (useNftDetection) {
this.start();
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/assets-controllers/src/Standards/ERC20Standard.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { toUtf8 } from '@ethereumjs/util';
import { Contract } from '@ethersproject/contracts';
import type { Web3Provider } from '@ethersproject/providers';
import { decodeSingle } from '@metamask/abi-utils';
import { ERC20 } from '@metamask/controller-utils';
import { abiERC20 } from '@metamask/metamask-eth-abis';
import { assertIsStrictHexString } from '@metamask/utils';
import { toUtf8 } from 'ethereumjs-util';
import type { BN } from 'ethereumjs-util';
import type BN from 'bn.js';

import { ethersBigNumberToBN } from '../assetsUtil';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
timeoutFetch,
} from '@metamask/controller-utils';
import { abiERC1155 } from '@metamask/metamask-eth-abis';
import type { BN } from 'ethereumjs-util';
import type * as BN from 'bn.js';

import { getFormattedIpfsUrl, ethersBigNumberToBN } from '../../../assetsUtil';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ControllerMessenger } from '@metamask/base-controller';
import { toHex } from '@metamask/controller-utils';
import { BN } from 'ethereumjs-util';
import BN from 'bn.js';

import { flushPromises } from '../../../tests/helpers';
import type {
Expand Down
Loading

0 comments on commit 2126c85

Please sign in to comment.