Skip to content

Commit

Permalink
chore: use keccak256 from ethereum-cryptography
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed Dec 22, 2024
1 parent c91b4ee commit 58cc2cf
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
17 changes: 10 additions & 7 deletions app/scripts/controllers/metametrics-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'),
);
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/json-rpc/eth_call.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
};
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 58cc2cf

Please sign in to comment.