I'm having troubles with understanding how script hash is computed in P2SH transactions.
From BIP-16 documentation (https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki):
scriptSig: [signature] {[pubkey] OP_CHECKSIG}
scriptPubKey: OP_HASH160 [20-byte-hash of {[pubkey] OP_CHECKSIG} ] OP_EQUAL
From Bitcoin Wiki (https://en.bitcoin.it/wiki/Script#Crypto):
OP_HASH160 ... The input is hashed twice: first with SHA-256 and then with RIPEMD-160.
So I've tried to compute script hash from a real transaction. I've randomly chosen this one: https://blockstream.info/tx/00e07f279dd05b9b68c40f21b43c57847e75c35cd3bbc2d80921eb037ef0c9a8?expand.
Next, I will be talking about input 0. I believe that the hash, which I'm supposed to get is
4bf411df4b0004e00006f88a4f83d127195b88c7
And the thing, that I'm supposed to hash is
5221036c3735b2bf370501c3b872498de54b39ab5afa83d8ce7f6aec43f63a812265b421032b03a42faf387dd5c604435cd48d26b8827fa28a5d4d0f9a18b5cefe443bb4102102ebbd4ecea67dd980fc4854cc13b1f10cefafdafe8b1eb8e5ce73939b59a0477c53ae
, as this is hex of the redeem script.
But when I compute HASH160, I get
94b1c88916b7c784b896be91b6924f15c2d4a344
, which is not, what I expect..
SHA256(5221036c3735b2bf370501c3b872498de54b39ab5afa83d8ce7f6aec43f63a812265b421032b03a42faf387dd5c604435cd48d26b8827fa28a5d4d0f9a18b5cefe443bb4102102ebbd4ecea67dd980fc4854cc13b1f10cefafdafe8b1eb8e5ce73939b59a0477c53ae) = a77da07651b8445cc38ff5bb96e1e38977c4efdcc21b767ca3e25b8919514e03
RIPEMD160(a77da07651b8445cc38ff5bb96e1e38977c4efdcc21b767ca3e25b8919514e03) = 94b1c88916b7c784b896be91b6924f15c2d4a344
Please explain to me, where is the mistake and what should I do instead. Am I mistaking in what should be hashed and what the result should be, or am I just computing SHA256 and RIPEMD160 wrong?
Good luck, 0x309.
PS: To compute hashes I run echo"..." | openssl sha256
, then I copy the hash (so that there is no "(stdin)= ") and run
echo "..." | openssl rmd160
.
xxd -r -p
which will do this conversion for you.