public static string GetGlobalAddress(IMicheline value) { var bytes = LocalForge.ForgeMicheline(value); var hash = Blake2b.GetDigest(bytes); return(Base58.Convert(hash, Prefix.expr)); }
public async Task CompareToFile() { var localForge = new LocalForge(); var settings = new JsonSerializerSettings { DateParseHandling = DateParseHandling.None }; var basePath = @"../../../operations"; var directories = Directory.GetDirectories(basePath); foreach (var directory in directories) { var jsonString = File.ReadAllText($"{directory}/unsigned.json"); var json = JsonConvert.DeserializeObject <JToken>(jsonString, settings); switch (json["contents"].Count()) { case int n when(n > 1): var managerOps = json["contents"] .Select(cont => cont["kind"].Value <string>() switch { "origination" => (ManagerOperationContent)cont.ToObject <OriginationContent>(), "reveal" => cont.ToObject <RevealContent>(), "transaction" => cont.ToObject <TransactionContent>(), "delegation" => cont.ToObject <DelegationContent>(), _ => throw new ArgumentException($"Unknown type {json["contents"][0]["kind"]}") }) .ToList(); var opByt = File.ReadAllText($"{directory}/forged.hex"); var localByt = await localForge.ForgeOperationGroupAsync(json["branch"].Value <string>(), managerOps); Assert.True(opByt == Hex.Convert(localByt), $"{directory}"); break;
public string GetKeyHash(IMicheline key) { var optimized = Key.Optimize(Micheline.FromBytes(key.ToBytes())); var packed = new byte[] { 5 }.Concat(LocalForge.ForgeMicheline(optimized)); var hash = Blake2b.GetDigest(packed); return(Base58.Convert(hash, Prefix.expr)); }
public async Task CompareUnforgedFromFile() { var forge = new LocalForge(); var options = new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, MaxDepth = 100_000, }; options.Converters.Add(new DateTimUtcTimezoneAppender()); var jsonComparer = new JsonElementComparer(true, true); var directories = Directory.GetDirectories("../../../Forging/operations"); JsonElement toJsonElement(object o) { // JSON serialize the unforged manager operation for the purpose of deserializing as JsonElement for comparison var serMop = JsonSerializer.Serialize(o, o.GetType(), options); // Deserialize the unforged manager operation to JsonElement for comparison return((JsonElement)DJson.Parse(serMop)); } foreach (var directory in directories) { var json = DJson.Read($"{directory}/unsigned.json", options); // Parse the op bytes var opBytes = Hex.Parse(File.ReadAllText($"{directory}/forged.hex")); // Unforge the op bytes var op = await forge.UnforgeOperationAsync(opBytes); // Serialize/deserialize each operation for the purpose of conversion to JsonElement for comparison var deserOps = op.Item2.Select(toJsonElement); // Assert branch Assert.Equal(json.branch, op.Item1); // Assert equivalent JSON operations Assert.Equal(json.contents.count, op.Item2.Count()); Assert.True(((JsonElement)json.contents).EnumerateArray().SequenceEqual(deserOps, jsonComparer)); } }
public async Task CompareForgedToFile() { var forge = new LocalForge(); var options = new JsonSerializerOptions { MaxDepth = 100_000 }; var directories = Directory.GetDirectories("../../../Forging/operations"); foreach (var directory in directories) { var op = (Operation)DJson.Read($"{directory}/unsigned.json", options); var opBytes = File.ReadAllText($"{directory}/forged.hex"); var localBytes = op.Contents.Count == 1 ? await forge.ForgeOperationAsync(op.Branch, op.Contents[0]) : await forge.ForgeOperationGroupAsync(op.Branch, op.Contents.Select(x => x as ManagerOperationContent)); Assert.True(opBytes == Hex.Convert(localBytes), directory); } }