Ejemplo n.º 1
0
        public override (object, int) Decode(byte[] data, int position)
        {
            Array result = Array.CreateInstance(_elementType.CSharpType, Length);

            if (_elementType.IsDynamic)
            {
                BigInteger currentOffset   = (Length - 1) * UInt.LengthInBytes;
                int        lengthsPosition = position;
                for (int i = 0; i < Length; i++)
                {
                    if (i != 0)
                    {
                        (currentOffset, lengthsPosition) = UInt.DecodeUInt(data, lengthsPosition);
                    }

                    object element;
                    (element, currentOffset) = _elementType.Decode(data, position + (int)currentOffset);
                    result.SetValue(element, i);
                }

                position = (int)currentOffset;
            }
            else
            {
                for (int i = 0; i < Length; i++)
                {
                    (object element, int newPosition) = _elementType.Decode(data, position);
                    result.SetValue(element, i);
                    position = newPosition;
                }
            }

            return(result, position);
        }
Ejemplo n.º 2
0
        public object[] Decode(AbiSignature signature, byte[] data)
        {
            string[] argTypeNames = new string[signature.Types.Length];
            for (int i = 0; i < signature.Types.Length; i++)
            {
                argTypeNames[i] = signature.Types[i].ToString();
            }

            if (!Bytes.AreEqual(data.Slice(0, 4), ComputeAddress(signature)))
            {
                throw new AbiException(
                          $"Signature in encoded ABI data is not consistent with {ComputeSignature(signature.Name, signature.Types)}");
            }

            int position = 4;

            object[] arguments       = new object[signature.Types.Length];
            int      dynamicPosition = 0;

            for (int i = 0; i < signature.Types.Length; i++)
            {
                AbiType type = signature.Types[i];
                if (type.IsDynamic)
                {
                    // TODO: do not have to decode this - can just jump 32 and check if first call and use dynamic position
                    (BigInteger offset, int nextPosition) = AbiType.UInt.DecodeUInt(data, position);
                    (arguments[i], dynamicPosition)       = type.Decode(data, 4 + (int)offset);
                    position = nextPosition;
                }
                else
                {
                    (arguments[i], position) = type.Decode(data, position);
                }
            }

            if (Math.Max(position, dynamicPosition) != data.Length)
            {
                throw new AbiException($"Unexpected data at position {position}");
            }

            return(arguments);
        }
Ejemplo n.º 3
0
        public override (object, int) Decode(byte[] data, int position)
        {
            BigInteger length;

            (length, position) = UInt.DecodeUInt(data, position);

            Array result = Array.CreateInstance(_elementType.CSharpType, (int)length);

            for (int i = 0; i < length; i++)
            {
                object element;
                (element, position) = _elementType.Decode(data, position);

                result.SetValue(element, i);
            }

            return(result, position);
        }