Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
 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);
 }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        // 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);
        }
Ejemplo n.º 8
0
 internal override void WriteValue(Utf8JsonWriter writer, IMicheline value)
 {
     if (value is MichelinePrim prim && prim.Prim == PrimType.Unit)
     {
         writer.WriteStartObject();
         writer.WriteEndObject();
     }
Ejemplo n.º 9
0
        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);
            }
        }
Ejemplo n.º 10
0
        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]);
            }
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
        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);
            }
        }
Ejemplo n.º 13
0
 // 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);
                 }
             }
         }
     }
 }
Ejemplo n.º 14
0
        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);
            }
        }
Ejemplo n.º 15
0
        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);
            }
        }
Ejemplo n.º 16
0
        public static string GetGlobalAddress(IMicheline value)
        {
            var bytes = LocalForge.ForgeMicheline(value);
            var hash  = Blake2b.GetDigest(bytes);

            return(Base58.Convert(hash, Prefix.expr));
        }
Ejemplo n.º 17
0
        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));
        }
Ejemplo n.º 18
0
        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;
        }
Ejemplo n.º 19
0
        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));
        }
Ejemplo n.º 20
0
        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);
        }
Ejemplo n.º 21
0
        public string Flatten(IMicheline value)
        {
            if (value is MichelineString micheString)
            {
                return(micheString.Value);
            }

            throw FormatException(value);
        }
Ejemplo n.º 22
0
        public override IMicheline Optimize(IMicheline value)
        {
            if (value is MichelineString micheStr)
            {
                return(new MichelineBytes(Base58.Parse(micheStr.Value, 3)));
            }

            return(value);
        }
Ejemplo n.º 23
0
        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));
        }
Ejemplo n.º 24
0
        public string Flatten(IMicheline value)
        {
            if (value is MichelineInt micheInt)
            {
                return(micheInt.Value.ToString());
            }

            throw FormatException(value);
        }
Ejemplo n.º 25
0
        public string Flatten(IMicheline value)
        {
            if (value is MichelineBytes micheBytes)
            {
                return(Hex.Convert(micheBytes.Value));
            }

            throw FormatException(value);
        }
Ejemplo n.º 26
0
        internal override void WriteValue(Utf8JsonWriter writer, IMicheline value)
        {
            if (!(value is MichelinePrim prim) || prim.Prim != PrimType.Pair)
            {
                throw FormatException(value);
            }

            Data.WriteValue(writer, prim);
        }
Ejemplo n.º 27
0
        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));
        }
Ejemplo n.º 28
0
 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
     });
 }
Ejemplo n.º 29
0
        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);
        }
Ejemplo n.º 30
0
        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);
        }