Bitcoin Forum
November 14, 2018, 12:31:41 PM *
News: Latest Bitcoin Core release: 0.17.0 [Torrent].
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: how bitcoin transactions work under the hood: a visual guide in json  (Read 498 times)
crusyn
Newbie
*
Offline Offline

Activity: 7
Merit: 0


View Profile
September 19, 2017, 04:26:03 PM
 #1

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
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1542198701
Hero Member
*
Offline Offline

Posts: 1542198701

View Profile Personal Message (Offline)

Ignore
1542198701
Reply with quote  #2

1542198701
Report to moderator
DannyHamilton
Legendary
*
Offline Offline

Activity: 2198
Merit: 1385



View Profile
September 19, 2017, 05:24:54 PM
 #2

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"

DannyHamilton
Legendary
*
Offline Offline

Activity: 2198
Merit: 1385



View Profile
September 19, 2017, 05:29:19 PM
 #3

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 ) )

DannyHamilton
Legendary
*
Offline Offline

Activity: 2198
Merit: 1385



View Profile
September 19, 2017, 05:35:15 PM
 #4

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.

DannyHamilton
Legendary
*
Offline Offline

Activity: 2198
Merit: 1385



View Profile
September 19, 2017, 05:40:26 PM
 #5

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.



crusyn
Newbie
*
Offline Offline

Activity: 7
Merit: 0


View Profile
September 19, 2017, 05:52:55 PM
 #6

Thanks for the comments!  Seriously appreciate the feedback.  I'll take a look over the next week.

-crusyn
DannyHamilton
Legendary
*
Offline Offline

Activity: 2198
Merit: 1385



View Profile
September 19, 2017, 05:55:32 PM
 #7

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.

link2yasar
Newbie
*
Offline Offline

Activity: 12
Merit: 0


View Profile
September 19, 2017, 06:12:49 PM
 #8

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.
crusyn
Newbie
*
Offline Offline

Activity: 7
Merit: 0


View Profile
September 19, 2017, 08:55:01 PM
 #9

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
mstone
Newbie
*
Offline Offline

Activity: 10
Merit: 0


View Profile
September 19, 2017, 09:01:35 PM
 #10

Even though I've spotted some errors this is a pretty useful resource, cheers!
crusyn
Newbie
*
Offline Offline

Activity: 7
Merit: 0


View Profile
September 19, 2017, 09:02:37 PM
 #11

Please let me know what the errors are so that I can correct them for future readers!

-crusyn
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 1582
Merit: 1739


bc1qshxkrpe4arppq89fpzm6c0tpdvx5cfkve2c8kl


View Profile WWW
September 19, 2017, 09:04:33 PM
 #12

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.

DannyHamilton
Legendary
*
Offline Offline

Activity: 2198
Merit: 1385



View Profile
September 19, 2017, 09:16:43 PM
 #13

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.

crusyn
Newbie
*
Offline Offline

Activity: 7
Merit: 0


View Profile
September 21, 2017, 03:41:52 PM
 #14

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.
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!