I’m working on a Bitcoin thesis for my master’s degree and i have some doubts of my understanding how bitcoin transaction works, so please correct me in everything i got wrong. Sorry for really long question, this is only my second posting, but i have so many wholes in my understanding of this.
- Persons A and B are Bitcoin network nodes. A wants to send bitcoins to B. He (his wallet) needs to create transaction to do so.
- Every transaction has inputs and outputs and inputs of every transaction are just references to some previous outputs of some other, older, transaction that someone made towards A. After bitcoins arrived for A they reside in his UTXO (every single person in network has their own UTXO database with their own collection of unspent outputs related to address they are in control of).
- To send bitcoins A needs to resolve the puzzle given in scriptPubKey of previous transaction’s output by using his scriptSig which his wallet generates upon making a transaction. Basically A just needs to confirm that he is in control of private key that is related to public key and therefor to Bitcoin address of his wallet.
- B receives bitcoins, his wallet stores them in UTXO, and they just lay there until B decides to send those bitcoins further.
- And circle goes on. I skipped detail about change address because i its not relevant for this question.
If we are talking about p2pkh:
Puzzle scriptPubKey that A needs to solve (along with other operators) contains publicKeyhash, operator OP_HASH160 and operator OP_EQUALVERIFY that should compare value of publicKeyHash with public hashed key of person A (public key is included in his scriptPubKey, so taking the operator OP_HASH160 will produce hash of public key).
But how the person that sent bitcoins to A knows value of publicKeyHash so he can populate his scriptPubKey? My assumption is that he reverse steps of generating Bitcoin address, by decoding it with Base58 and taking out the control bit? But i couldn’t find confirmation of this online, sorry if missed it.
If we are talking about m of n transaction:
Assume that A is controlling an address that requires multisig for future use of bitcoins.
Someone wants to send A bitcoins and needs to create scriptPubKey and it should look like this
m pubKey_1, pubkey_2, …pubKey_m n OP_CHECKMULTISIGVERIFY
How does the person that wans to send A bitcoins knows public keys of people that are related to A’s bitcoin address? And further more, how does he knows the value of ‘m’? Isn’t it logical that A determines what is the minimal number of signatures? If i understood this transaction has no use no more, but still i want to understand how did it work.
If we are talking about P2SH:
if A is in control of P2SH address, how does the scriptPubKey looks for person that creates transaction towards A? Does that person needs to be in control of P2SH?
When A wants to send bitcoins, I understand that whole scriptSig now contains the scriptPubKey operators and operands, but new generated scriptPubKey is now the part that confuses me.
OP_HASH160 hash(scriptSig) OP_EQUAL should be the look of scriptPubKey.
And person that receives bitcoins from A should know how to solve this. But my question is how? How can he solve this when his scriptSig will be different than one written down in scriptPubKey. I know im missing a lot here since this protocol is wide popular, and to me is not making any sense.
What is the correct moment of verifying this scripts and transaction itself. As i said above, i assume scriptPubKey and scriptSig, after they are concatenated are evaluated when person wants to send bitcoins. But what happens to transaction after that? Does it travel around the network and other client wallets verify them by checking sender’s UTXO base (which is available for everyone?). Is there any additional checks by those other nodes?
After checking is done is it only then that transaction goes to memory pool and waits there until miner picks it up?
After miner include it in block, is transaction checked again trough some protocol that is checking the block?
Does miner checks the transaction himself again?
Again, sorry for a lot of questions and thank you in advance. Stay healthy.