Author

Topic: how bitcoin transactions work under the hood: a visual guide in json (Read 618 times)

newbie
Activity: 7
Merit: 2
Thank you @DannyHamilton and @achow101.  I have made the following changes as suggested:

-Fixed phrasing to use "greater than or equal to the sum of the value of inputs"
-Added "d" to "ripem" hash name, fixed in the illustrations
-Swaped the locking and unlocking scripts.  You are right, the scriptPubKey is commonly called the locking part of the script
-Updated to correct work "throw an error"
-changed commission to transaction fee
-noted that this example considers the p2pkh but other scripts are possible.
legendary
Activity: 3472
Merit: 4801
The sighash perimage algorithm takes the current transaction, 0's out all scriptsigs, puts the scriptPubKey of the current input's UTXO in the scriptsig, and then appends the sighash flag to the end. That preimage is then hashed and signed. See https://bitcoin.stackexchange.com/questions/3374/how-to-redeem-a-basic-tx and https://github.com/bitcoin/bitcoin/blob/efb4383ef6c69e801ddca160a53e7fb4ee8b156a/src/script/interpreter.cpp#L1176

Wow. I thought I knew how that worked.  How have I been active here for nearly 5 years and only just now discovered that I misunderstood this process. Glad to learn something new.

Thanks.  I'll remove my post and read up on this to fill in this and any other gaps I may have in my knowledge around this process.
staff
Activity: 3458
Merit: 6793
Just writing some code
This part is completely wrong:
Quote
What is signed by the signature in the spending transaction?
- snip -
It is neither the current or previous transaction, but rather a hybrid of both.  It includes the scriptPubKey from the previous transaction, which gave them ownership in the first place and

The scriptPubKey of the previous transaction is NOT signed by the sender of the current transaction.

Only the current transaction (except the signature since that hasn’t been created yet) is signed by the sender.
No, that part is correct. The sighash perimage algorithm takes the current transaction, 0's out all scriptsigs, puts the scriptPubKey of the current input's UTXO in the scriptsig, and then appends the sighash flag to the end. That preimage is then hashed and signed. See https://bitcoin.stackexchange.com/questions/3374/how-to-redeem-a-basic-tx and https://github.com/bitcoin/bitcoin/blob/efb4383ef6c69e801ddca160a53e7fb4ee8b156a/src/script/interpreter.cpp#L1176



Some comments in addition to those that DannyHamilton has left:

Quote
remainder commission for miners
Usually that "commission" is called the transaction fee, not a commission.



scriptPubKey's can contain other scripts too, such as P2SH outputs scripts, bare multisig scripts, OP_RETURN scripts, and really any script that fits in the scriptPubKey. It is not just limited to the P2PKH script. Perhaps you should mention that.
newbie
Activity: 7
Merit: 2
Please let me know what the errors are so that I can correct them for future readers!

-crusyn
newbie
Activity: 10
Merit: 1
Even though I've spotted some errors this is a pretty useful resource, cheers!
newbie
Activity: 7
Merit: 2
Hey-

Yeah I have to figure out how to make the wordpress template more user friendly, any suggestions about how to do it are welcome.

You can get the full res images by getting rid of the querystring of the image URL when you click open image in a new tab/new window:

For example in:

https://crusyn.files.wordpress.com/2017/08/01-currenttransaction3.png?w=525

drop the "?w=525"

And you will get the full res:

https://crusyn.files.wordpress.com/2017/08/01-currenttransaction3.png

Does that help?

-crusyn
newbie
Activity: 12
Merit: 0
https://crusyn.com/2017/08/28/bitcoin-transactions-visual-guide-json/

I put together this post after having a tough time figuring out bitcoin transactions on a technical level myself. I tried making it easy by putting together sequential illustrations to support my prose.

Appreciate any feedback, whether technical, or stylistic!!

-crusyn

Very informative and helpful to understand overall architecture. but i am not able to fully grasp the idea as your provided images are not in good quality. can you kindly provide us the full version of these images.
legendary
Activity: 3472
Merit: 4801
You've used an incorrect word.

Quote
If they are unequal it would through an error and quit which would indicate to anyone running this script that this spend is not valid or unauthorized.

through:
pronounced: THroo
(preposition or adverb)
1. moving in one side and out of the other side of (an opening, channel, or location).
"stepping boldly through the doorway"

2. continuing in time toward completion of (a process or period).
"he showed up halfway through the second act"


(adjective)
1. continuing or valid to the final destination.
"a through train from Boston"

2. traffic that passes from one side of a place to another in the course of a longer journey.
"neighborhoods from which through traffic would be excluded"




I suspect you meant to use the word "throw":

throw:
pronounced: THrō
(verb)
2. cause to enter suddenly a particular state or condition.
newbie
Activity: 7
Merit: 2
Thanks for the comments!  Seriously appreciate the feedback.  I'll take a look over the next week.

-crusyn
legendary
Activity: 3472
Merit: 4801
Are you sure you have this part correct?

Quote
I have seen people call the scriptPubKey the unlocking part of the script and the scriptSig the locking part

Generally, when I hear people talk about it, they say it the other way around.

The scriptPubKey is a lock on the transaction output that prevents anybody from spending that output unless they are able to unlock it.

The scriptSig unlocks the output so that it can be used as an input in the current transaction.


legendary
Activity: 3472
Merit: 4801
It is also confusing where you say:
Quote
The private key is as used to sign the current transaction and the output of the source transaction and generate the sender public key.

The entire output of the source transaction is not signed in the current transaction (only the scriptPubKey).  The current transaction is signed (without signing the scriptSig that will store the signature).  Signing the current transaction does include signing the input element of the current transaction (which is a hash of the source transaction and index of output location in the source transaction).

See achow101's response below.  I apparently misunderstood this process and have some of the details wrong in this post.
legendary
Activity: 3472
Merit: 4801
Just found a small typing error as well.

You state:
Quote
The public key is hashed by ripem-160( sha-256( $publickey ) )

It should actually say:
Quote
The public key is hashed by ripemd-160( sha-256( $publickey ) )
legendary
Activity: 3472
Merit: 4801
I found one small error so far...

You say:
Quote
It is useful to note that in order for this transaction to be valid the sum of the inputs need to be greater that the sum of the outputs.

This is not exactly true.

To be accurate you should either say:
Quote
It is useful to note that in order for this transaction to be valid the sum of the inputs need to be greater than or equal to the sum of the outputs.

or

Quote
It is useful to note that in order for this transaction to be valid the sum of the inputs must not be less than the sum of the outputs.

Additionally, there is a possibility of some confusion in your phrasing.

While unlikley, it is possible that a beginner might mistakenly believe that the phrases "sum of the inputs" and "sum of the outputs" mean "sum of the quantity" instead of "sum of the value".  In otherwords, they might think that you have to have 2 or more inputs if you have 2 or more outputs.

As such, it might be better to use the phrases "sum of the values of the inputs" and "sum of the values of the outputs"
newbie
Activity: 7
Merit: 2
https://crusyn.com/2017/08/28/bitcoin-transactions-visual-guide-json/

I put together this post after having a tough time figuring out bitcoin transactions on a technical level myself. I tried making it easy by putting together sequential illustrations to support my prose.

You can get the full res images by getting rid of the querystring of the image URL when you click open image in a new tab/new window (see post below for details).

Appreciate any feedback, whether technical, or stylistic!!

-crusyn
Jump to: