<p>I'm trying to extract p2sh addresses from blocks and I am not doing something correctly. What I have tried is:</p>
<pre><code>    def decodep2sh(self, redeemscript):
        sha256 = hashlib.sha256(bytes.fromhex(redeemscript)).hexdigest()
        h = RIPEMD160.new()
        h.update(bytes.fromhex(sha256))
        final_address = base58check.b58encode(bytes.fromhex(h.hexdigest()))
        return final_address
</code></pre>
<p>Could anyone tell me what I might be doing wrong?</p>

CC BY-SA 4.0
2023-02-24T20:34:31.003Z
Are you building this program from some example? If so can you provide the example.
Poseidon
CC BY-SA 4.0
2023-02-24T20:37:49.093Z
What do you mean by an example?
bitcoinluvr6969
CC BY-SA 4.0
2023-02-24T20:39:14.277Z
Where did you get the instructions to decode a redeem script this way? Im not inferring its wrong, but if you are learning to do this from some source providing that context would be helpful.
Poseidon
CC BY-SA 4.0
2023-02-24T20:40:43.643Z
Oh I see! Sorry for the confusion. No, I am doing this as part of my research at my university. I listening to blocks come in real time and trying to see if I can extract addresses.
bitcoinluvr6969
CC BY-SA 4.0
2023-02-24T20:47:43.057Z
I found this interesting article https://btcleak.com/2020/07/19/we-solved-a-p2sh-transaction-puzzle-and-redeemed-bitcoin/ which includes this python formula, perhaps it will work here.

>>> import binascii, hashlib, base58
>>> script = '51'
>>> base58.b58encode_check(binascii.unhexlify('05'+ hashlib.new('ripemd160', hashlib.sha256(binascii.unhexlify(script)).digest()).hexdigest())).decode()
'3MaB7QVq3k4pQx3BhsvEADgzQonLSBwMdj'
Poseidon
CC BY-SA 4.0
2023-02-24T22:01:22.657Z
You should use existing Bitcoin libraries to do the parsing; there exist several in just Python alone. Based on other questions you're asking here, I infer you're basically trying to reverse engineer the transaction format. That'll take a long time, and will likely cause you to miss many edge cases even if you get it to work for most transactions.
Pieter Wuille
CC BY-SA 4.0
2023-02-27T14:29:20.450Z
@PieterWuille I appreciate what you're saying but my professor has explicitly told me I'm not allowed to rely on 3rd party libraries very much (so not at all).
bitcoinluvr6969
CC BY-SA 4.0
2023-02-27T14:57:25.907Z
@bitcoinluvr6969 So is this homework?
Pieter Wuille
CC BY-SA 4.0
2023-02-27T15:05:20.397Z
@PieterWuille No, sorry for the confusion. I work in a research lab.
bitcoinluvr6969
CC BY-SA 4.0
2023-02-27T15:10:48.690Z
That's a truly bizarre request from your professor then.
Pieter Wuille
<transactions><p2sh>
How to extract p2sh from tx block
bitcoinluvr6969
# How to extract p2sh from tx block

(Asked by: username_0 on 2023-02-24)

I'm trying to extract p2sh addresses from blocks and I am not doing something correctly. What I have tried is:

```
    def decodep2sh(self, redeemscript):
        sha256 = hashlib.sha256(bytes.fromhex(redeemscript)).hexdigest()
        h = RIPEMD160.new()
        h.update(bytes.fromhex(sha256))
        final_address = base58check.b58encode(bytes.fromhex(h.hexdigest()))
        return final_address

```

Could anyone tell me what I might be doing wrong?

**Tags:** transactions, p2sh

> **Comments:**
> **username_1**: Are you building this program from some example? If so can you provide the example.  
> **username_0**: What do you mean by an example?  
> **username_1**: Where did you get the instructions to decode a redeem script this way? Im not inferring its wrong, but if you are learning to do this from some source providing that context would be helpful.  
> **username_0**: Oh I see! Sorry for the confusion. No, I am doing this as part of my research at my university. I listening to blocks come in real time and trying to see if I can extract addresses.  
> **username_1**: I found this interesting article https://btcleak.com/2020/07/19/we-solved-a-p2sh-transaction-puzzle-and-redeemed-bitcoin/ which includes this python formula, perhaps it will work here.  >>> import binascii, hashlib, base58 >>> script = '51' >>> base58.b58encode_check(binascii.unhexlify('05'+ hashlib.new('ripemd160', hashlib.sha256(binascii.unhexlify(script)).digest()).hexdigest())).decode() '3MaB7QVq3k4pQx3BhsvEADgzQonLSBwMdj'  
> **username_2**: You should use existing Bitcoin libraries to do the parsing; there exist several in just Python alone. Based on other questions you're asking here, I infer you're basically trying to reverse engineer the transaction format. That'll take a long time, and will likely cause you to miss many edge cases even if you get it to work for most transactions.  
> **username_0**: @username_2 I appreciate what you're saying but my professor has explicitly told me I'm not allowed to rely on 3rd party libraries very much (so not at all).  
> **username_2**: @username_0 So is this homework?  
> **username_0**: @username_2 No, sorry for the confusion. I work in a research lab.  
> **username_2**: That's a truly bizarre request from your professor then.  

---

## 0 answers

