ethers.js
ethers.js copied to clipboard
wallet.encry
Ethers Version
5.6.8
Search Terms
wallet.encrypt
Describe the Problem
when I create a keystore,I can't import the json file with metamask, but I can load ethers.Wallet.fromEncryptedJson
Code Snippet
async function createEncryWallet(privateKey, password, provider) {
var data = ''
var wallet = GetAccount(privateKey, provider)
function callback(progress) {
// console.log("Encrypting: " + parseInt(progress * 100) + "% complete");
}
let encryptPromise = wallet.encrypt(password, callback);
encryptPromise.then(function(json) {
return json
})
return await encryptPromise
}
Contract ABI
No response
Errors
key derivation failed - possibly wrong passphrase
Environment
node.js (v12 or newer)
Environment (Other)
"secp256k1": "^4.0.3" "keccak": "^3.0.2"
var loadFromFile = function (pwd) { var filePath = "./wallet/0x818411ea51940CedfDE263b6a725a55E5f0F3d5d.json" var content = fs.readFileSync(filePath).toString() ethers.Wallet.fromEncryptedJson(content, pwd).then(function(wallet) { console.log("Address: " + wallet.address); console.log("private key" + wallet.privateKey) // "Address: 0x88a5C2d9919e46F883EB62F7b8Dd9d0CC45bc290" }).catch( (err) => { console.log(err) }) } loadFromFile(123456)
it works well,but can't import to metamask
I have a theory about the root cause.
The generated encrypted JSON has a "Crypto" property (with a capital C), however all tools I've used out there expects it to be all lowercase.
Make sure to change "Crypto" to "crypto" and it will probably work.
Found the culprit: https://github.com/ethers-io/ethers.js/blob/608864fc3f00390e1260048a157af00378a98e41/packages/json-wallets/src.ts/keystore.ts#L332
@ricmoo I can issue a PR to fix this, but it's literally a 1-char diff.
To corroborate the theory, if you go to the Web3 Secret Storage Definition section of the Ethereum wiki, you will notice that all "crypto" properties start with a lower c.
Thanks! Yeah, don’t worry about a PR. I’ll test and make the change. Thanks for the research though. :)
Just to mention this was there since ethers v4. I remember from my first web3 job for a startup, when I looked into this issue that some investors weren't able to load their keystore on the application. Not sure if there is a consensus/standard on using Crypto or crypto, but afair I've seen parity keystores using lowercase.
Awesome! Thanks for the info by the way. This definitely didn't used to be the case; all the test vectors generated by Geth have a "C" but those generated by Parity have a lower-case "c").
It's also why the ethers parser allows any case to be used, because it was so chaotic. :)
It will be fixed in v5.7.0.