<p>Currently im trying to build an explorer and im stuck on the block/listing transactions.</p>
<p>Currently this is my code.</p>
<pre><code>foreach($info['tx'] AS $key =&gt; $value){
            $txinfo = callBTC(&quot;getrawtransaction&quot;,array(&quot;\&quot;$value\&quot;&quot;,true));
            echo '&lt;pre&gt;';
            print_r($txinfo);
            echo '&lt;/pre&gt;';
            echo '&lt;div class=&quot;tx&quot;&gt;&lt;div class=&quot;txRow&quot;&gt;&lt;div class=&quot;txCat&quot;&gt;Hash&lt;/div&gt;&lt;div class=&quot;txInfo&quot;&gt;&lt;div class=&quot;overflow&quot;&gt;&lt;a href=&quot;index?txid='.$value.'&quot;&gt;'.$value.'&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;';
            echo '&lt;div class=&quot;txRow&quot;&gt;&lt;div class=&quot;txCat&quot;&gt;Time&lt;/div&gt;&lt;div class=&quot;txInfo&quot;&gt;'.date(&quot;Y-m-d H:i:s&quot;,$txinfo['time']).'&lt;/div&gt;&lt;/div&gt;';
            echo '&lt;div class=&quot;txRow&quot;&gt;&lt;div class=&quot;txCat&quot;&gt;From&lt;/div&gt;&lt;div class=&quot;txInfo&quot;&gt;&lt;div class=&quot;overflow&quot;&gt;';
            foreach($txinfo['vin'] AS $key2 =&gt; $value2){
                if(!empty($txinfo['vin'][$key2]['coinbase'])){
                    echo '&lt;div class=&quot;confirmed&quot;&gt;COINBASE - Newly Created Coins&lt;/div&gt;';
                }else{
                    $from = callBTC(&quot;getrawtransaction&quot;,array(&quot;\&quot;$value2[txid]\&quot;&quot;,true));
                    echo '&lt;pre&gt;';
                    print_r($from);
                    echo '&lt;/pre&gt;';
                    echo $from['vout'][0]['scriptPubKey']['addresses'][0].'&lt;br/&gt;';
                }
            }
            echo '&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;';
            echo '&lt;div class=&quot;txRow&quot;&gt;&lt;div class=&quot;txCat&quot;&gt;To&lt;/div&gt;&lt;div class=&quot;txInfo&quot;&gt;&lt;div class=&quot;overflow&quot;&gt;';
            foreach($txinfo['vout'] AS $key3 =&gt; $value3){
                if(!empty($value3['scriptPubKey']['addresses'])){
                    foreach($value3['scriptPubKey']['addresses'] AS $key4 =&gt; $value4){
                        echo '&lt;a href=&quot;index?addid='.$value4.'&quot;&gt;'.$value4.'&lt;/a&gt;&lt;br/&gt;';
                    }
                }
            }
            echo '&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;';
            echo '&lt;div class=&quot;txRow&quot;&gt;&lt;div class=&quot;txCat&quot;&gt;Fee&lt;/div&gt;&lt;div class=&quot;txInfo&quot;&gt;&lt;/div&gt;&lt;/div&gt;';
            echo '&lt;div class=&quot;txRow&quot;&gt;&lt;div class=&quot;txCat&quot;&gt;Amount&lt;/div&gt;&lt;div class=&quot;txInfo&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;';
        }
