Skip to content

Commit 479897b

Browse files
committed
chore: use keccak256 from ethereum-cryptography
1 parent c91b4ee commit 479897b

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,
@@ -523,13 +524,15 @@ export default class MetaMetricsController extends BaseController<
523524
}
524525

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

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@
383383
"eth-ens-namehash": "^2.0.8",
384384
"eth-lattice-keyring": "^0.12.4",
385385
"eth-method-registry": "^4.0.0",
386+
"ethereum-cryptography": "^2.2.1",
386387
"ethereumjs-util": "^7.0.10",
387388
"extension-port-stream": "^3.0.0",
388389
"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
@@ -26827,6 +26827,7 @@ __metadata:
2682726827
eth-ens-namehash: "npm:^2.0.8"
2682826828
eth-lattice-keyring: "npm:^0.12.4"
2682926829
eth-method-registry: "npm:^4.0.0"
26830+
ethereum-cryptography: "npm:^2.2.1"
2683026831
ethereumjs-util: "npm:^7.0.10"
2683126832
ethers: "npm:5.7.0"
2683226833
extension-port-stream: "npm:^3.0.0"

0 commit comments

Comments
 (0)