ethers.js icon indicating copy to clipboard operation
ethers.js copied to clipboard

wallet.encry

Open encrylife opened this issue 3 years ago • 6 comments

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"

encrylife avatar Jun 13 '22 14:06 encrylife

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

encrylife avatar Jun 13 '22 14:06 encrylife

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.

hbarcelos avatar Jun 21 '22 23:06 hbarcelos

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.

hbarcelos avatar Jun 22 '22 12:06 hbarcelos

Thanks! Yeah, don’t worry about a PR. I’ll test and make the change. Thanks for the research though. :)

ricmoo avatar Jun 22 '22 13:06 ricmoo

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.

zemse avatar Jun 26 '22 11:06 zemse

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.

ricmoo avatar Jul 14 '22 01:07 ricmoo

This has been updated in v5.7.0.

Try it out and let me know if you have any problems.

Thanks! :)

ricmoo avatar Aug 19 '22 21:08 ricmoo