Ejemplo n.º 1
0
        public void Test_single_bool(AbiEncodingStyle encodingStyle)
        {
            AbiType      type      = AbiType.Bool;
            AbiSignature signature = new AbiSignature("abc", type);

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, true);
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(true, arguments[0]);
        }
Ejemplo n.º 2
0
        public void Test_single_uint(AbiEncodingStyle encodingStyle)
        {
            AbiType      type      = AbiType.UInt256;
            AbiSignature signature = new AbiSignature("abc", type);

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, BigInteger.Zero);
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(UInt256.Zero, arguments[0]);
        }
Ejemplo n.º 3
0
        public void Test_bytes_invalid_length(AbiEncodingStyle encodingStyle)
        {
            AbiType type = new AbiBytes(19);

            byte[]       data      = new byte[23];
            AbiSignature signature = new AbiSignature("abc", type);

            Assert.Throws <AbiException>(() => _abiEncoder.Encode(encodingStyle, signature, data));
        }
Ejemplo n.º 4
0
        public void Test_single_uint32(AbiEncodingStyle encodingStyle)
        {
            AbiType      type      = new AbiUInt(32);
            AbiSignature signature = new AbiSignature("abc", type);

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, 123U);
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(new BigInteger(123U), arguments[0]);
        }
Ejemplo n.º 5
0
        public void Dynamic_array_of_string(AbiEncodingStyle encodingStyle)
        {
            AbiType      type      = new AbiArray(AbiType.String);
            AbiSignature signature = new AbiSignature("abc", type);

            string[] data      = { "a", "bc", "def" };
            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, new object[] { data });
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(arguments[0], data);
        }
Ejemplo n.º 6
0
        public void Dynamic_array_of_uint(AbiEncodingStyle encodingStyle)
        {
            AbiType      type      = new AbiArray(AbiType.UInt256);
            AbiSignature signature = new AbiSignature("abc", type);

            BigInteger[] data      = { 1, 2, 3 };
            byte[]       encoded   = _abiEncoder.Encode(encodingStyle, signature, data);
            object[]     arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(arguments[0], data);
        }
Ejemplo n.º 7
0
        public void Decode_should_be_called()
        {
            var abi          = Substitute.For <IAbiEncoder>();
            var data         = new byte[] { 100, 200 };
            var abiSignature = new AbiSignature("test", AbiType.String);
            const AbiEncodingStyle abiEncodingStyle = AbiEncodingStyle.Packed;

            abi.Decode(new AbiEncodingInfo(abiEncodingStyle, abiSignature), data);
            abi.Received().Decode(abiEncodingStyle, abiSignature, data);
        }
Ejemplo n.º 8
0
        public void Encode_should_be_called()
        {
            var abi          = Substitute.For <IAbiEncoder>();
            var parameters   = new object[] { "p1" };
            var abiSignature = new AbiSignature("test", AbiType.String);
            const AbiEncodingStyle abiEncodingStyle = AbiEncodingStyle.Packed;

            abi.Encode(new AbiEncodingInfo(abiEncodingStyle, abiSignature), parameters);
            abi.Received().Encode(abiEncodingStyle, abiSignature, parameters);
        }
Ejemplo n.º 9
0
        public void Test_fixed(AbiEncodingStyle encodingStyle)
        {
            AbiFixed     type      = AbiType.Fixed;
            BigRational  data      = BigRational.FromBigInt(123456789) * BigRational.Reciprocal(BigRational.Pow(BigRational.FromInt(10), type.Precision));
            AbiSignature signature = new AbiSignature("abc", type);

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, data);
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(arguments[0], data);
        }
Ejemplo n.º 10
0
        public void Test_single_address_no_signature(AbiEncodingStyle encodingStyle)
        {
            AbiType      type      = AbiType.Address;
            AbiSignature signature = new AbiSignature("abc", type);
            Address      arg       = new Address(Keccak.OfAnEmptyString);

            byte[]   encoded   = _abiEncoder.Encode(AbiEncodingStyle.None, signature, arg);
            object[] arguments = _abiEncoder.Decode(AbiEncodingStyle.None, signature, encoded);
            Assert.AreEqual(arguments[0], arg);
        }
Ejemplo n.º 11
0
        public void Test_string(AbiEncodingStyle encodingStyle)
        {
            AbiType      type      = AbiType.String;
            string       data      = "def";
            AbiSignature signature = new AbiSignature("abc", type);

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, data);
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(arguments[0], data);
        }
