Hi guys
I have a question regarding the bitcoin transaction signature
In the TX output locking script we don't deal with the Receiver Public key use actually only the Receiver Bitcoin address (<pubKeyHash> in the script below - as I understand):
Yes, with P2PKH, you don't deal with Public keys prima facie when you're sending bitcoins to someone, you just need the hash of their public key (i.e bitcoin address), however when the recipient wants to spend the UTXO you sent then s/he has to provide the public key that hashes to the address specified in the locking script (scriptPubkey) of the transaction.
OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
And when the Receiver tries to use the output he must provide the unlocking script:
<sig> <pub key>
where <sig> - is a newly created TX signature with the Receiver Private key and <pub key> - is the Receiver public key (derived from the Private key)
Yes, this is correct.
But imagine that some bad guy wants to use my UTXO.
This bad guy has
1. His Private Key,
2. His Public key (Derived from the Private key)
3. He knows my Bitcoin address (this is quite public info)
If you've used the address before to send a transaction (redeem a UTXO), then he has your public key too because you need to provide it before you can spend any UTXO.
However having all these won't help as long as he doesn't have your private key.
Now the bad guy creates a transaction, signs it with his Private Key and generates an Unlocking script <bad guy TX sig> <Bad guy Public Key>
On the stage of verification we connect the unlocking and unlocking scrips:
<bad guy TX sig> <Bad guy Public Key> OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
Of course the script fails on the stage of OP_EQUALVERIFY because the Bitcoin address generated from the swindler Public Key is not equal to the <pubKeyHash> of the Receiver (specified in the locking sctipt)
But ... let's imagine(just hypothetically) that this verification passed
Like I said earlier, whenever you spend bitcoins received to an address, then your public key is revealed, but that's okay because it's a PUBLIC key, and it has to be revealed without any loss in security (barring a functional quantum computer)
So that stage can pass because he has your public key.
and we went to the next step - Transaction verification with OP_CHECKSIG. And my main confusion is that I realize that OP_CHECKSIG verification will always pass because what was signed with the bad Private key will be always verified correctly with his Public key because regardless of the bad guy intentions - his Public - Private key pair is a valid pair.
Absolutely NOT.
Bitcoins are a chain of UTXOs.
Any bitcoins you're spending has to specify the requirements for the recipient to unlock to spend also.
With P2PKH it's the public key of the address provided and the signature of the transaction being spent.
So if he has HIS private key, with HIS public key, then he CAN'T spend YOUR UTXO because HIS public key WAS NOT specified in the locking script (scriptPubkey) of the transaction he's spending.
A bitcoin transaction from A -> B simply states: let B spend this UTXO iff he provides the public key for this address, and if he signs the transaction AGAINST the public key provided.
So since he's using HIS public key and not yours, then there's is frankly NO Utco that can be spent because it does NOT exist.
The existing UTXO is the one with YOUR bitcoin address, and hence YOUR public key.
If HIS public key is hashed it will result in a DIFFERENT bitcoin address entirely, barring a collision which means SHA256 & RIPEMD are broken.
So in essence he'll actually be spending HIS bitcoins and not yours because he's providing HIS public key and address.
Another scenario: let's say you've spent from an address before and now your public key is revealed.
The attacker now has your public key and inserts it in a transaction.
The first part verifies because the public key provided (yours) hashes to the bitcoin address specified in the UTXO to be spent (also yours)
The signature part however will fail if he doesn't have your private key, because the transaction he creates HAS to be signed by a private key, and the signed TX MUST verify against the public key provided.
So as I see OP_CHECKSIG does not make any sense (it always true) at all and only a Bitcoin address verification assures that the Receivers address derives from his Public key - but it looks too weak
Please explain/correct me where I'm wrong.
Thanks a lot.
If a digital signature wasn't required then anyone could spend anyone else's UTXOs with just the public key because when an address makes a transaction the public key of the address is revealed.
For you to sign a digital signature you'll need the PRIVATE KEY of the associated public key. If the attacker has only your public key (this is revealed when you make a transaction), then he can't spend your UTXOs because he needs to sign the transaction itself with the private key of the public key provided, and the signed transaction is verified against the public key.