0

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] =&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

)

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] =&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

)

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?

Derek Hill
  • 23
  • 1
  • 4
  • 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. – Pieter Wuille Sep 13 '21 at 21:12
  • 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 Sep 13 '21 at 21:16
  • 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. – Pieter Wuille Sep 13 '21 at 21:22
  • its not meaningless when you are trying to build an explorer. Its helpful to know what address the coins came from. – Derek Hill Sep 13 '21 at 21:23
  • @DerekHill FROM and TO are meaningless. Inputs, Outputs and associated addresses are okay. I have tried answering your question. Hope you don't use FROM and TO in the block explorer else it will be a misleading block explorer. –  Sep 13 '21 at 22:07
  • 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 Sep 13 '21 at 23:00

1 Answers1

1

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

There is no 'from' address in any transaction as mentioned by Sipa in the comments.

Is it possible to assign a single "from" and a single "to" address to Bitcoin transactions?

but when i spit out the transaction for the tx id ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6

How can i programmatically determine the right address consistently?

I will share steps to check inputs and outputs for this transaction using Bitcoin Core:

$ bitcoin-cli getrawtransaction 0d45dacc0e50066a55c13b54cdcd6e365bb90be3a492c55bf4200b01d02fd748

020000000001015ced69c0d591aa8616d5be813dca34382be7ff86e8215d6c1dfb36226b912e120100000000feffffff02cf141d00000000001600142cda63f440de6ab858cf2550daf3b25522d6c66f27b5d1af01000000160014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab0247304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401210258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280caec1f00
$ bitcoin-cli decoderawtransaction 020000000001015ced69c0d591aa8616d5be813dca34382be7ff86e8215d6c1dfb36226b912e120100000000feffffff02cf141d00000000001600142cda63f440de6ab858cf2550daf3b25522d6c66f27b5d1af01000000160014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab0247304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401210258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280caec1f00
{
  "txid": "ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6",
  "hash": "b388c750f3d15f1fcdb320fbc3398a65a34a3eb6933ca1738729f22a4a8a5924",
  "version": 2,
  "size": 222,
  "vsize": 141,
  "weight": 561,
  "locktime": 2092234,
  "vin": [
    {
      "txid": "122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c",
      "vout": 1,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "txinwitness": [
        "304402201c4ff3fa6d7d212760db2c03e39bc824756c10bbac77a10db7665c383e16600b0220146f62d2c0a28d335ecad0f38227259ead389d409259f3af693628443695bbf401",
        "0258471daabd690819d6cbb34b8ca75f98ba24604960301ce7c90cc5131ab07280"
      ],
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 0.01905871,
      "n": 0,
      "scriptPubKey": {
        "asm": "0 2cda63f440de6ab858cf2550daf3b25522d6c66f",
        "hex": "00142cda63f440de6ab858cf2550daf3b25522d6c66f",
        "address": "tb1q9ndx8azqme4tskx0y4gd4uaj253dd3n0386xkn",
        "type": "witness_v0_keyhash"
      }
    },
    {
      "value": 72.44723495,
      "n": 1,
      "scriptPubKey": {
        "asm": "0 e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab",
        "hex": "0014e179ad3cb0d3bc53cba564cc2e05bc2dc18de1ab",
        "address": "tb1qu9u6609s6w798ja9vnxzupdu9hqcmcdtvm87gx",
        "type": "witness_v0_keyhash"
      }
    }
  ]
}
Inputs Outputs
"txid": "122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c", "vout": 1 tb1q9ndx8azqme4tskx0y4gd4uaj253dd3n0386xkn (0.01905871)
tb1qu9u6609s6w798ja9vnxzupdu9hqcmcdtvm87gx (72.44723495)

To get more information about input being spent in this transaction:

$ bitcoin-cli getrawtransaction 122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c

02000000000101be3977a2761996cb71f48ee332846ac59d5402e1c37e709e6ae69db5c2ad39270000000000feffffff02ff76130000000000160014184337352372a5fa6218dd57ea1057305d39c77dd209efaf01000000160014c4520316f8d1dee0a19acde950d0540701d3d01302473044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e0121036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296ac8ec1f00
$ bitcoin-cli decoderawtransaction 02000000000101be3977a2761996cb71f48ee332846ac59d5402e1c37e709e6ae69db5c2ad39270000000000feffffff02ff76130000000000160014184337352372a5fa6218dd57ea1057305d39c77dd209efaf01000000160014c4520316f8d1dee0a19acde950d0540701d3d01302473044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e0121036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296ac8ec1f00

Check vout:1 in the results below:

