public override void Encode(ref AbiEncodeBuffer buff)
        {
            var uintEncoder = UInt256Encoder.UncheckedEncoders.Get();

            try
            {
                // write data offset position into header
                int offset = buff.HeadLength + buff.DataAreaCursorPosition;
                uintEncoder.Encode(buff.HeadCursor, offset);
                buff.IncrementHeadCursor(UInt256.SIZE);

                // write array item count into data buffer
                int len = _val.Count();
                uintEncoder.Encode(buff.DataAreaCursor, len);
                buff.IncrementDataCursor(UInt256.SIZE);

                // write payload into data buffer
                var payloadBuffer = new AbiEncodeBuffer(buff.DataAreaCursor, Enumerable.Repeat(_info.ArrayItemInfo, len).ToArray());
                foreach (var item in _val)
                {
                    _itemEncoder.SetValue(item);
                    _itemEncoder.Encode(ref payloadBuffer);
                    buff.IncrementDataCursor(_itemEncoder.GetEncodedSize());
                }
            }
            finally
            {
                UInt256Encoder.UncheckedEncoders.Put(uintEncoder);
            }
        }
Esempio n. 2
0
        public override void Encode(ref AbiEncodeBuffer buff)
        {
            Span <byte> utf8 = UTF8.GetBytes(_val);

            var uintEncoder = UInt256Encoder.UncheckedEncoders.Get();

            try
            {
                // write data offset position into header
                int offset = buff.HeadLength + buff.DataAreaCursorPosition;
                uintEncoder.Encode(buff.HeadCursor, offset);
                buff.IncrementHeadCursor(UInt256.SIZE);

                // write payload len into data buffer
                int len = utf8.Length;
                uintEncoder.Encode(buff.DataAreaCursor, len);
                buff.IncrementDataCursor(UInt256.SIZE);

                // write payload into data buffer
                utf8.CopyTo(buff.DataAreaCursor);
                int padded = PadLength(len, UInt256.SIZE);
                buff.IncrementDataCursor(padded);
            }
            finally
            {
                UInt256Encoder.UncheckedEncoders.Put(uintEncoder);
            }
        }
Esempio n. 3
0
 public override void Encode(ref AbiEncodeBuffer buffer)
 {
     ValidateArrayLength();
     foreach (var item in _val)
     {
         _itemEncoder.SetValue(item);
         _itemEncoder.Encode(ref buffer);
     }
 }
Esempio n. 4
0
        public override void Encode(ref AbiEncodeBuffer buff)
        {
            // bytes<M>: enc(X) is the sequence of bytes in X padded with trailing
            // zero-bytes to a length of 32 bytes.
            int i = 0;

            foreach (byte b in _val)
            {
                buff.HeadCursor[i++] = b;
            }

            buff.IncrementHeadCursor(UInt256.SIZE);
        }
Esempio n. 5
0
        public void Int24_1()
        {
            int num     = 77216;
            var encoder = EncoderFactory.LoadEncoder("int24", num);

            Assert.IsType <Int32Encoder>(encoder);
            var encodedSize = encoder.GetEncodedSize();

            Assert.Equal(32, encodedSize);
            Span <byte> data = new byte[encodedSize];
            var         buff = new AbiEncodeBuffer(data, "int24");

            encoder.Encode(ref buff);
            Assert.Equal(0, buff.HeadCursor.Length);
            var result = HexUtil.GetHexFromBytes(data, hexPrefix: false);

            Assert.Equal("0000000000000000000000000000000000000000000000000000000000012da0", result);
        }
Esempio n. 6
0
        public void Int56()
        {
            var num     = (long)-11118;
            var encoder = EncoderFactory.LoadEncoder("int56", num);

            Assert.IsType <Int64Encoder>(encoder);
            var encodedSize = encoder.GetEncodedSize();

            Assert.Equal(32, encodedSize);
            Span <byte> data = new byte[encodedSize];
            var         buff = new AbiEncodeBuffer(data, "int56");

            encoder.Encode(ref buff);
            Assert.Equal(0, buff.HeadCursor.Length);
            var result = HexUtil.GetHexFromBytes(data, hexPrefix: false);

            Assert.Equal("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd492", result);
        }
Esempio n. 7
0
        public void Address()
        {
            Address myAddr  = "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe";
            var     encoder = EncoderFactory.LoadEncoder("address", myAddr);

            Assert.IsType <AddressEncoder>(encoder);
            var encodedSize = encoder.GetEncodedSize();

            Assert.Equal(32, encodedSize);
            Span <byte> data   = new byte[encodedSize];
            var         buffer = new AbiEncodeBuffer(data, "address");

            encoder.Encode(ref buffer);
            Assert.Equal(0, buffer.HeadCursor.Length);
            var result = HexUtil.GetHexFromBytes(data, hexPrefix: false);

            Assert.Equal("00000000000000000000000011f4d0a3c12e86b4b5f39b213f7e19d048276dae", result);
        }
