public static MyJson.JsonNode_Object Export(NeoModule module, byte[] script)
        {
            var sha256 = System.Security.Cryptography.SHA256.Create();

            byte[] hash256 = sha256.ComputeHash(script);
            var    hash    = CryptoUtils.RIPEMD160(hash256);

            var outjson = new MyJson.JsonNode_Object();

            //hash
            StringBuilder sb = new StringBuilder();

            sb.Append("0x");
            foreach (var b in hash.Reverse().ToArray())
            {
                sb.Append(b.ToString("x02"));
            }
            outjson.SetDictValue("hash", sb.ToString());

            //entrypoint
            outjson.SetDictValue("entrypoint", "Main");
            var mainmethod = module.mapMethods[module.mainMethod];

            if (mainmethod != null)
            {
                var name = mainmethod.displayName;
                outjson.SetDictValue("entrypoint", name);
            }
            //functions
            var funcsigns = new MyJson.JsonNode_Array();

            outjson["functions"] = funcsigns;

            List <string> names = new List <string>();

            foreach (var function in module.mapMethods)
            {
                var mm = function.Value;
                if (mm.inSmartContract == false)
                {
                    continue;
                }
                if (mm.isPublic == false)
                {
                    continue;
                }
                var ps       = mm.name.Split(new char[] { ' ', '(' }, StringSplitOptions.RemoveEmptyEntries);
                var funcsign = new MyJson.JsonNode_Object();

                funcsigns.Add(funcsign);
                var funcname = ps[1];
                if (funcname.IndexOf("::") > 0)
                {
                    var sps = funcname.Split(new string[] { "::" }, StringSplitOptions.RemoveEmptyEntries);
                    funcname = sps.Last();
                }
                funcsign.SetDictValue("name", function.Value.displayName);
                if (names.Contains(function.Value.displayName))
                {
                    throw new Exception("abi not allow same name functions");
                }
                names.Add(function.Value.displayName);
                MyJson.JsonNode_Array funcparams = new MyJson.JsonNode_Array();
                funcsign["parameters"] = funcparams;
                if (mm.paramtypes != null)
                {
                    foreach (var v in mm.paramtypes)
                    {
                        var ptype = ConvType(v.type);
                        var item  = new MyJson.JsonNode_Object();
                        funcparams.Add(item);

                        item.SetDictValue("name", v.name);
                        item.SetDictValue("type", ptype);
                    }
                }

                var rtype = ConvType(mm.returntype);
                funcsign.SetDictValue("returntype", rtype);
            }

            //events
            var eventsigns = new MyJson.JsonNode_Array();

            outjson["events"] = eventsigns;
            foreach (var events in module.mapEvents)
            {
                var mm = events.Value;

                var ps       = mm.name.Split(new char[] { ' ', '(' }, StringSplitOptions.RemoveEmptyEntries);
                var funcsign = new MyJson.JsonNode_Object();

                eventsigns.Add(funcsign);

                funcsign.SetDictValue("name", events.Value.displayName);
                MyJson.JsonNode_Array funcparams = new MyJson.JsonNode_Array();
                funcsign["parameters"] = funcparams;
                if (mm.paramtypes != null)
                {
                    foreach (var v in mm.paramtypes)
                    {
                        var ptype = ConvType(v.type);
                        var item  = new MyJson.JsonNode_Object();
                        funcparams.Add(item);

                        item.SetDictValue("name", v.name);
                        item.SetDictValue("type", ptype);
                    }
                }
                var rtype = ConvType(mm.returntype);
                funcsign.SetDictValue("returntype", rtype);
            }

            return(outjson);
        }