Ejemplo n.º 12
0
        public void Test_bytes(AbiEncodingStyle encodingStyle)
        {
            AbiType type = new AbiBytes(19);

            byte[]       data      = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
            AbiSignature signature = new AbiSignature("abc", type);

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, data);
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.True(Bytes.AreEqual((byte[])arguments[0], data));
        }
Ejemplo n.º 13
0
        public void Dynamic_array_of_dynamic_array_of_uint(AbiEncodingStyle encodingStyle)
        {
            AbiType      type      = new AbiArray(new AbiArray(AbiType.UInt256));
            AbiSignature signature = new AbiSignature("abc", type);

            UInt256[]   element   = { 1, 2, 3 };
            UInt256[][] data      = { element, element };
            byte[]      encoded   = _abiEncoder.Encode(encodingStyle, signature, new object[] { data });
            object[]    arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(data, arguments[0]);
        }
Ejemplo n.º 14
0
        public void Fixed_array_of_uint(AbiEncodingStyle encodingStyle)
        {
            AbiType type = new AbiFixedLengthArray(AbiType.UInt256, 2);

            UInt256[]    data      = { 1, 1 };
            AbiSignature signature = new AbiSignature("abc", type);

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, data);
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(data, arguments[0]);
        }
Ejemplo n.º 15
0
        public void Test_single_function(AbiEncodingStyle encodingStyle)
        {
            AbiType type = AbiType.Function;

            byte[]       data      = new byte[24];
            AbiSignature signature = new AbiSignature("abc", type);

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, data);
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.True(Bytes.AreEqual((byte[])arguments[0], data));
        }
Ejemplo n.º 16
0
        public void Tuple_with_inner_tuple_with_inner_tuple(AbiEncodingStyle encodingStyle)
        {
            AbiType type = new AbiTuple(new AbiTuple(new AbiTuple(AbiType.UInt256)));

            AbiSignature signature = new AbiSignature("abc", type);

            ValueTuple <ValueTuple <ValueTuple <UInt256> > > tupleception = new ValueTuple <ValueTuple <ValueTuple <UInt256> > >(new ValueTuple <ValueTuple <UInt256> >(new ValueTuple <UInt256>(88888)));

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, tupleception);
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(tupleception, arguments[0]);
        }
Ejemplo n.º 17
0
        public void Tuple_with_inner_static_tuple(AbiEncodingStyle encodingStyle)
        {
            AbiType type = new AbiTuple(AbiType.UInt256, new AbiTuple(AbiType.UInt256, AbiType.Address), AbiType.Bool);

            AbiSignature signature = new AbiSignature("abc", type);

            ValueTuple <UInt256, ValueTuple <UInt256, Address>, bool> staticTuple = new ValueTuple <UInt256, ValueTuple <UInt256, Address>, bool>((UInt256)1000, new ValueTuple <UInt256, Address>((UInt256)400, Address.SystemUser), true);

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, staticTuple);
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(staticTuple, arguments[0]);
        }
Ejemplo n.º 18
0
        public void Dynamic_tuple(AbiEncodingStyle encodingStyle)
        {
            AbiType type = new AbiTuple(AbiType.DynamicBytes, AbiType.Address, AbiType.DynamicBytes);

            AbiSignature signature = new AbiSignature("abc", type);

            ValueTuple <byte[], Address, byte[]> dynamicTuple = new ValueTuple <byte[], Address, byte[]>(Bytes.FromHexString("0x004749fa3d"), Address.SystemUser, Bytes.Zero32);

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, dynamicTuple);
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(dynamicTuple, arguments[0]);
        }
Ejemplo n.º 19
0
        public void Fixed_array_of_fixed_array_of_uint(AbiEncodingStyle encodingStyle)
        {
            AbiType type = new AbiFixedLengthArray(new AbiFixedLengthArray(AbiType.UInt256, 2), 3);

            BigInteger[]   element   = { 1, 1 };
            BigInteger[][] data      = { element, element, element };
            AbiSignature   signature = new AbiSignature("abc", type);

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, new object[] { data });
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(arguments[0], data);
        }
