<p>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</p>

<pre><code>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){}
}
</code></pre>

CC BY-SA 4.0
2018-06-17T00:00:38.560Z
Could you please explane isMine method?
Ran Novr
CC BY-SA 4.0
2018-06-17T08:25:39.747Z
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
CC BY-SA 4.0
2018-06-16T20:00:10.147Z
2018-06-16T20:00:10.147Z
Zombie
<p>I'm not very familiar with bitcoinj but it appears to me that you need to do the following.</p>

<p>You're passed the transaction that was received (<code>tx</code>).  Call <a href="https://bitcoinj.github.io/javadoc/0.14/org/bitcoinj/core/Transaction.html#getOutputs--" rel="nofollow noreferrer"><code>getOutputs</code></a> to get a list of the transaction's outputs, and for each one, call <a href="https://bitcoinj.github.io/javadoc/0.14/org/bitcoinj/core/TransactionOutput.html#getAddressFromP2PKHScript-org.bitcoinj.core.NetworkParameters-" rel="nofollow noreferrer"><code>getAddressFromP2PKHScript</code></a> to determine the address credited by that output.</p>

<p>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.</p>

CC BY-SA 4.0
2018-06-16T01:08:49.357Z
2018-06-16T01:08:49.357Z
Nate Eldredge
<p>I need to receive bitcoins from the customers, and then add to theirs virtual account some credits. I use <code>BitcoinJ</code> wallet and I want to give each customer the unique Address created by </p>

<pre><code>walletAppKit.wallet().freshReceiveAddress()
</code></pre>

<p>But when I receive coins I can't define customer who send me them.</p>

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

<bitcoinj>
BitcoinJ how to understand receive Address?
Ran Novr
# BitcoinJ how to understand receive Address?

(Asked by: username_0 on 2018-06-16)

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.

**Tags:** bitcoinj


---

## 2 answers

### Answer by username_1 on 2018-06-16. Score: 0 | Accepted answer

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){}
}

```

> **Comments:**
> **username_0**: Could you please explane isMine method?  
> **username_1**: 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-  

---

### Answer by username_2 on 2018-06-16. Score: 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`](https://bitcoinj.github.io/javadoc/0.14/org/bitcoinj/core/Transaction.html#getOutputs--) to get a list of the transaction's outputs, and for each one, call [`getAddressFromP2PKHScript`](https://bitcoinj.github.io/javadoc/0.14/org/bitcoinj/core/TransactionOutput.html#getAddressFromP2PKHScript-org.bitcoinj.core.NetworkParameters-) 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.


---

