Ejemplo n.º 1
0
        public override byte[] Encode(object arg)
        {
            if (arg is Array input)
            {
                if (input.Length != Length)
                {
                    throw new AbiException(AbiEncodingExceptionMessage);
                }

                if (_elementType.IsDynamic)
                {
                    byte[][]   encodedItems  = new byte[Length * 2 - 1][];
                    BigInteger currentOffset = (Length - 1) * UInt.LengthInBytes;
                    int        i             = 0;
                    foreach (object o in input)
                    {
                        encodedItems[Length + i - 1] = _elementType.Encode(o);
                        if (i != 0)
                        {
                            currentOffset      += new BigInteger(encodedItems[Length + i - 1].Length);
                            encodedItems[i - 1] = UInt.Encode(currentOffset);
                        }

                        i++;
                    }

                    return(Bytes.Concat(encodedItems));
                }
                else
                {
                    byte[][] encodedItems = new byte[Length][];
                    int      i            = 0;
                    foreach (object o in input)
                    {
                        encodedItems[i++] = _elementType.Encode(o);
                    }

                    return(Bytes.Concat(encodedItems));
                }
            }

            throw new AbiException(AbiEncodingExceptionMessage);
        }
Ejemplo n.º 2
0
        public byte[] Encode(AbiSignature signature, params object[] arguments)
        {
            List <byte[]> dynamicParts  = new List <byte[]>();
            List <byte[]> headerParts   = new List <byte[]>();
            BigInteger    currentOffset = arguments.Length * AbiType.UInt.LengthInBytes;

            for (int i = 0; i < arguments.Length; i++)
            {
                AbiType type = signature.Types[i];
                if (type.IsDynamic)
                {
                    headerParts.Add(AbiType.UInt.Encode(currentOffset));
                    byte[] encoded = type.Encode(arguments[i]);
                    currentOffset += encoded.Length;
                    dynamicParts.Add(encoded);
                }
                else
                {
                    headerParts.Add(type.Encode(arguments[i]));
                }
            }

            byte[][] encodedParts = new byte[1 + headerParts.Count + dynamicParts.Count][];
            encodedParts[0] = ComputeAddress(signature);
            for (int i = 0; i < headerParts.Count; i++)
            {
                encodedParts[1 + i] = headerParts[i];
            }

            for (int i = 0; i < dynamicParts.Count; i++)
            {
                encodedParts[1 + headerParts.Count + i] = dynamicParts[i];
            }

            return(Bytes.Concat(encodedParts));
        }
Ejemplo n.º 3
0
        public override byte[] Encode(object arg)
        {
            if (arg is Array input)
            {
                byte[][] encodedItems = new byte[input.Length + 1][];
                int      i            = 0;
                encodedItems[i++] = UInt.Encode((BigInteger)input.Length);
                foreach (object o in input)
                {
                    encodedItems[i++] = _elementType.Encode(o);
                }

                return(Bytes.Concat(encodedItems));
            }

            throw new AbiException(AbiEncodingExceptionMessage);
        }