<p>I've been working on this as a part of a bigger Bitcoin tools Python 2 library for a while. You can find it on <a href="https://github.com/sr-gi/bitcoin_tools" rel="nofollow noreferrer">github</a>.</p>

<p>You can run the code on <a href="https://github.com/sr-gi/bitcoin_tools/blob/dev/bitcoin_tools/ldb_parser.py" rel="nofollow noreferrer">ldb_parser.py</a> which will give a txt with all the UTXOs from the chainstate parsed in a json. (Notice that this is a huge amount of data.)</p>

<p>Then you can call the <a href="https://github.com/sr-gi/bitcoin_tools/blob/dev/bitcoin_tools/utils.py#L333" rel="nofollow noreferrer">decode_utxo</a> function using the data from the json and store the result in a file to analyze it. (This file will be even bigger!).</p>

<p>Here you have an example (once you have run ldb_parser.py):</p>

<pre><code>from bitcoin_tools.utils import load_conf_file, decode_utxo
from json import loads, dumps

fin_name = "utxos.txt"
fout_name = "parsed_utxos.txt"

# Load config file
cfg = load_conf_file()

fin = open(cfg.data_path + fin_name, 'r')
# Output file
fout = open(cfg.data_path + fout_name, 'w')

for line in fin:
    data = loads(line[:-1])
    utxo = decode_utxo(data["value"])

    fout.write(dumps(utxo) + '\n')

fout.close()
</code></pre>

<p>And each line you will get in parsed_utxos.txt look like:</p>

<pre><code>{"coinbase": 0, "version": 1, "outs": [{"index": 1, "amount": 14250000, "out_type": 0, "data": "865e218ff25929eee880e0e3b6f95280b2d05443"}], "height": 468349}
{"coinbase": 0, "version": 1, "outs": [{"index": 0, "amount": 132000, "out_type": 1, "data": "0b2a00367244680f6da18acd861a08f0a89cb3b4"}], "height": 449294}
{"coinbase": 0, "version": 1, "outs": [{"index": 1, "amount": 2423800, "out_type": 1, "data": "7f172a63c49c5d03e3307d432bd6b784b69d0e0d"}, {"index": 2, "amount": 10000000, "out_type": 1, "data": "1d0c4b60e8270f9b6ca5f167f08a5466a0cee565"}], "height": 474328}
...
</code></pre>

<p>Where each entry in <code>outs</code> in an output and, and <code>data</code> corresponds to the transaction data (hash160 of the address for P2PKH transactions).</p>

<p>Now, you have to take into account some considerations:</p>

<p>Not every single UTXO is a P2PKH, so what you will find in <code>data</code> depends on the <code>out_type</code> field.</p>

<p><code>out_types</code> <code>0</code> and <code>1</code> correspond to P2PKH and P2SH respectively, and will have 20 byte of data (hash160 of the public key for P2PKH and scriptHash for P2SH).</p>

<p><code>out_types</code> <code>2,3,4</code> and <code>5</code> correspond to P2PK outputs, and will contain 33 bytes of data (1 byte for the type of public key and 32 bytes for the actual key).</p>

<p>Finally, any other <code>out_type</code> will imply that the data hold by the UTXO is not compressed, and the value will correspond to the data size + the number of special scripts (<code>nSpecialScripts</code>) which is currently 6). This is the case of P2MS transactions and non-std transactions.</p>

<p>All this has been directly extracted from the Bitcoin Core source code.</p>

<p>Finally, notice that in order to use the library you will need to install the python dependencies in requeriments.txt and create a conf.py file to set your chainstate path and data path (or modify the code to not use the config file).</p>

CC BY-SA 3.0
2017-08-30T16:34:56.777Z
I'm working on merging all this to master branch (it's currently on dev), so links may vary in the future).
sr_gi
CC BY-SA 3.0
2017-08-31T04:49:41.587Z
Note that the database format for the UTXO set changes in Bitcoin Core 0.15 (it becomes easier).
Pieter Wuille
CC BY-SA 3.0
2017-08-31T13:50:22.573Z
@PieterWuille Thank you for pointing this out, I'll update the code.
sr_gi
CC BY-SA 3.0
2017-09-03T23:42:26.547Z
@sr-gi you update the code?
Denis Leonov
CC BY-SA 3.0
2017-09-04T01:22:17.993Z
Not yet, but it should definitely work for any version before 0.15.
sr_gi
CC BY-SA 3.0
2017-11-04T16:02:40.533Z
@Denis the code has been updated with the decoder for v.015. It's part of the dev branch, and will be merged to master during the next week.
sr_gi
CC BY-SA 3.0
2017-11-06T07:32:16.660Z
@sr-gi please tell where is calculated and stored utxo set in your last update?
Denis Leonov
CC BY-SA 3.0
2017-11-06T15:47:45.957Z
You can find it under dev branch (it will be merged with master along this week). The decoding function for 0.15 is here: https://github.com/sr-gi/bitcoin_tools/blob/dev/bitcoin_tools/analysis/status/utils.py#L178-L263
sr_gi
CC BY-SA 3.0
2017-08-30T16:33:08.103Z
2017-08-30T16:33:08.103Z
sr_gi
<p>The UTXO set is stored in the <code>chainstate</code> folder. It is stored in LevelDB databases which may do some optimizations which may make the actual data on disk difficult to parse (e.g. some compression). If you want to read that data, you should modify Bitcoin Core to give it to you instead of trying to read it from files on disk. You could also open the databases in a LevelDB viewer, but that could corrupt your databases and is otherwise not recommended.</p>