</code></pre>
<p>i got the to addresses fine but whne it comes to the from field i struggle to determine which is the valid address to use.</p>
<p>Lets use block 2092235 for example</p>
<p><a href="https://www.blockchain.com/btc-testnet/block/000000000000003f69d7c8cb4943df2d7a273883eb72024f3305122ed2dc7271?page=1" rel="nofollow noreferrer">https://www.blockchain.com/btc-testnet/block/000000000000003f69d7c8cb4943df2d7a273883eb72024f3305122ed2dc7271?page=1</a></p>
<p>I use blockchain.com to check against my site to ensure im displaying the proper data.</p>
<p>but when i spit out the transaction for the tx id ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6 i get this...</p>
<pre><code>Array
(
    [txid] =&gt; ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6
    [hash] =&gt; b388c750f3d15f1fcdb320fbc3398a65a34a3eb6933ca1738729f22a4a8a5924
    [version] =&gt; 2
    [size] =&gt; 222
    [vsize] =&gt; 141
    [weight] =&gt; 561
    [locktime] =&gt; 2092234
    [vin] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [txid] =&gt; 122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c
                    [vout] =&gt; 1
                    [scriptSig] =&gt; Array
                        (
                            [asm] =&gt; 
                            [hex] =&gt; 
                        )

                    [txinwitness] =&gt; Array
                        (
                            [0] =&gt; 304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401
                            [1] =&gt; 0258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280
                        )

                    [sequence] =&gt; 4294967294
                )

        )

    [vout] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [value] =&gt; 0.01905871
                    [n] =&gt; 0
                    [scriptPubKey] =&gt; Array
                        (
                            [asm] =&gt; 0 2cda63f440de6ab858cf2550daf3b25522d6c66f
                            [hex] =&gt; 00142cda63f440de6ab858cf2550daf3b25522d6c66f
                            [reqSigs] =&gt; 1
                            [type] =&gt; witness_v0_keyhash
                            [addresses] =&gt; Array
                                (
                                    [0] =&gt; tb1q9ndx8azqme4tskx0y4gd4uaj253dd3n0386xkn
                                )

                        )

                )

            [1] =&gt; Array
                (
                    [value] =&gt; 72.44723495
                    [n] =&gt; 1
                    [scriptPubKey] =&gt; Array
                        (
                            [asm] =&gt; 0 e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab
                            [hex] =&gt; 0014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab
                            [reqSigs] =&gt; 1
                            [type] =&gt; witness_v0_keyhash
                            [addresses] =&gt; Array
                                (
                                    [0] =&gt; tb1qu9u6609s6w798ja9vnxzupdu9hqcmcdtvm87gx
                                )

                        )

                )

        )

    [hex] =&gt; 020000000001015ced69c0d591aa8616d5be813dca34382be7ff86e8215d6c1dfb36226b912e120100000000feffffff02cf141d00000000001600142cda63f440de6ab858cf2550daf3b25522d6c66f27b5d1af01000000160014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab0247304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401210258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280caec1f00
    [blockhash] =&gt; 000000000000003f69d7c8cb4943df2d7a273883eb72024f3305122ed2dc7271
    [confirmations] =&gt; 1418
    [time] =&gt; 1631039838
    [blocktime] =&gt; 1631039838
)
</code></pre>
<p>then i get the tx id from the vin field to get info on the transaction for that address(the from field) and i get this.</p>
<pre><code>Array
(
    [txid] =&gt; 122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c
    [hash] =&gt; 5fb52307d771c7dadf9f06006340a0acb5ff9ebe15d215d98f08e749d0ee2e59
    [version] =&gt; 2
    [size] =&gt; 222
    [vsize] =&gt; 141
    [weight] =&gt; 561
    [locktime] =&gt; 2092232
    [vin] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [txid] =&gt; 2739adc2b59de66a9e707ec3e102549dc56a8432e38ef471cb961976a27739be
                    [vout] =&gt; 0
                    [scriptSig] =&gt; Array
                        (
                            [asm] =&gt; 
                            [hex] =&gt; 
                        )

                    [txinwitness] =&gt; Array
                        (
                            [0] =&gt; 3044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e01
                            [1] =&gt; 036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296a
                        )

                    [sequence] =&gt; 4294967294
                )

        )

    [vout] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [value] =&gt; 0.01275647
                    [n] =&gt; 0
                    [scriptPubKey] =&gt; Array
                        (
                            [asm] =&gt; 0 184337352372a5fa6218dd57ea1057305d39c77d
                            [hex] =&gt; 0014184337352372a5fa6218dd57ea1057305d39c77d
                            [reqSigs] =&gt; 1
                            [type] =&gt; witness_v0_keyhash
                            [addresses] =&gt; Array
                                (
                                    [0] =&gt; tb1qrppnwdfrw2jl5cscm4t75yzhxpwnn3mamh6ff8
                                )

                        )

                )

            [1] =&gt; Array
                (
                    [value] =&gt; 72.46645714
                    [n] =&gt; 1
                    [scriptPubKey] =&gt; Array
                        (
                            [asm] =&gt; 0 c4520316f8d1dee0a19acde950d0540701d3d013
                            [hex] =&gt; 0014c4520316f8d1dee0a19acde950d0540701d3d013
                            [reqSigs] =&gt; 1
                            [type] =&gt; witness_v0_keyhash
                            [addresses] =&gt; Array
                                (
                                    [0] =&gt; tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep
                                )

                        )

                )

        )

    [hex] =&gt; 02000000000101be3977a2761996cb71f48ee332846ac59d5402e1c37e709e6ae69db5c2ad39270000000000feffffff02ff76130000000000160014184337352372a5fa6218dd57ea1057305d39c77dd209efaf01000000160014c4520316f8d1dee0a19acde950d0540701d3d01302473044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e0121036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296ac8ec1f00
    [blockhash] =&gt; 00000000000000d299298de1cfb14276667b7d55e8a1cf43c9dc825babadf058
    [confirmations] =&gt; 1420
    [time] =&gt; 1631039256
    [blocktime] =&gt; 1631039256
)
</code></pre>
<p>by looking at blockchain i know which address it SHOULD take (tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep)...but when i use php and check for last key in array its not always the correct address in other transactions as sometimes the address i need is in a different index(same array tho just different index).</p>
<p>How can i programmatically determine the right address consistently?</p>

