Beispiel #1
0
        public ActionResult ScriptCheck(string savedScript = null)
        {
            SavedScript script = null;
            Guid        id;

            if (!String.IsNullOrEmpty(savedScript) && Guid.TryParse(savedScript, out id))
            {
                try
                {
                    script = repo.GetScript(id);
                }
                catch
                {
                }
            }


            var model = new ScriptCheckModel()
            {
                ScriptPubKey = "OP_DUP OP_HASH160 <Alice.PubkeyHash> OP_EQUALVERIFY OP_CHECKSIG",
                ScriptSig    = "<Alice.Signature> <Alice.Pubkey>",
            };

            if (script != null)
            {
                model.SavedScriptLink = GetScriptLink(script.Id);
                model.ScriptPubKey    = script.ScriptPubKey;
                model.ScriptSig       = script.ScriptSig;
            }
            return(ScriptCheck(model));
        }
Beispiel #2
0
        public ActionResult SaveScript(ScriptCheckModel model)
        {
            SavedScript saved = new SavedScript();

            saved.ScriptPubKey = model.ScriptPubKey;
            saved.ScriptSig    = model.ScriptSig;
            repo.InsertScript(saved);
            return(RedirectToAction("ScriptCheck", "Home", new
            {
                savedScript = saved.Id.ToString()
            }));
        }
Beispiel #3
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));
        }