<p>Quick and dirty code for your task. Hope it helps.</p>

<p>1) Let us create address from secret.</p>

<pre><code>static void p2sh_create ( )
{
  const MyByteArray secret ( QByteArray ( "My secret phrase" ) );
  const MyKey32 sha256 ( secret.sha256 ( ) );
  const MyByteArray redeemScript ( MyByteArray ( ).putInt8 ( OP_SHA256 ).putPush ( sha256 ).putInt8 ( OP_EQUAL ) );
  const MyKey20 hash160 ( redeemScript.hash160 ( ) );
  const QString address ( hash160.toStringP2SH ( ) );

  qDebug ( ) &lt;&lt; "secret      =" &lt;&lt; QString ( secret.toHex ( ) ) &lt;&lt; "(in hex)";
  qDebug ( ) &lt;&lt; "sha256      =" &lt;&lt; sha256.toStringRev ( );
  qDebug ( ) &lt;&lt; "redeemScript=" &lt;&lt; QString ( redeemScript.toHex ( ) );
  qDebug ( ) &lt;&lt; "hash160     =" &lt;&lt; QString ( hash160.toHex ( ) );
  qDebug ( ) &lt;&lt; "address     =" &lt;&lt; address;
}
</code></pre>

<p>The output is:</p>

<pre><code>secret      = "4d792073656372657420706872617365" (in hex)
sha256      = "73d83ecabab6ba96a47f03d0e21ffbdfeaab5d337b7e93a0cc2e85019190fb3f"
redeemScript= "a82073d83ecabab6ba96a47f03d0e21ffbdfeaab5d337b7e93a0cc2e85019190fb3f87"
hash160     = "fa3156d1e95111b0105fb538711bf8e19dadf1bd"
address     = "2NG482PHrj6f41UoqUHYGVkUR39oB3UvWkE"
</code></pre>

<p>2) Now let us fund the address <code>2NG482PHrj6f41UoqUHYGVkUR39oB3UvWkE</code>. Follow the link <a href="https://testnet.coinfaucet.eu/en/" rel="nofollow noreferrer">https://testnet.coinfaucet.eu/en/</a> and ask for some test coins. Testnet faucet sent me 0.86978283 TBTC in transaction <a href="https://testnet.smartbit.com.au/tx/21c181c6052f6266ea6c936cf86111d6a2662a1f1e3251560b718fb8b1e6457d" rel="nofollow noreferrer">https://testnet.smartbit.com.au/tx/21c181c6052f6266ea6c936cf86111d6a2662a1f1e3251560b718fb8b1e6457d</a></p>

<p>3) Now create spending tx from this address.</p>

<pre><code>static void p2sh_spend ( )
{
    const MyByteArray secret ( QByteArray ( "My secret phrase" ) );
    const MyKey32 sha256 ( secret.sha256 ( ) );
    const MyByteArray redeemScript ( MyByteArray ( ).putInt8 ( OP_SHA256 ).putPush ( sha256 ).putInt8 ( OP_EQUAL ) );
    const MyByteArray tx ( MyByteArray ( )
      .putInt32 ( TX_VERSION )
      .putInt8 ( 1 )
      .putArray ( MyKey32::fromHex ( "21c181c6052f6266ea6c936cf86111d6a2662a1f1e3251560b718fb8b1e6457d" ) )
      .putInt32 ( 1 )
      .putPrefixed ( MyByteArray ( ).putPush ( secret ).putPush ( redeemScript ) )
      .putInt32 ( TX_SEQUENCE )
      .putInt8 ( 1 )
      .putInt64 ( 86978283 - 200 )
      .putPrefixed ( MyByteArray ( ).putInt8 ( OP_HASH160 ).putPush ( MyKey20::of ( "2N8cj6kf3RkPHzQSCG13bvq6iXXWWPuGurZ" ) ).putInt8 ( OP_EQUAL ) )
      .putInt32 ( TX_LOCK ) );
    const MyKey32 txid ( tx.sha256d ( ) );

    qDebug ( ) &lt;&lt; "tx   =" &lt;&lt; QString ( tx.toHex ( ) );
    qDebug ( ) &lt;&lt; "txid =" &lt;&lt; txid.toString ( );
}
</code></pre>

<p>The output is:</p>

<pre><code>tx   = "01000000017d45e6b1b88f710b5651321e1f2a66a2d61161f86c936cea66622f05c681c1210100000035104d79207365637265742070687261736523a82073d83ecabab6ba96a47f03d0e21ffbdfeaab5d337b7e93a0cc2e85019190fb3f87ffffffff01232e2f050000000017a914a89aec4cd53e6d74215332459b7fea3ec4aca9758700000000"
txid = "75e0904ca3f8895c6f6af45f1c7f3d1f9dcee32814ca144852bce669a5ee4421"
</code></pre>