Esempio n. 8
0
        public void Int256_2()
        {
            BigInteger num     = 4294923588;
            var        encoder = EncoderFactory.LoadEncoder("int256", num);

            Assert.IsType <Int256Encoder>(encoder);
            var encodedSize = encoder.GetEncodedSize();

            Assert.Equal(32, encodedSize);
            Span <byte> data = new byte[encodedSize];
            var         buff = new AbiEncodeBuffer(data, "int256");

            encoder.Encode(ref buff);
            Assert.Equal(0, buff.HeadCursor.Length);
            var result = HexUtil.GetHexFromBytes(data, hexPrefix: false);

            Assert.Equal("00000000000000000000000000000000000000000000000000000000ffff5544", result);
        }
Esempio n. 9
0
        public void Int64FixedArray()
        {
            long[] bytes   = new long[] { 1, 4546, long.MaxValue, 0, long.MaxValue };
            var    encoder = EncoderFactory.LoadEncoder("int64[5]", bytes, EncoderFactory.LoadEncoder("int64", default(long)));

            Assert.IsType <FixedArrayEncoder <long> >(encoder);
            var encodedSize = encoder.GetEncodedSize();

            Assert.True(encodedSize % 32 == 0);
            Assert.Equal(160, encodedSize);
            Span <byte> data = new byte[encodedSize];
            var         buff = new AbiEncodeBuffer(data, "int64[5]");

            encoder.Encode(ref buff);
            Assert.Equal(0, buff.HeadCursor.Length);
            var result = HexUtil.GetHexFromBytes(data);

            Assert.Equal("000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000011c20000000000000000000000000000000000000000000000007fffffffffffffff00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007fffffffffffffff", result);
        }
Esempio n. 10
0
        public void Bytes_M()
        {
            byte[] bytes   = HexUtil.HexToBytes("072696e74657220746f6f6b20612067616c6c6579206");
            var    encoder = EncoderFactory.LoadEncoder("bytes22", bytes);

            Assert.IsType <BytesMEncoder>(encoder);
            var encodedSize = encoder.GetEncodedSize();

            Assert.True(encodedSize % 32 == 0);
            Assert.Equal(32, encodedSize);
            Span <byte> data = new byte[encodedSize];
            var         buff = new AbiEncodeBuffer(data, "bytes22");

            encoder.Encode(ref buff);
            Assert.Equal(0, buff.HeadCursor.Length);
            var result = HexUtil.GetHexFromBytes(data);

            Assert.Equal("072696e74657220746f6f6b20612067616c6c657920600000000000000000000", result);
        }
Esempio n. 11
0
        public void LargeString()
        {
            var str     = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
            var encoder = EncoderFactory.LoadEncoder("string", str);

            Assert.IsType <StringEncoder>(encoder);
            var encodedSize = encoder.GetEncodedSize();

            Assert.True(encodedSize % 32 == 0);
            Assert.Equal(320, encodedSize);
            Span <byte> data = new byte[encodedSize];
            var         buff = new AbiEncodeBuffer(data, "string");

            encoder.Encode(ref buff);
            Assert.Equal(0, buff.HeadCursor.Length);
            var result = HexUtil.GetHexFromBytes(data);

            Assert.Equal("000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000f54c6f72656d20497073756d2069732073696d706c792064756d6d792074657874206f6620746865207072696e74696e6720616e64207479706573657474696e6720696e6475737472792e204c6f72656d20497073756d20686173206265656e2074686520696e6475737472792773207374616e646172642064756d6d79207465787420657665722073696e6365207468652031353030732c207768656e20616e20756e6b6e6f776e207072696e74657220746f6f6b20612067616c6c6579206f66207479706520616e6420736372616d626c656420697420746f206d616b65206120747970652073706563696d656e20626f6f6b2e0000000000000000000000", result);
        }
Esempio n. 12
0
        public void StringUnicode()
        {
            var str     = "utf8; 4 bytes: 𠾴; 3 bytes: ⏰ works!";
            var encoder = EncoderFactory.LoadEncoder("string", str);

            Assert.IsType <StringEncoder>(encoder);
            var encodedSize = encoder.GetEncodedSize();

            Assert.True(encodedSize % 32 == 0);
            Assert.Equal(128, encodedSize);
            Span <byte> data = new byte[encodedSize];
            var         buff = new AbiEncodeBuffer(data, "string");

            encoder.Encode(ref buff);
            Assert.Equal(0, buff.HeadCursor.Length);
            var result = HexUtil.GetHexFromBytes(data);

            Assert.Equal("00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000028757466383b20342062797465733a20f0a0beb43b20332062797465733a20e28fb020776f726b7321000000000000000000000000000000000000000000000000", result);
        }
