WalletAppKit creates an address every time Bitcoinj gets initialized

0

I'm implementing an app using BitcoinJ, but every time I run the code, a new address is created. Is there any way to work with the very first address that was created and stop creating new addresses?

walletAppKit = new WalletAppKit(parameters, walletDir, "my_wallet") {
        @Override
        protected void onSetupCompleted() {

        if (wallet().getImportedKeys().size() < 1) 
           wallet().importKey(new ECKey());
           walletAppKit.peerGroup().setBloomFilterFalsePositiveRate(0.00001);
           setupWalletListeners(wallet());

           for (Address address: wallet().getIssuedReceiveAddresses()){
                Log.d("myLogs", address.toBase58());
           }
        }
    };
walletAppKit.setBlockingStartup(false);
walletAppKit.setUserAgent("RocketWallet", "1.0");
walletAppKit.startAsync();

sdelvalle57

Posted 2017-07-07T09:06:02.890

Reputation: 101

Answers

1

According to the documentation:

getImportedKeys() returns a list of the non-deterministic keys that have been imported into the wallet, or the empty list if none.

Try to change:

if (wallet().getImportedKeys().size() < 1)
    wallet().importKey(new ECKey());

to:

if (wallet().getIssuedReceiveKeys().size() < 1)
    wallet().freshReceiveKey();

To get the first address in your wallet, you can use:

wallet.getIssuedReceiveKeys().get(0).toAddress(TestNet3Params.get()).toBase58().toString();

Purab

Posted 2017-07-07T09:06:02.890

Reputation: 11