CC BY-SA 3.0
2017-08-30T19:17:52.897Z
would you propose any leveldb viewer?
Badr Bellaj
CC BY-SA 3.0
2017-08-30T15:22:38.520Z
2017-08-30T15:22:38.520Z
Ava Chow
<p>How to get a list of unspent transactions outputs (UTXO) from local files?</p>

<p>Where is stored this data? In what files this data is fully present?</p>

<p>I understand that this list is in "chainstate". But may be there is a more secure way to get it?</p>

<p>I try to extract it from the local files which Bitcoin Client dumps on disk.</p>

<p>I think there would be a "Crypto-proof UTXO set".</p>

<blockchain><bitcoin-core><bitcoin-core-development><utxo>
Getting a crypto proof UTXO set
Denis Leonov
# Getting a crypto proof UTXO set

(Asked by: username_0 on 2017-07-14)

How to get a list of unspent transactions outputs (UTXO) from local files?

Where is stored this data? In what files this data is fully present?

I understand that this list is in "chainstate". But may be there is a more secure way to get it?

I try to extract it from the local files which Bitcoin Client dumps on disk.

I think there would be a "Crypto-proof UTXO set".

**Tags:** blockchain, bitcoin-core, bitcoin-core-development, utxo


---

## 2 answers

### Answer by username_1 on 2017-08-30. Score: 2 | Accepted answer

I've been working on this as a part of a bigger Bitcoin tools Python 2 library for a while. You can find it on github.
You can run the code on ldb_parser.py which will give a txt with all the UTXOs from the chainstate parsed in a json. (Notice that this is a huge amount of data.)
Then you can call the decode_utxo function using the data from the json and store the result in a file to analyze it. (This file will be even bigger!).
Here you have an example (once you have run ldb_parser.py):
from bitcoin_tools.utils import load_conf_file, decode_utxo
from json import loads, dumps

fin_name = "utxos.txt"
fout_name = "parsed_utxos.txt"

# Load config file
cfg = load_conf_file()

fin = open(cfg.data_path + fin_name, 'r')
# Output file
fout = open(cfg.data_path + fout_name, 'w')

for line in fin:
    data = loads(line[:-1])
    utxo = decode_utxo(data["value"])

    fout.write(dumps(utxo) + '\n')

fout.close()

And each line you will get in parsed_utxos.txt look like:
{"coinbase": 0, "version": 1, "outs": [{"index": 1, "amount": 14250000, "out_type": 0, "data": "865e218ff25929eee880e0e3b6f95280b2d05443"}], "height": 468349}
{"coinbase": 0, "version": 1, "outs": [{"index": 0, "amount": 132000, "out_type": 1, "data": "0b2a00367244680f6da18acd861a08f0a89cb3b4"}], "height": 449294}
{"coinbase": 0, "version": 1, "outs": [{"index": 1, "amount": 2423800, "out_type": 1, "data": "7f172a63c49c5d03e3307d432bd6b784b69d0e0d"}, {"index": 2, "amount": 10000000, "out_type": 1, "data": "1d0c4b60e8270f9b6ca5f167f08a5466a0cee565"}], "height": 474328}
...

Where each entry in outs in an output and, and data corresponds to the transaction data (hash160 of the address for P2PKH transactions).
Now, you have to take into account some considerations:
Not every single UTXO is a P2PKH, so what you will find in data depends on the out_type field.
out_types 0 and 1 correspond to P2PKH and P2SH respectively, and will have 20 byte of data (hash160 of the public key for P2PKH and scriptHash for P2SH).
out_types 2,3,4 and 5 correspond to P2PK outputs, and will contain 33 bytes of data (1 byte for the type of public key and 32 bytes for the actual key).
Finally, any other out_type will imply that the data hold by the UTXO is not compressed, and the value will correspond to the data size + the number of special scripts (nSpecialScripts) which is currently 6). This is the case of P2MS transactions and non-std transactions.
All this has been directly extracted from the Bitcoin Core source code.
Finally, notice that in order to use the library you will need to install the python dependencies in requeriments.txt and create a conf.py file to set your chainstate path and data path (or modify the code to not use the config file).

> **Comments:**
> **username_1**: I'm working on merging all this to master branch (it's currently on dev), so links may vary in the future).  
> **username_2**: Note that the database format for the UTXO set changes in Bitcoin Core 0.15 (it becomes easier).  
> **username_1**: @PieterWuille Thank you for pointing this out, I'll update the code.  
> **username_0**: @sr-gi you update the code?  
> **username_1**: Not yet, but it should definitely work for any version before 0.15.  
> **username_1**: @Denis the code has been updated with the decoder for v.015. It's part of the dev branch, and will be merged to master during the next week.  
> **username_0**: @sr-gi please tell where is calculated and stored utxo set in your last update?  
> **username_1**: You can find it under dev branch (it will be merged with master along this week). The decoding function for 0.15 is here: https://github.com/sr-gi/bitcoin_tools/blob/dev/bitcoin_tools/analysis/status/utils.py#L178-L263  

---

### Answer by username_3 on 2017-08-30. Score: 2

The UTXO set is stored in the `chainstate` folder. It is stored in LevelDB databases which may do some optimizations which may make the actual data on disk difficult to parse (e.g. some compression). If you want to read that data, you should modify Bitcoin Core to give it to you instead of trying to read it from files on disk. You could also open the databases in a LevelDB viewer, but that could corrupt your databases and is otherwise not recommended.

> **Comments:**
> **username_4**: would you propose any leveldb viewer?  

---

