Bitcoin Transaction explained in detail #6

hello everyone welcome to a prime number
today I like to talk about the Bitcoin
transactions in previous episode we
talked about how does the Bitcoin work
by using a use case that Alice want to
send Bitcoin to Bob it’s starting from
using Alice mobile phone to send a
transaction to any of the Bitcoin
network node but we didn’t dive into
what the transaction looks like what in
the transaction and how to pay in the
transaction fees in today’s video it
could be more information, a lot of
concepts we also talked about the script
the video may be a little long but it is
very informational now let’s take a look
what is a Bitcoin transaction a Bitcoin
transaction has two main parts we called
the inputs and outputs so this is the
input and this is output the list of
inputs referencing to the previous
transactions output so for example
Alice want to send let’s say 10 Bitcoin
to Bob
so using this transaction to do that
so the Alice need to reference to a
previous transaction output which prove
that Alice has more than 10 bitcoins so
let’s say this is the index 0 input
which references to one of the output
let’s say index 1 output from a previous
transaction and it only have like five
so that Alice need another input
which may come from another
transactio’s output which for
example is from index 0 it has like
eight Bitcoin so in total we have 13 so
that it has enough bitcoins so the
output is we I send it to Alice sends to
Bob so the index for example we have
should have two output indexes one is
this is to Bob and this is 10
BTC since we have in total 13 so the
rule is you have to consume all the
inputs to come up with the output so in
total we have here 5 plus 8 is 13 so we
have this is 10 so we have only another
3 so usually so we the wallet will
create a transaction which will pay
Alice herself was usually it should be 3
BTC to Alice to herself but in reality
those kind of transaction will not be
picked up because it doesn’t pay the
transaction fee so in reality it usually
would come up with for example 2.99
BTC so that’ll be 2.99
so in total is 12.99 and there’s no
field such as for fees or transaction
fees so the difference from the input
and output is the transaction fee which
the miner will will take it so you may
have in mind that the blockchain is
appended one by one the block is
referenced to the previous block
so don’t don’t be confused the
transaction reference with blocks
reference it’s a totally different
concept because this could be a block
with multiple transactions but this is
it may not be the previous block it
could be ten blocks away or even
thousands of blocks away and this may be
even further maybe thousands of blocks
away so the reference of the transaction
is exist in parallel with the reference
of the blocks and there’s no
relationships so you may wondering so
since Alice want to send coins to Bob
so it seems there’s no easy way to know
like the balance that Alice has so when the wallet
wants you create a transaction and
the Alice want to send Bitcoin want to
send ten bitcoins the wallet doesn’t
know how much bitcoins how many bitcoins
Alice has so it has to go backward to
check the unspent output for any
transactions which sent to Alice and
add them up and see whether Alice has
enough balance so it is not efficient at
all you could go backward until the
Genesis block just in order to know the
balance of a identity, of an account. let’s
say Bob want to send Bitcoin to Charlie
and so the Bob can create another
transaction
which the input is referencing to the
output the index 0 so it could have only
have one index. Bob now
has already like 10 bitcoins and he only
wanted to send 5 bitcoins so only one
input is needed so that will add up to
this is 10 BTC because the output of the
previous transaction sent to Bob has 10
and the output is there will be 2 output 1
will be to Charlie
that is 5 BTC, and another is to
Bob himself that will be close to
another five but since Bob need to
pay the transaction fee as well so Bob
will get four dot something BTC back so
this is basically how the transaction is
in Bitcoin let’s take a look at the real
transaction in Bitcoin I randomly pick
up a transaction which is almost two
years ago is because nowadays the
Bitcoin has upgraded to support the
segregated witness transaction that’s a
little bit different from the
transaction which we just talked about I
will cover the segwit transaction in
the following video but the basic
concept are almost the same so here is a
transaction which is sending from this
address this is the input
and this is the output so two different
addresses actually both have been spent.
so the total input is 26.99
total output is 26.89
as I said the difference will
be the transaction fees so the
transaction fee here is 0.00339 BTC and divided by
how many bytes it has so it will be 1520.179 Satoshi per byte here estimated
BTC transact which means given a
transaction you’re seeing you don’t know
which output is the transaction the
sender wants to send to because it could
be sending to himself for the change
right this is called a change address
but a change address may not be the same
as the sender address since a lot of
the wallet can generate on the fly the
public key for anonymous purpose so just
looking at the transaction you can only
guess for example for this one the for
bitcoin is the actual transaction it
wants send to and the 22.98 is the
change to himself so let’s dive into a
little bit more about the transaction so
so this is the transaction which we just
saw this is the raw data of the
transaction and here you are seeing
you’re seeing here this is the input and
this is the previous transaction ID and
the previous output index. this is the
index 1 and this is the signature and
the public key which used to prove that
you are the owner of the previous output
and this transaction has two output
which sent into 2 person here you can see
that so this one has value 4 the 4
Bitcoin and this is 22 and this the
scriptPubKey is actually what we just
talked about is the receiver public key
or receiver Bitcoin address but it’s not
just address itself is actually a script
which will be run by the miner I’ll talk
further look at the transaction ID here
which referencing to a previous
transaction the output index 1 so I just
opened this transaction 06ee write 06ee
and the output has two indexes the
index 1 has this value which is which
matches what we saw from the input and
this is the the script pub key means if
you want to redeem this Bitcoin what the
script need to be run so it needs to be
run without errors so let’s take a look
how to run the operation in the
script what language it is
and
how it works so I just copy the script
signature and from the input of the
transaction and the referenced output
script public key this one into notepad
so this is the input and this is the
this is the output which the input
reference it to so let’s see how the
script language works in order to redeem
the coin and prove that you own a coin
and you can spend the coin so the
Bitcoin script language is it doesn’t
has a name it just called scripting
language and the design goal for this
language is it has to be simple and
compact and it is a stack
based language which means you put if
you have ever done a project like a
calculator you know what I mean so you
put the operand in to the stack and you
put the operation into the stack so once
you put operation into the stack it will
actually get value from the stack and
operate it and it removes on the stack
after it’s done so it’s similar to that
so the Bitcoin script language is
inspired by Forth language and it
provides some operations which is
cryptography operations and the beauty
of this language is it limits on the
time and memory and there’s no looping
so it guarantees the script will run and
ends in a certain time so the way how it
works is the Bitcoin software which runs
on the miner will concatenate the input
of the transaction
with the previous output of the
transaction which the input is
referencing to so here it will put the
input of transaction the first 3044
this is the signature and second part
02f blabla this is the public
key and then the operation is operation
OP_DUP operation OP_HASH160 and this is
following is 61284 blablabla
that’s the public key hashed
and then another operation called OP_EQUALVERIFY and then another operation to
check the signature OP_CHECKSIG
so the stack based operations or stack
stack based scripts works like this
so the first it got is 3044 which is the signature so it
grabbed a signature and put it at the
bottom of this stack so this is the
signature and then it put the public
keys on the stack this is coming from
this data and then is the operation
duplicate operation so what the
duplicated operation does is to get the
item on top of the stack and duplicate
it so at this time it will have another
public key on the stack and it’s a hash
function what it does is it will get the
top of the stack and calculate the hash
160 so once that is been executed the
top of the stack is not public key
anymore
that is the public key which is hashed
and then it got a public key hashed so
that’s the data which the output
transaction so whoever want to send a
Bitcoin is providing what public key the
hashed public I want to send to so
this is from the script of the output so
we just pull it the data here and then
the operation to verify so what the
verify means what it does is to get
those two values from the top of the
stack and check if they’re equal so this
is equal verification verify so what is
if it’s run successfully it will remove
from the stack of those two public keys
and it’s verified there they’re the same
and at last is the signature check so what
does signature check it what it does is
since on the stack I have the public key
and the signature and I have the
transaction so in previous episode we’ve
discussed this how the digital digital
signature works so you have the public
key you have your signature you have the
data so that you can verify whether the
signature is valid and whether
the public key since we already compared
the public key is the one the whoever
sent a Bitcoin to so that once the
script executed successfully so that you
can claim say I’m the owner of the
public key so that I can redeem the
Bitcoin the transaction we just saw is
the v1 transaction you can see the
transaction version in
the metadata and since the transaction
is two years old it could be a little
different from most of the transaction
you see nowadays in the Bitcoin network
because most of them is v2 already which
support the segregated witness there’s a
little bit different we can talk about
this in the next video thanks for
watching subscribe if you like it see
you next time