Key exported from BitcoinJ is different from the key shown by bitcoind

1

I am quite new to Bitcoin. I am trying to use Bitcoinj with regtest mode. I have created the following application:

public class TestBitcoinj {
    private final static BigInteger ONE = new BigInteger("12345");
    private final static BigInteger TWO = new BigInteger("67890");

    public static void main(String ... args) throws Exception {
        final ECKey from = new ECKey(ONE);
        final ECKey to = new ECKey(TWO);
        System.out.format("from: %s, to: %s\n", from.toAddress(RegTestParams.get()), to.toAddress(RegTestParams.get()));

        final WalletAppKit kit = new WalletAppKit(RegTestParams.get(), new File("/tmp/bitcoinj"), "mirko_btc") {
            protected void onSetupCompleted() {
                System.out.println("Key chain size: " + wallet().getKeychainSize());
                if (wallet().getKeys().isEmpty()) {
                    wallet().addKey(from);
                }
            }
        };

        kit.setAutoSave(true);
        kit.connectToLocalHost();
        System.out.println("State: " + kit.startAndWait());

        System.out.println("Address: " + kit.wallet().getKeys().get(0).toAddress(RegTestParams.get()));
        System.out.println("Private key: " + kit.wallet().getKeys().get(0).getPrivateKeyEncoded(RegTestParams.get()));
        System.out.println("Balance: " + kit.wallet().getWatchedBalance());
        System.out.println("Done");
        System.out.println("State: " + kit.stopAndWait());

    }
}

I have started bitcoind with the following:

bitcoind -regtest

I have generated some blocks just to have some money:

bitcoin-cli -regtest setgenerate true 101

Then I have run my program just to have the private key, which is:

Chiave privata: cNQM8FUZ9tJeNKNNJPiwHZpWkREqzzk5JhioYJWZaszHa5vgsLct

Now I import that private key in bitcoind using the following:

bitcoin-cli -regtest importprivkey cNQM8FUZ9tJeNKNNJPiwHZpWkREqzzk5JhioYJWZaszHa5vgsLct mirko4

Finally, I have noticed that my program dumps the following:

Indirizzo: mrtwCcpmvRYU5wBDaUP8LiaW85nGEkbauM

whereas if I run the following:

bitcoin-cli -regtest getaccountaddress mirko4

I get the following address:

mrXizN9m9cyKgvBKqCVzTvMAWJoVgnBBev

That address is far different from the one I have dumped using Bitcoinj. However, the private key should be the same.

Probably I am missing something, but I can not figure out what.

Thank you very much for your support.

fcracker79

Posted 2014-10-05T17:36:05.707

Reputation: 209

1Try dumpprivkey mrtwCcpmvRYU5wBDaUP8LiaW85nGEkbauM using Bitcoin-cliWizard Of Ozzie 2014-10-05T18:06:29.257

Hi Aussie,thanks for your response. Yes, the provided private key is cNQM8FUZ9tJeNKNNJPiwHZpWkREqzzk5JhioYJWZaszHa5vgsLct, which is the one I would expect. In addition to that, if I import the same private key multiple times creating different accounts, I have different public keys.fcracker79 2014-10-05T18:58:46.310

Sorry I have found the solution. The dumped format is not comparable to ECKey. If I want to use the dumped format, I have to use the class DumpedPrivateKey. Thanks for the help!fcracker79 2014-10-05T19:20:17.027

Answers

1

Sorry I have found the solution. The dumped privkey is not comparable to the address. If I want to use the dumped format, I have to use the class DumpedPrivateKey.

Thanks for the help!

After Aussie Cryptocurrency's request, I am pointing out the problem in the code. What I was doing was trying to import the private key of the 'from' keypair using bitcoin-cli. However, it is important to know that such operations must be run using DumpedPrivateKey: this class can manage private keys in the format that bitcoin-cli likes, whereas in my previous tests I was trying to import the private key simply Base-58 encoding the private key. The odd thing is that if you do so, the key seems to be imported, but the private key is completely different to the expected one.

Hope this helps.

fcracker79

Posted 2014-10-05T17:36:05.707

Reputation: 209

Can you elaborate on your answer above? If you can point out the code that was the issue it'd be a great help for others (possibly me!) in the futureWizard Of Ozzie 2014-10-06T01:07:12.690