6

I am trying to generate SegWit paper wallet address from WIF:

My WIF is L5mHKZsCLS27nSoGM3RdAwuxjvg7XhJdP25LgqdXe6zF11wpWdbT.

I am converting WIF to compressed public key:

Compressed public key: 03fac6879502c4c939cfaadc45999c7ed7366203ad523ab83ad5502c71621a85bb

I am creating P2SH-P2WPKH address using instructions at Creation of P2SH-P2WPKH Address

using following algorithm:

  1. Calculate the RIPEMD160 of the SHA256 of a public key:

SHA256 of public key: cfad24b0bc2bba2c8bb2c8d619dca2b74221930793bca50df73856f0bbba10c9

RIPEMD160 of SHA256 hash: d5e5d05edfe2ed61099bf3f0d53be2775bbc6d0d

  1. Create P2SH redeemScript as OP_PUSH publicKeyHash:

redeemScript: 0014d5e5d05edfe2ed61099bf3f0d53be2775bbc6d0d

  1. Generate scriptPubKey as OP_HASH160 hash160(redeemScript) OP_EQUAL

scriptPubKey: a914a19949e546c2f4d410cbb061c986b9ff3099ae7087

  1. Generate RIPEMD160 hash of scriptPubKey:

hash: dc62e525af22d1f0f17cc2a091cf2c9fb36bf553

  1. Generate address with 0x05 prefix and double SHA256 hash checksum:

result: 3MnK46gvixm8hk7mJTQTAqenniSodjXPrJ

Expected result according to segwitaddress is:

33voQqbNAYyig272KjcX8GkucWn2x25WEg

What am I doing wrong?

Update

Thanks to the accepted answer, here are correct steps to do this:

Public key - compressed: 
03fac6879502c4c939cfaadc45999c7ed7366203ad523ab83ad5502c71621a85bb

SHA256(public key) =
cfad24b0bc2bba2c8bb2c8d619dca2b74221930793bca50df73856f0bbba10c9

RIPEMD160(SHA256(public key)) =
7646c030f7e75b80f0a31cdcab731e6f424f22b2

redeemScript (OP_0 pubkeyHash160):
00147646c030f7e75b80f0a31cdcab731e6f424f22b2

SHA256(redeemScript) =
a10e523968ba784d24ccd54e613d8f747d6649e42b1df4fdcec6658262620651

RIPEMD160(SHA256(redeemScript)) =
188ba16284702258959d8bb63bb9a5d979b57875

P2SH address base58(0x05 | hash | 4-byte sha256 checksum) =
33voQqbNAYyig272KjcX8GkucWn2x25WEg
Jakub Fedyczak
  • 235
  • 1
  • 7

1 Answers1

2
  1. Create P2SH redeemScript as OP_PUSH publicKeyHash:

redeemScript: 0014d5e5d05edfe2ed61099bf3f0d53be2775bbc6d0d

It's actually `OP_0 but you have resulted in the right thing anyways.

  1. Generate scriptPubKey as OP_HASH160 hash160(redeemScript) OP_EQUAL

scriptPubKey: a914a19949e546c2f4d410cbb061c986b9ff3099ae7087

  1. Generate RIPEMD160 hash of scriptPubKey:

hash: dc62e525af22d1f0f17cc2a091cf2c9fb36bf553

These steps are incorrect. The thing that is hashed for a P2SH address is the redeemScript, not the scriptPubKey. Instead of doing step 3, you would skip directly to step 4 and hash the redeemScript instead of the scriptPubKey.

Andrew Chow
  • 67,209
  • 5
  • 76
  • 149
  • Hi Andrew, I was hoping you could help. I think I'm not generating the checksum correctly. Before the base58 encoding, this is the string I have: 05188ba16284702258959d8bb63bb9a5d979b5787555c30444. After base58 that's 33voQqbNAYyig272KjcX8GkucWn2uJk5WT. Last 6 characters are incorrect. Which part am I getting wrong? – user2298995 Dec 17 '17 at 07:34
  • @user2298995 Your checksum is wrong. The hex should be 05188BA16284702258959D8BB63BB9A5D979B57875BFE2FCFD – Andrew Chow Dec 17 '17 at 16:39
  • Thank you very much for your comment! How is the checksum generated then? I took the 4 bytes of sha256(sha256(redeemScript)). Is it not the proper way? – user2298995 Dec 18 '17 at 17:46
  • 2
    No, it is the first 4 bytes of the sha256(sha256(payload)) where the payload is the data you are encoding with the base58 check encoding. In this case, the payload is 05188BA16284702258959D8BB63BB9A5D979B57875 – Andrew Chow Dec 18 '17 at 17:59
  • Oh thank you very much. Very appreciated! – user2298995 Dec 18 '17 at 19:12