What is the difference between transactioninput.getFromAddress() and transactioninput.getConnectedOutput().getAddressFromP2SH(networkparameter))?

1

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).

Aqqqq

Posted 2017-02-28T08:53:49.980

Reputation: 277

I have tried with input.getOutpoint().getConnectedOutput() , which does not make a difference.Aqqqq 2017-02-28T09:34:20.703

Which library are you using?Shabahat M. Ayubi 2017-03-01T08:08:27.950

I am using bitconj.Aqqqq 2017-03-01T08:47:07.677

Answers

2

After going through bitcoinj API documentation 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.

Shabahat M. Ayubi

Posted 2017-02-28T08:53:49.980

Reputation: 1 409

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 2017-03-01T17:53:53.790

2

Shabahat'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?

Nick ODell

Posted 2017-02-28T08:53:49.980

Reputation: 26 536

Is "connect" here mean "getConnectedOutput"? Just to make sure.Aqqqq 2017-03-01T19:52:50.413

@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 2017-03-01T19:53:55.557