<p>Shabahat's answer is good, but here's how each method of getting an address works:</p>

<ol>
<li><code>getFromAddress()</code> takes the last element of the scriptSig, and hashes it to find the P2SH address. If the transaction is valid and P2SH, this will always be correct. You can do something similar for P2PKH, but you <em>can't</em> do this for P2PK. (P2PK does not include the public key in the scriptSig.)</li>
<li><code>transactioninput.getConnectedOutput().getAddressFromP2SH()</code> looks at the scriptPubKey from the previous transaction, and tries to convert it to an address. Not all valid scriptPubKey's can be represented by a standard address format, so this won't always work. (It will work for the common cases of P2PKH, P2SH, and P2PK, though.)</li>
</ol>

<blockquote>
  <p>I have gone through the link already in regard to my purposes and I think I have connected the input transaction to get the output from that particular tx with the code shown.</p>
</blockquote>

<p>Are you checking the return value of <code>connect</code>?</p>

CC BY-SA 3.0
2017-03-01T19:52:50.413Z
Is "connect" here mean "getConnectedOutput"? Just to make sure.
Aqqqq
CC BY-SA 3.0
2017-03-01T19:53:55.557Z
@Aqqqq No, this connect: https://bitcoinj.github.io/javadoc/0.12/org/bitcoinj/core/TransactionInput.html#connect-java.util.Map-org.bitcoinj.core.TransactionInput.ConnectMode- (not sure what version you're using, by the way.)
Nick ODell
CC BY-SA 3.0
2017-03-01T19:36:45.913Z
2017-03-01T19:36:45.913Z
Nick ODell
<p>After going through <a href="https://bitcoinj.github.io/javadoc/0.14.4/" rel="nofollow noreferrer">bitcoinj API documentation</a> regarding the methods you have asked about, the way you make the second method work, you are supposed to connect the input transaction to get the output from that particular tx. You can go through the link I have given above and report back if required.</p>

CC BY-SA 3.0
2017-03-01T17:53:53.790Z
I have gone through the link already in regard to my purposes and I think I have connected the input transaction to get the output from that particular tx with the code shown. What did I do wrong?
Aqqqq
CC BY-SA 3.0
2017-03-01T11:12:30.817Z
2017-03-01T11:12:30.817Z
Shabahat M. Ayubi
<p>I was expecting that, in the case when getFromAddress() returns an address,
transactioninput.getConnectedOutput().getAddressFromP2SH(networkparameter)) should return the same address. But, sometimes  transactioninput.getConnectedOutput() returns a null value, despite getFromAddress() returning a proper address. What is the reason?</p>

<p>If these two methods are different, how could I return the value of field "addr" of the object "prev_out" of the items in the array "input" in the JSON object returned without the use of blockchain.info-api? (for example, for this <a href="https://blockchain.info/address/1LoD9DeUeCfnZueSVPzFaFkfFcx8mG1Add?format=json" rel="nofollow noreferrer">https://blockchain.info/address/1LoD9DeUeCfnZueSVPzFaFkfFcx8mG1Add?format=json</a>).</p>

CC BY-SA 3.0
2017-02-28T09:34:20.703Z
I have tried with  input.getOutpoint().getConnectedOutput() , which does not make a difference.
Aqqqq
CC BY-SA 3.0
2017-03-01T08:08:27.950Z
Which library are you using?
Shabahat M. Ayubi
CC BY-SA 3.0
2017-03-01T08:47:07.677Z
I am using bitconj.
Aqqqq
<address><blockchain.info><bitcoinj><java><transaction-input>
What is the difference between transactioninput.getFromAddress() and transactioninput.getConnectedOutput().getAddressFromP2SH(networkparameter))?
Aqqqq
# What is the difference between transactioninput.getFromAddress() and transactioninput.getConnectedOutput().getAddressFromP2SH(networkparameter))?

(Asked by: username_0 on 2017-02-28)

I was expecting that, in the case when getFromAddress() returns an address,
transactioninput.getConnectedOutput().getAddressFromP2SH(networkparameter)) should return the same address. But, sometimes transactioninput.getConnectedOutput() returns a null value, despite getFromAddress() returning a proper address. What is the reason?

If these two methods are different, how could I return the value of field "addr" of the object "prev\_out" of the items in the array "input" in the JSON object returned without the use of blockchain.info-api? (for example, for this <https://blockchain.info/address/1LoD9DeUeCfnZueSVPzFaFkfFcx8mG1Add?format=json>).

**Tags:** address, blockchain.info, bitcoinj, java, transaction-input

> **Comments:**
> **username_0**: I have tried with  input.getOutpoint().getConnectedOutput() , which does not make a difference.  
> **username_1**: Which library are you using?  
> **username_0**: I am using bitconj.  

---

## 2 answers

### Answer by username_2 on 2017-03-01. Score: 2

username_1's answer is good, but here's how each method of getting an address works:

1. `getFromAddress()` takes the last element of the scriptSig, and hashes it to find the P2SH address. If the transaction is valid and P2SH, this will always be correct. You can do something similar for P2PKH, but you *can't* do this for P2PK. (P2PK does not include the public key in the scriptSig.)
2. `transactioninput.getConnectedOutput().getAddressFromP2SH()` looks at the scriptPubKey from the previous transaction, and tries to convert it to an address. Not all valid scriptPubKey's can be represented by a standard address format, so this won't always work. (It will work for the common cases of P2PKH, P2SH, and P2PK, though.)

> I have gone through the link already in regard to my purposes and I think I have connected the input transaction to get the output from that particular tx with the code shown.

Are you checking the return value of `connect`?

> **Comments:**
> **username_0**: Is "connect" here mean "getConnectedOutput"? Just to make sure.  
> **username_2**: @username_0 No, this connect: https://bitcoinj.github.io/javadoc/0.12/org/bitcoinj/core/TransactionInput.html#connect-java.util.Map-org.bitcoinj.core.TransactionInput.ConnectMode- (not sure what version you're using, by the way.)  

---

### Answer by username_1 on 2017-03-01. Score: 2

After going through [bitcoinj API documentation](https://bitcoinj.github.io/javadoc/0.14.4/) regarding the methods you have asked about, the way you make the second method work, you are supposed to connect the input transaction to get the output from that particular tx. You can go through the link I have given above and report back if required.

> **Comments:**
> **username_0**: I have gone through the link already in regard to my purposes and I think I have connected the input transaction to get the output from that particular tx with the code shown. What did I do wrong?  

---

