public override byte[] EncodeList(IList l)
        {
            if (_elementType.IsDynamic())
            {
                var elems = new byte[l.Count + 1 + l.Count][];
                elems[0] = _intTypeEncoder.EncodeInt(l.Count);

                var currentSize = 0;
                for (var i = 0; i < l.Count; i++)
                {
                    elems[i + 1]           = _intTypeEncoder.EncodeInt((l.Count * 32) + currentSize); //location element
                    elems[i + 1 + l.Count] = _elementType.Encode(l[i]);
                    currentSize            = currentSize + elems[i + 1 + l.Count].Length;
                }
                return(ByteUtil.Merge(elems));
            }
            else
            {
                var elems = new byte[l.Count + 1][];
                elems[0] = _intTypeEncoder.EncodeInt(l.Count);
                for (var i = 0; i < l.Count; i++)
                {
                    elems[i + 1] = _elementType.Encode(l[i]);
                }
                return(ByteUtil.Merge(elems));
            }
        }
Ejemplo n.º 2
0
        public override byte[] EncodeList(IList l)
        {
            if (l.Count != arraySize)
            {
                throw new Exception("List size (" + l.Count + ") != " + arraySize);
            }

            if (elementType.IsDynamic())
            {
                var elems       = new byte[arraySize + arraySize][];
                var currentSize = 0;
                for (var i = 0; i < l.Count; i++)
                {
                    elems[i]           = intTypeEncoder.EncodeInt((l.Count * 32) + currentSize);
                    elems[i + l.Count] = elementType.Encode(l[i]);
                    currentSize        = currentSize + elems[i + l.Count].Length;
                }
                return(ByteUtil.Merge(elems));
            }
            else
            {
                var elems = new byte[arraySize][];
                for (var i = 0; i < l.Count; i++)
                {
                    elems[i] = elementType.Encode(l[i]);
                }
                return(ByteUtil.Merge(elems));
            }
        }
        public byte[] Encode(object value, bool checkEndian)
        {
            if (!(value is byte[]))
            {
                throw new Exception("byte[] value expected for type 'bytes'");
            }
            var bb  = (byte[])value;
            var ret = new byte[((bb.Length - 1) / 32 + 1) * 32]; // padding 32 bytes

            //It should always be Big Endian.
            if (BitConverter.IsLittleEndian && checkEndian)
            {
                bb = bb.Reverse().ToArray();
            }

            Array.Copy(bb, 0, ret, 0, bb.Length);

            return(ByteUtil.Merge(_intTypeEncoder.EncodeInt(bb.Length), ret));
        }
        public byte[] Encode(object value)
        {
            if (value.IsNumber())
            {
                var bigInt = BigInteger.Parse(value.ToString());
                return(_intTypeEncoder.EncodeInt(bigInt));
            }

            var stringValue = value as string;

            if (stringValue != null)
            {
                var returnBytes = new byte[32];
                var bytes       = Encoding.UTF8.GetBytes(stringValue);
                if (bytes.Length > 32)
                {
                    throw new ArgumentException("After retrieving the UTF8 bytes for the string, it is longer than 32 bytes, please using the string type, or a byte array if the string was a hex value");
                }
                Array.Copy(bytes, 0, returnBytes, 0, bytes.Length);
                return(returnBytes);
            }

            var bytesValue = value as byte[];

            if (bytesValue != null)
            {
                if (bytesValue.Length > 32)
                {
                    throw new ArgumentException("Expected byte array no bigger than 32 bytes");
                }

                var returnArray = new byte[((bytesValue.Length - 1) / 32 + 1) * 32];
                Array.Copy(bytesValue, 0, returnArray, 0, bytesValue.Length);
                return(returnArray);
            }

            throw new ArgumentException("Expected Numeric Type or String to be Encoded as Bytes32");
        }