How to add a scriptSig to a P2SH transaction?

I am trying to add a scriptSig to a P2SH (not multi-signature) transaction. Any help with this would be appreciated.

I created the scriptSig using Bitcore’s Script. I am trying to add it to a transaction by doing:

transaction = transaction.applySignature(unlockingScript);

I am getting this error message:

TypeError: this.inputs[e.inputIndex] is undefined
bitcore.min.js (line 4, col 30850)
…otype.applySignature=function(e){return this.inputs[e.inputIndex].addSignature(t…

Here is the code that I have written:

<!DOCTYPE html>
    <script src="bitcore/bitcore.min.js"></script>
    <script src="bitcore-explorers/bitcore-explorers.min.js"></script>
        var bitcore = require('bitcore');
        bitcore.Transaction = require('bitcore').Transaction;
        bitcore.Script = require('bitcore').Script;
        bitcore.Buffer = require('bitcore').Buffer;        
        var network = "testnet";
        var lockingScript = "";
        var unlockingScript = "";        
        var holdingAddress = "";
        var toAddress = "n1yigjdP1sKUm6n3ZvdAjA5xwY3MeyiMNZ";
        var amount = 990000;
        var transaction = "";

        //get scripts
        var g_utxos = [{"address":"2MwEv1p64YJJowC8HzUtsBDyweKWgutmqv1","txid":"cb0573919f768f8bead54168b9ab49de3ef811d168e7d0fbe41008b9ce7eff40","vout":0,"ts":1426187331,"scriptPubKey":"a9142bd17fded06462cfad6402f4b86ccc06e5c543e587","amount":0.01,"confirmations":4,"confirmationsFromCache":false}];
        //create script using bitcore
        function getScripts(){
            var Buffer = require('bitcore').Buffer;
            var Script = require('bitcore').Script;

            //create redeam script
            var redeamScript = new Script();
            redeamScript.add(new bitcore.deps.Buffer("87Pmu6Sh7L8AGg7Ee3dn OP_EQUAL"));

            //create locking script using toScriptHashOut()
            var initialLockingScript = redeamScript.toScriptHashOut();
            lockingScript = new Script();
            lockingScript.add(new bitcore.deps.Buffer("OP_HASH160"));
            var hashedRedeamScriptBuffer = initialLockingScript.chunks[1].buf;
            lockingScript.add(new bitcore.deps.Buffer(hashedRedeamScriptBuffer));
            lockingScript.add(new bitcore.deps.Buffer("OP_EQUAL"));

            //create unlocking script
            unlockingScript = new Script();            
            unlockingScript.add(new bitcore.deps.Buffer("87Pmu6Sh7L8AGg7Ee3dn"));
            unlockingScript.add(new bitcore.deps.Buffer("87Pmu6Sh7L8AGg7Ee3dn OP_EQUAL"));
            //verify unlocking and locking script
            var verified = bitcore.Script.Interpreter().verify(unlockingScript, lockingScript);
        //create transactions
        function createTransaction(){            
            var transaction = new bitcore.Transaction();
            transaction = transaction.from(g_utxos);
            transaction =, amount);
            transaction = transaction.applySignature(unlockingScript);
            return transaction;

You can just use transaction.addInput(input), no need to use “sign” as in this case it wouldn’t make much sense…

Thanks for the suggestion to use addInput. It worked and I was able to build a transaction that returned true when its verify method was called.

Here is working rewritten function:

function createTransaction(){            
            var prevTxId = new bitcore.deps.Buffer(g_utxos[0].txid);
            var outputIndex = g_utxos[0].vout
            var sequenceNumber = 4294967295;
            var param = {};
            param.prevTxId = prevTxId;
            param.outputIndex = outputIndex;
            param.sequenceNumber = sequenceNumber;
            param.script = unlockingScript;
            var input = new bitcore.Transaction.Input(param);
            var transaction = new bitcore.Transaction();
            transaction = transaction.fee(0.0001*100000000);
            transaction =, amount);
            transaction = transaction.addInput(input, unlockingScript, 1000000);
            var verify = transaction.verify();
            return transaction;

I’m glad you were able to make it work! I think I’d add some documentation regarding addInput on the official docs.

Just a comment on this:

I’d use bitcore.Unit.fromBTC(0.0001).toSatoshis() for stuff like that, to make sure I’m not making any silly mistakes.

Thanks for the helpful tip about toSatoshis().

This is a link for a wrapup on this and related posts see:

Here one of the best developers blogs. Helped me couple times, may be useful for you. Good luck!