CC BY-SA 4.0
2021-09-13T21:12:08.047Z
Bitcoin transactions do not have a well-defined "from" address. Transactions have inputs, and each of those inputs may or may not have an identifiable address the spent coins were previously assigned to. The result is zero, one, or more "from" addresses, but these don't correspond to any real-world property; in particular, they cannot be used to identify the sender, or to find a refund address. Multiple people can (and do) sometimes jointly produce a single transaction that moves funds belonging to all of them, for example.
Pieter Wuille
CC BY-SA 4.0
2021-09-13T21:16:35.680Z
agreed but not the question i was asking. i provided the txid to pull the information regarding the from address. please reread it again i have already explained it,
Derek Hill
CC BY-SA 4.0
2021-09-13T21:22:50.133Z
Yes, I'm not trying to answer your question, only pointing out that what you're trying to do is meaningless. If someone wants to answer your actual question, they can.
Pieter Wuille
CC BY-SA 4.0
2021-09-13T21:23:47.840Z
its not meaningless when you are trying to build an explorer. Its helpful to know what address the coins came from.
Derek Hill
CC BY-SA 4.0
2021-09-13T23:00:10.657Z
https://www.blockchain.com/btc-testnet/block/000000000000003f69d7c8cb4943df2d7a273883eb72024f3305122ed2dc7271?page=1

Blockchain uses it and i understand it fine. Ofcourse there is no from and to its vin and vout. If you looked at the code you would know that i understand this.
Derek Hill
<bitcoin-cli><php>
determining the address a transaction comes from
Derek Hill
# determining the address a transaction comes from

(Asked by: username_0 on 2021-09-13)

Currently im trying to build an explorer and im stuck on the block/listing transactions.

Currently this is my code.

