Replace ethereumjs-wallet with an in-package V3 keystore implementation - #792
Open
palango wants to merge 2 commits into
Open
Replace ethereumjs-wallet with an in-package V3 keystore implementation#792palango wants to merge 2 commits into
palango wants to merge 2 commits into
Conversation
ethereumjs-wallet is deprecated and pulled in the unmaintained browserify crypto stack (ethereum-cryptography@0.1.3, pbkdf2, sha.js, cipher-base, secp256k1@4, elliptic), carrying several critical advisories including pbkdf2 returning static keys for Uint8Array input. Implement V3 encrypt/decrypt directly on @noble/hashes and @noble/ciphers, both already used across sibling SDK packages. The keystore format is unchanged and geth-generated keystores still decrypt; decryption now accepts only aes-128-ctr rather than passing the file's cipher field to an arbitrary cipher constructor.
The V3 spec allows pbkdf2 as well as scrypt, but keystores in the wild almost always use scrypt, so the pbkdf2 branch shipped with only its rejection path covered. Add the Web3 Secret Storage pbkdf2 test vector, verified against ethereumjs-wallet before use.
🦋 Changeset detectedLatest commit: 1434bfd The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
Contributor
size-limit report 📦
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #792 +/- ##
==========================================
+ Coverage 64.52% 69.61% +5.08%
==========================================
Files 33 213 +180
Lines 919 11312 +10393
Branches 114 1973 +1859
==========================================
+ Hits 593 7875 +7282
- Misses 313 3349 +3036
- Partials 13 88 +75
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
@celo/keystoresdepended onethereumjs-wallet, which is deprecated and pulled in the unmaintained browserify crypto stack. That subtree carried four critical advisories, the worst of them badly matched to a keystore package:pbkdf2@3.1.2— silently disregardsUint8Arrayinput and returns static keys, and separately returns predictable zero-filled memory for non-normalized inputsha.js@2.4.11andcipher-base@1.0.4— missing type checks allowing hash rewindelliptic@6.5.4(viasecp256k1@4.0.3) — private key extraction in ECDSAAll of it entered through a single direct dependency used in exactly one file.
What changed
V3 encrypt/decrypt now lives in
packages/sdk/keystores/src/v3-keystore.ts, built on@noble/hashesand@noble/ciphers. Both are already pinned at these exact versions across ~10 sibling SDK packages, so no new versions enter the lockfile —yarn.lockshrinks by 300 lines.Confirmed gone from the dependency tree:
ethereumjs-wallet,ethereumjs-util,ethereum-cryptography@0.1.3,pbkdf2,sha.js,cipher-base,create-hash,create-hmac,browserify-aes.The public
KeystoreBaseAPI is unchanged.Compatibility
The keystore format is untouched: encryption still uses geth's defaults (scrypt n=262144, r=8, p=1, AES-128-CTR, keccak256 MAC), and geth-generated keystores still decrypt.
Beyond the unit tests, I ran a bidirectional cross-check against the real
ethereumjs-wallet@1.0.2. All 10 checks passed:address,crypto,id,version)crypto/kdfparamsfield sets identicalv3Filenamebyte-identicalOne deliberate behaviour change
Decryption now accepts only
aes-128-ctrand throws otherwise. Previously the file'scipherfield was passed straight intocreateDecipheriv, i.e. an untrusted string selected the cipher. Every real V3 keystore usesaes-128-ctr; exotic ones would now be rejected.Tests
35 pass (was 20). New coverage: geth fixture round-trips, tampered-ciphertext MAC rejection, unsupported cipher/KDF, fresh salt/IV/id per encryption, address normalisation, filename convention.
The
pbkdf2branch previously had only its rejection path covered, so it now has a known-answer test using the Web3 Secret Storage spec vector — validated againstethereumjs-walletbefore being added as a fixture, rather than generated by the code under test.Known gaps, not addressed here
pbkdf2iteration count is unbounded. A hostile keystore withc=2e9costs ~43 min of CPU (measured). Not a regression — the previous code passed attacker-controlledcinto a synchronous pbkdf2, blocking the event loop outright. scrypt is bounded: noble rejects absurdn/rin ~0ms.TypeErrorrather than a definedV3ErrorMessages. Robustness only; nothing is silently accepted and no wrong key is ever produced.changeKeystorePassphrasedropsblspublickey— pre-existing, tracked in changeKeystorePassphrase silently drops blspublickey from validator keystores #793.Note for the reviewer
packages/sdk/keystores/src/file-keystore.tshas pre-existing biome import-order drift that failsbiome checkonmastertoday. It is not in this diff — biome's--writekept sweeping it in and I backed it out to keep the diff honest. It needs its own cleanup and will fail CI lint independently of this PR.PR-Codex overview
This PR replaces the deprecated
ethereumjs-walletdependency with a new implementation using@noble/hashesand@noble/ciphers, enhancing security and maintaining compatibility with existing keystore formats.Detailed summary
ethereumjs-walletdependency.@noble/ciphersand@noble/hashesdependencies.encryptV3anddecryptV3functions for keystore handling.KeystoreBasemethods to use the new encryption and decryption functions.encryptV3anddecryptV3functionalities.