<p>4) Now push the tx to the network: <a href="https://testnet.smartbit.com.au/txs/pushtx" rel="nofollow noreferrer">https://testnet.smartbit.com.au/txs/pushtx</a></p>

<p>5) Voila! <a href="https://testnet.smartbit.com.au/tx/75e0904ca3f8895c6f6af45f1c7f3d1f9dcee32814ca144852bce669a5ee4421" rel="nofollow noreferrer">https://testnet.smartbit.com.au/tx/75e0904ca3f8895c6f6af45f1c7f3d1f9dcee32814ca144852bce669a5ee4421</a> </p>

CC BY-SA 4.0
2018-06-17T11:49:26.600Z
Thanks a lot! I will accept your answer but I wish I can reproduce this, I guess is C++? Do you have any suggestion on where to start? I remember  a while ago about Visual C++ ( I am on windows) but not sure if it's what I need to reproduce your sample.
Robert
CC BY-SA 4.0
2018-06-17T18:50:14.050Z
Seconding this^.
Jan Janssen
CC BY-SA 4.0
2018-06-17T20:06:12.140Z
you are welcome to ask me any additional info/sources. i am not sure that understand your task correctly. my code is just a quick example
amaclin
CC BY-SA 4.0
2018-06-17T07:01:45.120Z
2018-06-17T07:53:05.390Z
2018-06-17T07:53:05.390Z
amaclin
<p>Suppose I have a redeemScript and I want to create a P2SH address corresponding to the hash of that redeemScript. How do I do it?</p>

<p>I was thinking of bitcore-lib but I'm not sure I am going in the right direction. Below is my staring point and I am not sure what to do next.</p>

<p>Also the <code>scriptPubKey</code> appears to be 0x04 and it has to be 0x05. </p>

<p>And how do I further generate my keys for this new address?</p>

<pre><code>&gt; var bitcore = require('bitcore-lib');
    undefined
    &gt; var redeemScript = bitcore.Script.fromASM('OP_SHA256 001xxxxxxxxxxxxxxxxxxxx6 OP_EQUAL');
    undefined
    &gt; var scriptPubKey = redeemScript.toScriptHashOut();
    undefined
    &gt; scriptPubKey
    &lt;Script: OP_HASH160 20 0x04xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx6 OP_EQUAL&gt;
    &gt; scriptPubKey.toAddress()
    3xxxxxxxxxxxx
    &gt; scriptPubKey.toHex()
    a914xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx287
</code></pre>

<p>Another option will be to use this: </p>

<p><a href="http://Bitcoinjs" rel="nofollow noreferrer">https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.js#L19</a></p>

<p>Thanks!</p>

CC BY-SA 4.0
2018-06-16T17:30:49.277Z
Do you really want to create ANYONE_CAN_SPEND address? (I can help you, but for Qt/C++, not for js)
amaclin
CC BY-SA 4.0
2018-06-16T17:46:11.817Z
This is for my thesis at University. Unfortunately I am still learning JS and I am afraid C++ will be too much over my head. Thank you.
Robert
<bitcoin-core>
How to create a P2SH address which will be the hash of a redeemScript
Robert
# How to create a P2SH address which will be the hash of a redeemScript

(Asked by: username_0 on 2018-06-16)

Suppose I have a redeemScript and I want to create a P2SH address corresponding to the hash of that redeemScript. How do I do it?

I was thinking of bitcore-lib but I'm not sure I am going in the right direction. Below is my staring point and I am not sure what to do next.

Also the `scriptPubKey` appears to be 0x04 and it has to be 0x05.

And how do I further generate my keys for this new address?

```
> var bitcore = require('bitcore-lib');
    undefined
    > var redeemScript = bitcore.Script.fromASM('OP_SHA256 001xxxxxxxxxxxxxxxxxxxx6 OP_EQUAL');
    undefined
    > var scriptPubKey = redeemScript.toScriptHashOut();
    undefined
    > scriptPubKey
    <Script: OP_HASH160 20 0x04xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx6 OP_EQUAL>
    > scriptPubKey.toAddress()
    3xxxxxxxxxxxx
    > scriptPubKey.toHex()
    a914xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx287

```

Another option will be to use this:

[https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.js#L19](http://Bitcoinjs)

Thanks!

**Tags:** bitcoin-core

> **Comments:**
> **username_1**: Do you really want to create ANYONE_CAN_SPEND address? (I can help you, but for Qt/C++, not for js)  
> **username_0**: This is for my thesis at University. Unfortunately I am still learning JS and I am afraid C++ will be too much over my head. Thank you.  

---

## 1 answers

### Answer by username_1 on 2018-06-17. Score: 3 | Accepted answer

Quick and dirty code for your task. Hope it helps.

1) Let us create address from secret.