Esempio n. 13
0
        public void String()
        {
            var str     = "Hello, world!";
            var encoder = EncoderFactory.LoadEncoder("string", str);

            Assert.IsType <StringEncoder>(encoder);
            var encodedSize = encoder.GetEncodedSize();

            Assert.True(encodedSize % 32 == 0);
            Assert.Equal(96, encodedSize);
            Span <byte> data = new byte[encodedSize];
            var         buff = new AbiEncodeBuffer(data, "string");

            encoder.Encode(ref buff);
            Assert.Equal(0, buff.HeadCursor.Length);
            var result = HexUtil.GetHexFromBytes(data);

            Assert.Equal("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20776f726c642100000000000000000000000000000000000000", result);
        }
Esempio n. 14
0
        public void DynamicBoolArray()
        {
            IEnumerable <bool> arr = new[] { true, true, false, true, };
            var encoder            = EncoderFactory.LoadEncoder("bool[]", arr, EncoderFactory.LoadEncoder("bool", default(bool)));

            Assert.IsType <DynamicArrayEncoder <bool> >(encoder);
            var encodedSize = encoder.GetEncodedSize();

            Assert.True(encodedSize % 32 == 0);
            Assert.Equal(192, encodedSize);
            Span <byte> data = new byte[encodedSize];
            var         buff = new AbiEncodeBuffer(data, "bool[]");

            encoder.Encode(ref buff);
            Assert.Equal(0, buff.HeadCursor.Length);
            var result = HexUtil.GetHexFromBytes(data);

            Assert.Equal("000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", result);
        }
Esempio n. 15
0
        public void Boolean_False()
        {
            bool boolean = false;
            var  encoder = EncoderFactory.LoadEncoder("bool", boolean);

            Assert.IsType <BoolEncoder>(encoder);
            var encodedSize = encoder.GetEncodedSize();

            Assert.True(encodedSize % 32 == 0);
            Assert.Equal(32, encodedSize);
            Span <byte> data = new byte[encodedSize];
            var         buff = new AbiEncodeBuffer(data, "bool");

            encoder.Encode(ref buff);
            Assert.Equal(0, buff.HeadCursor.Length);
            var result = HexUtil.GetHexFromBytes(data);

            Assert.Equal("0000000000000000000000000000000000000000000000000000000000000000", result);
        }
Esempio n. 16
0
        public void Bytes()
        {
            byte[] bytes   = HexUtil.HexToBytes("207072696e74657220746f6f6b20612067616c6c6579206f66207479706520616e6420736372616d626c656420697420746f206d616b65206120747970");
            var    encoder = EncoderFactory.LoadEncoder("bytes", bytes);

            Assert.IsType <BytesEncoder>(encoder);
            var encodedSize = encoder.GetEncodedSize();

            Assert.True(encodedSize % 32 == 0);
            Assert.Equal(128, encodedSize);
            Span <byte> data = new byte[encodedSize];
            var         buff = new AbiEncodeBuffer(data, "bytes");

            encoder.Encode(ref buff);
            Assert.Equal(0, buff.HeadCursor.Length);
            var result = HexUtil.GetHexFromBytes(data);

            Assert.Equal("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003d207072696e74657220746f6f6b20612067616c6c6579206f66207479706520616e6420736372616d626c656420697420746f206d616b65206120747970000000", result);
        }
Esempio n. 17
0
        public void UInt8FixedArray()
        {
            byte[] bytes   = HexUtil.HexToBytes("072696e746");
            var    encoder = EncoderFactory.LoadEncoder("uint8[5]", bytes, EncoderFactory.LoadEncoder("uint8", default(byte)));

            Assert.IsType <FixedArrayEncoder <byte> >(encoder);
            var encodedSize = encoder.GetEncodedSize();

            Assert.True(encodedSize % 32 == 0);
            Assert.Equal(160, encodedSize);
            Span <byte> data = new byte[encodedSize];
            var         buff = new AbiEncodeBuffer(data, "uint8[5]");

            encoder.Encode(ref buff);
            Assert.Equal(0, buff.HeadCursor.Length);
            var result = HexUtil.GetHexFromBytes(data);

            Assert.Equal("00000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000026000000000000000000000000000000000000000000000000000000000000009600000000000000000000000000000000000000000000000000000000000000e70000000000000000000000000000000000000000000000000000000000000046", result);
        }
