Isn’t the raw hex transaction in hexadecimal? If so, how do you encode / decode other than the blockchain decoder?
As HCP has indicated...
It is NOT hex encoded ASCII. You can't just convert the hex values into ASCII character representations and expect to get anything useful out of it.
It is hex encoded binary data.
If you are aware that a single BYTE of data is represented with 2 hex characters, and you know how to convert from hexadecimal to decimal, then we can walk through that transaction you posted together to see exactly what is happening. Follow along...
The first 4 bytes (8 characters) are ALWAYS a transaction version number. In this case that is:
Note that bitcoin uses "little-endian" byte order, so converting those bytes to the format in which you might commonly see a hexadecimal number written we get 0x00000001. Converting that to decimal we see that this is a version 1 transaction type.
The next byte indicates the number of inputs in the transaction. In this case that is:
As a hexidecimal number that would be commonly be written as 0x02. Converting that to decimal we see that there are exactly 2 inputs in this transaction.
The next thing in the transaction is the inputs. Each input consists of:
- Transaction ID of the output being spent
- Offset of the output in that transaction
- Script length
- Txin-script (Also known as: scriptSig)
- sequence_no
So, we should have 2 sets of that information.
A bitcoin transaction ID is a 256 bit hash, so it is 32 bytes long. Therefore the next 32 bytes should be the transaction ID from the first input...
Remember that bitcoin uses little-endian byte order. Typically when transaction IDs are shown to humans they are placed in big-endian byte order, so we'll need to reverse the order of these bytes to see the transaction ID in the format that most people are generally familiar with...
Next is the index into that transaction of the output that is being spent as an input in this transaction. This is a 4 byte value.
Looks like the index value is 0
Following that we have the a value that indicates how long (in bytes) the script is:
Converting the value 0x8b from hex to decimal we see that the script will be the next 139 bytes.
Next we have the Txin-script of the first input:
If we want to analyze that script, we can break it into its component parts...
The first byte we see is:
Looking at the bitcoin scripting language, we see that this is an indication to push that quantity of the following bytes onto the script processing stack. Since 0x48 is 72 in decimal, the next 72 bytes will be pushed to the processing stack. In the case of the typical P2PKH address, this will end up being the digital signature. As such, we can see that the signature is represented in the transaction as:
The next byte we see is:
Looking at the bitcoin scripting language, we see that this is an indication to push that quantity of the following bytes onto the script processing stack. Since 0x41 is 65 in decimal the next 65 bytes will be pushed to the processing stack. In the case of the typical P2PKH address, this will end up being the public key. As such, we can see that the public key is represented in the transaction as:
We've now reached the end of the Txin-script details. Next comes the sequence_number which is always 4 bytes:
That's the end of the first input. The next byte therefore starts the next input...
Inputs start with a A bitcoin transaction ID. A transaction ID is a 256 bit hash, so it is 32 bytes long. Therefore the next 32 bytes should be the transaction ID from the second input...
Remember that bitcoin uses little-endian byte order. Typically when transaction IDs are shown to humans they are placed in big-endian byte order, so we'll need to reverse the order of these bytes to see the transaction ID in the format that most people are generally familiar with...
Next is the index into that transaction of the output that is being spent as an input in this transaction. This is a 4 byte value.
Looks like the index value is 0
Following that we have the a value that indicates how long (in bytes) the script is:
Converting the value 0x8b from hex to decimal we see that the script will be the next 139 bytes.
Next we have the Txin-script of the second input:
You can walk through the steps from the first Txin-script if you want to break this one apart, I'm not going to repeat that here.
Next comes the sequence_number which is always 4 bytes:
We've now reached the end of both inputs. The next part of the transaction is the outputs...
The next byte is an indication of the number of outputs in the transaction:
Converting this from hex to decimal, we see there will be 2 outputs.
Each output consists of:
- A value
- Script length
- A Txout-script (Also known as scriptPubKey)
The value is always 8 bytes. So the next 8 bytes is the value of the first output:
Converting this to big-endian byte order we get:
Converting 0x025317c0 to decimal we see that the value of the first output is 39000000 satoshi (or 0.39 BTC).
Next comes a value indicating how many bytes long the Txout-script is.
The next byte is:
Indicating that the script is 0x19 (which is decimal is 25) bytes long.
We can break this script into its components if we like. The first byte we see in the script is:
Looking at the bitcoin scripting language, we see that this is the OP_DUP code.
Next we see:
We can see that this is the OP_HASH160 code.
Next we see:
We can see in the scripting language that this is an indication to push that many bytes onto the processing stack. Converting the hex 0x14 to a decimal value we see that we need to push the next 20 bytes to the stack.
That would be these 20 bytes:
The next byte is:
We can see that this is the OP_EQUALVERIFY code.
Finally at the end of the script we see:
We can see that this is the OP_CHECKSIG code.
So, the 25 byte output script of this transaction is:
That's the end of the first output. The next byte therefore starts the next output...
As we saw earlier, the first thing in an output is an 8 byte value.
Converting this to big-endian byte order we get:
Converting 0x093d1cc0 to decimal we see that the value of the first output is 155000000 satoshi (or 1.55 BTC).
Next comes a value indicating how many bytes long the Txout-script is.
The next byte is:
Indicating that the script is 0x19 (which is decimal is 25) bytes long.
You can walk through the steps from the first Txout-script if you want to break this one apart, I'm not going to repeat that here.
Once we get past all the outputs, the only thing left in the transaction is a 4 byte lock_time. As we can see from your transaction:
The lock time is 0.
That's it. That's the entire transaction decoded. Hope this was helpful