public void SignInputs(Transaction transaction, SignerBag bag) { foreach (var transactionInput in transaction.Inputs) { var redeem = bag.Find(transactionInput.Outpoint); Thrower.If(redeem.IsNull()).Throw <TransactionException>("redeem script was not found"); var redeemScript = new Script.Script(CryptoUtil.ConvertHex(redeem.ScriptPubKeyHex)); var signature = this.CalculateSignature(transaction, transactionInput.Outpoint, redeem.PrivateKey.Key, redeemScript, Transaction.SigHash.All); var signedScript = ScriptBuilder.CreateInputScript(signature, redeem.PrivateKey.Key); transactionInput.ScriptBytes = signedScript.GetProgram(); } }
public TransactionSignature CalculateSignature(Transaction transaction, TransactionOutPoint outPoint, EcKey key, Script.Script redeemScript, Transaction.SigHash hashType) { // at the moment only signing all the outputs is supported Thrower.If(hashType != Transaction.SigHash.All).Throw <TransactionException>("Only SigHash type 'All' supported"); //// clone the transaction and clear all the inputs //// only the inputs for the equivalent output needs to be present for signing var signTx = transaction.Clone(); signTx.Inputs.ForEach(input => input.ScriptBytes = Enumerable.Empty <byte>().ToArray()); // set the redeem script and clear it of 'OP_CODESEPARATOR' var connectedScript = redeemScript.GetProgram(); var redeemConnectedScript = Script.Script.RemoveAllInstancesOfOp(connectedScript, ScriptOpCodes.OP_CODESEPARATOR); signTx.FindInput(outPoint).ScriptBytes = redeemConnectedScript; // serialize then hash the transaction to HEX and sign it. var trxHex = this.Serializer.ToHex(signTx, hashType); var hash = CryptoUtil.Sha256HashTwice(CryptoUtil.ConvertHex(trxHex)); return(new TransactionSignature(key.Sign(hash), hashType)); }