Example #1
0
        public void Can_read_json(string type, AbiType expectedType, Exception expectedException, object[] components)
        {
            var    converter = new AbiParameterConverter();
            var    model     = new { name = "theName", type, components };
            string json      = JsonConvert.SerializeObject(model);

            using (var jsonReader = new JsonTextReader(new StringReader(json)))
            {
                try
                {
                    var result      = converter.ReadJson(jsonReader, typeof(AbiParameter), null, false, new JsonSerializer());
                    var expectation = new AbiParameter()
                    {
                        Name = "theName", Type = expectedType
                    };
                    expectedException.Should().BeNull();
                    result.Should().BeEquivalentTo(expectation);
                }
                catch (Exception e)
                {
                    if (e.GetType() != expectedException?.GetType())
                    {
                        throw;
                    }
                }
            }
        }
Example #2
0
        public void Test_single_uint_with_casting(AbiEncodingStyle encodingStyle)
        {
            AbiType      type      = AbiType.UInt256;
            AbiSignature signature = new AbiSignature("abc", type);

            byte[]   encoded   = _abiEncoder.Encode(encodingStyle, signature, UInt256.One);
            object[] arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(BigInteger.One, arguments[0]);

            encoded   = _abiEncoder.Encode(encodingStyle, signature, 1L);
            arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(BigInteger.One, arguments[0]);

            encoded   = _abiEncoder.Encode(encodingStyle, signature, 1UL);
            arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(BigInteger.One, arguments[0]);

            encoded   = _abiEncoder.Encode(encodingStyle, signature, 1);
            arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(BigInteger.One, arguments[0]);

            encoded   = _abiEncoder.Encode(encodingStyle, signature, 1U);
            arguments = _abiEncoder.Decode(encodingStyle, signature, encoded);
            Assert.AreEqual(BigInteger.One, arguments[0]);
        }
Example #3
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(BigInteger.Zero, arguments[0]);
        }
Example #4
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]);
        }
Example #5
0
        public void Test_single_int()
        {
            AbiType      type      = AbiType.Int;
            AbiSignature signature = new AbiSignature("abc", type);

            byte[]   encoded   = _abiEncoder.Encode(signature, BigInteger.MinusOne);
            object[] arguments = _abiEncoder.Decode(signature, encoded);
            Assert.AreEqual(BigInteger.MinusOne, arguments[0]);
        }
Example #6
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);
        }
Example #7
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);
        }
Example #8
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));
        }
Example #9
0
        public void Test_single_function()
        {
            AbiType type = AbiType.Function;

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

            byte[]   encoded   = _abiEncoder.Encode(signature, data);
            object[] arguments = _abiEncoder.Decode(signature, encoded);
            Assert.True(Bytes.UnsafeCompare((byte[])arguments[0], data));
        }
Example #10
0
        public void Test_dynamic_bytes(AbiEncodingStyle encodingStyle)
        {
            AbiType type = AbiType.DynamicBytes;

            byte[] data = new byte[17] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17
            };
            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));
        }
Example #11
0
 public static void RegisterAbiTypeFactory(this IAbiDefinitionParser parser, AbiType abiType) =>
 parser.RegisterAbiTypeFactory(new AbiTypeFactory(abiType));
 public AbiTypeFactory(AbiType abiType)
 {
     _abiType = abiType;
 }