<p>bitcoinjs-lib has examples for <a href="https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.js#L115" rel="nofollow noreferrer">multisig P2SH</a>, <a href="https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.js#L151" rel="nofollow noreferrer">single-key Segwit wrapped in P2SH</a>, and <a href="https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.js#L183" rel="nofollow noreferrer">multisig Segwit wrapped in P2SH</a>.</p>

CC BY-SA 4.0
2018-06-15T13:44:25.703Z
Thanks! I think this is what I am looking for https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.js#L151. So this is how you create a P2SH address from the redeemScript hashed? Also it says 'bitcoin.ECPair.fromWIF('cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87JcbXMTcA' The WIF is the redeemScript between ''?
Robert
CC BY-SA 4.0
2018-06-15T13:58:49.983Z
Multisig addresses still require private keys. Here, you have multiple private keys in WIF, which are used to build a redeem script (a multisig redeem script is just `m <pubkeys> n OP_CHECKMULTISIG` for an m of n multisig, so you can derive the public keys from the private key.
Raghav Sood
CC BY-SA 4.0
2018-06-15T14:03:00.117Z
I understand multisig. That's not what I meant. I am referring to TRXO looking like HASH160 PUSHDATA(20)[xxxxxxxxxxxxx] EQUAL, no multisig required.
Robert
CC BY-SA 4.0
2018-06-15T14:05:29.013Z
That's the locking script for the output. When combined with the signatures and redeem script, it forms a complete script. In essence, the signatures following by the redeem script will first check that the multisig requirements are met. Then, the redeemscript is HASH160'd, which is then compared to the push(20) to ensure that the redeem script corresponds to the address
Raghav Sood
CC BY-SA 4.0
2018-06-15T14:54:44.547Z
I see but my question is about non-multisigs. I am trying understand TRXO's of the "type": "scripthash" NOT type": "multyisig". How are those making use of the redeemScript?
Robert
CC BY-SA 4.0
2018-06-15T15:36:08.080Z
Essentially the same way. Any P2SH address is a hash of the redeem script. Any tx spending that must supply the redeem script so that it can be hashed and OP_EQUAL'd against what's in the address. Additionally, they must supply any information required to evaluate the redeem script to true. In the case of multisig, this is signatures. You can make other P2SH addresses that require a math problem, or the preimage for another hash, or any number of other things
Raghav Sood
CC BY-SA 4.0
2018-06-15T21:29:31.167Z
Thank you very much for your help. One last thing, I know it sounds silly: how exactly can I create a P2SH address from let's say a string of numbers and letters? How can I hash that string and then test it against another address to see if I can get the OP_EQUAL on the stack? My guess is that I have to use an API like Node and Bitcoinjs like you suggested.
Robert
CC BY-SA 4.0
2018-06-16T00:20:23.850Z
The hash portion is simply HASH_160. Assuming your string of numbers and letters are a valid redeem script, you can pass it through HASH_160 to get the hash, and wrap it in the P2SH version code (0x05). You could technically do this with any random string, but you will never be able to spend from it unless it's a valid redeem script
Raghav Sood
CC BY-SA 4.0
2018-06-15T13:21:55.730Z
2018-06-15T13:21:55.730Z
Raghav Sood
<p>I have the following example, on which I admit I am stuck:</p>

<p>Bob sends 0.5 BTC to Carla, who is the owner of a P2SH address of the form 3xxxxxx. </p>

<p>For Carla to spend the 0.5 BTC and pay Josh, who also has a 3xxxxxx P2SH address, she needs the redeemScript from Bob. </p>

<p>Let's say she has that from Bob and Carla sends to Josh the 0.5 BTC. Now we have a TRXO of 0.5 which can be seen on blockchain explorer under Josh's address. </p>

<p>To spend the TRXO, Josh also needs the redeemScript, right? Is it the same one as Bob's redeemScript?</p>

<p>According to Andreas's book, the redeemScript required for spending from a P2SH address is revealed after the first Output spent from the funding address.  </p>

<p>So simply by looking at the scriptSig: under Bob's INPUT (e.g. PUSHDATA(66)[xxxxxxxxxxxxxxxxxxxxx], and taking the redeemScript from there anyone can hash that and spend the 0.5 BTC? If so, how?</p>

<p>The TRXO is not based on publickey as in the case of addresses of the form 1xxxx so it's not associated with Josh's address but rather with anyone who can produce a hash of the redeemScript.   </p>

<p>This below explains a lot in previous chapters but the practical side of the article is still in draft.  </p>

<p><a href="https://github.com/ChristopherA/Learning-Bitcoin-from-the-Command-Line/blob/master/08_5_Spending_a_P2SH_Transaction.md" rel="nofollow noreferrer">https://github.com/ChristopherA/Learning-Bitcoin-from-the-Command-Line/blob/master/08_5_Spending_a_P2SH_Transaction.md</a></p>

<p>The [draft] side is what I want to understand. I think this is very important because many addresses are P2SH these days. Most are multisig, yes, very secure. But if it's not multisig, isn't it a vulnerability to access the redeemScript just by looking at a previous TRX INPUT? </p>

<p>I use Node JS and can access libs, on Windows + Bitcoin core.I want to test these on testnet and maybe with a small amount on Livenet.  </p>

<p>Thanks!</p>

CC BY-SA 4.0
2021-04-27T15:38:25.010Z
In the answer for https://bitcoin.stackexchange.com/questions/105796/how-to-spend-from-a-custom-p2sh-script-using-bitcore-lib/105797#105797 I gave the instructions for bitcore-lib
MCCCS
<bitcoin-core>
Where can I find examples of Node JS code for P2SH transactions (funding and spending)
Robert
# Where can I find examples of Node JS code for P2SH transactions (funding and spending)

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

I have the following example, on which I admit I am stuck:

Bob sends 0.5 BTC to Carla, who is the owner of a P2SH address of the form 3xxxxxx.

For Carla to spend the 0.5 BTC and pay Josh, who also has a 3xxxxxx P2SH address, she needs the redeemScript from Bob.

Let's say she has that from Bob and Carla sends to Josh the 0.5 BTC. Now we have a TRXO of 0.5 which can be seen on blockchain explorer under Josh's address.

To spend the TRXO, Josh also needs the redeemScript, right? Is it the same one as Bob's redeemScript?

According to Andreas's book, the redeemScript required for spending from a P2SH address is revealed after the first Output spent from the funding address.

So simply by looking at the scriptSig: under Bob's INPUT (e.g. PUSHDATA(66)[xxxxxxxxxxxxxxxxxxxxx], and taking the redeemScript from there anyone can hash that and spend the 0.5 BTC? If so, how?

The TRXO is not based on publickey as in the case of addresses of the form 1xxxx so it's not associated with Josh's address but rather with anyone who can produce a hash of the redeemScript.

This below explains a lot in previous chapters but the practical side of the article is still in draft.

<https://github.com/ChristopherA/Learning-Bitcoin-from-the-Command-Line/blob/master/08_5_Spending_a_P2SH_Transaction.md>

The [draft] side is what I want to understand. I think this is very important because many addresses are P2SH these days. Most are multisig, yes, very secure. But if it's not multisig, isn't it a vulnerability to access the redeemScript just by looking at a previous TRX INPUT?

I use Node JS and can access libs, on Windows + Bitcoin core.I want to test these on testnet and maybe with a small amount on Livenet.

Thanks!

**Tags:** bitcoin-core

> **Comments:**
> **username_1**: In the answer for https://bitcoin.stackexchange.com/questions/105796/how-to-spend-from-a-custom-p2sh-script-using-bitcore-lib/105797#105797 I gave the instructions for bitcore-lib  

---

## 1 answers

### Answer by username_2 on 2018-06-15. Score: 0 | Accepted answer

bitcoinjs-lib has examples for [multisig P2SH](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.js#L115), [single-key Segwit wrapped in P2SH](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.js#L151), and [multisig Segwit wrapped in P2SH](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.js#L183).

> **Comments:**
> **username_0**: Thanks! I think this is what I am looking for https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.js#L151. So this is how you create a P2SH address from the redeemScript hashed? Also it says 'bitcoin.ECPair.fromWIF('cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87JcbXMTcA' The WIF is the redeemScript between ''?  
> **username_2**: Multisig addresses still require private keys. Here, you have multiple private keys in WIF, which are used to build a redeem script (a multisig redeem script is just `m <pubkeys> n OP_CHECKMULTISIG` for an m of n multisig, so you can derive the public keys from the private key.  
> **username_0**: I understand multisig. That's not what I meant. I am referring to TRXO looking like HASH160 PUSHDATA(20)[xxxxxxxxxxxxx] EQUAL, no multisig required.  
> **username_2**: That's the locking script for the output. When combined with the signatures and redeem script, it forms a complete script. In essence, the signatures following by the redeem script will first check that the multisig requirements are met. Then, the redeemscript is HASH160'd, which is then compared to the push(20) to ensure that the redeem script corresponds to the address  
> **username_0**: I see but my question is about non-multisigs. I am trying understand TRXO's of the "type": "scripthash" NOT type": "multyisig". How are those making use of the redeemScript?  
> **username_2**: Essentially the same way. Any P2SH address is a hash of the redeem script. Any tx spending that must supply the redeem script so that it can be hashed and OP_EQUAL'd against what's in the address. Additionally, they must supply any information required to evaluate the redeem script to true. In the case of multisig, this is signatures. You can make other P2SH addresses that require a math problem, or the preimage for another hash, or any number of other things  
> **username_0**: Thank you very much for your help. One last thing, I know it sounds silly: how exactly can I create a P2SH address from let's say a string of numbers and letters? How can I hash that string and then test it against another address to see if I can get the OP_EQUAL on the stack? My guess is that I have to use an API like Node and Bitcoinjs like you suggested.  
> **username_2**: The hash portion is simply HASH_160. Assuming your string of numbers and letters are a valid redeem script, you can pass it through HASH_160 to get the hash, and wrap it in the P2SH version code (0x05). You could technically do this with any random string, but you will never be able to spend from it unless it's a valid redeem script  

---