예제 #2
0
        //Console.WriteLine("helo ha:"+args[0]); //普通输出
        //Console.WriteLine("<WARN> 这是一个严重的问题。");//警告输出,黄字
        //Console.WriteLine("<WARN|aaaa.cs(1)> 这是ee一个严重的问题。");//警告输出,带文件名行号
        //Console.WriteLine("<ERR> 这是一个严重的问题。");//错误输出,红字
        //Console.WriteLine("<ERR|aaaa.cs> 这是ee一个严重的问题。");//错误输出,带文件名
        //Console.WriteLine("SUCC");//输出这个表示编译成功
        //控制台输出约定了特别的语法
        public static bool Execute(string filename, string filepdb, ILogger log)
        {
            string onlyname = System.IO.Path.GetFileNameWithoutExtension(filename);

            ILModule mod = new ILModule(log);

            System.IO.Stream fs    = null;
            System.IO.Stream fspdb = null;

            //open file
            try
            {
                fs = System.IO.File.OpenRead(filename);

                if (System.IO.File.Exists(filepdb))
                {
                    fspdb = System.IO.File.OpenRead(filepdb);
                }
            }
            catch (Exception err)
            {
                log.Log("Open File Error:" + err.ToString());
                return(false);
            }

            var exePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            var curPath = Directory.GetCurrentDirectory();

            Directory.SetCurrentDirectory(exePath);

            //load module
            try
            {
                mod.LoadModule(fs, fspdb);
            }
            catch (Exception err)
            {
                log.Log("LoadModule Error:" + err.ToString());
                return(false);
            }
            byte[] bytes       = null;
            bool   bSucc       = false;
            string jsonstr     = null;
            string debugmapstr = null;

            //convert and build
            try
            {
                var        conv   = new ModuleConverter(log);
                ConvOption option = new ConvOption();
                option.useNep8 = false;
                option.useSysCallInteropHash = false;
                NeoModule am = conv.Convert(mod, option);

                Directory.SetCurrentDirectory(curPath);

                bytes = am.Build();
                log.Log("convert succ");


                try
                {
                    var           outjson = FuncExport.Export(am, bytes);
                    StringBuilder sb      = new StringBuilder();
                    outjson.ConvertToStringWithFormat(sb, 0);
                    jsonstr = sb.ToString();
                    log.Log("gen abi succ");
                }
                catch (Exception err)
                {
                    log.Log("gen abi Error:" + err.ToString());
                }

                try
                {
                    var           outjson = DebugInfo.ExportDebugInfo(onlyname, am);
                    StringBuilder sb      = new StringBuilder();
                    outjson.ConvertToStringWithFormat(sb, 0);
                    debugmapstr = sb.ToString();
                    log.Log("gen debug map succ");
                }
                catch (Exception err)
                {
                    log.Log("gen debug map Error:" + err.ToString());
                }
            }
            catch (Exception err)
            {
                log.Log("Convert Error:" + err.ToString());
                return(false);
            }
            //write bytes
            try
            {
                string bytesname = onlyname + ".avm";

                System.IO.File.Delete(bytesname);
                System.IO.File.WriteAllBytes(bytesname, bytes);
                log.Log("write:" + bytesname);
                bSucc = true;
            }
            catch (Exception err)
            {
                log.Log("Write Bytes Error:" + err.ToString());
                return(false);
            }
            try
            {
                string abiname = onlyname + ".abi.json";

                System.IO.File.Delete(abiname);
                System.IO.File.WriteAllText(abiname, jsonstr);
                log.Log("write:" + abiname);
                bSucc = true;
            }
            catch (Exception err)
            {
                log.Log("Write abi Error:" + err.ToString());
                return(false);
            }
            try
            {
                string debugname = onlyname + ".debug.json";

                System.IO.File.Delete(debugname);
                System.IO.File.WriteAllText(debugname, debugmapstr);
                log.Log("write:" + debugname);
                bSucc = true;
            }
            catch (Exception err)
            {
                log.Log("Write abi Error:" + err.ToString());
                return(false);
            }
            try
            {
                fs.Dispose();
                if (fspdb != null)
                {
                    fspdb.Dispose();
                }
            }
            catch
            {
            }

            return(bSucc);
        }
예제 #3
0
        public static MyJson.JsonNode_Object ExportDebugInfo(string avmName, NeoModule module)
        {
            var outjson = new MyJson.JsonNode_Object();

            var           debugMap          = new List <DebugMapEntry>();
            DebugMapEntry currentDebugEntry = null;

            var fileMap = new Dictionary <string, int>();

            List <byte> bytes = new List <byte>();

            foreach (var c in module.total_Codes.Values)
            {
                if (c.debugcode != null && c.debugline > 0 && c.debugline < 2000)
                {
                    currentDebugEntry          = new DebugMapEntry();
                    currentDebugEntry.startOfs = debugMap.Count > 0 ? bytes.Count : 0;
                    currentDebugEntry.endOfs   = currentDebugEntry.startOfs;
                    currentDebugEntry.url      = c.debugcode;
                    currentDebugEntry.line     = c.debugline;

                    if (!fileMap.ContainsKey(c.debugcode))
                    {
                        fileMap[c.debugcode] = fileMap.Count + 1;
                    }

                    debugMap.Add(currentDebugEntry);
                }
                else
                if (currentDebugEntry != null)
                {
                    currentDebugEntry.endOfs = bytes.Count;
                }
                bytes.Add((byte)c.code);
                if (c.bytes != null)
                {
                    for (var i = 0; i < c.bytes.Length; i++)
                    {
                        bytes.Add(c.bytes[i]);
                    }
                }
            }

            var hash = CalculateMD5(bytes.ToArray());

            string compilerName = System.AppDomain.CurrentDomain.FriendlyName.ToLowerInvariant();
            var    version      = Assembly.GetEntryAssembly().GetName().Version.ToString();

            var avmInfo = new MyJson.JsonNode_Object();

            avmInfo.Add("name", new MyJson.JsonNode_ValueString(avmName));
            avmInfo.Add("hash", new MyJson.JsonNode_ValueString(hash));

            var compilerInfo = new MyJson.JsonNode_Object();

            compilerInfo.Add("name", new MyJson.JsonNode_ValueString(compilerName));
            compilerInfo.Add("version", new MyJson.JsonNode_ValueString(version));

            var fileInfo = new MyJson.JsonNode_Array();

            foreach (var entry in fileMap)
            {
                var fileEntry = new MyJson.JsonNode_Object();
                fileEntry.Add("id", new MyJson.JsonNode_ValueNumber(entry.Value));
                fileEntry.Add("url", new MyJson.JsonNode_ValueString(entry.Key));
                fileInfo.AddArrayValue(fileEntry);
            }

            var mapInfo = new MyJson.JsonNode_Array();

            foreach (var entry in debugMap)
            {
                if (!fileMap.ContainsKey(entry.url))
                {
                    continue;
                }

                var fileID = fileMap[entry.url];

                var mapEntry = new MyJson.JsonNode_Object();
                mapEntry.Add("start", new MyJson.JsonNode_ValueNumber(entry.startOfs));
                mapEntry.Add("end", new MyJson.JsonNode_ValueNumber(entry.endOfs));
                mapEntry.Add("file", new MyJson.JsonNode_ValueNumber(fileID));
                mapEntry.Add("line", new MyJson.JsonNode_ValueNumber(entry.line));
                mapInfo.AddArrayValue(mapEntry);
            }

            outjson["avm"]      = avmInfo;
            outjson["compiler"] = compilerInfo;
            outjson["files"]    = fileInfo;
            outjson["map"]      = mapInfo;

            return(outjson);
        }