Пример #1
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));;
            }
Пример #2
0
            public IRlpItem EncodeWithJson(JToken item)
            {
                if (this.Nullable && (item.Type == JTokenType.Null))
                {
                    return(new RlpArray());
                }

                Reserved reserved = new Reserved();

                reserved.Features = (item["Features"] as JValue).ToObject <int>();

                if (item["Unused"] != null && item["Unused"].Type != JTokenType.Null)
                {
                    if (item["Unused"] is JArray)
                    {
                        if ((item["Unused"] as JArray).Count > 0)
                        {
                            reserved.Unused = new List <byte[]>();
                            foreach (var data in (item["Unused"] as JArray))
                            {
                                string jValue = (data as JValue).Value as string;
                                if (jValue.Length != 0)
                                {
                                    if (jValue.IsHexString())
                                    {
                                        reserved.Unused.Add(jValue.ToBytes());
                                    }
                                    else
                                    {
                                        throw new ArgumentException("unused value is not hexstring");
                                    }
                                }
                                else
                                {
                                    throw new ArgumentException("invalid reserved fields: not trimmed");
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new ArgumentException("invalid item type");
                    }
                }
                return(this.EncodeToRlp(reserved));
            }
Пример #3
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);
                }
            }