I am trying to use Bitcoinj for my app. I am using regtest mode. I have a balance of 42 BTC on my account:
bitcoin-cli -regtest getbalance mirko3 -> 42.00000000 My code follows:
public class ProvaBitcoinj { public static void main(String ... args) throws Exception { final DumpedPrivateKey dumpedPrivateKey = new DumpedPrivateKey(RegTestParams.get(), "cNDSqymhJbqmRJRpx3QPM8KBZuca2WePFkzj2uezp5TZhqHX4q4d"); final ECKey key = dumpedPrivateKey.getKey(); System.out.println("BTC address that will be added: " + key.toAddress(RegTestParams.get())); System.out.println("Private key that will be added: " + key.getPrivateKeyEncoded(RegTestParams.get())); final WalletAppKit kit = new WalletAppKit(RegTestParams.get(), new File("/tmp/bitcoinj"), "test_btc") { protected void onSetupCompleted() { System.out.println("Key chain size: " + wallet().getKeychainSize()); for (ECKey k : wallet().getKeys()) { wallet().removeKey(k); } wallet().addKey(key); } }; kit.setAutoSave(true); kit.connectToLocalHost(); System.out.println("Started!: " + kit.startAndWait()); System.out.println("Keys: " + kit.wallet().getKeychainSize()); System.out.println("BTC 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().getBalance()); // kit.wallet().addEventListener(new AbstractWalletEventListener() { // @Override // public void onCoinsReceived(Wallet wallet, Transaction tx, // BigInteger prevBalance, BigInteger newBalance) { // System.out.println("TX!!!!!!!!!!!!!"); // System.out.println(tx.getValueSentToMe(wallet)); // System.out.println(wallet.getBalance()); // } // }); // // Thread.sleep(Long.MAX_VALUE); for (Transaction tx : kit.wallet().getTransactions(true)) { System.out.println(tx.getValueSentFromMe(kit.wallet())); } System.out.println("Stopping"); System.out.println("Stopped!: " + kit.stopAndWait()); } } This programs dumps the following output:
BTC address that will be added: mnJRedAFBzRScnnKp5eS5CgR165uGi75tm Private key that will be added: cSY1oeXfTADVXg2WnMzR2uMqQWqXNNia424SF2mHRPr54Ruj9Fzs Key chain size: 1 Started!: RUNNING Keys: 1 BTC Address: mnJRedAFBzRScnnKp5eS5CgR165uGi75tm Private key: cSY1oeXfTADVXg2WnMzR2uMqQWqXNNia424SF2mHRPr54Ruj9Fzs Balance 0 Stopping Stopped!: TERMINATED I have verified that the key pair is correct:
bitcoin-cli -regtest getaccountaddress mirko3 -> mnJRedAFBzRScnnKp5eS5CgR165uGi75tm bitcoin-cli -regtest dumpprivkey mnJRedAFBzRScnnKp5eS5CgR165uGi75tm -> cSY1oeXfTADVXg2WnMzR2uMqQWqXNNia424SF2mHRPr54Ruj9Fzs I have also noticed that if I run the commented out code and send some money using bitcoin-cli, I receive correctly events and the wallet is charged with the amount I have sent. At a certain point, I could see my wallet with 2 BTC, but the other 40 ones were never found and, anyway, after some tests also the 2 BTC have disappeared.
I am totally confused, any help would be greatly appreciated.
Thank you