{
  "txid": "122e916b2236fb1d6c5d21e886ffe72b3834ca3d81bed51686aa91d5c069ed5c",
  "hash": "5fb52307d771c7dadf9f06006340a0acb5ff9ebe15d215d98f08e749d0ee2e59",
  "version": 2,
  "size": 222,
  "vsize": 141,
  "weight": 561,
  "locktime": 2092232,
  "vin": [
    {
      "txid": "2739adc2b59de66a9e707ec3e102549dc56a8432e38ef471cb961976a27739be",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "txinwitness": [
        "3044022077216db352b3024fbb92ff798b618dc3a45fe727077872d2ee5e699ea8fc839d02207dde0f94b0d2cd8900dda6c189134c5d7614278a582e2e1f131c0e16be86534e01",
        "036e8d75e18796979cdfebcc82d44930d5265ad3820156932194bd7cda5192296a"
      ],
      "sequence": 4294967294
    }
  ],
  "vout": [
    {
      "value": 0.01275647,
      "n": 0,
      "scriptPubKey": {
        "asm": "0 184337352372a5fa6218dd57ea1057305d39c77d",
        "hex": "0014184337352372a5fa6218dd57ea1057305d39c77d",
        "address": "tb1qrppnwdfrw2jl5cscm4t75yzhxpwnn3mamh6ff8",
        "type": "witness_v0_keyhash"
      }
    },
    {
      "value": 72.46645714,
      "n": 1,
      "scriptPubKey": {
        "asm": "0 c4520316f8d1dee0a19acde950d0540701d3d013",
        "hex": "0014c4520316f8d1dee0a19acde950d0540701d3d013",
        "address": "tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep",
        "type": "witness_v0_keyhash"
      }
    }
  ]
}

We have all the information about inputs and outputs for this transaction similar to most of the block explorers:

Inputs Outputs
tb1qc3fqx9hc680wpgv6eh54p5z5quqa85qn35meep (72.46645714) tb1q9ndx8azqme4tskx0y4gd4uaj253dd3n0386xkn (0.01905871)
tb1qu9u6609s6w798ja9vnxzupdu9hqcmcdtvm87gx (72.44723495)

You can do everything using Bitcoin Core, JSON-RPC and PHP. Example:

<?php

$curl = curl_init();

curl_setopt_array($curl, array( CURLOPT_URL => 'http://localhost:18222/', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS =>'{"jsonrpc": "1.0", "id": "curltest", "method": "getrawtransaction", "params": ["ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6"]}', CURLOPT_HTTPHEADER => array( 'Authorization: Basic dXNlcjM6cGFzc3dvcmQz', 'Content-Type: text/plain' ), ));

$response = curl_exec($curl);

curl_close($curl); echo $response;

  • i understand all of that but that wasnt the issue the issue is that sometimes the vout addresses of the address(pulled by vin txid) that sent it. the issue is that sometimes the address is indexed at [0] or [1] and i cant figure out code wise to determine which address is the one i need to display. – Derek Hill Sep 13 '21 at 22:58
  • I shared the steps for transaction id shared by you in the question. I can answer the confusion about inputs if you give relevant example. –  Sep 13 '21 at 23:02
  • check these 2 transactions.

    ac709fc580430d51ab741e339edd184c349306c9881f95085851eef0652b1fb6

    99daea8f3d8179cb539b99a00bc0458a4070b1a94de873f51e05b34ccc6afc90

    the first tx the right vout address would be in vout->[1] as the array index. but the second hash the right address is in vout[0].

    How can i design it so it knows the right address to display.

    – Derek Hill Sep 13 '21 at 23:31
  • and yes i am using curl/shell_exec. – Derek Hill Sep 13 '21 at 23:41
  • You will get index number(s) for inputs in decoderawtransaction. So just need to read the JSON response and save [vin][vout] or use in the code. Doesn't matter if its 0 or 1. Save response in variable and use it accordingly. I think APIs that you are using also provide this although I would prefer using my own node if creating an explorer. If issue is related to JSON and PHP, https://stackoverflow.com would be a better place to ask related questions. –  Sep 13 '21 at 23:45
  • i am using my own node. i use a php function to bootleg results by curls. And i understand what you are saying its just that when you check the vout what signifies that its the right address. Maybe for the 122....hash tx its in the second entry in vout, but the other hash its the first entry...so...how do i know which one is the right address code wise. – Derek Hill Sep 13 '21 at 23:59
  • Sorry I thought you are using blockchain.com or some other API –  Sep 14 '21 at 00:20
  • no i was using blockchain.com as a reference to verify my data is correct., fees, addresses, block info etc... – Derek Hill Sep 14 '21 at 01:25
  • anything to offer? – Derek Hill Sep 14 '21 at 20:34
  • @DerekHill Can we discuss this in https://chat.stackexchange.com/rooms/8089/mempool ? –  Sep 14 '21 at 22:25