<p>It seems the first question can be accomplished with something like:  </p>

<pre><code>//after creating newtx and specifying input and outputs  
newtx.Inputs[0].ScriptSig = privatekey.ScriptPubKey;  
newtx.Sign(privatekey, false);  //puts signature and pubkey in ScriptSig  
//use those plus additional parameters in script  
newtx.Inputs[0].ScriptSig = new Script(  
   newtx.Inputs[0].ScriptSig + " " +  
   //leave signature and pubkey as first parameters pushed onto stack  
   //then add additional parameter (type PubKey)  
   Op.GetPushOp(additionalParameter.ToBytes()) + " " +  
   //then add script whose hash was in original P2SH trans  
   Op.GetPushOp(scriptIn.ToBytes()));  
</code></pre>

<p>The scriptIn is the same as the usual P2KH script preceded by a check for the hash of another parameter  </p>

<pre><code>scriptIn = new Script(  
        "OP_HASH160 "  
        + Op.GetPushOp(additionalParameter.Hash.ToBytes())  
        + " OP_EQUALVERIFY"  
        + " OP_DUP"  
        + " OP_HASH160 "  
        + Op.GetPushOp(pubkeyhash.ToBytes())  
        + " OP_EQUALVERIFY"  
        + " OP_CHECKSIG"  
        );  
</code></pre>

<p>But when the transaction is sent it gets the following error:<br>
    16: mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)  </p>

<p>Not sure why it's failing.</p>

CC BY-SA 4.0
2023-12-19T06:36:59.833Z
the signature you are providing in `newtx.Inputs[0].ScriptSig` might not be valid for the script in `scriptIn`.
Mani T
CC BY-SA 4.0
2018-12-14T19:37:06.207Z
2018-12-14T19:59:54.693Z
2018-12-14T19:59:54.693Z
BPFL
<p>After creating a new NBitcoin transaction (with a single pay-to-script hash input and multiple outputs), is there any way to calculate the transaction signature and manually put it in the inputs[0].ScriptSig, along with other parameters used by the script?</p>

<p>A related issue is whether (and how) non-standard scripts can be used in P2SH transactions.</p>

<signature><non-standard-transaction><nbitcoin>
NBitcoin get signature, use spending P2SH transaction with non-standard script
BPFL
# NBitcoin get signature, use spending P2SH transaction with non-standard script

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

After creating a new NBitcoin transaction (with a single pay-to-script hash input and multiple outputs), is there any way to calculate the transaction signature and manually put it in the inputs[0].ScriptSig, along with other parameters used by the script?

A related issue is whether (and how) non-standard scripts can be used in P2SH transactions.

**Tags:** signature, non-standard-transaction, nbitcoin


---

## 1 answers

### Answer by username_0 on 2018-12-14. Score: 0

It seems the first question can be accomplished with something like:  
//after creating newtx and specifying input and outputs  
newtx.Inputs[0].ScriptSig = privatekey.ScriptPubKey;  
newtx.Sign(privatekey, false);  //puts signature and pubkey in ScriptSig  
//use those plus additional parameters in script  
newtx.Inputs[0].ScriptSig = new Script(  
   newtx.Inputs[0].ScriptSig + " " +  
   //leave signature and pubkey as first parameters pushed onto stack  
   //then add additional parameter (type PubKey)  
   Op.GetPushOp(additionalParameter.ToBytes()) + " " +  
   //then add script whose hash was in original P2SH trans  
   Op.GetPushOp(scriptIn.ToBytes()));  

The scriptIn is the same as the usual P2KH script preceded by a check for the hash of another parameter  
scriptIn = new Script(  
        "OP_HASH160 "  
        + Op.GetPushOp(additionalParameter.Hash.ToBytes())  
        + " OP_EQUALVERIFY"  
        + " OP_DUP"  
        + " OP_HASH160 "  
        + Op.GetPushOp(pubkeyhash.ToBytes())  
        + " OP_EQUALVERIFY"  
        + " OP_CHECKSIG"  
        );  

But when the transaction is sent it gets the following error:
    16: mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)  
Not sure why it's failing.

> **Comments:**
> **username_1**: the signature you are providing in `newtx.Inputs[0].ScriptSig` might not be valid for the script in `scriptIn`.  

---

