<p>Bitcoin transactions don't have a sender's address.</p>

<p>What you're doing is inferring what address previously controlled some or all of the funds involved in the transaction. But:</p>

<ul>
<li>Previously controlling the funds doesn't mean you found the sender. In case the coins were held by a custodial exchange, the address will be the exchange, and not the sender of the funds. In particular, sending coins back there won't make them reach the right person.</li>
<li>Transactions can be jointly constructed using multiple participants. You can only guess which is which.</li>
<li>The script that previously controlled the funds may not have an associates address at all.</li>
</ul>

<p>If you want to know what the sender's address of a transaction is, ask the person who sent it (for example if you need a refund address, ask it before showing an address to pay to). If it's not a transaction you're involved in, it's probably none of your business.</p>

CC BY-SA 4.0
2019-07-24T06:11:41.623Z
2019-07-24T06:11:41.623Z
Pieter Wuille
<pre><code>public Address getFromAddress(TransactionInput txIn) {
    // get script from connected output
    TransactionOutput txOut = txIn.getConnectedOutput();
    Script txOutScript = txOut.getScriptPubKey();

    // get address from script
    Address fromAddress = txOutScript.getToAddress(MainNetParams.get());

    return fromAddress;
}
</code></pre>

CC BY-SA 4.0
2019-02-23T22:45:45.503Z
thank you! which TransactionInput should I check? if I use tx.getInput(0) I get a NullPointerException. Should I iterate over all the inputs or is it something else I'm missing?
Johnny
CC BY-SA 4.0
2019-02-25T23:09:50.780Z
This is the problem with trying to find the sender. A  "sender" isn't a clear idea for a Bitcoin transaction. A transaction's inputs could come from multiple sources. Ultimately, determining which input(s) to consider the "sender" is up to you. As for the NullPointerException, coinbase transactions have no inputs, and I'm guessing that's probably where you're getting the exception. You'll want to skip over the transaction if `tx.isCoinBase()`.
Vecna
CC BY-SA 4.0
2019-02-23T22:34:45.937Z
2019-02-23T22:34:45.937Z
Vecna
<p>I'm trying to modify the simple <a href="https://bitcoinj.github.io/simple-gui-wallet" rel="nofollow noreferrer">wallettemplate</a> to get the sender's address in the transaction list. However since TransactionInput.getFromAddress() doesn't exist now and getConnectedOutput().getAddressFromP2PKHScript() is deprecated, I don't know which is the right way to do so. </p>

CC BY-SA 4.0
2019-02-23T22:35:21.413Z
It's not entirely clear to me **why** `TransactionOutput.getAddressFromP2PKHScript()` has been deprecated. It seems to me that perhaps  Andreas Schildbach (who [deprecated the method](https://github.com/bitcoinj/bitcoinj/commit/bc8828c8585cfd3ce581d6fbbc8a37d86776fbba)) is trying to discourage people trying to determine a "sender" of a transaction in such a way because of the ambiguity involved (for instance if a transaction has multiple "senders". (See, e.g., [here](https://groups.google.com/forum/#!searchin/bitcoinj/getAddressFromP2PKHScript()|sort:date/bitcoinj/02mDeOiHeuk/BDmr-KulBgAJ))
Vecna
<bitcoinj><p2pkh>
how to get the sender's address in bitcoinj
Johnny
# how to get the sender's address in bitcoinj

(Asked by: username_0 on 2019-02-22)

I'm trying to modify the simple [wallettemplate](https://bitcoinj.github.io/simple-gui-wallet) to get the sender's address in the transaction list. However since TransactionInput.getFromAddress() doesn't exist now and getConnectedOutput().getAddressFromP2PKHScript() is deprecated, I don't know which is the right way to do so.

**Tags:** bitcoinj, p2pkh

> **Comments:**
> **username_1**: It's not entirely clear to me **why** `TransactionOutput.getAddressFromP2PKHScript()` has been deprecated. It seems to me that perhaps  Andreas Schildbach (who [deprecated the method](https://github.com/bitcoinj/bitcoinj/commit/bc8828c8585cfd3ce581d6fbbc8a37d86776fbba)) is trying to discourage people trying to determine a "sender" of a transaction in such a way because of the ambiguity involved (for instance if a transaction has multiple "senders". (See, e.g., [here](https://groups.google.com/forum/#!searchin/bitcoinj/getAddressFromP2PKHScript()|sort:date/bitcoinj/02mDeOiHeuk/BDmr-KulBgAJ))  

---

## 2 answers

### Answer by username_2 on 2019-07-24. Score: 1

Bitcoin transactions don't have a sender's address.

What you're doing is inferring what address previously controlled some or all of the funds involved in the transaction. But:

* Previously controlling the funds doesn't mean you found the sender. In case the coins were held by a custodial exchange, the address will be the exchange, and not the sender of the funds. In particular, sending coins back there won't make them reach the right person.
* Transactions can be jointly constructed using multiple participants. You can only guess which is which.
* The script that previously controlled the funds may not have an associates address at all.

If you want to know what the sender's address of a transaction is, ask the person who sent it (for example if you need a refund address, ask it before showing an address to pay to). If it's not a transaction you're involved in, it's probably none of your business.


---

### Answer by username_1 on 2019-02-23. Score: -1

```
public Address getFromAddress(TransactionInput txIn) {
    // get script from connected output
    TransactionOutput txOut = txIn.getConnectedOutput();
    Script txOutScript = txOut.getScriptPubKey();

    // get address from script
    Address fromAddress = txOutScript.getToAddress(MainNetParams.get());

    return fromAddress;
}

```

> **Comments:**
> **username_0**: thank you! which TransactionInput should I check? if I use tx.getInput(0) I get a NullPointerException. Should I iterate over all the inputs or is it something else I'm missing?  
> **username_1**: This is the problem with trying to find the sender. A  "sender" isn't a clear idea for a Bitcoin transaction. A transaction's inputs could come from multiple sources. Ultimately, determining which input(s) to consider the "sender" is up to you. As for the NullPointerException, coinbase transactions have no inputs, and I'm guessing that's probably where you're getting the exception. You'll want to skip over the transaction if `tx.isCoinBase()`.  

---

