<blockquote>
<p>Is it possible to get the address and the value of the input from the blk.dat files?</p>
</blockquote>
<p>Yes. It is possible. After all, that's effectively what blockchain explorers do.</p>
<p>I would try to do something like the following</p>
<ul>
<li>Use the previous transaction id to look up and obtain the contents of the previous transaction.</li>
<li>Use the index to retrieve the specific output of the previous transaction being used as an input in the current transaction</li>
<li>Obtain the value from that output.</li>
<li>Derive an address from the locking script (AKA ScriptPubKey) of that output.</li>
</ul>
<hr />
<p>Potentially useful:</p>
<ul>
<li><a href="https://bitcoin.stackexchange.com/q/853/13866">What do the different .dat files contain?</a></li>
<li><a href="https://bitcoin.stackexchange.com/q/92957/13866">How to parse transaction script to address the correct way?</a></li>
<li><a href="https://bitcoin.stackexchange.com/q/100508/13866">Can you break down what data is encoded into a bech32 address?</a></li>
<li><a href="https://bitcoin.stackexchange.com/q/49596/13866">How to get addresses from scriptPubKey in segwit transactions?</a></li>
<li><a href="https://bitcoin.stackexchange.com/q/113631/13866">In what format does a block store the transaction data?</a></li>
<li><a href="https://bitcoin.stackexchange.com/q/113697/13866">What are the parts of a Bitcoin transaction in segwit format?</a></li>
</ul>

