public (string, IMicheline) Normalize(string entrypoint, IMicheline value) { if (!Entrypoints.TryGetValue(entrypoint, out var schema)) { throw new ArgumentException("Entrypoint doesn't exist"); } var resultEntypoint = entrypoint; var resultValue = value; while (schema is OrSchema or && value is MichelinePrim prim && (prim.Prim == PrimType.Left || prim.Prim == PrimType.Right) && prim.Args?.Count == 1) { schema = prim.Prim == PrimType.Left ? or.Left : or.Right; value = prim.Args[0]; if (schema.Field?.Length > 0) { resultEntypoint = schema.Field; resultValue = value; } } return(resultEntypoint, resultValue); }
public override IMicheline Optimize(IMicheline value) { if (value is MichelineString micheStr) { if (BigInteger.TryParse(micheStr.Value, out var timestamp)) { return(new MichelineInt(timestamp)); } if (DateTimeOffset.TryParse(micheStr.Value, out var datetime)) { var seconds = (long)(datetime - new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero)).TotalSeconds; return(new MichelineInt(new BigInteger(seconds))); } if (micheStr.Value?.Length == 0) { return(new MichelineInt(0)); } throw FormatException(value); } return(value); }
public override IMicheline Optimize(IMicheline value) { if (value is MichelineString micheStr) { byte[] res; switch (micheStr.Value.Substring(0, 3)) { case "eds": case "sps": res = Base58.Parse(micheStr.Value, 5); break; case "p2s": res = Base58.Parse(micheStr.Value, 4); break; case "sig": res = Base58.Parse(micheStr.Value, 3); break; default: throw FormatException(value); } return(new MichelineBytes(res)); } return(value); }
IMicheline Uncomb(IMicheline value) { if (value is MichelineArray array) { value = new MichelinePrim { Prim = PrimType.Pair, Args = array }; } if (value is MichelinePrim p && p.Prim == PrimType.Pair && p.Args?.Count > 2) { value = new MichelinePrim { Prim = p.Prim, Annots = p.Annots, Args = new List <IMicheline>(2) { p.Args[0], new MichelinePrim { Prim = PrimType.Pair, Args = p.Args.Skip(1).ToList() } } }; } return(value); }
public override IMicheline Optimize(IMicheline value) { if (value is MichelineString micheStr) { var bytes = Base58.Parse(micheStr.Value, 4); byte[] res; switch (micheStr.Value.Substring(0, 4)) { case "edpk": res = new byte[33]; res[0] = 0; break; case "sppk": res = new byte[34]; res[0] = 1; break; case "p2pk": res = new byte[34]; res[0] = 2; break; default: throw FormatException(value); } bytes.CopyTo(res, 1); return(new MichelineBytes(res)); } return(value); }
public static IMicheline Replace(this IMicheline micheline, IMicheline oldNode, IMicheline newNode) { if (micheline == oldNode) { return(newNode); } if (micheline is MichelineArray arr && arr.Count > 0) { for (int i = 0; i < arr.Count; i++) { arr[i] = arr[i].Replace(oldNode, newNode); } return(arr); } if (micheline is MichelinePrim prim && prim.Args != null) { for (int i = 0; i < prim.Args.Count; i++) { prim.Args[i] = prim.Args[i].Replace(oldNode, newNode); } return(prim); } return(micheline); }
// max recursion depth 10000, according to Michelson docs public static IMicheline Expand(IMicheline node, Dictionary <string, IMicheline> constants) { if (node is MichelineArray array) { for (int i = 0; i < array.Count; i++) { array[i] = Expand(array[i], constants); } return(array); } if (node is not MichelinePrim prim || prim.Args == null) { return(node); } if (prim.Prim == PrimType.constant) { return(Expand(constants[(prim.Args[0] as MichelineString).Value], constants)); } for (int i = 0; i < prim.Args.Count; i++) { prim.Args[i] = Expand(prim.Args[i], constants); } return(prim); }
internal override void WriteValue(Utf8JsonWriter writer, IMicheline value) { if (value is MichelinePrim prim && prim.Prim == PrimType.Unit) { writer.WriteStartObject(); writer.WriteEndObject(); }
public string Flatten(IMicheline value) { if (value is MichelineString micheString) { return(micheString.Value); } else if (value is MichelineBytes micheBytes) { if (micheBytes.Value.Length != 21) { return(Hex.Convert(micheBytes.Value)); } var prefix = micheBytes.Value[0] == 0 ? Prefix.tz1 : micheBytes.Value[0] == 1 ? Prefix.tz2 : micheBytes.Value[0] == 2 ? Prefix.tz3 : null; if (prefix == null) { return(Hex.Convert(micheBytes.Value)); } var bytes = micheBytes.Value.GetBytes(1, micheBytes.Value.Length - 1); return(Base58.Convert(bytes, prefix)); } else { throw FormatException(value); } }
internal override void WriteValue(Utf8JsonWriter writer, IMicheline value) { value = Uncomb(value); if (!(value is MichelinePrim pair) || pair.Prim != PrimType.Pair) { throw FormatException(value); } if (pair.Args?.Count != 2) { throw new FormatException($"Invalid 'pair' prim args count"); } if (Kind == PairKind.Object) { writer.WriteStartObject(); Left.WriteProperty(writer, pair.Args[0]); Right.WriteProperty(writer, pair.Args[1]); writer.WriteEndObject(); } else { Left.WriteValue(writer, pair.Args[0]); Right.WriteValue(writer, pair.Args[1]); } }
public override IMicheline Optimize(IMicheline value) { if (value is MichelineString micheStr) { var bytes = Base58.Parse(micheStr.Value, 3); var res = new byte[21]; switch (micheStr.Value.Substring(0, 3)) { case "tz1": res[0] = 0; break; case "tz2": res[0] = 1; break; case "tz3": res[0] = 2; break; default: throw FormatException(value); } bytes.CopyTo(res, 1); return(new MichelineBytes(res)); } return(value); }
public string Flatten(IMicheline value) { if (value is MichelineString micheString) { return(micheString.Value); } else if (value is MichelineBytes micheBytes) { var prefix = micheBytes.Value[0] == 0 && micheBytes.Value.Length == 33 ? Prefix.edpk : micheBytes.Value[0] == 1 && micheBytes.Value.Length == 34 ? Prefix.sppk : micheBytes.Value[0] == 2 && micheBytes.Value.Length == 34 ? Prefix.p2pk : null; if (prefix == null) { return(Hex.Convert(micheBytes.Value)); } var bytes = micheBytes.Value.GetBytes(1, micheBytes.Value.Length - 1); return(Base58.Convert(bytes, prefix)); } else { throw FormatException(value); } }
// max recursion depth 10000, according to Michelson docs public static IEnumerable <string> Find(IMicheline node) { if (node is MichelineArray array) { foreach (var item in array) { foreach (var c in Find(item)) { yield return(c); } } } else if (node is MichelinePrim prim && prim.Args != null) { if (prim.Prim == PrimType.constant) { yield return((prim.Args[0] as MichelineString).Value); } else { foreach (var arg in prim.Args) { foreach (var c in Find(arg)) { yield return(c); } } } } }
internal override void WriteValue(Utf8JsonWriter writer, IMicheline value) { if (!(value is MichelinePrim prim)) { throw FormatException(value); } if (prim.Prim == PrimType.None) { writer.WriteNullValue(); } else if (prim.Prim == PrimType.Some) { if (prim.Args?.Count != 1) { throw new FormatException("Invalid 'Some' prim args count"); } Some.WriteValue(writer, prim.Args[0]); } else { throw FormatException(value); } }
internal override TreeView GetTreeView(TreeView parent, IMicheline value, string name = null, Schema schema = null) { if (value is not MichelinePrim prim) { throw FormatException(value); } if (prim.Prim == PrimType.None) { return(base.GetTreeView(parent, value, name)); } else if (prim.Prim == PrimType.Some) { if (prim.Args?.Count != 1) { throw new FormatException("Invalid 'Some' prim args count"); } var treeView = base.GetTreeView(parent, value, name); treeView.Children = new(1); treeView.Children.Add(Some.GetTreeView(treeView, prim.Args[0], name ?? Name)); return(treeView); } else { throw FormatException(value); } }
public static string GetGlobalAddress(IMicheline value) { var bytes = LocalForge.ForgeMicheline(value); var hash = Blake2b.GetDigest(bytes); return(Base58.Convert(hash, Prefix.expr)); }
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 ContractStorage(IMicheline storage) { if ((storage as MichelinePrim)?.Prim != PrimType.storage) { throw new ArgumentException("Invalid micheline: expected prim storage"); } Schema = new StorageSchema(storage as MichelinePrim).Schema; }
public string Humanize(string entrypoint, IMicheline value, JsonWriterOptions options = default) { if (!Entrypoints.TryGetValue(entrypoint, out var schema)) { throw new ArgumentException("Entrypoint doesn't exist"); } return(schema.Humanize(value, options)); }
public override IMicheline Optimize(IMicheline value) { if (value is MichelinePrim prim && prim.Prim == PrimType.Some) { prim.Args[0] = Some.Optimize(prim.Args[0]); } return(value); }
public string Flatten(IMicheline value) { if (value is MichelineString micheString) { return(micheString.Value); } throw FormatException(value); }
public override IMicheline Optimize(IMicheline value) { if (value is MichelineString micheStr) { return(new MichelineBytes(Base58.Parse(micheStr.Value, 3))); } return(value); }
public IMicheline Optimize(string entrypoint, IMicheline value, bool immutable = true) { if (!Entrypoints.TryGetValue(entrypoint, out var schema)) { throw new ArgumentException("Entrypoint doesn't exist"); } return(schema.Optimize(immutable ? Micheline.FromBytes(value.ToBytes()) : value)); }
public string Flatten(IMicheline value) { if (value is MichelineInt micheInt) { return(micheInt.Value.ToString()); } throw FormatException(value); }
public string Flatten(IMicheline value) { if (value is MichelineBytes micheBytes) { return(Hex.Convert(micheBytes.Value)); } throw FormatException(value); }
internal override void WriteValue(Utf8JsonWriter writer, IMicheline value) { if (!(value is MichelinePrim prim) || prim.Prim != PrimType.Pair) { throw FormatException(value); } Data.WriteValue(writer, prim); }
internal override TreeView GetTreeView(TreeView parent, IMicheline value, string name = null, Schema schema = null) { if (!(value is MichelinePrim prim) || prim.Prim != PrimType.Pair) { throw FormatException(value); } return(Data.GetTreeView(parent, value, name ?? Name, this)); }
internal virtual TreeView GetTreeView(TreeView parent, IMicheline value, string name = null, Schema schema = null) { return(new TreeView { Name = name ?? Name, Schema = schema ?? this, Value = value, Parent = parent }); }
public string Flatten(IMicheline value) { if (value is MichelinePrim michePrim && (michePrim.Prim == PrimType.True || michePrim.Prim == PrimType.False)) { return((michePrim.Prim == PrimType.True).ToString().ToLower()); } throw FormatException(value); }
public override IMicheline Optimize(IMicheline value) { if (value is MichelineInt micheInt) { var el = (micheInt.Value % Order + Order) % Order; return(new MichelineBytes(el.ToByteArray())); } return(value); }