Esempio n. 1
0
        public dynamic DecodeFromRlp(IRlpItem rlp)
        {
            string data = (rlp as RlpItem).RlpData.ToHexString();

            if (this._byteLength == 0)
            {
                return(data != "0x" ? data : "");
            }
            else if (data.Length - 2 <= this._byteLength)
            {
                if (this.Nullable && data.Replace("0x", String.Empty).Length == 0)
                {
                    data = "";
                }
                else
                {
                    data = "0x" + data.Replace("0x", String.Empty).PadLeft(this._byteLength, '0');
                }
                return(data);
            }
            else
            {
                throw new ArgumentException("Exceed the maximum byteLength");
            }
        }
Esempio n. 2
0
        public JValue DecodeToJson(IRlpItem rlp)
        {
            byte[] value  = this.DecodeFromRlp(rlp);
            string hexStr = value.ToHexString();

            return(new JValue(hexStr != "0x" ? hexStr : ""));
        }
Esempio n. 3
0
        public RlpArray EncodeWithJson(JToken item)
        {
            if (this.Nullable && (item == null || item.Type == JTokenType.Null))
            {
                return(new RlpArray());
            }
            RlpArray result = new RlpArray();

            foreach (IRlpKind kind in Properties)
            {
                if (kind is IRlpScalarKind)
                {
                    IRlpItem rlpItem = (kind as IRlpScalarKind).EncodeWithJson(item[kind.Name]);
                    result.Add(rlpItem);
                }
                else if (kind is IRlpArrayKind)
                {
                    RlpArray rArray = (kind as IRlpArrayKind).EncodeWithJson(item[kind.Name]);
                    result.Add(rArray);
                }
                else if (kind is IRplStructKind)
                {
                    RlpArray rArray = (kind as IRplStructKind).EncodeWithJson(item[kind.Name]);
                    result.Add(rArray);
                }
                else if (kind is IRlpCustomKind)
                {
                    IRlpItem rlpItem = (kind as IRlpCustomKind).EncodeWithJson(item[kind.Name]);
                    result.Add(rlpItem);
                }
            }
            return(result);
        }
