예제 #1
0
        public static byte[] Encode(string?methodSignature, params dynamic[] values)
        {
            var encoder = new ContractEncoder(methodSignature, values);

            if (values.GetType() == typeof(byte[][]))
            {
                encoder = encoder.Write((byte[][])values);
            }
            else
            {
                encoder = values.Aggregate(encoder, (current, value) => current.Write(value));
            }

            return(encoder.ToByteArray());
        }
예제 #2
0
        public object[] Decode(string signature)
        {
            var parts = signature.Split('(');

            if (parts.Length == 1)
            {
                return(DecodeSimpleTypes(signature));
            }
            if (parts.Length == 2)
            {
                if (_binaryReaderStatic.ReadUInt32() != ContractEncoder.MethodSignatureAsInt(signature))
                {
                    throw new ContractAbiException("Decoded ABI does not match method signature");
                }
            }
            parts[1] = parts[1].TrimEnd(')');
            if (parts[1] == "")
            {
                return(new object[] {});
            }
            var types        = parts[1].Split(',');
            var result       = new List <object>(types.Length);
            var staticInput  = _inputData.Skip(4).Take(types.Length * 32).ToArray();
            var dynamicInput = _inputData.Skip(4 + staticInput.Length).ToArray();

            _binaryReaderStatic  = new BinaryReader(new MemoryStream(staticInput));
            _binaryReaderDynamic = new BinaryReader(new MemoryStream(dynamicInput));
            result.AddRange(types.Select(type => type switch
            {
                "uint256" => (object)DecodeUInt256(),
                "uint256[]" => DecodeUInt256List(),
                "uint" => DecodeUInt256(),
                "uint[]" => DecodeUInt256List(),
                "address" => DecodeUInt160(),
                "uint160" => DecodeUInt160(),
                "address[]" => DecodeUInt160List(),
                "uint160[]" => DecodeUInt160List(),
                "bytes[]" => DecodeBytesList(),
                "bytes" => DecodeBytes(),
                _ => throw new ContractAbiException("Unsupported type in method signature (" + type + ")")
            }));