예제 #1
0
        public override byte[] Encode(object arg, bool packed)
        {
            if (arg is BigRational input)
            {
                if (_denominator != input.Denominator)
                {
                    throw new AbiException(AbiEncodingExceptionMessage);
                }

                return(UInt.Encode(input.Numerator, packed));
            }

            throw new AbiException(AbiEncodingExceptionMessage);
        }
예제 #2
0
        public override byte[] Encode(object arg)
        {
            if (arg is Address input)
            {
                byte[] bytes = input.Bytes;
                return UInt.Encode(bytes.ToUnsignedBigInteger());
            }

            if (arg is string stringInput)
            {
                return Encode(new Address(stringInput));
            }

            throw new AbiException(AbiEncodingExceptionMessage);
        }
예제 #3
0
        public override byte[] Encode(object arg, bool packed)
        {
            if (arg is byte[] input)
            {
                int    paddingSize   = (1 + input.Length / PaddingMultiple) * PaddingMultiple;
                byte[] lengthEncoded = UInt.Encode(new BigInteger(input.Length), packed);
                return(Bytes.Concat(lengthEncoded, packed ? input : input.PadRight(paddingSize)));
            }

            if (arg is string stringInput)
            {
                return(Encode(Encoding.ASCII.GetBytes(stringInput), packed));
            }

            throw new AbiException(AbiEncodingExceptionMessage);
        }
예제 #4
0
        public override byte[] Encode(object arg, bool packed)
        {
            if (arg is Array input)
            {
                byte[][] encodedItems = new byte[input.Length + 1][];
                int      i            = 0;
                encodedItems[i++] = UInt.Encode((BigInteger)input.Length, packed);
                foreach (object o in input)
                {
                    encodedItems[i++] = _elementType.Encode(o, packed);
                }

                return(Bytes.Concat(encodedItems));
            }

            throw new AbiException(AbiEncodingExceptionMessage);
        }
예제 #5
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);
        }