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?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityPaste 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:
nothing_special_delete_me.txt
at C:\
geth account import C:\nothing_special_delete_me.txt
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:
cd c:\Ethereum-Wallet-win64-0-7-3\resources\node\geth
(insert your correct path there)Fatal: keyfile must be given as argument
while doing the same. PS: I am running on linux macine.
Oct 10, 2016 at 9:02
Use the following command in the geth console
web3.personal.importRawKey("<Private Key>","<New Password>")
personal
methods. See ethereum.stackexchange.com/questions/1413/… for more info
Nov 14, 2016 at 16:01
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:
Open TextEdit
Paste key into TextEdit without any extra characters or quotations
Save the file as nothing_special_delete_me.txt
to your Desktop
Open Terminal, run command:
geth account import ~/Desktop/nothing_special_delete_me.txt
After successful import, delete the file from your desktop.
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
.
Fatal: keyfile must be given as argument
> I am still unable to resolve it.
Oct 21, 2016 at 5:53
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.
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.
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)
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.