<p>Repeat after me: Bitcoin transactions do not have a return address. Bitcoin transactions do not have a return address.</p>

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

<ul>
<li>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).</li>
<li>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).</li>
<li>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).</li>
<li>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.</li>
</ul>

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

CC BY-SA 3.0
2018-01-02T09:18:13.167Z
2018-01-02T09:18:13.167Z
Pieter Wuille
<p>I was reading a <a href="https://bitcoin.stackexchange.com/a/5982/1891">popular answer</a> 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.</p>

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

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

<p>Here is a part which is unclear for me:</p>

<p><code>input_raw_tx['vout'][input['vout']]['scriptPubKey']['addresses'][0]</code></p>

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

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

<transactions><address><json-rpc>
Can anyone explain this pseudocode to find a returning address?
Salvador Dali
# Can anyone explain this pseudocode to find a returning address?

(Asked by: username_0 on 2017-12-29)

I was reading a [popular answer](https://bitcoin.stackexchange.com/a/5982/1891) 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 = <relevant transaction id>
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?

**Tags:** transactions, address, json-rpc


---

## 1 answers

### Answer by username_1 on 2018-01-02. Score: 3

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.


---

