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();
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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.