Can anyone explain this pseudocode to find a returning address? I was reading a popular answer which explains how to find the address of the person who sent a transaction and the pseudocode in that answer does not make much sense to me.

txid = &lt;relevant transaction id&gt;
addresses = []
raw_tx = decoderawtransaction(getrawtransaction(txid))
for(input in raw_tx['vin']) {
  input_raw_tx = decoderawtransaction(getrawtransaction(input['txid']))
  addresses.push(input_raw_tx['vout'][input['vout']]['scriptPubKey']['addresses'][0])
}


Here is the part which I understood. A wallet which created txid can use many inputs from different addresses to send that amount of money. This is why we iterate over input in raw_tx['vin'] and create a list of addresses addresses = [].

Here is a part which is unclear for me:

input_raw_tx['vout'][input['vout']]['scriptPubKey']['addresses'][0]

input_raw_tx['vout'] returns the list. So input['vout'] should return the index of that list. But it also returns a list. Can anyone explain what is going on?

Also that answer was written in old 2013. Has anything changed in these 4 years and is there an easier way to find the returning address as of the beginning of 2918?
Repeat after me: Bitcoin transactions do not have a return address. Bitcoin transactions do not have a return address.

As you say, a transaction can have multiple inputs. Each input spends a coin that was previously controlled a particular address/key or script. However, these "input addressee" are not return addresses, for multiple reasons:


The different inputs could belong to different people/entities, for example if they use CoinJoin or simar protocols. You don't know which is the sender of your payment (the pseudocode above will just return all of the sender addresses).
There may not be a recognizable address for every input (for example if it was sent through a high-level protocol), but only an arbitrary script (the pseudocode above will fail in this case).
Even if there is a clear address, it may belong to a service the sender used - and sending coins back to it would credit the service or an arbitrary user of it rather than the sender of your transaction (the pseudocode would give the service's address in this case).
Even if there is an address that indeed belonged to the sender, they may not expect a payment to it, and perhaps not have kept the private keys for it around.


In general, if you want to refund some money, there is only one solution: ask the sender ahead of time for a refund address.