My calculation of the hash160 doesn't seem to be correct - what are my sins?
Tx spending the input: https://www.blockchain.com/btc/tx/d490cc0a5b9799e09b02dd4b20a6d7ca297c09c425983efc13b382f6023d0376
Spent output's scriptPubKey:
OP_HASH160 032d2c15611c7b4b6941e62011e58cb5e204baa5 OP_EQUAL
Spending signature:
'txinwitness': ['3045022100b36d2efceb87bd89466d10ee31e5c76c775d9df71095a39c38bd8c5d64fc78d202201dddf5579d993d013eb73b58110e3d280364ec47b1d7617c2f6d842e7d243d6f01',
'02d0a9b9f420d22e3c1de27e87c6ff2ec33da21302cf07688b53d72c07e88d0422']
- So the pubkey is
'02d0a9b9f420d22e3c1de27e87c6ff2ec33da21302cf07688b53d72c07e88d0422'
My understanding is that the redeem script should be:
2102d0a9b9f420d22e3c1de27e87c6ff2ec33da21302cf07688b53d72c07e88d0422ac
(classic P2SH withOP_CHECKSIG
) or0002d0a9b9f420d22e3c1de27e87c6ff2ec33da21302cf07688b53d72c07e88d0422
for P2WPKH-nested-in-P2SH. [this is wrong, see below]
The expected hash output of the redeem script is:
032d2c15611c7b4b6941e62011e58cb5e204baa5
(see scriptPubKey above)
My hash outputs:
ripe160(sha256('2102d0a9b9f420d22e3c1de27e87c6ff2ec33da21302cf07688b53d72c07e88d0422ac'))
=9e75bbd28314946ea5bcea5e217982fce698b041
ripe160('0002d0a9b9f420d22e3c1de27e87c6ff2ec33da21302cf07688b53d72c07e88d0422')
=1c868bec66729c87e4cb60ec6d1c8846ad86d0ee
Saw this answer (P2sh redeem script hash), my calculations work fine with those inputs, but not with the above.
Related question: what is the role of the sigScript
0014cc38458a6e5c8c09e1d8fac905fb1024c8678d88
in the input if we have the txinwitness?
EDIT: mystery solved
Since scriptSig was not empty, it means that the transaction was a nested P2SH transaction. In this case it is a P2WPKH nested in BIP16 P2SH.
So in order to get the expected redeem script hash 032d2c15611c7b4b6941e62011e58cb5e204baa5
(which is base58 transformed, with a 0x05 byte appended at the beginning, to obtain the bitcoin address 31yp4jocJSRG4Wa8aV7ocKSXKcdWzkHy61
):
using the scriptSig asm = 0014cc38458a6e5c8c09e1d8fac905fb1024c8678d88
so
ripe160(sha256('0014cc38458a6e5c8c09e1d8fac905fb1024c8678d88'))
= 032d2c15611c7b4b6941e62011e58cb5e204baa5
= desired result (which can be base58 transformed into the bitcoin address).
The scriptSig asm is in this case a P2WKH redeem script, which is obtained as from the pubkey:
0x00 + ripe160(sha256('02d0a9b9f420d22e3c1de27e87c6ff2ec33da21302cf07688b53d72c07e88d0422'))
= 00 cc38458a6e5c8c09e1d8fac905fb1024c8678d88