How to add a scriptSig to a P2SH transaction?

#1

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>
<html>
<head></head>
<body>
    <script src="bitcore/bitcore.min.js"></script>
    <script src="bitcore-explorers/bitcore-explorers.min.js"></script>
    <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
        getScripts();
        
        //utxos
        var g_utxos = [{"address":"2MwEv1p64YJJowC8HzUtsBDyweKWgutmqv1","txid":"cb0573919f768f8bead54168b9ab49de3ef811d168e7d0fbe41008b9ce7eff40","vout":0,"ts":1426187331,"scriptPubKey":"a9142bd17fded06462cfad6402f4b86ccc06e5c543e587","amount":0.01,"confirmations":4,"confirmationsFromCache":false}];
        
        createTransaction();
            
        //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 = transaction.to(toAddress, amount);
            
            transaction = transaction.applySignature(unlockingScript);
            
            return transaction;
        }
        
    </script>
</body>
</html>
0 Likes

(esteban) #2

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

0 Likes

#3

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 = transaction.to(toAddress, amount);
            transaction = transaction.addInput(input, unlockingScript, 1000000);
            
            var verify = transaction.verify();
            
            return transaction;
}
0 Likes

(esteban) #4

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.

0 Likes

#5

Thanks for the helpful tip about toSatoshis().

0 Likes

#6

This is a link for a wrapup on this and related posts see:
manual-creation-of-custom-p2sh-transaction

0 Likes

(Barney) #7

Here http://djangostars.com/blog/ one of the best developers blogs. Helped me couple times, may be useful for you. Good luck!

0 Likes