```
foreach($info['tx'] AS $key => $value){
            $txinfo = callBTC("getrawtransaction",array("\"$value\"",true));
            echo '<pre>';
            print_r($txinfo);
            echo '</pre>';
            echo '<div class="tx"><div class="txRow"><div class="txCat">Hash</div><div class="txInfo"><div class="overflow"><a href="index?txid='.$value.'">'.$value.'</a></div></div></div>';
            echo '<div class="txRow"><div class="txCat">Time</div><div class="txInfo">'.date("Y-m-d H:i:s",$txinfo['time']).'</div></div>';
            echo '<div class="txRow"><div class="txCat">From</div><div class="txInfo"><div class="overflow">';
            foreach($txinfo['vin'] AS $key2 => $value2){
                if(!empty($txinfo['vin'][$key2]['coinbase'])){
                    echo '<div class="confirmed">COINBASE - Newly Created Coins</div>';
                }else{
                    $from = callBTC("getrawtransaction",array("\"$value2[txid]\"",true));
                    echo '<pre>';
                    print_r($from);
                    echo '</pre>';
                    echo $from['vout'][0]['scriptPubKey']['addresses'][0].'<br/>';
                }
            }
            echo '</div></div></div>';
            echo '<div class="txRow"><div class="txCat">To</div><div class="txInfo"><div class="overflow">';
            foreach($txinfo['vout'] AS $key3 => $value3){
                if(!empty($value3['scriptPubKey']['addresses'])){
                    foreach($value3['scriptPubKey']['addresses'] AS $key4 => $value4){
                        echo '<a href="index?addid='.$value4.'">'.$value4.'</a><br/>';
                    }
                }
            }
            echo '</div></div></div>';
            echo '<div class="txRow"><div class="txCat">Fee</div><div class="txInfo"></div></div>';
            echo '<div class="txRow"><div class="txCat">Amount</div><div class="txInfo"></div></div></div>';
        }

```

i got the to addresses fine but whne it comes to the from field i struggle to determine which is the valid address to use.

Lets use block 2092235 for example

<https://www.blockchain.com/btc-testnet/block/000000000000003f69d7c8cb4943df2d7a273883eb72024f3305122ed2dc7271?page=1>

I use blockchain.com to check against my site to ensure im displaying the proper data.

but when i spit out the transaction for the tx id ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6 i get this...

```
Array
(
    [txid] => ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6
    [hash] => b388c750f3d15f1fcdb320fbc3398a65a34a3eb6933ca1738729f22a4a8a5924
    [version] => 2
    [size] => 222
    [vsize] => 141
    [weight] => 561
    [locktime] => 2092234
    [vin] => Array
        (
            [0] => Array
                (
                    [txid] => 122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c
                    [vout] => 1
                    [scriptSig] => Array
                        (
                            [asm] => 
                            [hex] => 
                        )

                    [txinwitness] => Array
                        (
                            [0] => 304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401
                            [1] => 0258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280
                        )

                    [sequence] => 4294967294
                )

        )

    [vout] => Array
        (
            [0] => Array
                (
                    [value] => 0.01905871
                    [n] => 0
                    [scriptPubKey] => Array
                        (
                            [asm] => 0 2cda63f440de6ab858cf2550daf3b25522d6c66f
                            [hex] => 00142cda63f440de6ab858cf2550daf3b25522d6c66f
                            [reqSigs] => 1
                            [type] => witness_v0_keyhash
                            [addresses] => Array
                                (
                                    [0] => tb1q9ndx8azqme4tskx0y4gd4uaj253dd3n0386xkn
                                )

                        )

                )

            [1] => Array
                (
                    [value] => 72.44723495
                    [n] => 1
                    [scriptPubKey] => Array
                        (
                            [asm] => 0 e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab
                            [hex] => 0014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab
                            [reqSigs] => 1
                            [type] => witness_v0_keyhash
                            [addresses] => Array
                                (
                                    [0] => tb1qu9u6609s6w798ja9vnxzupdu9hqcmcdtvm87gx
                                )

                        )

                )

        )

    [hex] => 020000000001015ced69c0d591aa8616d5be813dca34382be7ff86e8215d6c1dfb36226b912e120100000000feffffff02cf141d00000000001600142cda63f440de6ab858cf2550daf3b25522d6c66f27b5d1af01000000160014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab0247304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401210258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280caec1f00
    [blockhash] => 000000000000003f69d7c8cb4943df2d7a273883eb72024f3305122ed2dc7271
    [confirmations] => 1418
    [time] => 1631039838
    [blocktime] => 1631039838
)

```

