How to import a private key to an encrypted wallet (on standard client)

7

1

I have a standard client (QT client on OSX) with an encrypted wallet. I've tried to import keys with pywallet, but while it claimed to be importing them, when I ran again the bitcoin client, it said wallet.dat was corrupted (yes the client wasn't running when I did the import).

I guess pywallet doesn't know about encryption and didn't care enough to check everything was ok before importing them.

So, how do I do this import?


Trying to run the command line client gets me the following error a few seconds after starting it (with -daemon option of course):

EXCEPTION: NSt8ios_base7failureE       
CDataStream::read() : end of data       
bitcoin in AppInit()       

Running the QT client with the -server option crashes too after a while (apparently it works correctly without that).

o0'.

Posted 2012-04-30T15:33:54.387

Reputation: 5 180

I managed to import them to MultiBit, however it isn't working anyway: http://bitcoin.stackexchange.com/questions/3549/why-does-multibit-claim-my-balance-is-zero-btc

o0'. 2012-05-02T19:24:46.633

Answers

5

Since 0.6.0, key import/export are available in the client itself, though only through RPC. The commands are called dumpprivkey and importprivkey. For example (on testnet):

$ ./bitcoind getnewaddress

n2JjZgLeCUgfubswxDm9zAaBGSLLHSLdNv

$ ./bitcoind dumpprivkey n2JjZgLeCUgfubswxDm9zAaBGSLLHSLdNv

cR4RtpaeAZY8MwrNvTQGX9PZ9pdCgFoLxuo4Pkwqx61broC4Gb4y

And on another wallet:

$ ./bitcoind importprivkey R4RtpaeAZY8MwrNvTQGX9PZ9pdCgFoLxuo4Pkwqx61broC4Gb4y

$ ./bitcoind validateaddress n2JjZgLeCUgfubswxDm9zAaBGSLLHSLdNv

{ "isvalid" : true, "address" : "n2JjZgLeCUgfubswxDm9zAaBGSLLHSLdNv", "ismine" : true, "pubkey" : "029e3a934cfc9f89d7a1c15f99b98479cabc81964f4972e7b8fa3989a788261695", "iscompressed" : true, "account" : "" }

Pieter Wuille

Posted 2012-04-30T15:33:54.387

Reputation: 54 032

Oh, apparently bitcoin-qt doesn't provide a server for the command line client to connect to; also looks like bitcoind isn't starting anymore, crashing with an exception a few seconds after it started... (does it support encrypted wallets? in the options I noticed nothing about that...)o0'. 2012-04-30T16:35:57.070

You need to run "bitcoin-qt -server" to get it to provide a server. And you'll need to have 'rpcuser=x' and 'rpcpassword=y' in your bitcoin.conf file, but I think bitcoin-qt will tell you that if you don't have it. bitcoind supports encrypted wallets, but make sure you're using the same version as the version of bitcoin-qt you have.Chris Moore 2012-04-30T16:37:17.133

1@ChrisMoore mmmh, doing so I only get errors -4"Error adding key to wallet" if I try to add priv_sipa or -5"Invalid private key" if I try to add privo0'. 2012-04-30T16:45:08.463

2Oh, you need to unencrypt your wallet temporarily so it can add the new key. I do the following, so my passphrase doesn't end up in my shell history. It unlocks the wallet for 60 seconds: read x; ./bitcoind walletpassphrase "$x" 60; unset xChris Moore 2012-04-30T17:29:20.983

I can't do that: if I run the server from command line, it crashes after a while, and the same happens if I run the QT client with the -server option.o0'. 2012-05-02T18:25:03.640

Then that's a bug. Can you report it on github? Nonetheless, this answer is how importing of keys is intended to be done.Pieter Wuille 2012-05-05T12:10:36.897