Esempio n. 1
0
            /*
             *      case "CHECKLOCKTIMEVERIFY":
             *          flags |= ScriptVerifyCheckLockTimeVerify
             *  case "CHECKSEQUENCEVERIFY":
             *          flags |= ScriptVerifyCheckSequenceVerify
             *  case "CLEANSTACK":
             *          flags |= ScriptVerifyCleanStack
             *  case "DERSIG":
             *          flags |= ScriptVerifyDERSignatures
             *  case "DISCOURAGE_UPGRADABLE_NOPS":
             *          flags |= ScriptDiscourageUpgradableNops
             *  case "LOW_S":
             *          flags |= ScriptVerifyLowS
             *  case "MINIMALDATA":
             *          flags |= ScriptVerifyMinimalData
             *  case "NONE":
             *          // Nothing.
             *  case "P2SH":
             *          flags |= ScriptBip16
             *  case "SIGPUSHONLY":
             *          flags |= ScriptVerifySigPushOnly
             *  case "STRICTENC":
             *          flags |= ScriptVerifyStrictEncoding
             *  case "SHA256":
             *          flags |= ScriptVerifySHA256
             *  default:
             *
             */

            private ParsedOpCode[] ParseOpcodes(string raw)
            {
                try
                {
                    var tokens = raw.Split(' ', '\t', '\n')
                                 .Select(s => s.Trim())
                                 .Where(s => s.Length > 0)
                                 .ToArray();

                    var builder = new ScriptBuilder();

                    for (int i = 0; i < tokens.Length; i++)
                    {
                        var token = tokens[i];
                        try
                        {
                            if (_hexRegex.IsMatch(token))
                            {
                                var match = _hexRegex.Match(token);
                                var isHex = match.Groups["prefix"]?.Value == "0x";
                                var value = match.Groups["value"].Value.Trim();

                                if (isHex)
                                {
                                    builder.AddRawScriptBytes(Hex.Decode(value));
                                }
                                else
                                {
                                    builder.AddInt64(long.Parse(value));
                                }
                            }

                            else if (_pushLiteralRegex.IsMatch(token))
                            {
                                var data = _pushLiteralRegex.Match(token).Groups["data"].Value.Trim();
                                builder.AddData(data);
                            }

                            else
                            {
                                var opCode = ParseOpcode(token);
                                builder.AddOpCode(opCode);
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                            throw new Exception("Token: " + token, e);
                        }
                    }

                    return(builder.ToScript().ParsedOpCodes);
                }
                catch (Exception e)
                {
                    throw new Exception("ParseOpCode fail.  Raw: " + raw, e);
                }
            }