Esempio n. 18
0
        public override void Encode(ref AbiEncodeBuffer buff)
        {
            // bytes, of length k(which is assumed to be of type uint256):
            // enc(X) = enc(k) pad_right(X), i.e.the number of bytes is encoded as a uint256
            // followed by the actual value of X as a byte sequence, followed  by the minimum
            // number of zero-bytes such that len(enc(X)) is a multiple of 32.
            // write length prefix

            var uintEncoder = UInt256Encoder.UncheckedEncoders.Get();

            try
            {
                // write data offset position into header
                int offset = buff.HeadLength + buff.DataAreaCursorPosition;
                uintEncoder.Encode(buff.HeadCursor, offset);
                buff.IncrementHeadCursor(UInt256.SIZE);

                // write payload len into data buffer
                int len = _val.Count();
                uintEncoder.Encode(buff.DataAreaCursor, len);
                buff.IncrementDataCursor(UInt256.SIZE);

                // write payload into data buffer
                int i = 0;
                foreach (byte b in _val)
                {
                    buff.DataAreaCursor[i++] = b;
                }

                int padded = PadLength(len, UInt256.SIZE);
                buff.IncrementDataCursor(padded);
            }
            finally
            {
                UInt256Encoder.UncheckedEncoders.Put(uintEncoder);
            }
        }
Esempio n. 19
0
        public void Int56Random()
        {
            var rand         = new Random();
            var int56Encoder = EncoderFactory.LoadEncoder("int56", default(long));

            for (var i = 0; i < 50_000; i++)
            {
                byte[]      num   = new byte[32];
                Span <byte> bytes = num;
                Span <long> view  = MemoryMarshal.Cast <byte, long>(bytes);

                byte[] tmp = new byte[7];
                rand.NextBytes(tmp);
                tmp.CopyTo(num, 25);

                var buff = new AbiDecodeBuffer(bytes, "int56");
                int56Encoder.Decode(ref buff, out var result);
                Span <byte> resultBuff    = new byte[32];
                var         abiEncodeBuff = new AbiEncodeBuffer(resultBuff, "int56");
                int56Encoder.SetValue(result);
                int56Encoder.Encode(ref abiEncodeBuff);
                Assert.Equal(bytes.Slice(25).ToHexString(), resultBuff.Slice(25).ToHexString());
            }
        }
Esempio n. 20
0
 public override void Encode(ref AbiEncodeBuffer buffer)
 {
     buffer.HeadCursor[UInt256.SIZE - 1] = _val ? (byte)1 : (byte)0;
     buffer.IncrementHeadCursor(UInt256.SIZE);
 }
        public void Encode(ref AbiEncodeBuffer buffer)
        {
            ValidateArrayLength();

            // If we have no elements, no work needs to be done.
            if (TypeInfo.ArrayDimensionSizes.Length == 0)
            {
                return;
            }

            // Create a variable to track our position.
            int[] encodingPosition = new int[TypeInfo.ArrayDimensionSizes.Length];

            // Get value as array (convert to array if its underlying type is something else)
            IList parentArray = _val is IList arr ? arr : _val.ToArray();

            // Loop for each element to index.
            bool reachedEnd = false;

            while (!reachedEnd)
            {
                // Define the parent array to resolve for this element.
                IList innerMostArray = parentArray;

                // Increment our decoding position.
                bool incrementing = true;
                for (int x = 0; x < encodingPosition.Length; x++)
                {
                    // If this isn't the final index (inner most array index), then it's an index to another array.
                    if (x < encodingPosition.Length - 1)
                    {
                        // Get value as array (convert to array if its underlying type is something else)
                        var inner = innerMostArray[encodingPosition[x]];
                        innerMostArray = inner is IList ia ? ia : ((inner as IEnumerable).Cast <object>()).ToArray();
                    }
                    else
                    {
                        // We've resolved the element to index.
                        _itemEncoder.SetValue(innerMostArray[encodingPosition[x]]);
                        _itemEncoder.Encode(ref buffer);
                    }

                    // Increment the index for this dimension
                    if (incrementing)
                    {
                        // Increment our position.
                        encodingPosition[x]++;

                        // Determine if we need to carry a digit.
                        if (encodingPosition[x] >= TypeInfo.ArrayDimensionSizes[x])
                        {
                            // Reset the digit, we will carry over to the next.
                            encodingPosition[x] = 0;
                        }
                        else
                        {
                            incrementing = false;
                        }
                    }
                }

                // If we incremented all digits and still have increment flag set, we overflowed our last element, so we reached the end
                reachedEnd = incrementing;
            }
        }
Esempio n. 22
0
 // encoded the same way as an uint160
 public override void Encode(ref AbiEncodeBuffer buffer)
 {
     MemoryMarshal.Write(buffer.HeadCursor.Slice(12), ref _val);
     buffer.IncrementHeadCursor(UInt256.SIZE);
 }