Esempio n. 4
0
        public T[] DecodeFromRlp <T>(IRlpItem rlp)
        {
            JArray json = this.DecodeToJson(rlp);

            if (this.Nullable && (rlp == null || rlp.RlpData.Length == 0))
            {
                return(default);
Esempio n. 5
0
        public dynamic DecodeFromRlp(IRlpItem rlp, Type type)
        {
            JToken  json  = this.DecodeToJson(rlp);
            dynamic value = JsonConvert.DeserializeObject(json.ToString(), type, new JsonBytesConverter());

            return(value);
        }
Esempio n. 6
0
        public static byte[] Encode(IRlpKind kind, dynamic obj)
        {
            IRlpItem rlpItem = null;

            if (kind is IRlpScalarKind)
            {
                rlpItem = (kind as IRlpScalarKind).EncodeToRlp(obj);
                return(rlpItem.Encode());
            }
            else if (kind is IRlpArrayKind)
            {
                if (obj is Array)
                {
                    rlpItem = (kind as IRlpArrayKind).EncodeToRlp(obj);
                    return(rlpItem.Encode());
                }
                else
                {
                    throw new ArgumentException("invalid item type");
                }
            }
            else if (kind is IRplStructKind)
            {
                rlpItem = (kind as IRplStructKind).EncodeToRlp(obj);
                return(rlpItem.Encode());
            }
            else if (kind is IRlpCustomKind)
            {
                rlpItem = (kind as IRlpCustomKind).EncodeToRlp(obj);
                return(rlpItem.Encode());
            }
            return(rlpItem.Encode());
        }
Esempio n. 7
0
        public dynamic DecodeFromRlp(IRlpItem rlp)
        {
            if (this.Nullable && (rlp == null || rlp.RlpData.Length == 0))
            {
                return(false);
            }

            return((rlp as RlpItem).DecodeToBoolean());
        }
Esempio n. 8
0
        public T[] DecodeFromRlp <T>(IRlpItem rlp)
        {
            JArray json = this.DecodeToJson(rlp);

            if (this.Nullable && (rlp == null || rlp.RlpData.Length == 0))
            {
                return(default(T[]));
            }
            dynamic array = JsonConvert.DeserializeObject(json.ToString(), typeof(T[]), new JsonBytesConverter());

            return(array);
        }
Esempio n. 9
0
        public JToken DecodeToJson(IRlpItem rlp)
        {
            JObject  json     = new JObject();
            RlpArray rlpArray = new RlpArray();

            if (rlp.RlpType == RlpType.Array)
            {
                rlpArray = (rlp as RlpArray);
            }
            else
            {
                rlpArray = ((new RlpArray()).Decode(rlp.RlpData) as RlpArray);
            }

            if (this.Nullable && rlpArray.Count == 0)
            {
                return(new JObject());
            }

            if (rlpArray.Count == Properties.Count)
            {
                for (int index = 0; index < rlpArray.Count; index++)
                {
                    IRlpKind kind = Properties[index];
                    if (kind is IRlpScalarKind)
                    {
                        JValue jvalue = (kind as IRlpScalarKind).DecodeToJson(rlpArray[index]);
                        json[kind.Name] = jvalue;
                    }
                    else if (kind is IRlpArrayKind)
                    {
                        var value = (kind as IRlpArrayKind).DecodeToJson(rlpArray[index]);
                        json[kind.Name] = value;
                    }
                    else if (kind is IRplStructKind)
                    {
                        var value = (kind as IRplStructKind).DecodeToJson(rlpArray[index]);
                        json[kind.Name] = value;
                    }
                    else if (kind is IRlpCustomKind)
                    {
                        var value = (kind as IRlpCustomKind).DecodeToJson(rlpArray[index]);
                        json[kind.Name] = value;
                    }
                }
            }
            else
            {
                throw new ArgumentException("Decode properties count not match properties count");
            }

            return(json);
        }
Esempio n. 10
0
            public JToken DecodeToJson(IRlpItem rlp)
            {
                Reserved reserved = this.DecodeFromRlp(rlp, typeof(Reserved));

                if (reserved == null)
                {
                    return(null);
                }
                string jsonstr = JsonConvert.SerializeObject(reserved, new JsonBytesConverter());

                return(JObject.Parse(jsonstr));;
            }
Esempio n. 11
0
 protected static bool TryDecode(byte[] rlp, out IRlpItem items)
 {
     try
     {
         items = Decode(rlp, 0);
         return(true);
     }
     catch
     {
         items = null;
         return(false);
     }
 }
Esempio n. 12
0
        public dynamic DecodeFromRlp(IRlpItem rlp)
        {
            if (this.Nullable && (rlp == null || rlp.RlpData.Length == 0))
            {
                return(new byte[0]);
            }

            if (this._maxBytes != 0 && rlp.RlpData.Length > this._maxBytes)
            {
                throw new ArgumentException("Exceed the maximum maxbytes");
            }
            return((rlp as RlpItem).RlpData);
        }
Esempio n. 13
0
        public dynamic DecodeFromRlp(IRlpItem rlp)
        {
            if (this.Nullable && (rlp == null || rlp.RlpData.Length == 0))
            {
                return("");
            }
            string value = (rlp as RlpItem).DecodeToString();

            if (this._maxLength != 0 && value.Length > this._maxLength)
            {
                throw new ArgumentException("Exceed the maxLength maxbytes");
            }
            return(value);
        }
Esempio n. 14
0
        public dynamic DecodeFromRlp(IRlpItem rlp, Type type)
        {
            if (!type.IsArray)
            {
                throw new ArgumentException("the type isn't array");
            }
            if (this.Nullable && (rlp == null || rlp.RlpData.Length == 0))
            {
                return(default(Type));
            }
            JArray  json  = this.DecodeToJson(rlp);
            dynamic array = JsonConvert.DeserializeObject(json.ToString(), type, new JsonBytesConverter());

            return(array);
        }
Esempio n. 15
0
        public JArray DecodeToJson(IRlpItem rlp)
        {
            JArray   jsonArray = new JArray();
            RlpArray list;

            if (rlp.RlpType == RlpType.Array)
            {
                list = (rlp as RlpArray);
            }
            else
            {
                list = (new RlpArray()).Decode(rlp.RlpData) as RlpArray;
            }

            if (this._maxLength != 0 && list.Count > this._maxLength)
            {
                throw new ArgumentException("Exceed the maximum maxLength");
            }

            foreach (IRlpItem rlpItem in list)
            {
                if (this.ItemKind is IRlpScalarKind)
                {
                    JValue jvalue = (this.ItemKind as IRlpScalarKind).DecodeToJson(rlpItem);
                    jsonArray.Add(jvalue);
                }
                else if (this.ItemKind is IRlpArrayKind)
                {
                    JArray items = (this.ItemKind as IRlpArrayKind).DecodeToJson(rlpItem);
                    jsonArray.Add(items);
                }
                else if (this.ItemKind is IRplStructKind)
                {
                    JToken item = (this.ItemKind as IRplStructKind).DecodeToJson(rlpItem);
                    jsonArray.Add(item);
                }
                else if (this.ItemKind is IRlpCustomKind)
                {
                    JToken item = (this.ItemKind as IRlpCustomKind).DecodeToJson(rlpItem);
                    jsonArray.Add(item);
                }
            }

            return(jsonArray);
        }
Esempio n. 16
0
            public dynamic DecodeFromRlp(IRlpItem rlp, Type type)
            {
                if (this.Nullable && (rlp == null || rlp.RlpData.Length == 0))
                {
                    return(null);
                }

                RlpArray rlpArray = new RlpArray();

                if (rlp.RlpType == RlpType.Array)
                {
                    rlpArray = (rlp as RlpArray);
                }
                else
                {
                    rlpArray = ((new RlpArray()).Decode(rlp.RlpData) as RlpArray);
                }

                if (rlpArray != null && rlpArray.Count != 0)
                {
                    if (rlpArray[rlpArray.Count - 1].RlpData.Length == 0)
                    {
                        throw new ArgumentException("invalid reserved fields: not trimmed");
                    }
                    Reserved reserved = new Reserved();
                    reserved.Features = new RlpIntKind().DecodeFromRlp(rlpArray[0]);
                    if (rlpArray.Count > 1)
                    {
                        reserved.Unused = new List <byte[]>();
                        for (int index = 1; index < rlpArray.Count; index++)
                        {
                            reserved.Unused.Add(new RlpBytesKind(true).DecodeFromRlp(rlpArray[index]));
                        }
                    }
                    return(reserved);
                }
                else
                {
                    return(null);
                }
            }
Esempio n. 17
0
        public RlpArray EncodeWithJson(JToken items)
        {
            if (this.Nullable && (items == null || items.Type == JTokenType.Null))
            {
                return(new RlpArray());
            }
            RlpArray rlpArray = new RlpArray();

            foreach (var item in (items as JArray))
            {
                if (this.ItemKind is IRlpScalarKind)
                {
                    IRlpItem rlpItem = (this.ItemKind as IRlpScalarKind).EncodeWithJson(item as JValue);
                    rlpArray.Add(rlpItem);
                }
                else if (this.ItemKind is IRlpArrayKind)
                {
                    if (item is JArray)
                    {
                        JArray   jArray = item as JArray;
                        RlpArray rArray = (this.ItemKind as IRlpArrayKind).EncodeWithJson(jArray);
                        rlpArray.Add(rArray);
                    }
                    else
                    {
                        throw new ArgumentException("invalid item type");
                    }
                }
                else if (this.ItemKind is IRplStructKind)
                {
                    RlpArray rArray = (this.ItemKind as IRplStructKind).EncodeWithJson(item);
                    rlpArray.Add(rArray);
                }
                else if (this.ItemKind is IRlpCustomKind)
                {
                    IRlpItem rlpItem = (this.ItemKind as IRlpCustomKind).EncodeWithJson(item);
                    rlpArray.Add(rlpItem);
                }
            }
            return(rlpArray);
        }
Esempio n. 18
0
            public dynamic DecodeFromRlp(IRlpItem rlp, Type type)
            {
                if (this.Nullable && (rlp == null || rlp.RlpData.Length == 0))
                {
                    return(null);
                }

                RlpArray rlpArray = new RlpArray();

                if (rlp.RlpType == RlpType.Array)
                {
                    rlpArray = rlp as RlpArray;
                }
                else
                {
                    rlpArray = new RlpArray().Decode(rlp.RlpData) as RlpArray;
                }

                if (rlpArray != null && rlpArray.Count != 0)
                {
                    if (rlpArray[^ 1].RlpData.Length == 0)
Esempio n. 19
0
        public IRlpItem EncodeToRlp(dynamic obj)
        {
            if (this.Nullable && obj == null)
            {
                return(new RlpItem());
            }

            bool canParse = long.TryParse(obj.ToString(), out long value);

            if (canParse)
            {
                IRlpItem item = value.EncodeToRlpItem();
                if (this._maxBytes != 0 && item.RlpData.Length > this._maxBytes)
                {
                    throw new ArgumentException("Exceed the maximum maxbytes");
                }
                return(item);
            }

            throw new ArgumentException("value type invalid");
        }
Esempio n. 20
0
        public JValue DecodeToJson(IRlpItem rlp)
        {
            long value = this.DecodeFromRlp(rlp);

            return(new JValue(value));
        }
Esempio n. 21
0
 public JValue DecodeToJson(IRlpItem rlp)
 {
     return(new JValue(this.DecodeFromRlp(rlp)));
 }
Esempio n. 22
0
        public JValue DecodeToJson(IRlpItem rlp)
        {
            BigInteger value = this.DecodeFromRlp(rlp);

            return(new JValue(value.ToString("")));
        }