Search code examples
bitcoin

How dose the bitcoin locking/unlocking script work?


When I read the book Mastering Bitcoin chapter 6

I try to follow the steps mentioned in this book, but I found out the result is not correct, where am I wrong?

A step-by-step execution of the combined script

The transaction is here:

Input Scripts:

ScriptSig: 
PUSHDATA(72)[3045022100884d142d86652a3f47ba4746ec719bbfbd040a570b1deccbb6498c75c4ae24cb02204b9f039ff08df09cbe9f6addac960298cad530a863ea8f53982c09db8f6e381301] 
PUSHDATA(65)[0484ecc0d46f1918b30928fa0e4ed99f16a0fb4fde0735e7ade8416ab9fe423cc5412336376789d172787ec3457eee41c04f4938de5cc17b4a10fa336a8d752adf]

Then I track the output that is included in this transaction. In which the output scripts like this:

DUP HASH160 PUSHDATA(20)[7f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a8] EQUALVERIFY CHECKSIG

Then I calculate the RIPMED160(SHA256(Pubk)) = "6df13de1f1d824380834e0d42e49e5e451a647cf"using the website calculator

But the result is not equal to 7f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a8 in output script.


Solution

  • You have the right idea, but I think you are hashing the hex string, not the binary data. If I calculate ripemd160(sha256(pubk)) using Python's hashlib library I get the correct result:

    $ python
    Python 2.7.13 (default, Nov 24 2017, 17:33:09) 
    [GCC 6.3.0 20170516] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import binascii, hashlib
    >>> sha256 = hashlib.new('sha256')
    >>> ripemd160 = hashlib.new('ripemd160')
    >>> sha256.update(binascii.unhexlify('0484ecc0d46f1918b30928fa0e4ed99f16a0fb4fde0735e7ade8416ab9fe423cc5412336376789d172787ec3457eee41c04f4938de5cc17b4a10fa336a8d752adf'))
    >>> ripemd160.update(sha256.digest())
    >>> ripemd160.hexdigest()
    '7f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a8'