Skip to content

Instantly share code, notes, and snippets.

@0x070696E65
Created May 2, 2023 09:45
Show Gist options
  • Save 0x070696E65/82217f8e29688fd1936d2376358fb5f3 to your computer and use it in GitHub Desktop.
Save 0x070696E65/82217f8e29688fd1936d2376358fb5f3 to your computer and use it in GitHub Desktop.
最大値とボーダーを渡してランダムな数値が超えるかどうか
using CatSdk.Symbol;
using CatSdk.Utils;
using System.Text.Json.Nodes;
using System.Runtime.Loader;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
var node = "https://hideyoshi.mydns.jp:3001";
async Task<string> GetDataFromApi(string _node, string _param)
{
var url = $"{_node}{_param}";
using var client = new HttpClient();
try
{
var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode) {
return await response.Content.ReadAsStringAsync();
}
throw new Exception($"Error: {response.StatusCode}");
}
catch (Exception ex) {
throw new Exception(ex.Message);
}
}
var hash = "90960DFFEE143AFCAD2AAF4DC52665530E73F8EC3BE09551855972EFE7C4D41C";
var jsonString = await GetDataFromApi(node, $"/transactions/confirmed/{hash}");
var transaction = JsonNode.Parse(jsonString);
var source = Converter.HexToUtf8((string)transaction["transaction"]["message"]);
var options = CSharpParseOptions.Default
.WithLanguageVersion(LanguageVersion.CSharp8);
var syntaxTree = CSharpSyntaxTree.ParseText(source, options, "MyRandom.cs");
var references = new MetadataReference[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
};
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
var compilation = CSharpCompilation.Create(
"MyRandom.dll",
new[] { syntaxTree },
references,
compilationOptions);
using var stream = new MemoryStream();
var emitResult = compilation.Emit(stream);
if (emitResult.Success)
{
stream.Seek(0, SeekOrigin.Begin);
var assembly = AssemblyLoadContext.Default.LoadFromStream(stream);
var randomType = assembly.GetType("MyRandom");
var method = randomType.GetMethod("IsBeyondBorder");
var result = ((int, bool))(method.Invoke(null, new object[]{10, 5}) ?? throw new InvalidOperationException());
Console.WriteLine($"result: {result.Item1}, {result.Item2}");
}
else
{
IEnumerable<Diagnostic> failures = emitResult.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
foreach (Diagnostic diagnostic in failures)
{
Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment