Skip to content

Commit

Permalink
parse nef file scripts (#3482)
Browse files Browse the repository at this point in the history
* parse nef file scripts

* nef file path support

---------

Co-authored-by: Vitor Nazário Coelho <vncoelho@gmail.com>
  • Loading branch information
Hecate2 and vncoelho authored Sep 13, 2024
1 parent ff58726 commit c6282cd
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/Neo.CLI/CLI/MainService.Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -441,6 +443,58 @@ private static string Base64Fixed(string str)
}
}

/// <summary>
/// Base64 .nef file Analysis
/// </summary>
[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;
}

/// <summary>
/// Checks if the string is null or cannot be printed.
/// </summary>
Expand Down

0 comments on commit c6282cd

Please sign in to comment.