```
static void p2sh_create ( )
{
  const MyByteArray secret ( QByteArray ( "My secret phrase" ) );
  const MyKey32 sha256 ( secret.sha256 ( ) );
  const MyByteArray redeemScript ( MyByteArray ( ).putInt8 ( OP_SHA256 ).putPush ( sha256 ).putInt8 ( OP_EQUAL ) );
  const MyKey20 hash160 ( redeemScript.hash160 ( ) );
  const QString address ( hash160.toStringP2SH ( ) );

  qDebug ( ) << "secret      =" << QString ( secret.toHex ( ) ) << "(in hex)";
  qDebug ( ) << "sha256      =" << sha256.toStringRev ( );
  qDebug ( ) << "redeemScript=" << QString ( redeemScript.toHex ( ) );
  qDebug ( ) << "hash160     =" << QString ( hash160.toHex ( ) );
  qDebug ( ) << "address     =" << address;
}

```

The output is:

```
secret      = "4d792073656372657420706872617365" (in hex)
sha256      = "73d83ecabab6ba96a47f03d0e21ffbdfeaab5d337b7e93a0cc2e85019190fb3f"
redeemScript= "a82073d83ecabab6ba96a47f03d0e21ffbdfeaab5d337b7e93a0cc2e85019190fb3f87"
hash160     = "fa3156d1e95111b0105fb538711bf8e19dadf1bd"
address     = "2NG482PHrj6f41UoqUHYGVkUR39oB3UvWkE"

```

2) Now let us fund the address `2NG482PHrj6f41UoqUHYGVkUR39oB3UvWkE`. Follow the link <https://testnet.coinfaucet.eu/en/> and ask for some test coins. Testnet faucet sent me 0.86978283 TBTC in transaction <https://testnet.smartbit.com.au/tx/21c181c6052f6266ea6c936cf86111d6a2662a1f1e3251560b718fb8b1e6457d>

3) Now create spending tx from this address.

```
static void p2sh_spend ( )
{
    const MyByteArray secret ( QByteArray ( "My secret phrase" ) );
    const MyKey32 sha256 ( secret.sha256 ( ) );
    const MyByteArray redeemScript ( MyByteArray ( ).putInt8 ( OP_SHA256 ).putPush ( sha256 ).putInt8 ( OP_EQUAL ) );
    const MyByteArray tx ( MyByteArray ( )
      .putInt32 ( TX_VERSION )
      .putInt8 ( 1 )
      .putArray ( MyKey32::fromHex ( "21c181c6052f6266ea6c936cf86111d6a2662a1f1e3251560b718fb8b1e6457d" ) )
      .putInt32 ( 1 )
      .putPrefixed ( MyByteArray ( ).putPush ( secret ).putPush ( redeemScript ) )
      .putInt32 ( TX_SEQUENCE )
      .putInt8 ( 1 )
      .putInt64 ( 86978283 - 200 )
      .putPrefixed ( MyByteArray ( ).putInt8 ( OP_HASH160 ).putPush ( MyKey20::of ( "2N8cj6kf3RkPHzQSCG13bvq6iXXWWPuGurZ" ) ).putInt8 ( OP_EQUAL ) )
      .putInt32 ( TX_LOCK ) );
    const MyKey32 txid ( tx.sha256d ( ) );

    qDebug ( ) << "tx   =" << QString ( tx.toHex ( ) );
    qDebug ( ) << "txid =" << txid.toString ( );
}

```

The output is:

```
tx   = "01000000017d45e6b1b88f710b5651321e1f2a66a2d61161f86c936cea66622f05c681c1210100000035104d79207365637265742070687261736523a82073d83ecabab6ba96a47f03d0e21ffbdfeaab5d337b7e93a0cc2e85019190fb3f87ffffffff01232e2f050000000017a914a89aec4cd53e6d74215332459b7fea3ec4aca9758700000000"
txid = "75e0904ca3f8895c6f6af45f1c7f3d1f9dcee32814ca144852bce669a5ee4421"

```

4) Now push the tx to the network: <https://testnet.smartbit.com.au/txs/pushtx>

5) Voila! <https://testnet.smartbit.com.au/tx/75e0904ca3f8895c6f6af45f1c7f3d1f9dcee32814ca144852bce669a5ee4421>

> **Comments:**
> **username_0**: Thanks a lot! I will accept your answer but I wish I can reproduce this, I guess is C++? Do you have any suggestion on where to start? I remember  a while ago about Visual C++ ( I am on windows) but not sure if it's what I need to reproduce your sample.  
> **username_2**: Seconding this^.  
> **username_1**: you are welcome to ask me any additional info/sources. i am not sure that understand your task correctly. my code is just a quick example  

---

