ethers.js: new Wallet(privateKey) throws "invalid hexlify value" when privateKey is not 0x prefixed
While upgrading to Ethers V5, we ran into
Error: invalid hexlify value (argument="value", value="[not copied here]", code=INVALID_ARGUMENT, version=bytes/5.0.6)
I20201120-11:39:47.012(-5)? at Logger.makeError (/Users/matthewyang/Desktop/blockchain/tokenfunder/tokenfunderapp/node_modules/@ethersproject/logger/src.ts/index.ts:205:28)
I20201120-11:39:47.013(-5)? at Logger.throwError (/Users/matthewyang/Desktop/blockchain/tokenfunder/tokenfunderapp/node_modules/@ethersproject/logger/src.ts/index.ts:217:20)
I20201120-11:39:47.013(-5)? at Logger.throwArgumentError (/Users/matthewyang/Desktop/blockchain/tokenfunder/tokenfunderapp/node_modules/@ethersproject/logger/src.ts/index.ts:221:21)
I20201120-11:39:47.013(-5)? at Object.hexlify (/Users/matthewyang/Desktop/blockchain/tokenfunder/tokenfunderapp/node_modules/@ethersproject/bytes/src.ts/index.ts:242:19)
I20201120-11:39:47.013(-5)? at new SigningKey (/Users/matthewyang/Desktop/blockchain/tokenfunder/tokenfunderapp/node_modules/@ethersproject/signing-key/src.ts/index.ts:35:44)
I20201120-11:39:47.032(-5)? at new Wallet (/Users/matthewyang/Desktop/blockchain/tokenfunder/tokenfunderapp/node_modules/@ethersproject/wallet/src.ts/index.ts:81:36)
This issue was brought up in https://www.gitmemory.com/issue/ethers-io/ethers.js/994/674504533
I assume this is not an intended feature, as all other tools accept non 0x prefixed private keys, and prefixing 0x to the private key causes issues with bunch of other libraries, so it’s a change specific for Ethers V5
Regardless of it’s intended or not, this should have been included in the breaking change log https://docs.ethers.io/v5/migration/ethers-v4/#migration-v4–contracts
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 30 (13 by maintainers)
Commits related to this issue
- Allow private keys to Wallet to omit the 0x prefix (#1166). — committed to ethers-io/ethers.js by ricmoo 4 years ago
No worries! We love the Ethers library and really appreciate your work, glad could be helpful.
The private keys I posted were from the latest Ganache
Quick check on Metamask also exports keys without ‘0x’
example the PrivateKeyWalletSubprovider module from the 0x project throws if private key starts with ‘0x’
Heya!
Ethers 100% requires all hex strings to be
0x
-prefixed and this is intentional.This was also required in v4 (I just installed v4 to verify). So, it is not a breaking change since v4.
In fact, if I do the same with ethers@2.0.0 The same thing happens. I believe the requirement was present since 0.0.1, but my memory gets a little foggy that far back, cut I seem to recall people at the time complaining, but I believe in explicitly declaring intentions, and since strings are used for myriad things in JavaScript, it is the least worst way to indicate the string’s intention as an octet-string.