Ejemplo n.º 20
0
        public void Dynamic_tuple_with_inner_dynamic_tuple(AbiEncodingStyle encodingStyle)
        {
            AbiType type = new AbiTuple(AbiType.DynamicBytes, new AbiTuple(AbiType.DynamicBytes, AbiType.Address), AbiType.Bool);

            AbiSignature signature = new AbiSignature("abc", type);

            ValueTuple <byte[], ValueTuple <byte[], Address>, bool> dynamicTuple = new ValueTuple <byte[], ValueTuple <byte[], Address>, bool>(Bytes.FromHexString("0x019283fa3d"), new ValueTuple <byte[], Address>(Bytes.FromHexString("0x019283fa3d"), Address.SystemUser), true);

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, dynamicTuple);
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(dynamicTuple, arguments[0]);
        }
Ejemplo n.º 21
0
        public byte[] Encode(AbiEncodingStyle encodingStyle, AbiSignature signature, params object[] arguments)
        {
            bool packed = (encodingStyle & AbiEncodingStyle.Packed) == AbiEncodingStyle.Packed;

            if (arguments.Length != signature.Types.Length)
            {
                throw new AbiException(
                          $"Insufficient parameters for {signature.Name}. Expected {signature.Types.Length} arguments but got {arguments.Length}");
            }

            List <byte[]> dynamicParts  = new();
            List <byte[]> headerParts   = new();
            BigInteger    currentOffset = arguments.Length * AbiType.UInt256.LengthInBytes;

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

            bool includeSig = encodingStyle == AbiEncodingStyle.IncludeSignature;
            int  sigOffset  = includeSig ? 1 : 0;

            byte[][] encodedParts = new byte[sigOffset + headerParts.Count + dynamicParts.Count][];

            if (includeSig)
            {
                encodedParts[0] = signature.Address;
            }

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

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

            return(Bytes.Concat(encodedParts));
        }
Ejemplo n.º 22
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);
        }
Ejemplo n.º 23
0
        public void Multiple_params_with_one_of_them_a_tuple_dynamic_first(AbiEncodingStyle encodingStyle)
        {
            AbiType type = new AbiTuple(AbiType.UInt256, AbiType.Address, AbiType.Bool);

            AbiSignature signature = new AbiSignature("abc", AbiType.String, type);

            ValueTuple <UInt256, Address, bool> staticTuple = new ValueTuple <UInt256, Address, bool>((UInt256)1000, Address.SystemUser, true);
            const string stringParam = "hello there!";

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, stringParam, staticTuple);
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(stringParam, arguments[0]);
            Assert.AreEqual(staticTuple, arguments[1]);
        }
Ejemplo n.º 24
0
        public byte[] Encode(AbiEncodingStyle encodingStyle, AbiSignature signature, params object[] arguments)
        {
            bool packed = (encodingStyle & AbiEncodingStyle.Packed) == AbiEncodingStyle.Packed;

            List <byte[]> dynamicParts  = new List <byte[]>();
            List <byte[]> headerParts   = new List <byte[]>();
            BigInteger    currentOffset = arguments.Length * AbiType.UInt256.LengthInBytes;

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

            bool includeSig = encodingStyle == AbiEncodingStyle.IncludeSignature;
            int  sigOffset  = includeSig ? 1 : 0;

            byte[][] encodedParts = new byte[sigOffset + headerParts.Count + dynamicParts.Count][];

            if (includeSig)
            {
                encodedParts[0] = ComputeAddress(signature);
            }

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

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

            return(Bytes.Concat(encodedParts));
        }
Ejemplo n.º 25
0
        public void Test_packed(AbiEncodingStyle encodingStyle)
        {
            Keccak  assetId    = Keccak.Compute("assetId");
            uint    expiryTime = (uint)new Timestamper().EpochSeconds + 86000;
            UInt256 value      = 1.Ether();
            uint    units      = 10U;

            byte[] salt = new byte[16];

            AbiSignature abiDef = new AbiSignature("example",
                                                   new AbiBytes(32),
                                                   new AbiUInt(32),
                                                   new AbiUInt(96),
                                                   new AbiUInt(32),
                                                   new AbiBytes(16),
                                                   AbiType.Address,
                                                   AbiType.Address);

            byte[] encoded = _abiEncoder.Encode(AbiEncodingStyle.Packed, abiDef, assetId.Bytes, units, value, expiryTime, salt, Address.Zero, Address.Zero);
            Assert.AreEqual(108, encoded.Length);
        }
Ejemplo n.º 26
0
 public AbiEncodingInfo(AbiEncodingStyle encodingStyle, AbiSignature signature)
 {
     EncodingStyle = encodingStyle;
     Signature     = signature;
 }