10

I'm trying to use ethers-ens which requires an ethereum account in JSON format. How can I export or generate an account.json from my metamask account? Is there another way to use ethers-ens without an account.json?

This is a sanitized example format based on account.json generated by ethers-ens:

{
    "address": "",
    "id": "",
    "version": ,
    "Crypto": {
        "cipher": "",
        "cipherparams": {
            "iv": ""
        },
        "ciphertext": "",
        "kdf": "",
        "kdfparams": {
            "salt": "",
            "n": ,
            "dklen":,
            "p": ,
            "r": 
        },
        "mac": ""
    }

}

4 Answers 4

8

Install ethereumjs-wallet package

npm install ethereumjs-wallet

Create file export-key-as-json.js with the following content;

const fs = require("fs")
const wallet = require("ethereumjs-wallet").default

const pk = new Buffer.from(process.argv[2], 'hex') // replace by correct private key
const account = wallet.fromPrivateKey(pk)
const password = process.argv[3] // will be required to unlock/sign after importing to a wallet like MyEtherWallet

account.toV3(password)
    .then(value => {
        const address = account.getAddress().toString('hex')
        const file = `UTC--${new Date().toISOString().replace(/[:]/g, '-')}--${address}.json`
        fs.writeFileSync(file, JSON.stringify(value))
    });

Run node export-key-as-json.js <your-private-key> <some-random-password>

After you run this, check the current working directory to find out that some new JSON file was generated.

3

While MetaMask plans to add the ability to export this file format (check that link to see if it's been done yet), it currently only allows exporting the hex-encoded private key for individual accounts. That's a series of 64 characters (0-9 and A-F), which you can copy to your clipboard.

This private key string is not encrypted, and so anywhere it can be read it can be used to control its account.

The JSON file format includes the encrypted key, with some information about how it was encrypted. This makes it good for saving to your hard drive.

Once you have this private key copied, you can import it into Geth or Parity, and then export the JSON file from one of those.

1

It must be a better way but here's how I did it.

Firstly it's important to understand that the mnemonic phrase could be used to generate an infinite number of private keys and the JSON Keystore format stores just one private key. That means that you'll need to do the following procedure for each private key you want to import on the Ethereum Wallet.

First you need to find the private key for a specific address:

  1. Go to MyEtherWallet.com
  2. Select the appropriated network on the upper right corner
  3. Click on 'View Wallet Info'
  4. Choose 'Mnemonic Phrase'
  5. Paste/type your mnemonic
  6. Type your password and unlock the account
  7. Select the correct derivation path. If the case of MetaMask that should be first one: m/44'/60'/0'/0
  8. Select the desired address
  9. You should see your private key under under field 'Private Key (unencrypted)'

Now that you have the private key you need to covert it to JSON Keystore file. You can do this using geth but in my case I've used a javascript library called ethereumjs-wallet as the following:

const fs = require("fs")
const wallet = require("ethereumjs-wallet")

const pk = new Buffer.from('c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3', 'hex') // replace by correct private key
const account = wallet.fromPrivateKey(pk)
const password = 'something' // will be required to unlock/sign after importing to a wallet like MyEtherWallet
const content = JSON.stringify(account.toV3(password))

// writes to a file
const address = account.getAddress().toString('hex')
const file = `UTC--${new Date().toISOString().replace(/[:]/g, '-')}--${address}`
fs.writeFileSync(file, content)

Hope somebody has a better alternative out there.

1
  • Couple of small fixes to your code: 1. const wallet = require("ethereumjs-wallet").default (or else use import) 2. toV3 returns a Promise so either await it, or enclose the rest of the code in a then clause. Commented Sep 20, 2020 at 17:51
0

Install the "MEW CX" Google Chrome extension, import the wallet using the MNENOMIC, and export "Keystore File (UTC/JSON)".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.