Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse nef file scripts #3482

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
AnnaShaleva marked this conversation as resolved.
Show resolved Hide resolved
{
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
Loading