<p>I could not find anywhere on the web about it. I was hopeless until I noticed that I could, without modifying the library.</p>
<p>First choose a similar class in <a href="https://github.com/bitpay/bitcore/tree/master/packages/bitcore-lib/lib/transaction/input" rel="nofollow noreferrer">Bitcore-lib</a>.</p>
<ul>
<li><a href="https://github.com/bitpay/bitcore/blob/master/packages/bitcore-lib/lib/transaction/input/multisigscripthash.js" rel="nofollow noreferrer">multisigscripthash</a> (&lt;== suggested) or publickeyhash for P2SH outputs</li>
<li>multisig or publickey for bare script outputs (don't use these, these are nonstandard)</li>
</ul>
<p>Then copy it to a new file in your project such as <a href="https://github.com/simpleledger/BitcoinFilesJS/blob/d36aa7b76b3cc137ae600fe08e73573874a13f9d/lib/pushoutput.ts" rel="nofollow noreferrer">this one**</a>. Do not use typescript classes even if your project is of kind Typescript.</p>
<p>Then change <code>require</code> paths. Also require <code>inherit</code> from NPM. As in the boilerplates above, we need to inherit from <code>Transaction.Input</code></p>
<p>A function such as &quot;buildP2SHPushIn&quot; in the example should produce the redeemscript. Remember, the last element of P2SH redeem scripts is a push of the P2SH script.</p>
<p>And finally use <code>addInput</code> instead of the higher-level function <code>from</code> to add inputs as <a href="https://github.com/simpleledger/BitcoinFilesJS/blob/d36aa7b76b3cc137ae600fe08e73573874a13f9d/lib/bfp.ts#L412-L430" rel="nofollow noreferrer">shown here**</a></p>
<hr />
<p>**The examples are for Bitcoin Cash, but they can be used the same way in Bitcoin. Do not copy from them, however!</p>

CC BY-SA 4.0
2021-04-27T16:03:06.053Z
Well, the boilerplates listed in "First choose a similar class in Bitcore." are for BTC so I hope my answer is more useful than it is dangerous. I wasn't aware of that incident
MCCCS
CC BY-SA 4.0
2021-04-27T15:37:21.830Z
2021-04-27T16:06:07.113Z
2021-04-27T16:06:07.113Z
MCCCS
<p>My Javascript/typescript project uses bitcore-lib. It uses <code>Script.buildScriptHashOut</code> to build custom P2SH outputs. How do I spend from this custom output?</p>

<p2sh><bitcore><javascript>
How to spend from a custom P2SH script using bitcore-lib
MCCCS
# How to spend from a custom P2SH script using bitcore-lib

(Asked by: username_0 on 2021-04-27)

My Javascript/typescript project uses bitcore-lib. It uses `Script.buildScriptHashOut` to build custom P2SH outputs. How do I spend from this custom output?

**Tags:** p2sh, bitcore, javascript


---

## 1 answers

### Answer by username_0 on 2021-04-27. Score: 0

I could not find anywhere on the web about it. I was hopeless until I noticed that I could, without modifying the library.

First choose a similar class in [Bitcore-lib](https://github.com/bitpay/bitcore/tree/master/packages/bitcore-lib/lib/transaction/input).

* [multisigscripthash](https://github.com/bitpay/bitcore/blob/master/packages/bitcore-lib/lib/transaction/input/multisigscripthash.js) (<== suggested) or publickeyhash for P2SH outputs
* multisig or publickey for bare script outputs (don't use these, these are nonstandard)

Then copy it to a new file in your project such as [this one\*\*](https://github.com/simpleledger/BitcoinFilesJS/blob/d36aa7b76b3cc137ae600fe08e73573874a13f9d/lib/pushoutput.ts). Do not use typescript classes even if your project is of kind Typescript.

Then change `require` paths. Also require `inherit` from NPM. As in the boilerplates above, we need to inherit from `Transaction.Input`

A function such as "buildP2SHPushIn" in the example should produce the redeemscript. Remember, the last element of P2SH redeem scripts is a push of the P2SH script.

And finally use `addInput` instead of the higher-level function `from` to add inputs as [shown here\*\*](https://github.com/simpleledger/BitcoinFilesJS/blob/d36aa7b76b3cc137ae600fe08e73573874a13f9d/lib/bfp.ts#L412-L430)

---

\*\*The examples are for Bitcoin Cash, but they can be used the same way in Bitcoin. Do not copy from them, however!

> **Comments:**
> **username_0**: Well, the boilerplates listed in "First choose a similar class in Bitcore." are for BTC so I hope my answer is more useful than it is dangerous. I wasn't aware of that incident  

---

