4

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.

2 Answers 2

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:

3
  • 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
    Dec 2, 2022 at 15:55
  • 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
    Dec 6, 2022 at 8:50
  • 1
    The txindex configuration option will cause Bitcoin Core to maintain an index of txids. You can use its RPC API - maybe gettxout (which I guess would work for UTXOs without txindex) Dec 6, 2022 at 10:57
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/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)

requirements.txt

#git+https://github.com/alecalve/python-bitcoin-blockchain-parser.git@c06f420995b345c9a193c8be6e0916eb70335863
git+https://github.com/cirosantilli/[email protected]
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.