In raw transaction, bitcoin using bytes of the public key, like:
Technically the raw transaction contains the Script, which usually is written with Bitcoin script that is a stack-based language. It can contain the PubKey in the old scripts, like P2PK but the community is making a big effort to move the bitcoin users to use the script that contains the hash of the public key. The script like Pay-to-Public-Key-Hash (P2PKH) or pay-to-witness-public-key-hash (P2WPKH).
from the script, it is possible to calculate the address but not vice versa.
In conclusion, all the addresses have a conversion like:
- P2PK, P2PKH: Start with 1.
- P2SH: start with 3.
- P2WSH, P2WPKH: Start with bc or with 3.
At this point, the answer should contain a resume of all the types of script and how the script is built, and also an introduction to the not standard script that is used extensively from the lightning network protocol.
But I think a very good summary is inside these two chapters of the Bitcoin book 'Transactions' and 'Advanced Transactions and Scripting', I can't do better
I'm not a fan of theory, maybe you can use also this code example to understand how the script is converted on a Bitcoin address.
Decompiler Bitcoin Script.
it is only an example of the conversion that could be done.
The usage of the example code is described inside the bash script run-decompiler.sh
An example with the following block is the following
With the following result
----------------------------------------| Results |-----------------------------------
### Script PAY TO PUBLIC KEY HASH ###
Raw Script PubKey: 76a914536ffa992491508dca0354e52f32a3a7a679a53a88ac
OP_DUP OP_HASH160 536ffa992491508dca0354e52f32a3a7a679a53a OP_EQUALVERIFY OP_CHECKSIG
The address: 18cBEMRxXHqzWWCxZNtU91F5sbUNKhL5PX
The Hash160 pubkey: 536ffa992491508dca0354e52f32a3a7a679a53a
The P2PSH address: 39JC9tvQ5CANbfuPgUZ4Zdc227m5uQnMRB
See the result here https://blockstream.info/address/18cBEMRxXHqzWWCxZNtU91F5sbUNKhL5PX
Please consider this example as an example of code, I developed this to understand how de script works. Can contains error. I will work to integrate this in my project and I will maintain this post update.
To answer at your comment that is
Thanks. I use you the code linked, the the result address codepad.org/nRJhvMC3 does not match the expected address blockchain.com/btc/block/1 So what's the usage of this Decompiler?
The decompiler takes the
hex of the script and try to calculate the corresponding address if it is a standard script.