64

This is such a basic question, but I can not figure out how to import a plain, unencrypted private key into geth or mist?

I tried geth account import 7[......]bla but it seems this command is always looking for an unencrypted keyfile.

Any ideas?

7 Answers 7

56

Paste the key into a text file, save it to disk and use the path to that file with geth account import. Here are some example Windows instructions that might help:

  1. Open Notepad
  2. Paste key into notepad without any extra characters or quotations
  3. Save the file as nothing_special_delete_me.txt at C:\
  4. Run the command, geth account import C:\nothing_special_delete_me.txt
  5. After successful import, delete the file at C:\nothing_special_delete_me.txt

If you do not have geth installed already, do the following steps after step 3, and the continue to step 4:

  • Open command prompt (cmd.exe).
  • Inside the Command Prompt, type the following command to go to the folder containing your Geth.exe file: cd c:\Ethereum-Wallet-win64-0-7-3\resources\node\geth (insert your correct path there)
1
  • I am getting Fatal: keyfile must be given as argument while doing the same. PS: I am running on linux macine. Oct 10, 2016 at 9:02
26

Use the following command in the geth console

web3.personal.importRawKey("<Private Key>","<New Password>")
1
13

Mist has no way to import via GUI, so you need to import using command line (geth). It will show up in your Mist immediately.

For Mac:

  1. Open TextEdit

  2. Paste key into TextEdit without any extra characters or quotations

  3. Save the file as nothing_special_delete_me.txt to your Desktop

  4. Open Terminal, run command:

    geth account import ~/Desktop/nothing_special_delete_me.txt
    
  5. After successful import, delete the file from your desktop.

4
  • "Paste the key into a text file" ... how can I see my private key before copy/paste it somewhere? In the Mist there is no option to take such an information?
    – user1770
    Apr 28, 2016 at 7:15
  • @metafl - if you have the account in mist then you don't need to import it into geth. It's already there . this explains where your mist keys are, but they are not raw private keys, they are encrypted keystore files. ethereum.stackexchange.com/questions/946/…
    – tayvano
    Apr 28, 2016 at 9:07
  • bash users: a one-step answer is here.
    – Tom Hale
    Oct 24, 2017 at 2:59
  • @TomHale The reason that it is recommended to use an external file is that doing it this way would store your private key in your .bash_history, which is unexpected for most and potentially bad
    – tayvano
    Nov 12, 2017 at 0:24
8

For Linux:

when you download the mist wallet, you'll get a folder. Inside the folder is where the mist wallet is. After you run the mist wallet, you need to get on cli.

Go to /whereveryousavedtheetherumfolder/node/resource/geth/ then run ./geth account import privekey-file.txt.

1
  • This doesn't work in my case, I get Fatal: keyfile must be given as argument> I am still unable to resolve it. Oct 21, 2016 at 5:53
2

Noting the popularity of this question I went ahead and built a tool such that if you have your raw private key, you can easily convert it into its public key counterpart and the derived Ethereum address.

You can then export the key in the appropriate format as defined here such that it can be utilised with Geth or Parity.

Input your private key enter image description here

Export your keyfile enter image description here

That said, I think it is worth noting that you should not be working with your private key directly anyway. Your private key controls access to your account.

2
  • 3
    NOTE: When using tools like this one, sending your private key to any 3rd party gives them full access to the contents of your wallet.
    – BadPirate
    Dec 31, 2017 at 16:45
  • That is not strictly true. If we saved your private key then we would have access to your account. We do NOT save your private key. The point still stands though - you should always be cautious and careful with your private key. Dec 31, 2017 at 17:26
2

Only use this method for throw-away, testing accounts since your key will be stored in your shell's history (thanks to @tayvano).

Using bash, where abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabca is your private key:

geth account import <(echo abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabca)
1
  • 3
    The reason that it is recommended to use an external file is that doing it this way would store your private key in your .bash_history, which is unexpected for most and potentially bad
    – tayvano
    Nov 12, 2017 at 0:24
0

With Geth, you can import pre-defined addresses(key-pairs) using --preload option of geth console command.

For non production environment, it could be convenient to add/import pre-defined addresses, to allocate enough initial balances, and to automatically unlock those accounts. You can use alloc property of genesis object, --preload option with geth console API for this purpose.

