Пример #1
0
        public ActionResult ScriptCheck(ScriptCheckModel model)
        {
            if (!string.IsNullOrEmpty(model.Share))
            {
                return(SaveScript(model));
            }

            model.ScriptPubKey = model.ScriptPubKey ?? "";
            model.ScriptSig    = model.ScriptSig ?? "";
            bool parseProblem = false;

            Dictionary <string, Keyset> sets = new Dictionary <string, Keyset>();

            try
            {
                model.ExecutedScriptPubKey = GetExecutedScript(model.ScriptPubKey, Script.Empty, sets);
            }
            catch (FormatException)
            {
                ModelState.AddModelError("ScriptPubKey", "Parsing error");
                parseProblem = true;
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("ScriptPubKey", ex.Message);
                parseProblem = true;
            }

            try
            {
                model.ExecutedScriptSig = GetExecutedScript(model.ScriptSig, model.ExecutedScriptPubKey ?? Script.Empty, sets);
            }
            catch (FormatException)
            {
                ModelState.AddModelError("ScriptSig", "Parsing error");
                parseProblem = true;
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("ScriptSig", ex.Message);
                parseProblem = true;
            }

            if (parseProblem)
            {
                return(View(model));
            }

            ScriptEvaluationContext ctx = new ScriptEvaluationContext();

            model.Result = new ScriptResultModel();
            var tx = Keyset.DummyTransaction();

            model.Result.Success     = ctx.VerifyScript(model.ExecutedScriptSig, model.ExecutedScriptPubKey, tx, 0, Money.Zero);
            model.Result.Error       = ctx.Error.ToString();
            model.Result.StackValues = ctx.Stack.Select(b =>
            {
                var hex     = Encoders.Hex.EncodeData(b);
                var boolean = CastToBool(b);
                var bignum  = Utils.BytesToBigInteger(b);
                return(new StackValueModel()
                {
                    Bool = boolean.ToString(),
                    Hex = hex,
                    Number = bignum.ToString()
                });
            }).ToArray();

            model.Result.CheckSigs = ctx.SignedHashes.Select(b =>
            {
                return(new CheckSigsModel()
                {
                    SignedHash = b.Hash,
                    ScriptCode = b.ScriptCode,
                    Signature = Encoders.Hex.EncodeData(b.Signature.ToBytes())
                });
            }).ToArray();
            tx.Inputs[0].ScriptSig = model.ExecutedScriptSig;
            model.Transaction      = tx.ToHex();
            return(View(model));
        }