BitcoinJ how to understand receive Address?

1

I need to receive bitcoins from the customers, and then add to theirs virtual account some credits. I use BitcoinJ wallet and I want to give each customer the unique Address created by

walletAppKit.wallet().freshReceiveAddress()

But when I receive coins I can't define customer who send me them.

walletAppKit.wallet().addCoinsReceivedEventListener((w, tx, prevBalance, newBalance) contains all information but I don't see any Address info to define customer.

Ran Novr

Posted 2018-06-16T00:42:16.850

Reputation: 13

Answers

0

There can be many recipients (in the same transaction), so you need to iterate for the correct ones. E.g. for incoming transactions, you can do

for (TransactionOutput output : tx.getOutputs())
{
        try
        {
                if (output.isMine(wallet))
                {
                        Script script = output.getScriptPubKey();
                        Address address = script.getToAddress(Constants.NETWORK_PARAMETERS,true);
                        // do something with the address
                }
        }
        catch (final ScriptException x){}
}

Zombie

Posted 2018-06-16T00:42:16.850

Reputation: 528

Could you please explane isMine method?Ran Novr 2018-06-17T00:00:38.560

This method Returns true if this output is to a key, or an address we have the keys for, in the wallet, Reference: https://bitcoinj.github.io/javadoc/0.14/org/bitcoinj/core/TransactionOutput.html#isMine-org.bitcoinj.core.TransactionBag-

Zombie 2018-06-17T08:25:39.747

0

I'm not very familiar with bitcoinj but it appears to me that you need to do the following.

You're passed the transaction that was received (tx). Call getOutputs to get a list of the transaction's outputs, and for each one, call getAddressFromP2PKHScript to determine the address credited by that output.

You should account for the possibility that there might be multiple outputs paying the same address, or multiple outputs paying different addresses that are in your wallet.

Nate Eldredge

Posted 2018-06-16T00:42:16.850

Reputation: 21 420