I'm trying to use bitcoinj to transfer the balance of the address derived from a random private key if that balance is non-zero.

I generate the private key and derive the address fine, then I use blockchain.info's API to check the address balance fine, but I'm not sure what I'm doing wrong with bitcoinj.

This line:

wallet.sendCoins(peerGroup, destination, wallet.getBalance());

I don't understand where it's getting the balance from. I haven't downloaded the blockchain and I've just created a new wallet from a random private key. Does this mean bitcoinj will always see this address as empty, even when I've confirmed that it is not empty?

I want to pass the value returned from the service into sendCoins (replacing "wallet.getBalance()" with the number of satoshis in the address), but that field requires a Coin and I can't figure out how to make a Coin with a specific value that I set.

How can I get this working the way that I want to? And is there any way to test it?

public class MainActivity extends AppCompatActivity {
    private final NetworkParameters params = MainNetParams.get();

    @Override protected void onCreate(Bundle savedInstanceState) {

        BigInteger max = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16);
        Random rnd = new Random();
        for (int i = 0; i < 1; i++) {
            try {
                BigInteger privkey = new BigInteger(new BigInteger(256, rnd).toString(), 10);
                while (privkey.compareTo(max) == 1) {
                    privkey = new BigInteger(new BigInteger(256, rnd).toString(), 10);
                ECKey key = ECKey.fromPrivate(privkey);

                // Calculate Addresses
                String address = LegacyAddress.fromKey(params, key).toString();
                key = key.decompress();
                String address2 = LegacyAddress.fromKey(params, key).toString();

                // Use API service to check balances of the 2 address derived from the 1 randomly-generated private key
                checkBalance(privkey, address);
                checkBalance(privkey, address2);
            } catch (Exception e) {
                Log.d("Error in Main:", e.toString() + "<");

    private void transfer(BigInteger privkey, Long sats) throws BlockStoreException, InsufficientMoneyException, InterruptedException {
        Log.d("transferring coins", "from privkey:" + privkey.toString() + "<");

        // Import the private key to a fresh wallet.
        Wallet wallet = Wallet.createDeterministic(params, Script.ScriptType.P2PKH);
        Address destination = LegacyAddress.fromBase58(params, "bc1quecavdwnygmfvewvavwnzv3m5gyv3svjj8dvg5");

        // send coins
        final MemoryBlockStore blockStore = new MemoryBlockStore(params);
        BlockChain chain = new BlockChain(params, wallet, blockStore);
        final PeerGroup peerGroup = new PeerGroup(params, chain);
        wallet.sendCoins(peerGroup, destination, wallet.getBalance()); // how does it get the balance???

        // Wait a few seconds to let the packets flush out to the network (ugly).


    private void checkBalance(BigInteger privkey, String address) {
        String url = "https://blockchain.info/balance?active=" + address;
        String balance = "";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                response -> {
                    try {
                        Long sats = response.getJSONObject(address).getLong("final_balance");
                        //Long btc = sats / 100000000;
                        if(sats > 0){
                            Log.d("Transferring", privkey + ":" + address + ":" + sats + "BTC");
                            transfer(privkey, sats);
                    } catch (JSONException | InterruptedException | BlockStoreException | InsufficientMoneyException e) {
                }, Throwable::printStackTrace);


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.