CC BY-SA 4.0
2022-12-02T15:55:34.957Z
I do not really have a full answer, but maybe to add to RGB's: Yes, but a transaction itself only identifies the outpoint of the UTXO it spends in an input. So, you need to find that previous transaction to read the output data there. It would probably help to run the full node with `txindex` to that end.
Murch
CC BY-SA 4.0
2022-12-06T08:50:33.073Z
It's a bit of a mystery to me how I can look up a specific transaction from raw data. I can use the txid of the input with a web api and get the transaction contents but this has proved to be a bit slow due to exceptions raised from too many requests to the server.
Andreas
CC BY-SA 4.0
2022-12-06T10:57:17.210Z
The `txindex` configuration option will cause Bitcoin Core to maintain an index of txids. You can use its RPC API - maybe [`gettxout`](https://developer.bitcoin.org/reference/rpc/gettxout.html) (which I guess would work for UTXOs without txindex)
RedGrittyBrick
CC BY-SA 4.0
2022-12-02T14:56:41.330Z
2022-12-02T15:02:28.297Z
2022-12-02T15:02:28.297Z
RedGrittyBrick
<p>E.g. in Python with <a href="https://github.com/alecalve/python-bitcoin-blockchain-parser" rel="nofollow noreferrer">https://github.com/alecalve/python-bitcoin-blockchain-parser</a>:</p>
<pre><code>#!/usr/bin/env python3

# stdlib
import os
import sys

# Third party
from bitcoin.core.script import *
import blockchain_parser.blockchain

datadir = '/home/ciro/snap/bitcoin-core/common/.bitcoin/blocks'
blockchain = blockchain_parser.blockchain.Blockchain(datadir)
for block in blockchain.get_ordered_blocks(
    os.path.join(datadir, 'index'),
    cache='cache.pkl',
):
    for txno, tx in enumerate(block.transactions):
        print(tx.txid)
        print('inputs')
        for inp in tx.inputs:
            print(inp.sequence_number)
            print(inp.witnesses)
            print(inp.script)
            print(inp.transaction_index)
</code></pre>
<p>requirements.txt</p>
<pre><code>#git+https://github.com/alecalve/python-bitcoin-blockchain-parser.git@c06f420995b345c9a193c8be6e0916eb70335863
git+https://github.com/cirosantilli/python-bitcoin-blockchain-parser.git@plyvel-1.5.1
requests==2.31.0
</code></pre>
<p>I needed a small patch on my fork: <a href="https://github.com/alecalve/python-bitcoin-blockchain-parser/pull/112" rel="nofollow noreferrer">https://github.com/alecalve/python-bitcoin-blockchain-parser/pull/112</a></p>
<p>Sample output:</p>
<pre><code>tx 63f9df910b54c60a391e0842cd485c455f9c7fb7c7c79eb07706b2c0ef7ebe93
4294967295
[]
Script(ffff001d 8101)
4294967295
tx 58d089afb985c772232e17dec60c9eed5c281d985cace881fb1f6b34c43e7500
4294967295
[]
Script(ffff001d 5e)
4294967295
</code></pre>
<p>At <a href="https://github.com/cirosantilli/bitcoin-strings-with-txids/blob/b560fc0bcfb6f0f64a256bdcd15b48810262dc67/main.py" rel="nofollow noreferrer">https://github.com/cirosantilli/bitcoin-strings-with-txids/blob/b560fc0bcfb6f0f64a256bdcd15b48810262dc67/main.py</a> I used some more of their functionality for my purposes which may serve as further reference.</p>
<p>I haven't researched a lot before choosing this one, but it kind of works. Sometimes I wonder if there's a faster one though in some compiled language, but this one gets there eventually.</p>

CC BY-SA 4.0
2024-02-13T22:55:12.653Z
2024-02-13T22:55:12.653Z
Ciro Santilli OurBigBook.com
<p>I'm developing a script that processes blk.dat files from bitcoin core. So far, I am able to retrieve the following attributes for transaction inputs:</p>
<pre><code>&quot;vin&quot;,
&quot;index&quot;,
&quot;sigScript&quot;,
&quot;sequence&quot;,
&quot;witness&quot;
</code></pre>
<p>Is it possible to get the address and the value of the input from the blk.dat files?</p>
<p>In case you need more info or the question is not clear, please let me know.</p>

<transactions><address><block><python><transaction-input>
Is it possible to retrieve input addresses and input values of bitcoin transactions from raw blk.dat files?
Andreas
# Is it possible to retrieve input addresses and input values of bitcoin transactions from raw blk.dat files?

(Asked by: username_0 on 2022-12-02)

I'm developing a script that processes blk.dat files from bitcoin core. So far, I am able to retrieve the following attributes for transaction inputs:

```
"vin",
"index",
"sigScript",
"sequence",
"witness"

```

Is it possible to get the address and the value of the input from the blk.dat files?

In case you need more info or the question is not clear, please let me know.

**Tags:** transactions, address, block, python, transaction-input


---

## 2 answers

### Answer by username_1 on 2022-12-02. Score: 3

> Is it possible to get the address and the value of the input from the blk.dat files?

Yes. It is possible. After all, that's effectively what blockchain explorers do.

I would try to do something like the following

* Use the previous transaction id to look up and obtain the contents of the previous transaction.
* Use the index to retrieve the specific output of the previous transaction being used as an input in the current transaction
* Obtain the value from that output.
* Derive an address from the locking script (AKA ScriptPubKey) of that output.

---

Potentially useful:

* [What do the different .dat files contain?](https://bitcoin.stackexchange.com/q/853/13866)
* [How to parse transaction script to address the correct way?](https://bitcoin.stackexchange.com/q/92957/13866)
* [Can you break down what data is encoded into a bech32 address?](https://bitcoin.stackexchange.com/q/100508/13866)
* [How to get addresses from scriptPubKey in segwit transactions?](https://bitcoin.stackexchange.com/q/49596/13866)
* [In what format does a block store the transaction data?](https://bitcoin.stackexchange.com/q/113631/13866)
* [What are the parts of a Bitcoin transaction in segwit format?](https://bitcoin.stackexchange.com/q/113697/13866)

> **Comments:**
> **username_2**: I do not really have a full answer, but maybe to add to RGB's: Yes, but a transaction itself only identifies the outpoint of the UTXO it spends in an input. So, you need to find that previous transaction to read the output data there. It would probably help to run the full node with `txindex` to that end.  
> **username_0**: It's a bit of a mystery to me how I can look up a specific transaction from raw data. I can use the txid of the input with a web api and get the transaction contents but this has proved to be a bit slow due to exceptions raised from too many requests to the server.  
> **username_1**: The `txindex` configuration option will cause Bitcoin Core to maintain an index of txids. You can use its RPC API - maybe [`gettxout`](https://developer.bitcoin.org/reference/rpc/gettxout.html) (which I guess would work for UTXOs without txindex)  

---

### Answer by username_3 on 2024-02-13. Score: 0

E.g. in Python with <https://github.com/alecalve/python-bitcoin-blockchain-parser>:

```
#!/usr/bin/env python3

# stdlib
import os
import sys

# Third party
from bitcoin.core.script import *
import blockchain_parser.blockchain

datadir = '/home/username_3/snap/bitcoin-core/common/.bitcoin/blocks'
blockchain = blockchain_parser.blockchain.Blockchain(datadir)
for block in blockchain.get_ordered_blocks(
    os.path.join(datadir, 'index'),
    cache='cache.pkl',
):
    for txno, tx in enumerate(block.transactions):
        print(tx.txid)
        print('inputs')
        for inp in tx.inputs:
            print(inp.sequence_number)
            print(inp.witnesses)
            print(inp.script)
            print(inp.transaction_index)

```

requirements.txt

```
#git+https://github.com/alecalve/python-bitcoin-blockchain-parser.git@c06f420995b345c9a193c8be6e0916eb70335863
git+https://github.com/cirosantilli/python-bitcoin-blockchain-parser.git@plyvel-1.5.1
requests==2.31.0

```

I needed a small patch on my fork: <https://github.com/alecalve/python-bitcoin-blockchain-parser/pull/112>

Sample output:

```
tx 63f9df910b54c60a391e0842cd485c455f9c7fb7c7c79eb07706b2c0ef7ebe93
4294967295
[]
Script(ffff001d 8101)
4294967295
tx 58d089afb985c772232e17dec60c9eed5c281d985cace881fb1f6b34c43e7500
4294967295
[]
Script(ffff001d 5e)
4294967295

```

At <https://github.com/cirosantilli/bitcoin-strings-with-txids/blob/b560fc0bcfb6f0f64a256bdcd15b48810262dc67/main.py> I used some more of their functionality for my purposes which may serve as further reference.

I haven't researched a lot before choosing this one, but it kind of works. Sometimes I wonder if there's a faster one though in some compiled language, but this one gets there eventually.


---

