Skip to content

Commit

Permalink
deps(keyring-controller): ethereumjs-wallet@^1.0.1 -> @ethereumjs/wal…
Browse files Browse the repository at this point in the history
…let@^2.0.4

- fix: wallet privatekey import fix

- fix: catch and throw error previously handled by ethereumjs-wallet
  • Loading branch information
legobeat committed Dec 24, 2024
1 parent e825794 commit f3788d0
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 208 deletions.
2 changes: 1 addition & 1 deletion packages/keyring-controller/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 95.51,
branches: 95.48,
functions: 100,
lines: 99.07,
statements: 99.08,
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
},
"dependencies": {
"@ethereumjs/util": "^8.1.0",
"@ethereumjs/wallet": "^2.0.4",
"@keystonehq/metamask-airgapped-keyring": "^0.14.1",
"@metamask/base-controller": "^7.1.0",
"@metamask/browser-passworder": "^4.3.0",
Expand All @@ -59,7 +60,6 @@
"@metamask/message-manager": "^11.0.3",
"@metamask/utils": "^11.0.1",
"async-mutex": "^0.5.0",
"ethereumjs-wallet": "^1.0.1",
"immer": "^9.0.6"
},
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,9 @@ describe('KeyringController', () => {
'',
somePassword,
]),
).rejects.toThrow('Unexpected end of JSON input');
).rejects.toThrow(
'Key derivation failed - possibly wrong passphrase',
);
});
});

Expand Down
25 changes: 17 additions & 8 deletions packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TxData, TypedTransaction } from '@ethereumjs/tx';
import { isValidPrivate, toBuffer, getBinarySize } from '@ethereumjs/util';
import { Wallet, thirdparty as importers } from '@ethereumjs/wallet';
import type {
MetaMaskKeyring as QRKeyring,
IKeyringState as IQRKeyringState,
Expand Down Expand Up @@ -41,7 +42,6 @@ import {
} from '@metamask/utils';
import { Mutex } from 'async-mutex';
import type { MutexInterface } from 'async-mutex';
import Wallet, { thirdparty as importers } from 'ethereumjs-wallet';
import type { Patch } from 'immer';

import { KeyringControllerError } from './constants';
Expand Down Expand Up @@ -1013,16 +1013,25 @@ export class KeyringController extends BaseController<

privateKey = remove0x(prefixed);
break;
case 'json':
let wallet;
const [input, password] = args;
case 'json': {
try {
wallet = importers.fromEtherWallet(input, password);
const getWallet = async (): Promise<Wallet> => {
const [input, password] = args;
try {
return await importers.fromEtherWallet(input, password);
} catch (e) {
return await Wallet.fromV3(input, password, true);
}
};
const wallet = await getWallet();
privateKey = bytesToHex(wallet.getPrivateKey());
break;
} catch (e) {
wallet = wallet || (await Wallet.fromV3(input, password, true));
throw new Error(
'Key derivation failed - possibly wrong passphrase',
);
}
privateKey = bytesToHex(wallet.getPrivateKey());
break;
}
default:
throw new Error(`Unexpected import strategy: '${strategy}'`);
}
Expand Down
Loading

0 comments on commit f3788d0

Please sign in to comment.