then i get the tx id from the vin field to get info on the transaction for that address(the from field) and i get this.

```
Array
(
    [txid] => 122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c
    [hash] => 5fb52307d771c7dadf9f06006340a0acb5ff9ebe15d215d98f08e749d0ee2e59
    [version] => 2
    [size] => 222
    [vsize] => 141
    [weight] => 561
    [locktime] => 2092232
    [vin] => Array
        (
            [0] => Array
                (
                    [txid] => 2739adc2b59de66a9e707ec3e102549dc56a8432e38ef471cb961976a27739be
                    [vout] => 0
                    [scriptSig] => Array
                        (
                            [asm] => 
                            [hex] => 
                        )

                    [txinwitness] => Array
                        (
                            [0] => 3044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e01
                            [1] => 036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296a
                        )

                    [sequence] => 4294967294
                )

        )

    [vout] => Array
        (
            [0] => Array
                (
                    [value] => 0.01275647
                    [n] => 0
                    [scriptPubKey] => Array
                        (
                            [asm] => 0 184337352372a5fa6218dd57ea1057305d39c77d
                            [hex] => 0014184337352372a5fa6218dd57ea1057305d39c77d
                            [reqSigs] => 1
                            [type] => witness_v0_keyhash
                            [addresses] => Array
                                (
                                    [0] => tb1qrppnwdfrw2jl5cscm4t75yzhxpwnn3mamh6ff8
                                )

                        )

                )

            [1] => Array
                (
                    [value] => 72.46645714
                    [n] => 1
                    [scriptPubKey] => Array
                        (
                            [asm] => 0 c4520316f8d1dee0a19acde950d0540701d3d013
                            [hex] => 0014c4520316f8d1dee0a19acde950d0540701d3d013
                            [reqSigs] => 1
                            [type] => witness_v0_keyhash
                            [addresses] => Array
                                (
                                    [0] => tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep
                                )

                        )

                )

        )

    [hex] => 02000000000101be3977a2761996cb71f48ee332846ac59d5402e1c37e709e6ae69db5c2ad39270000000000feffffff02ff76130000000000160014184337352372a5fa6218dd57ea1057305d39c77dd209efaf01000000160014c4520316f8d1dee0a19acde950d0540701d3d01302473044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e0121036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296ac8ec1f00
    [blockhash] => 00000000000000d299298de1cfb14276667b7d55e8a1cf43c9dc825babadf058
    [confirmations] => 1420
    [time] => 1631039256
    [blocktime] => 1631039256
)

```

by looking at blockchain i know which address it SHOULD take (tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep)...but when i use php and check for last key in array its not always the correct address in other transactions as sometimes the address i need is in a different index(same array tho just different index).

How can i programmatically determine the right address consistently?

**Tags:** bitcoin-cli, php

> **Comments:**
> **username_1**: Bitcoin transactions do not have a well-defined "from" address. Transactions have inputs, and each of those inputs may or may not have an identifiable address the spent coins were previously assigned to. The result is zero, one, or more "from" addresses, but these don't correspond to any real-world property; in particular, they cannot be used to identify the sender, or to find a refund address. Multiple people can (and do) sometimes jointly produce a single transaction that moves funds belonging to all of them, for example.  
> **username_0**: agreed but not the question i was asking. i provided the txid to pull the information regarding the from address. please reread it again i have already explained it,  
> **username_1**: Yes, I'm not trying to answer your question, only pointing out that what you're trying to do is meaningless. If someone wants to answer your actual question, they can.  
> **username_0**: its not meaningless when you are trying to build an explorer. Its helpful to know what address the coins came from.  
> **username_0**: https://www.blockchain.com/btc-testnet/block/000000000000003f69d7c8cb4943df2d7a273883eb72024f3305122ed2dc7271?page=1  Blockchain uses it and i understand it fine. Ofcourse there is no from and to its vin and vout. If you looked at the code you would know that i understand this.  

---

## 0 answers

