From 58cc2cf808658e90cb14bdc27ebd27dc0b9eec69 Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Wed, 30 Oct 2024 03:27:09 +0000 Subject: [PATCH] chore: use keccak256 from ethereum-cryptography --- .../controllers/metametrics-controller.ts | 17 ++++++++++------- package.json | 1 + test/e2e/json-rpc/eth_call.spec.ts | 6 +++--- .../transaction-alerts.stories.js | 6 ++++-- yarn.lock | 1 + 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/app/scripts/controllers/metametrics-controller.ts b/app/scripts/controllers/metametrics-controller.ts index 9668a9f25b09..4febe844c364 100644 --- a/app/scripts/controllers/metametrics-controller.ts +++ b/app/scripts/controllers/metametrics-controller.ts @@ -8,11 +8,12 @@ import { size, sum, } from 'lodash'; -import { bufferToHex, keccak } from 'ethereumjs-util'; +import { keccak256 } from 'ethereum-cryptography/keccak'; import { v4 as uuidv4 } from 'uuid'; import { NameControllerState, NameType } from '@metamask/name-controller'; import { AccountsControllerState } from '@metamask/accounts-controller'; import { + add0x, getErrorMessage, Hex, isErrorWithMessage, @@ -523,13 +524,15 @@ export default class MetaMetricsController extends BaseController< } generateMetaMetricsId(): string { - return bufferToHex( - keccak( - Buffer.from( - String(Date.now()) + - String(Math.round(Math.random() * Number.MAX_SAFE_INTEGER)), + return add0x( + Buffer.from( + keccak256( + Buffer.from( + String(Date.now()) + + String(Math.round(Math.random() * Number.MAX_SAFE_INTEGER)), + ), ), - ), + ).toString('hex'), ); } diff --git a/package.json b/package.json index 8bcb1985e69a..4fe435c434ee 100644 --- a/package.json +++ b/package.json @@ -383,6 +383,7 @@ "eth-ens-namehash": "^2.0.8", "eth-lattice-keyring": "^0.12.4", "eth-method-registry": "^4.0.0", + "ethereum-cryptography": "^2.2.1", "ethereumjs-util": "^7.0.10", "extension-port-stream": "^3.0.0", "fast-json-patch": "^3.1.1", diff --git a/test/e2e/json-rpc/eth_call.spec.ts b/test/e2e/json-rpc/eth_call.spec.ts index fedc08ea645b..611eb01d5223 100644 --- a/test/e2e/json-rpc/eth_call.spec.ts +++ b/test/e2e/json-rpc/eth_call.spec.ts @@ -1,5 +1,5 @@ import { strict as assert } from 'assert'; -import { keccak } from 'ethereumjs-util'; +import { keccak256 } from 'ethereum-cryptography/keccak'; import { defaultGanacheOptions, withFixtures } from '../helpers'; import { Driver } from '../webdriver/driver'; import FixtureBuilder from '../fixture-builder'; @@ -35,8 +35,8 @@ describe('eth_call', function () { // eth_call await driver.openNewPage(`http://127.0.0.1:8080`); - const balanceOf = `0x${keccak( - Buffer.from('balanceOf(address)'), + const balanceOf = `0x${Buffer.from( + keccak256(Buffer.from('balanceOf(address)')), ).toString('hex')}`; const walletAddress = '0x5cfe73b6021e818b776b421b1c4db2474086a7e1'; const request = JSON.stringify({ diff --git a/ui/pages/confirmations/components/transaction-alerts/transaction-alerts.stories.js b/ui/pages/confirmations/components/transaction-alerts/transaction-alerts.stories.js index 99a60a94e25d..7c66511a13ec 100644 --- a/ui/pages/confirmations/components/transaction-alerts/transaction-alerts.stories.js +++ b/ui/pages/confirmations/components/transaction-alerts/transaction-alerts.stories.js @@ -1,6 +1,6 @@ import React from 'react'; import { Provider } from 'react-redux'; -import { keccak } from 'ethereumjs-util'; +import { keccak256 } from 'ethereum-cryptography/keccak'; import { cloneDeep } from 'lodash'; import { GasFeeContextProvider } from '../../../../contexts/gasFee'; import configureStore from '../../../../store/store'; @@ -44,7 +44,9 @@ const customTransaction = ({ }; // just simulate hash if not provided if (!props?.hash) { - tx.hash = `0x${keccak(Buffer.from(JSON.stringify(tx))).toString('hex')}`; + tx.hash = `0x${Buffer.from( + keccak256(Buffer.from(JSON.stringify(tx))), + ).toString('hex')}`; } return tx; }; diff --git a/yarn.lock b/yarn.lock index f45eb5cf233e..f4156a752d46 100644 --- a/yarn.lock +++ b/yarn.lock @@ -26827,6 +26827,7 @@ __metadata: eth-ens-namehash: "npm:^2.0.8" eth-lattice-keyring: "npm:^0.12.4" eth-method-registry: "npm:^4.0.0" + ethereum-cryptography: "npm:^2.2.1" ethereumjs-util: "npm:^7.0.10" ethers: "npm:5.7.0" extension-port-stream: "npm:^3.0.0"