Exemplo n.º 1
0
        internal static (Array, int) DecodeSequence(Type elementType, int length, IEnumerable <AbiType> types, byte[] data, bool packed, int startPosition)
        {
            Array sequence        = Array.CreateInstance(elementType, length);
            int   position        = startPosition;
            int   dynamicPosition = 0;

            using IEnumerator <AbiType> typesEnumerator = types.GetEnumerator();
            object?item;

            for (int i = 0; i < length; i++)
            {
                typesEnumerator.MoveNext();
                AbiType type = typesEnumerator.Current;

                if (type.IsDynamic)
                {
                    (UInt256 offset, int nextPosition) = UInt256.DecodeUInt(data, position, packed);
                    (item, dynamicPosition)            = type.Decode(data, startPosition + (int)offset, packed);
                    position = nextPosition;
                }
                else
                {
                    (item, position) = type.Decode(data, position, packed);
                }

                sequence.SetValue(item, i);
            }

            return(sequence, Math.Max(position, dynamicPosition));
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public object[] Decode(AbiEncodingStyle encodingStyle, AbiSignature signature, byte[] data)
        {
            bool packed     = (encodingStyle & AbiEncodingStyle.Packed) == AbiEncodingStyle.Packed;
            bool includeSig = encodingStyle == AbiEncodingStyle.IncludeSignature;
            int  sigOffset  = includeSig ? 4 : 0;

            string[] argTypeNames = new string[signature.Types.Length];
            for (int i = 0; i < signature.Types.Length; i++)
            {
                argTypeNames[i] = signature.Types[i].ToString();
            }

            int position = 0;

            if (encodingStyle == AbiEncodingStyle.IncludeSignature)
            {
                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)}");
                }

                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.UInt256.DecodeUInt(data, position, packed);
                    (arguments[i], dynamicPosition)       = type.Decode(data, sigOffset + (int)offset, packed);
                    position = nextPosition;
                }
                else
                {
                    (arguments[i], position) = type.Decode(data, position, packed);
                }
            }

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

            return(arguments);
        }
Exemplo n.º 4
0
        public override (object, int) Decode(byte[] data, int position, bool packed)
        {
            Array result = Array.CreateInstance(_elementType.CSharpType, Length);

            if (_elementType.IsDynamic)
            {
                position += (Length - 1) * UInt256.LengthInBytes;
            }

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

            return(result, position);
        }
Exemplo n.º 5
0
        public override (object, int) Decode(byte[] data, int position, bool packed)
        {
            BigInteger length;

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

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

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

                result.SetValue(element, i);
            }

            return(result, position);
        }