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.

3
  • 1
    Try dumpprivkey mrtwCcpmvRYU5wBDaUP8LiaW85nGEkbauM using Bitcoin-cli Commented Oct 5, 2014 at 18:06
  • 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
    Commented Oct 5, 2014 at 18:58
  • 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
    Commented Oct 5, 2014 at 19:20

1 Answer 1

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.

1
  • 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 future Commented Oct 6, 2014 at 1:07

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.