Script Decoding Made Easy

5 replies 82 views
b3ar2015Member
Posts: 23 · Reputation: 191
#1Oct 9, 2022, 11:31 PM
Hey everyone, Super excited to join this community. I want to build a program that can decode any bitcoin UTXO script (even spent ones, not an issue here). I'm on the hunt for a clear, comprehensive, and current description of a script payload. I’ve searched around but haven’t found anything that feels totally updated or straightforward enough. Also, if anyone could outline the basic steps to decode a payload, I’d really appreciate it. That way, I could give the implementation a shot myself. Here are some of the challenges I’m running into: - I get that there are tons of script types, but is the spec general enough to let me whip up a simple implementation? - How do I take a public key and convert it into a base58 bitcoin address? - Is it actually feasible to have a one-size-fits-all method to decode every possible script? I mainly code in Python, so if you know of any libraries that fit these needs (especially with the latest bitcoin protocol changes like native segwit), that’d be fantastic. Thanks for taking the time to read this. Thanks for any help!
6 Reply Quote Share
im_apeHero Member
Posts: 629 · Reputation: 3824
#2Oct 11, 2022, 11:48 AM
Start here: https://en.bitcoin.it/wiki/Script Then read the interpreter class https://github.com/bitcoin/bitcoin/blob/master/src/script/interpreter.cpp The evaluation starts on line #L406 There are only about a handful of standard output scripts in UTXOs: If this is all you want, it is very easy to implement. You just have to know which byte represents which OP code and how to read the script as bytes. If you mean P2PKH address, then you take the public key (33 byte compressed is common but it can be 65 byte uncompressed) then compute its SHA256 hash then take that and compute its RIPEMD160 hash. Then add (prepend) the version byte to the start of it (version for MainNet is 0) then feed the resulting 33 bytes to the Base58 encoder that returns the result with a checksum. Yes but that is harder to implement since you'd have to implement the entire interpreter class I posted above.
6 Reply Quote Share
b3ar2015Member
Posts: 23 · Reputation: 191
#3Oct 11, 2022, 07:49 PM
Thank you sir for the very clear explanation, this is very much helpful and extremely appreciated. I realized that I have been trying to extract an address from a P2SH script. The transaction in question is https://live.blockcypher.com/btc/tx/bfa5b2de068fbb1b963e479138b3b5db0670f584d60d45cf4ee50a85b4e1f483/ If you decode it here https://live.blockcypher.com/btc/decodetx/ You get Now I’ve found this message: https://bitcointalk.org/index.php?topic=5265034.msg54884971#msg54884971 Also, some code on the internet: So P2PK script do not seem to be able to hold an address, which was confusing me totally. Actually, it seems like blockcypher is taking the public key, hash it using https://learnmeabitcoin.com/technical/keys/public-key/hash/#hash160-tool, add a null byte and base58 encode everything, which effectively gives from The value Not sure if this address has any practical usage
3 Reply Quote Share
im_apeHero Member
Posts: 629 · Reputation: 3824
#4Oct 12, 2022, 12:21 AM
That's a weird thing block explorers have been doing for as long as I remember. They convert P2PK outputs into P2PKH and show the balance locked in the P2PK script as the balance of the P2PKH address. Maybe its because they wanted to make searching easier since there is no human readable format (aka an address) defined for P2PK scripts; and since the private key is the same it make sense to some extent.
5 Reply Quote Share
the_bearMember
Posts: 24 · Reputation: 187
#5Oct 12, 2022, 02:42 AM
Imagine how many questions there will be "what is the address of Satoshi Nakamoto?"
1 Reply Quote Share
LuckyCoinLegendary
Posts: 832 · Reputation: 4795
#6Oct 12, 2022, 07:18 AM
ZPyWallet contains a module for decoding scripts here: https://github.com/ZenulAbidin/zpywallet/blob/master/zpywallet/transactions/decode.py It is almost self-contained, if you want it in a single file just copy and paste the relevant imports into one file. In the scripts/ folder you will also find decoding functions for every single opcode. Disclosure: I made this library. I have tested this part of the code, but there are some areas that are way outside this file that still need some unit testing. Feel free to use it if you want.
3 Reply Quote Share

Related topics