For example, if you have the following pre-defined addresses generated before (You can use online generators or libraries like eth-account in Python.

private-key, address
ba75c5fd16ae1151dc9f961e94e219994c6335a5b4148c624142243fb76306d6, 0xd3ae0500e21008c89ca4746be7522340c67f5730
097dd6aedb87b3b5e541cfb9ef8d4beb7a66084dd80d99c2e51aeabeae320980, 0x76aaacd6169017644ba7530b7d956e1fcc0178ad
abae82647f5881a398f7eede8910803d65470a7cbaee9ddda90dcdcdc8dcdacf, 0xeb875bb0eec43d779c4d539ca34e89fb0868d867
cc1af47cbc9de0c9a1e1049c1a62ddb9e08440d16093803d74e93f1cea3458ee, 0x0ccca39f9c169996c8380bf7c529dc3b9ff6198a
3e48c4e748b8f5baf6f870c5c4d2a0147390c94e778a9ca67de945bffeb2f72a, 0xd738b71007cffbec9b0a7e99088b132858f5e270

Below is the script to import above accounts(keys/addresses), to unlock them and to starting a miner using the first account.


var passphrase = 'none';
var accts = personal.listAccounts;
if (accts.length == 0){

  var keys = [
    "ba75c5fd16ae1151dc9f961e94e219994c6335a5b4148c624142243fb76306d6",
    "097dd6aedb87b3b5e541cfb9ef8d4beb7a66084dd80d99c2e51aeabeae320980",
    "abae82647f5881a398f7eede8910803d65470a7cbaee9ddda90dcdcdc8dcdacf",
    "cc1af47cbc9de0c9a1e1049c1a62ddb9e08440d16093803d74e93f1cea3458ee",
    "3e48c4e748b8f5baf6f870c5c4d2a0147390c94e778a9ca67de945bffeb2f72a"
  ]

  for(var i = 0; i < keys.length; i++){ 
    personal.importRawKey(keys[i], passphrase);
  }
  
  console.log('Successfully imported ', keys.length, ' keys into the keystore');
  accts = personal.listAccounts;
}

for(var i = 0; i < accts.length; i++){
  personal.unlockAccount(accts[i], passphrase);
}
console.log('Successfully unlocked ', accts.length, ' accounts');

miner.setEtherbase(accts[0])
miner.start(1)
console.log('Miner started for ', accts[0]);

In the above script, personal.listAccounts, personal.importRawKey(), personal.unlockAccount(), miner.setEtherbase(), and miner.start are geth's console APIs.

personal.importRawKey() API imports specified key and address persistently and so is enough to be called once. But, personal.unlockAccount() is not persistent but is valid until the current geth instance. So, unlock is necessary whenever a geth is started.

To execute above script(geth-setup.js) every time to run geth, --preload option could be used.

$ geth --datadir ./data --nodiscover --verbosity 2 --allow-insecure-unlock \
     --preload ./scripts/geth-setup.js \
     --http --http.addr localhost --http.port 8545 console >> geth.log 2>&1

Don't miss --allow-insecure-unlock option, to avoid permission error from personal.unlockAccount().

Accounts can be imported and unlocked like above, but they have no balance yet. To allocate enough ether to those accounts, we can use alloc property of genesis configuration (genesis.json). With current example, the genesis.json would be like the following.

{
  "config": {
    "chainId": 2021,
    "homesteadBlock": 0,
    "eip150Block": 0,
    "eip155Block": 0,
    "eip158Block": 0,
    "byzantiumBlock": 0,
    "constantinopleBlock": 0,
    "petersburgBlock": 0,
    "istanbulBlock": 0,    
    "muirglacierblock": 0,
    "ethash": {
      "fixeddifficulty": 100
    }
  },
  "nonce": "0x0",
  "difficulty": "0x60",
  "gasLimit": "0x1fffffffffffff",
  "coinbase": "0x0000000000000000000000000000000000000000",
  "alloc": {
    "d3ae0500e21008c89ca4746be7522340c67f5730": { "balance": "10000000000000000000000" },
    "76aaacd6169017644ba7530b7d956e1fcc0178ad": { "balance": "10000000000000000000000" },
    "eb875bb0eec43d779c4d539ca34e89fb0868d867": { "balance": "10000000000000000000000" },
    "0ccca39f9c169996c8380bf7c529dc3b9ff6198a": { "balance": "10000000000000000000000" },
    "d738b71007cffbec9b0a7e99088b132858f5e270": { "balance": "10000000000000000000000" }
  }
}

This genesis block is used before the import of accounts. But the alloc configuration (which is for the accounts not yet imported or created when the genesis block is created) would take effect later when the accounts are imported.

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