Skip to content

Commit c6282cd

Browse files
Hecate2vncoelho
andauthored
parse nef file scripts (#3482)
* parse nef file scripts * nef file path support --------- Co-authored-by: Vitor Nazário Coelho <[email protected]>
1 parent ff58726 commit c6282cd

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/Neo.CLI/CLI/MainService.Tools.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
using Neo.Extensions;
1515
using Neo.IO;
1616
using Neo.SmartContract;
17+
using Neo.VM;
1718
using Neo.Wallets;
1819
using System;
1920
using System.Collections.Generic;
2021
using System.Globalization;
22+
using System.IO;
2123
using System.Linq;
2224
using System.Numerics;
2325
using System.Reflection;
@@ -441,6 +443,58 @@ private static string Base64Fixed(string str)
441443
}
442444
}
443445

446+
/// <summary>
447+
/// Base64 .nef file Analysis
448+
/// </summary>
449+
[ParseFunction("Base64 .nef file Analysis")]
450+
private string? NefFileAnalyis(string base64)
451+
{
452+
byte[] nefData;
453+
if (File.Exists(base64)) // extension name not considered
454+
nefData = File.ReadAllBytes(base64);
455+
else
456+
{
457+
try
458+
{
459+
nefData = Convert.FromBase64String(base64);
460+
}
461+
catch { return null; }
462+
}
463+
NefFile nef;
464+
Script script;
465+
bool verifyChecksum = false;
466+
bool strictMode = false;
467+
try
468+
{
469+
nef = NefFile.Parse(nefData, true);
470+
verifyChecksum = true;
471+
}
472+
catch (FormatException)
473+
{
474+
nef = NefFile.Parse(nefData, false);
475+
}
476+
catch { return null; }
477+
try
478+
{
479+
script = new Script(nef.Script, true);
480+
strictMode = true;
481+
}
482+
catch (BadScriptException)
483+
{
484+
script = new Script(nef.Script, false);
485+
}
486+
catch { return null; }
487+
string? result = ScriptsToOpCode(Convert.ToBase64String(nef.Script.ToArray()));
488+
if (result == null)
489+
return null;
490+
string prefix = $"\r\n# Compiler: {nef.Compiler}";
491+
if (!verifyChecksum)
492+
prefix += $"\r\n# Warning: Invalid .nef file checksum";
493+
if (!strictMode)
494+
prefix += $"\r\n# Warning: Failed in {nameof(strictMode)}";
495+
return prefix + result;
496+
}
497+
444498
/// <summary>
445499
/// Checks if the string is null or cannot be printed.
446500
/// </summary>

0 commit comments

Comments
 (0)