Skip to content

Commit 04d97e2

Browse files
committed
chore: use keccak256 from ethereum-cryptography
1 parent 0e78011 commit 04d97e2

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

app/scripts/controllers/metametrics-controller.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import {
88
size,
99
sum,
1010
} from 'lodash';
11-
import { bufferToHex, keccak } from 'ethereumjs-util';
11+
import { keccak256 } from 'ethereum-cryptography/keccak';
1212
import { v4 as uuidv4 } from 'uuid';
1313
import { NameControllerState, NameType } from '@metamask/name-controller';
1414
import { AccountsControllerState } from '@metamask/accounts-controller';
1515
import {
16+
add0x,
1617
getErrorMessage,
1718
Hex,
1819
isErrorWithMessage,
@@ -522,13 +523,15 @@ export default class MetaMetricsController extends BaseController<
522523
}
523524

524525
generateMetaMetricsId(): string {
525-
return bufferToHex(
526-
keccak(
527-
Buffer.from(
528-
String(Date.now()) +
529-
String(Math.round(Math.random() * Number.MAX_SAFE_INTEGER)),
526+
return add0x(
527+
Buffer.from(
528+
keccak256(
529+
Buffer.from(
530+
String(Date.now()) +
531+
String(Math.round(Math.random() * Number.MAX_SAFE_INTEGER)),
532+
),
530533
),
531-
),
534+
).toString('hex'),
532535
);
533536
}
534537

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@
384384
"eth-ens-namehash": "^2.0.8",
385385
"eth-lattice-keyring": "^0.12.4",
386386
"eth-method-registry": "^4.0.0",
387+
"ethereum-cryptography": "^2.2.1",
387388
"ethereumjs-util": "^7.0.10",
388389
"extension-port-stream": "^3.0.0",
389390
"fast-json-patch": "^3.1.1",

test/e2e/json-rpc/eth_call.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { strict as assert } from 'assert';
2-
import { keccak } from 'ethereumjs-util';
2+
import { keccak256 } from 'ethereum-cryptography/keccak';
33
import { defaultGanacheOptions, withFixtures } from '../helpers';
44
import { Driver } from '../webdriver/driver';
55
import FixtureBuilder from '../fixture-builder';
@@ -35,8 +35,8 @@ describe('eth_call', function () {
3535

3636
// eth_call
3737
await driver.openNewPage(`http://127.0.0.1:8080`);
38-
const balanceOf = `0x${keccak(
39-
Buffer.from('balanceOf(address)'),
38+
const balanceOf = `0x${Buffer.from(
39+
keccak256(Buffer.from('balanceOf(address)')),
4040
).toString('hex')}`;
4141
const walletAddress = '0x5cfe73b6021e818b776b421b1c4db2474086a7e1';
4242
const request = JSON.stringify({

ui/pages/confirmations/components/transaction-alerts/transaction-alerts.stories.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { Provider } from 'react-redux';
3-
import { keccak } from 'ethereumjs-util';
3+
import { keccak256 } from 'ethereum-cryptography/keccak';
44
import { cloneDeep } from 'lodash';
55
import { GasFeeContextProvider } from '../../../../contexts/gasFee';
66
import configureStore from '../../../../store/store';
@@ -44,7 +44,9 @@ const customTransaction = ({
4444
};
4545
// just simulate hash if not provided
4646
if (!props?.hash) {
47-
tx.hash = `0x${keccak(Buffer.from(JSON.stringify(tx))).toString('hex')}`;
47+
tx.hash = `0x${Buffer.from(
48+
keccak256(Buffer.from(JSON.stringify(tx))),
49+
).toString('hex')}`;
4850
}
4951
return tx;
5052
};

yarn.lock

+1
Original file line numberDiff line numberDiff line change
@@ -26938,6 +26938,7 @@ __metadata:
2693826938
eth-ens-namehash: "npm:^2.0.8"
2693926939
eth-lattice-keyring: "npm:^0.12.4"
2694026940
eth-method-registry: "npm:^4.0.0"
26941+
ethereum-cryptography: "npm:^2.2.1"
2694126942
ethereumjs-util: "npm:^7.0.10"
2694226943
ethers: "npm:5.7.0"
2694326944
extension-port-stream: "npm:^3.0.0"

0 commit comments

Comments
 (0)