3
1
I'm writing a small script that will dump the utxo database to a text file. As far as I'm aware, these are the most common script patterns indicated by the type field inside each value:
e.g. value: b98276a2ec7700cbc2986ff9aed6825920aece14aa6f5382ca5580
<----><----><><-------------------------------------->
/ / \ \
height/coinbase value type script data
0x00 = P2PKH (hash160 public key)
0x01 = P2SH (hash160 script)
0x02 = P2PK
0x03 = P2PK
0x04 = P2PK (uncompressed)
0x05 = P2PK (uncompressed)
It seems as though the script type is there so that you only need to store the minimal amount of script data inside the database (e.g. the unique public keys and script hashes inside P2PK, P2PKH, and P2SH).
Anyway, would I be correct in assuming that you could only get an address from script types 0 and 1 (by base58 encoding the script data)?
In other words, the chainstate leveldb does not include any witness data to allow you get addresses for utxos using P2WPKH and P2WSH scripts?
EDIT: Here's the finished tool: https://github.com/in3rsha/bitcoin-utxo-dump
Ha, had the completely the wrong idea with where I was going there. Thanks for clearing that up. – inersha – 2019-03-29T18:52:25.013
Seeing as P2WPKH and P2WSH scripts are stored without special compression, what does the script
typefield indicate for these entries? From a quick look it appears that P2WPKH are type28and P2WSH are type40. I'm not sure what the number indicates when it's greater than5. – inersha – 2019-03-30T11:35:51.113It seems as though if the
typeis greater than5, then it indicates the size of the upcoming script. Although the size given is actually 6 bytes greater, so you should subtract 6 to get the actual size of the upcoming script. – inersha – 2019-03-30T12:09:35.583A brief description of the compression can be found here: https://github.com/bitcoin/bitcoin/blob/master/src/compressor.h#L25. There are only 6 types,
– Andrew Chow – 2019-03-30T16:11:46.1670x00is P2PKH,0x01is P2SH,0x02and0x03are compressed pubkeys. Those bytes are actually part of the pubkey itself.0x04and0x05are uncompressed pubkeys. After that, the first byte is thescript size + 6, so the real size isbyte - 6.Thank you. Why is it that
0x05is used for uncompressed public keys, seeing as they always start with0x04? Why not just use0x04for all uncompressed public keys? – inersha – 2019-03-30T16:17:59.603The uncompressed pubkeys are compressed when they are added to the db. 0x04 and 0x05 are used to indicate that the key is supposed to be uncompressed and those indicate whether the y value is even or odd so that the full uncompressed key can be retrieved. It's basically the same thing as for normal pubkey compression, just with different values to indicate that the script uses an uncompressed key and not a compressed one. – Andrew Chow – 2019-03-30T19:14:21.290
Gotcha, thank you very much, you have explained everything! – inersha – 2019-03-30T19:28:56.183