diff --git a/src/Neo.CLI/CLI/MainService.Tools.cs b/src/Neo.CLI/CLI/MainService.Tools.cs index 60de31493a..c000b48655 100644 --- a/src/Neo.CLI/CLI/MainService.Tools.cs +++ b/src/Neo.CLI/CLI/MainService.Tools.cs @@ -14,10 +14,12 @@ using Neo.Extensions; using Neo.IO; using Neo.SmartContract; +using Neo.VM; using Neo.Wallets; using System; using System.Collections.Generic; using System.Globalization; +using System.IO; using System.Linq; using System.Numerics; using System.Reflection; @@ -441,6 +443,58 @@ private static string Base64Fixed(string str) } } + /// + /// Base64 .nef file Analysis + /// + [ParseFunction("Base64 .nef file Analysis")] + private string? NefFileAnalyis(string base64) + { + byte[] nefData; + if (File.Exists(base64)) // extension name not considered + nefData = File.ReadAllBytes(base64); + else + { + try + { + nefData = Convert.FromBase64String(base64); + } + catch { return null; } + } + NefFile nef; + Script script; + bool verifyChecksum = false; + bool strictMode = false; + try + { + nef = NefFile.Parse(nefData, true); + verifyChecksum = true; + } + catch (FormatException) + { + nef = NefFile.Parse(nefData, false); + } + catch { return null; } + try + { + script = new Script(nef.Script, true); + strictMode = true; + } + catch (BadScriptException) + { + script = new Script(nef.Script, false); + } + catch { return null; } + string? result = ScriptsToOpCode(Convert.ToBase64String(nef.Script.ToArray())); + if (result == null) + return null; + string prefix = $"\r\n# Compiler: {nef.Compiler}"; + if (!verifyChecksum) + prefix += $"\r\n# Warning: Invalid .nef file checksum"; + if (!strictMode) + prefix += $"\r\n# Warning: Failed in {nameof(strictMode)}"; + return prefix + result; + } + /// /// Checks if the string is null or cannot be printed. ///