Skip to content

Commit 291f006

Browse files
committed
chore(keyring-controller): replace ethereumjs-util with @ethereumjs/util and @metamask/utils
- test(keyring-controller): Update expected error message when importing invalid key
1 parent f8ea210 commit 291f006

File tree

4 files changed

+13
-23
lines changed

4 files changed

+13
-23
lines changed

packages/keyring-controller/package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"test:watch": "jest --watch"
3232
},
3333
"dependencies": {
34+
"@ethereumjs/util": "^8.1.0",
3435
"@keystonehq/metamask-airgapped-keyring": "^0.13.1",
3536
"@metamask/base-controller": "^4.1.1",
3637
"@metamask/browser-passworder": "^4.3.0",
@@ -41,7 +42,6 @@
4142
"@metamask/message-manager": "^7.3.8",
4243
"@metamask/utils": "^8.3.0",
4344
"async-mutex": "^0.2.6",
44-
"ethereumjs-util": "^7.0.10",
4545
"ethereumjs-wallet": "^1.0.1",
4646
"immer": "^9.0.6"
4747
},
@@ -71,9 +71,6 @@
7171
"registry": "https://registry.npmjs.org/"
7272
},
7373
"lavamoat": {
74-
"allowScripts": {
75-
"ethereumjs-util>ethereum-cryptography>keccak": false,
76-
"ethereumjs-util>ethereum-cryptography>secp256k1": false
77-
}
74+
"allowScripts": {}
7875
}
7976
}

packages/keyring-controller/src/KeyringController.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import type { EthKeyring } from '@metamask/keyring-api';
1515
import { wordlist } from '@metamask/scure-bip39/dist/wordlists/english';
1616
import type { KeyringClass } from '@metamask/utils';
1717
import {
18+
bytesToHex,
1819
isValidHexAddress,
1920
type Hex,
2021
type Keyring,
2122
type Json,
2223
} from '@metamask/utils';
23-
import { bufferToHex } from 'ethereumjs-util';
2424
import * as sinon from 'sinon';
2525
import * as uuid from 'uuid';
2626

@@ -985,9 +985,7 @@ describe('KeyringController', () => {
985985
AccountImportStrategy.privateKey,
986986
['123'],
987987
),
988-
).rejects.toThrow(
989-
'Expected private key to be an Uint8Array with length 32',
990-
);
988+
).rejects.toThrow('Cannot import invalid private key.');
991989

992990
await expect(
993991
controller.importAccountWithStrategy(
@@ -1273,7 +1271,7 @@ describe('KeyringController', () => {
12731271
describe('when the keyring for the given address supports signPersonalMessage', () => {
12741272
it('should sign personal message', async () => {
12751273
await withController(async ({ controller, initialState }) => {
1276-
const data = bufferToHex(Buffer.from('Hello from test', 'utf8'));
1274+
const data = bytesToHex(Buffer.from('Hello from test', 'utf8'));
12771275
const account = initialState.keyrings[0].accounts[0];
12781276
const signature = await controller.signPersonalMessage({
12791277
data,
@@ -2344,7 +2342,7 @@ describe('KeyringController', () => {
23442342
),
23452343
);
23462344

2347-
const data = bufferToHex(
2345+
const data = bytesToHex(
23482346
Buffer.from('Example `personal_sign` message', 'utf8'),
23492347
);
23502348
const qrKeyring = signProcessKeyringController.state.keyrings.find(

packages/keyring-controller/src/KeyringController.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { TxData, TypedTransaction } from '@ethereumjs/tx';
2+
import { isValidPrivate, toBuffer, getBinarySize } from '@ethereumjs/util';
23
import type {
34
MetaMaskKeyring as QRKeyring,
45
IKeyringState as IQRKeyringState,
@@ -27,22 +28,16 @@ import type {
2728
KeyringClass,
2829
} from '@metamask/utils';
2930
import {
31+
add0x,
3032
assertIsStrictHexString,
33+
bytesToHex,
3134
hasProperty,
3235
isObject,
3336
isValidHexAddress,
3437
isValidJson,
3538
remove0x,
3639
} from '@metamask/utils';
3740
import { Mutex } from 'async-mutex';
38-
import {
39-
addHexPrefix,
40-
bufferToHex,
41-
isValidPrivate,
42-
toBuffer,
43-
stripHexPrefix,
44-
getBinarySize,
45-
} from 'ethereumjs-util';
4641
import Wallet, { thirdparty as importers } from 'ethereumjs-wallet';
4742
import type { Patch } from 'immer';
4843

@@ -1059,7 +1054,7 @@ export class KeyringController extends BaseController<
10591054
if (!importedKey) {
10601055
throw new Error('Cannot import an empty key.');
10611056
}
1062-
const prefixed = addHexPrefix(importedKey);
1057+
const prefixed = add0x(importedKey);
10631058

10641059
let bufferedPrivateKey;
10651060
try {
@@ -1076,7 +1071,7 @@ export class KeyringController extends BaseController<
10761071
throw new Error('Cannot import invalid private key.');
10771072
}
10781073

1079-
privateKey = stripHexPrefix(prefixed);
1074+
privateKey = remove0x(prefixed);
10801075
break;
10811076
case 'json':
10821077
let wallet;
@@ -1086,7 +1081,7 @@ export class KeyringController extends BaseController<
10861081
} catch (e) {
10871082
wallet = wallet || (await Wallet.fromV3(input, password, true));
10881083
}
1089-
privateKey = bufferToHex(wallet.getPrivateKey());
1084+
privateKey = bytesToHex(wallet.getPrivateKey());
10901085
break;
10911086
default:
10921087
throw new Error(`Unexpected import strategy: '${strategy}'`);

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,7 @@ __metadata:
22362236
dependencies:
22372237
"@ethereumjs/common": ^3.2.0
22382238
"@ethereumjs/tx": ^4.2.0
2239+
"@ethereumjs/util": ^8.1.0
22392240
"@keystonehq/bc-ur-registry-eth": ^0.9.0
22402241
"@keystonehq/metamask-airgapped-keyring": ^0.13.1
22412242
"@lavamoat/allow-scripts": ^2.3.1
@@ -2252,7 +2253,6 @@ __metadata:
22522253
"@types/jest": ^27.4.1
22532254
async-mutex: ^0.2.6
22542255
deepmerge: ^4.2.2
2255-
ethereumjs-util: ^7.0.10
22562256
ethereumjs-wallet: ^1.0.1
22572257
immer: ^9.0.6
22582258
jest: ^27.5.1

0 commit comments

Comments
 (0)