Today Luke Asks:
In the previous videos, You spoke about scriptSig. Well, what’s it?
Nice question Luke, the scriptSig is an important part of Bitcoin’s transaction.
You can imagine a legacy transaction (not SegWit) like the image below:
- Version
- Inputs number (how many inputs)
- Input
- Outputs number
- Outputs number (how many outputs)
- locktime
The scriptSig is inside each input and it contains all elements in order to satisfy the linked UTXO. Pay attention please! In SegWit transaction, the scriptSig is empty: this is the way through wich the Bitcoin protocol solves the Transaction malleability!
In Action
Get a transaction from the mainnet blockchain
$ bitcoin-cli getrawtransaction c2297c9fefdd058e2ce5477868bcbf88bd4f860fbb552399053e3666687809bd 2
The transaction has one input and two outputs. Our focus is on the input part, inside the array vin.
"vin": [
{
"txid": "d44cbca5911e53322e14fe0617f078dd1f162a7dcb97f83690eac285ed7ebe80",
"vout": 1,
"scriptSig": {
"asm": "3044022059515b358d938d04c812177a2eefba52a4427b9e807c28538148e04edf042f3b022057e00e46acec7708b3087bfaadf25001ff449759b063aa9f39f5eb6606ceeef7[ALL] 04794dffa10783c305d72c44acc36003760a53c03a1e5529747a5ef7eef7c87c6c19ba26c7eee03ab6da9115d11bce3a46dd21aede86af19c3ee19eeb7f8d92732",
"hex": "473044022059515b358d938d04c812177a2eefba52a4427b9e807c28538148e04edf042f3b022057e00e46acec7708b3087bfaadf25001ff449759b063aa9f39f5eb6606ceeef7014104794dffa10783c305d72c44acc36003760a53c03a1e5529747a5ef7eef7c87c6c19ba26c7eee03ab6da9115d11bce3a46dd21aede86af19c3ee19eeb7f8d92732"
},
"sequence": 4294967295
}
],
Let’s analyze its hex (hexdecimal): it’s the scriptSig. and save it in an environment variable SCRIPTSIG.
SCRIPTSIG=473044022059515b358d938d04c812177a2eefba52a4427b9e807c28538148e04edf042f3b022057e00e46acec7708b3087bfaadf25001ff449759b063aa9f39f5eb6606ceeef7014104794dffa10783c305d72c44acc36003760a53c03a1e5529747a5ef7eef7c87c6c19ba26c7eee03ab6da9115d11bce3a46dd21aede86af19c3ee19eeb7f8d92732
We know that it’s a P2PKH transaction: that’s why we can search the digital signature and the public key. We should know the Bitcoin Script, for this time, it’s enough to know that there are constants to understand how many hex char get it.
$ echo $SCRIPTSIG | cut -c 1–2
thanks to cut method, I’m able to get the byte portion
47 in base2 is 71. it represents the bytes.
We know that a byte can be represented with 2 hex chars.
71*2 = 142.
It means that we need to consider 142 hex char after 47.
With the snippet below, you can get the result with a simple command.
$ expr `echo "ibase=16; $(printf 47)" | bc` "*" 2
We can see the part of digital signature
$ echo $SCRIPTSIG | cut -c 3–142
3044022059515b358d938d04c812177a2eefba52a4427b9e807c28538148e04edf042f3b022057e00e46acec7708b3087bfaadf25001ff449759b063aa9f39f5eb6606ceeef7
$ printf 3044022059515b358d938d04c812177a2eefba52a4427b9e807c28538148e04edf042f3b022057e00e46acec7708b3087bfaadf25001ff449759b063aa9f39f5eb6606ceeef7 | wc -c
Using the wc method, we can see that the length is 140 instead of 142. Why?
The next byte is the flag. It represents how the transaction is signed. Often you can find 01, it represents SIGHASH_ALL.
$ echo $SCRIPTSIG | cut -c 143–144
01
The next byte is a constant (Bitcoin Script)
$ echo $SCRIPTSIG | cut -c 145–146
41
It represents how many hex char get next
I can repeat the same operation
$ expr `echo "ibase=16; $(printf 41)" | bc` "*" 2
130
I can get the hex characters
$ echo $SCRIPTSIG | cut -c 147–277
04794dffa10783c305d72c44acc36003760a53c03a1e5529747a5ef7eef7c87c6c19ba26c7eee03ab6da9115d11bce3a46dd21aede86af19c3ee19eeb7f8d92732
These bytes are the public key, and it’s mandatory to verify the digital signature and satisfy the UTXO.
How is it possible?
Check the UTXO using getrawtransaction method
$ bitcoin-cli getrawtransaction d44cbca5911e53322e14fe0617f078dd1f162a7dcb97f83690eac285ed7ebe80 2
At the second position of array, we can see the UTXO that I want to unlock. We can know thanks to the vout the value inside vin (Input)
What is the value after HASH160?
ba716926b9313ca3bcf2791cf96a0f5f89472261
It’s the digest of our public key gets from SHA256 and RIPEMD160 functions.
$ printf $(echo 04794dffa10783c305d72c44acc36003760a53c03a1e5529747a5ef7eef7c87c6c19ba26c7eee03ab6da9115d11bce3a46dd21aede86af19c3ee19eeb7f8d92732 | xxd -r -p | openssl sha256| sed ‘s/^.* //’) |xxd -r -p | openssl ripemd160 | sed ‘s/^.* //’
The script will check public key hash and the digital signature.
The Bitcoin protocol asks:
The hash (HASH160) of public key inside the scriptSig is the same of the hash that I can Find in UTXO?
Can I verify the digital signature with the public key that I find inside the scriptSig?
In the book Bitcoin from theory to practice we analyze byte per byte the P2SH transaction, and we analyze the DER convention as well.
See ya!
Today Luke Asks:
In the previous videos, You spoke about scriptSig. Well, what’s it?
Nice question Luke, the scriptSig is an important part of Bitcoin’s transaction.
You can imagine a legacy transaction (not SegWit) like the image below:
- Version
- Inputs number (how many inputs)
- Input
- Outputs number
- Outputs number (how many outputs)
- locktime
The scriptSig is inside each input and it contains all elements in order to satisfy the linked UTXO. Pay attention please! In SegWit transaction, the scriptSig is empty: this is the way through wich the Bitcoin protocol solves the Transaction malleability!
In Action
Get a transaction from the mainnet blockchain
$ bitcoin-cli getrawtransaction c2297c9fefdd058e2ce5477868bcbf88bd4f860fbb552399053e3666687809bd 2
The transaction has one input and two outputs. Our focus is on the input part, inside the array vin.
"vin": [
{
"txid": "d44cbca5911e53322e14fe0617f078dd1f162a7dcb97f83690eac285ed7ebe80",
"vout": 1,
"scriptSig": {
"asm": "3044022059515b358d938d04c812177a2eefba52a4427b9e807c28538148e04edf042f3b022057e00e46acec7708b3087bfaadf25001ff449759b063aa9f39f5eb6606ceeef7[ALL] 04794dffa10783c305d72c44acc36003760a53c03a1e5529747a5ef7eef7c87c6c19ba26c7eee03ab6da9115d11bce3a46dd21aede86af19c3ee19eeb7f8d92732",
"hex": "473044022059515b358d938d04c812177a2eefba52a4427b9e807c28538148e04edf042f3b022057e00e46acec7708b3087bfaadf25001ff449759b063aa9f39f5eb6606ceeef7014104794dffa10783c305d72c44acc36003760a53c03a1e5529747a5ef7eef7c87c6c19ba26c7eee03ab6da9115d11bce3a46dd21aede86af19c3ee19eeb7f8d92732"
},
"sequence": 4294967295
}
],
Let’s analyze its hex (hexdecimal): it’s the scriptSig. and save it in an environment variable SCRIPTSIG.
SCRIPTSIG=473044022059515b358d938d04c812177a2eefba52a4427b9e807c28538148e04edf042f3b022057e00e46acec7708b3087bfaadf25001ff449759b063aa9f39f5eb6606ceeef7014104794dffa10783c305d72c44acc36003760a53c03a1e5529747a5ef7eef7c87c6c19ba26c7eee03ab6da9115d11bce3a46dd21aede86af19c3ee19eeb7f8d92732
We know that it’s a P2PKH transaction: that’s why we can search the digital signature and the public key. We should know the Bitcoin Script, for this time, it’s enough to know that there are constants to understand how many hex char get it.
$ echo $SCRIPTSIG | cut -c 1–2
thanks to cut method, I’m able to get the byte portion
47 in base2 is 71. it represents the bytes.
We know that a byte can be represented with 2 hex chars.
71*2 = 142.
It means that we need to consider 142 hex char after 47.
With the snippet below, you can get the result with a simple command.
$ expr `echo "ibase=16; $(printf 47)" | bc` "*" 2
We can see the part of digital signature
$ echo $SCRIPTSIG | cut -c 3–142
3044022059515b358d938d04c812177a2eefba52a4427b9e807c28538148e04edf042f3b022057e00e46acec7708b3087bfaadf25001ff449759b063aa9f39f5eb6606ceeef7
$ printf 3044022059515b358d938d04c812177a2eefba52a4427b9e807c28538148e04edf042f3b022057e00e46acec7708b3087bfaadf25001ff449759b063aa9f39f5eb6606ceeef7 | wc -c
Using the wc method, we can see that the length is 140 instead of 142. Why?
The next byte is the flag. It represents how the transaction is signed. Often you can find 01, it represents SIGHASH_ALL.
$ echo $SCRIPTSIG | cut -c 143–144
01
The next byte is a constant (Bitcoin Script)
$ echo $SCRIPTSIG | cut -c 145–146
41
It represents how many hex char get next
I can repeat the same operation
$ expr `echo "ibase=16; $(printf 41)" | bc` "*" 2
130
I can get the hex characters
$ echo $SCRIPTSIG | cut -c 147–277
04794dffa10783c305d72c44acc36003760a53c03a1e5529747a5ef7eef7c87c6c19ba26c7eee03ab6da9115d11bce3a46dd21aede86af19c3ee19eeb7f8d92732
These bytes are the public key, and it’s mandatory to verify the digital signature and satisfy the UTXO.
How is it possible?
Check the UTXO using getrawtransaction method
$ bitcoin-cli getrawtransaction d44cbca5911e53322e14fe0617f078dd1f162a7dcb97f83690eac285ed7ebe80 2
At the second position of array, we can see the UTXO that I want to unlock. We can know thanks to the vout the value inside vin (Input)
What is the value after HASH160?
ba716926b9313ca3bcf2791cf96a0f5f89472261
It’s the digest of our public key gets from SHA256 and RIPEMD160 functions.
$ printf $(echo 04794dffa10783c305d72c44acc36003760a53c03a1e5529747a5ef7eef7c87c6c19ba26c7eee03ab6da9115d11bce3a46dd21aede86af19c3ee19eeb7f8d92732 | xxd -r -p | openssl sha256| sed ‘s/^.* //’) |xxd -r -p | openssl ripemd160 | sed ‘s/^.* //’
The script will check public key hash and the digital signature.
The Bitcoin protocol asks:
The hash (HASH160) of public key inside the scriptSig is the same of the hash that I can Find in UTXO?
Can I verify the digital signature with the public key that I find inside the scriptSig?
In the book Bitcoin from theory to practice we analyze byte per byte the P2SH transaction, and we analyze the DER convention as well.
See ya!
📒 Book Bitcoin from theory to practice (Amazon)
📒 Book Bitcoin from theory to practice (accept bitcoin)