示例#1
0
        public static ScriptSigs CombineSignatures(Script scriptPubKey, TransactionChecker checker, ScriptSigs input1, ScriptSigs input2)
        {
            if (scriptPubKey == null)
            {
                scriptPubKey = new Script();
            }

            var         scriptSig1  = input1.ScriptSig;
            var         scriptSig2  = input2.ScriptSig;
            HashVersion hashVersion = HashVersion.Original;
            var         isWitness   = input1.WitSig != WitScript.Empty || input2.WitSig != WitScript.Empty;

            if (isWitness)
            {
                scriptSig1  = input1.WitSig.ToScript();
                scriptSig2  = input2.WitSig.ToScript();
                hashVersion = HashVersion.Witness;
            }

            var context = new ScriptEvaluationContext();

            context.ScriptVerify = ScriptVerify.StrictEnc;
            context.EvalScript(scriptSig1, checker, hashVersion);

            var stack1 = context.Stack.AsInternalArray();

            context = new ScriptEvaluationContext();
            context.ScriptVerify = ScriptVerify.StrictEnc;
            context.EvalScript(scriptSig2, checker, hashVersion);

            var stack2 = context.Stack.AsInternalArray();
            var result = CombineSignatures(scriptPubKey, checker, stack1, stack2, hashVersion);

            if (result == null)
            {
                return(scriptSig1.Length < scriptSig2.Length ? input2 : input1);
            }
            if (!isWitness)
            {
                return new ScriptSigs()
                       {
                           ScriptSig = result,
                           WitSig    = WitScript.Empty
                       }
            }
            ;
            else
            {
                return(new ScriptSigs()
                {
                    ScriptSig = input1.ScriptSig.Length < input2.ScriptSig.Length ? input2.ScriptSig : input1.ScriptSig,
                    WitSig = new WitScript(result)
                });
            }
        }
示例#2
0
        public bool VerifyScript(Script scriptSig, Script scriptPubKey, Transaction txTo, int nIn)
        {
            ScriptEvaluationContext evaluationCopy = null;

            if (!EvalScript(scriptSig, txTo, nIn))
            {
                return(false);
            }
            if ((ScriptVerify & ScriptVerify.P2SH) != 0)
            {
                evaluationCopy = Clone();
            }
            if (!EvalScript(scriptPubKey, txTo, nIn))
            {
                return(false);
            }

            if (Result == null || Result.Value == false)
            {
                return(false);
            }

            // Additional validation for spend-to-script-hash transactions:
            if (((ScriptVerify & ScriptVerify.P2SH) != 0) && scriptPubKey.IsPayToScriptHash)
            {
                this.Load(evaluationCopy);
                evaluationCopy = this;
                if (!scriptSig.IsPushOnly)
                {
                    return(false);
                }

                // stackCopy cannot be empty here, because if it was the
                // P2SH  HASH <> EQUAL  scriptPubKey would be evaluated with
                // an empty stack and the EvalScript above would return false.
                if (evaluationCopy.Stack.Count == 0)
                {
                    throw new InvalidProgramException("stackCopy cannot be empty here");
                }

                var    pubKeySerialized = evaluationCopy.Stack.Pop();
                Script pubKey2          = new Script(pubKeySerialized);

                if (!evaluationCopy.EvalScript(pubKey2, txTo, nIn))
                {
                    return(false);
                }

                return(evaluationCopy.Result != null && evaluationCopy.Result.Value);
            }
            return(true);
        }
示例#3
0
        public static Script CombineSignatures(Script scriptPubKey, Transaction transaction, int n, Script scriptSig1, Script scriptSig2)
        {
            if (scriptPubKey == null)
            {
                scriptPubKey = new Script();
            }
            ScriptEvaluationContext context = new ScriptEvaluationContext();

            context.ScriptVerify = ScriptVerify.StrictEnc;
            context.EvalScript(scriptSig1, transaction, n);

            var stack1 = context.Stack.Reverse().ToArray();

            context = new ScriptEvaluationContext();
            context.ScriptVerify = ScriptVerify.StrictEnc;
            context.EvalScript(scriptSig2, transaction, n);

            var stack2 = context.Stack.Reverse().ToArray();

            return(CombineSignatures(scriptPubKey, transaction, n, stack1, stack2));
        }