private void LoadNonIndexedFields(Dictionary <string, SearchField> fieldDictionary) { var nonIndexedProperties = PropertiesExtractor .GetPropertiesWithParameterAttribute(_eventType) .Select(p => new { property = p, parameter = p.GetCustomAttribute <ParameterAttribute>() }) .Where(p => p.parameter != null) .Where(p => p.parameter.Parameter.Indexed == false) .OrderBy(p => p.parameter.Order) .ToArray(); foreach (var nonIndexedProperty in nonIndexedProperties) { var searchFieldAttribute = nonIndexedProperty.property.GetCustomAttribute <SearchField>() ?? new SearchField(); if (searchFieldAttribute.Ignore) { continue; } searchFieldAttribute.SourceProperty = nonIndexedProperty.property; searchFieldAttribute.DataType = nonIndexedProperty.property.PropertyType; if (string.IsNullOrEmpty(searchFieldAttribute.Name)) { searchFieldAttribute.Name = nonIndexedProperty.property.Name; } fieldDictionary[searchFieldAttribute.Name] = searchFieldAttribute; } }
public T DecodeFunctionInput <T>(T functionInput, string sha3Signature, string data) { if (!sha3Signature.StartsWith("0x")) { sha3Signature = "0x" + sha3Signature; } if (!data.StartsWith("0x")) { data = "0x" + data; } if ((data == "0x") || (data == sha3Signature)) { return(functionInput); } if (data.StartsWith(sha3Signature)) { data = data.Substring(sha3Signature.Length); } var type = typeof(T); var properties = PropertiesExtractor.GetPropertiesWithParameterAttribute(type); DecodeAttributes(data, functionInput, properties.ToArray()); return(functionInput); }
public byte[] EncodeParametersFromTypeAttributes(Type type, object instanceValue) { var properties = PropertiesExtractor.GetPropertiesWithParameterAttribute(type); var parameterObjects = new List <ParameterAttributeValue>(); foreach (var property in properties) { var parameterAttribute = property.GetCustomAttribute <ParameterAttribute>(true); #if DOTNET35 var propertyValue = property.GetValue(instanceValue, null); #else var propertyValue = property.GetValue(instanceValue); #endif TupleType tupleType; if ((tupleType = parameterAttribute.Parameter.ABIType as TupleType) != null) { attributesToABIExtractor.InitTupleComponentsFromTypeAttributes(property.PropertyType, tupleType); propertyValue = GetTupleComponentValuesFromTypeAttributes(property.PropertyType, propertyValue); } parameterObjects.Add(new ParameterAttributeValue { ParameterAttribute = parameterAttribute, Value = propertyValue }); } var abiParameters = parameterObjects.OrderBy(x => x.ParameterAttribute.Order) .Select(x => x.ParameterAttribute.Parameter) .ToArray(); var objectValues = parameterObjects.OrderBy(x => x.ParameterAttribute.Order).Select(x => x.Value).ToArray(); return(EncodeParameters(abiParameters, objectValues)); }
public object DecodeAttributes(byte[] output, Type objectType) { var properties = PropertiesExtractor.GetPropertiesWithParameterAttribute(objectType); var objectResult = Activator.CreateInstance(objectType); return(DecodeAttributes(output, objectResult, properties.ToArray())); }
public List <ParameterAttributeValue> GetParameterAttributeValues(Type type, object instanceValue) { var properties = PropertiesExtractor.GetPropertiesWithParameterAttribute(type); var parameterObjects = new List <ParameterAttributeValue>(); foreach (var property in properties) { var parameterAttribute = property.GetCustomAttribute <ParameterAttribute>(true); #if DOTNET35 var propertyValue = property.GetValue(instanceValue, null); #else var propertyValue = property.GetValue(instanceValue); #endif if (parameterAttribute.Parameter.ABIType is TupleType tupleType) { attributesToABIExtractor.InitTupleComponentsFromTypeAttributes(property.PropertyType, tupleType); propertyValue = GetTupleComponentValuesFromTypeAttributes(property.PropertyType, propertyValue); } parameterObjects.Add(new ParameterAttributeValue { ParameterAttribute = parameterAttribute, Value = propertyValue }); } return(parameterObjects); }
public object[] GetTupleComponentValuesFromTypeAttributes(Type type, object instanceValue) { var properties = PropertiesExtractor.GetPropertiesWithParameterAttribute(type); var propertiesInOrder = properties.Where(x => x.IsDefined(typeof(ParameterAttribute), true)) .OrderBy(x => x.GetCustomAttribute <ParameterAttribute>(true).Order); var parameterObjects = new List <object>(); foreach (var property in propertiesInOrder) { var parameterAttribute = property.GetCustomAttribute <ParameterAttribute>(true); #if DOTNET35 var propertyValue = property.GetValue(instanceValue, null); #else var propertyValue = property.GetValue(instanceValue); #endif if (parameterAttribute.Parameter.ABIType is TupleType) { propertyValue = GetTupleComponentValuesFromTypeAttributes(property.PropertyType, propertyValue); } parameterObjects.Add(propertyValue); } return(parameterObjects.ToArray()); }
public void Verify_Range() => PropertiesExtractor.Extract(new RangeAttribute(4, 6)) .Should() .HaveCount(2) .And .Contain("minimum", 4) .And .Contain("maximum", 6);
public void GetProperties_ErrorConfigFileNotFound() { EcGlobalConfigurations temp = new EcGlobalConfigurations { Filename = "notexist", Path = ".\\test" }; PropertiesExtractor.GetProperties(temp); }
public void Verify_FileExtension() => PropertiesExtractor.Extract(new FileExtensionsAttribute() { Extensions = "jsx" }) .Should() .HaveCount(1) .And .Contain("extensions", "jsx");
public T DecodeTopics <T>(T eventDTO, object[] topics, string data) { var type = typeof(T); var properties = PropertiesExtractor.GetPropertiesWithParameterAttribute(type); var indexedProperties = properties.Where(x => x.GetCustomAttribute <ParameterAttribute>(true).Parameter.Indexed == true).OrderBy(x => x.GetCustomAttribute <ParameterAttribute>(true).Order).ToArray(); var dataProperties = properties.Where(x => x.GetCustomAttribute <ParameterAttribute>(true).Parameter.Indexed == false).OrderBy(x => x.GetCustomAttribute <ParameterAttribute>(true).Order).ToArray(); // Take one off topics count to skip signature if event is not anonymous var topicCount = !_isAnonymousEvent ? (topics.Length - 1) : topics.Length; var indexedPropertiesCount = indexedProperties.Length; if (indexedPropertiesCount != (topicCount)) { throw new Exception($"Number of indexes don't match the number of topics. Indexed Properties {indexedPropertiesCount}, Topics : {topicCount}"); } var topicNumber = 0; foreach (var topic in topics) { //skip the first one as it is the signature for not anonymous events if (!_isAnonymousEvent && topicNumber == 0) { topicNumber = topicNumber + 1; continue; } var property = _isAnonymousEvent ? indexedProperties[topicNumber] : indexedProperties[topicNumber - 1]; var attribute = property.GetCustomAttribute <ParameterAttribute>(true); //skip dynamic types as the topic value is the sha3 keccak if (!attribute.Parameter.ABIType.IsDynamic()) { eventDTO = DecodeAttributes(topic.ToString(), eventDTO, property); } else { if (property.PropertyType != typeof(string)) { throw new Exception( "Indexed Dynamic Types (string, arrays) value is the Keccak SHA3 of the value, the property type of " + property.Name + "should be a string"); } #if DOTNET35 property.SetValue(eventDTO, topic.ToString(), null); #else property.SetValue(eventDTO, topic.ToString()); #endif } topicNumber = topicNumber + 1; } eventDTO = DecodeAttributes(data, eventDTO, dataProperties.ToArray()); return(eventDTO); }
public void GetProperties_FromCache() { MemoryCache.Default.Add(EcConfigResources.CacheKey_Properties, new Dictionary <string, string> { { "key", "value" }, { "key1", "value1" } }, DateTime.Now.AddDays(1)); var properties = PropertiesExtractor.GetProperties(_ecConfigs); Assert.AreEqual("value", properties.First(x => x.Key == "key").Value); }
public void Verify_StringLength() => PropertiesExtractor.Extract(new StringLengthAttribute(5) { MinimumLength = 2 }) .Should() .HaveCount(2) .And .Contain("minimumLength", 2) .And .Contain("maximumLength", 5);
private static Parameter[] ExtractParametersFromAttributes(Type contractMessageType) { var properties = PropertiesExtractor.GetPropertiesWithParameterAttribute(contractMessageType); var parameters = new List <Parameter>(); foreach (var property in properties) { var parameterAttribute = property.GetCustomAttribute <ParameterAttribute>(true); parameters.Add(parameterAttribute.Parameter); } return(parameters.ToArray()); }
public async Task ShouldBeSupportedForSomeOrAllProperties() { // the result of a known query var jsonEncododeQueryBytes = "{'rawBytes':'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUZvcmQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARmllc3RhAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARQSBuaWNlIGxpdHRsZSBjYXIAAAAAAAAAAAAAAAAAAAA='}"; var deserializedWrapper = JObject.Parse(jsonEncododeQueryBytes); var rawOutputFromQuery = (byte[])deserializedWrapper["rawBytes"].ToObject(typeof(byte[])); //// vanilla code generation - no properties overriden or changed with "new" operator //// bytes32 properties are returned as byte[] var codeGeneratedFunctionDto = new GetVehicleOutputDTO(); var vanillaProperties = PropertiesExtractor .GetPropertiesWithParameterAttribute(codeGeneratedFunctionDto.GetType()).ToArray(); new FunctionCallDecoder().DecodeAttributes(rawOutputFromQuery, codeGeneratedFunctionDto, vanillaProperties); var bytes32Decoder = new Bytes32TypeDecoder(); Assert.Equal(BigInteger.One, codeGeneratedFunctionDto.Vehicle.Id); Assert.Equal("Ford", bytes32Decoder.Decode <string>(codeGeneratedFunctionDto.Vehicle.Manufacturer)); Assert.Equal("Fiesta", bytes32Decoder.Decode <string>(codeGeneratedFunctionDto.Vehicle.Model)); Assert.Equal("A nice little car", codeGeneratedFunctionDto.Vehicle.Description); //// repeating each property on dto with "new" keyword in order to return a different .net type var dtoWhereEveryPropertyIsNew = new GetVehicleOutputDTO_WithAllNewPropertyTypes(); var propertiesForAllNew = PropertiesExtractor .GetPropertiesWithParameterAttribute(dtoWhereEveryPropertyIsNew.GetType()).ToArray(); new FunctionCallDecoder().DecodeAttributes(rawOutputFromQuery, dtoWhereEveryPropertyIsNew, propertiesForAllNew); Assert.Equal(BigInteger.One, dtoWhereEveryPropertyIsNew.Vehicle.Id); Assert.Equal("Ford", dtoWhereEveryPropertyIsNew.Vehicle.Manufacturer); Assert.Equal("Fiesta", dtoWhereEveryPropertyIsNew.Vehicle.Model); Assert.Equal("A nice little car", dtoWhereEveryPropertyIsNew.Vehicle.Description); // custom "vehicle_custom" class which inherits the code generated "vehicle" class // bytes32 properties for Manufacturer and Model created as "new" properties var modifiedDtoWithSomeNewProperties = new GetVehicleOutputDTO_WithSomeNewPropertyTypes(); var propertiesForModifiedDto = PropertiesExtractor .GetPropertiesWithParameterAttribute(modifiedDtoWithSomeNewProperties.GetType()).ToArray(); new FunctionCallDecoder().DecodeAttributes(rawOutputFromQuery, modifiedDtoWithSomeNewProperties, propertiesForModifiedDto); Assert.Equal(BigInteger.One, modifiedDtoWithSomeNewProperties.Vehicle.Id); Assert.Equal("Ford", modifiedDtoWithSomeNewProperties.Vehicle.Manufacturer); Assert.Equal("Fiesta", modifiedDtoWithSomeNewProperties.Vehicle.Model); Assert.Equal("A nice little car", modifiedDtoWithSomeNewProperties.Vehicle.Description); }
public TopicFilterContainer() { var indexedParameters = PropertiesExtractor .GetPropertiesWithParameterAttribute(typeof(T)) .Select(p => new TopicFilter(p, p.GetCustomAttribute <ParameterAttribute>())) .Where(p => p.ParameterAttribute?.Parameter.Indexed ?? false) .OrderBy(p => p.ParameterAttribute.Order) .ToArray(); Empty = indexedParameters.Length == 0; Topic1 = indexedParameters.Length > 0 ? indexedParameters[0] : TopicFilter.Empty; Topic2 = indexedParameters.Length > 1 ? indexedParameters[1] : TopicFilter.Empty; Topic3 = indexedParameters.Length > 2 ? indexedParameters[2] : TopicFilter.Empty; Topics = new [] { Topic1, Topic2, Topic3 }; }
public T DecodeTopics <T>(object[] topics, string data) where T : new() { var type = typeof(T); var result = new T(); var properties = PropertiesExtractor.GetPropertiesWithParameterAttribute(type); var indexedProperties = properties.Where(x => x.GetCustomAttribute <ParameterAttribute>(true).Parameter.Indexed == true).OrderBy(x => x.GetCustomAttribute <ParameterAttribute>(true).Order).ToArray(); var dataProperties = properties.Where(x => x.GetCustomAttribute <ParameterAttribute>(true).Parameter.Indexed == false).OrderBy(x => x.GetCustomAttribute <ParameterAttribute>(true).Order).ToArray(); var topicNumber = 0; foreach (var topic in topics) { //skip the first one as it is the signature if (topicNumber > 0) { var property = indexedProperties[topicNumber - 1]; var attribute = property.GetCustomAttribute <ParameterAttribute>(true); //skip dynamic types as the topic value is the sha3 keccak if (!attribute.Parameter.ABIType.IsDynamic()) { result = DecodeAttributes(topic.ToString(), result, property); } else { if (property.PropertyType != typeof(string)) { throw new Exception( "Indexed Dynamic Types (string, arrays) value is the Keccak SHA3 of the value, the property type of " + property.Name + "should be a string"); } #if DOTNET35 property.SetValue(result, topic.ToString(), null); #else property.SetValue(result, topic.ToString()); #endif } } topicNumber = topicNumber + 1; } result = DecodeAttributes(data, result, dataProperties.ToArray()); return(result); }
public T DecodeFunctionOutput <T>(T functionOutputResult, string output) { if (output == "0x") { return(functionOutputResult); } var type = typeof(T); var function = FunctionOutputAttribute.GetAttribute <T>(); if (function == null) { throw new ArgumentException("Generic Type should have a Function Ouput Attribute"); } var properties = PropertiesExtractor.GetPropertiesWithParameterAttribute(type); DecodeAttributes(output, functionOutputResult, properties.ToArray()); return(functionOutputResult); }
public T DecodeFunctionOutput <T>(T functionOutputResult, string output) { if (output == "0x") { return(functionOutputResult); } var type = typeof(T); var function = FunctionOutputAttribute.GetAttribute <T>(); if (function == null) { throw new ArgumentException($"Unable to decode to '{typeof(T).Name}' because the type does not apply attribute '[{nameof(FunctionOutputAttribute)}]'."); } var properties = PropertiesExtractor.GetPropertiesWithParameterAttribute(type); DecodeAttributes(output, functionOutputResult, properties.ToArray()); return(functionOutputResult); }
public T DecodeConstructorParameters <T>(T deploymentObject, string data) { var swarmExtractor = new ByteCodeSwarmExtractor(); if (swarmExtractor.HasSwarmAddress(data)) { return(DecodeConstructorParameters(deploymentObject, swarmExtractor.GetByteCodeIncludingSwarmAddressPart(data), data)); } else { var properties = PropertiesExtractor.GetPropertiesWithParameterAttribute(typeof(T)); if (properties.Any()) { throw new Exception( "Data supplied does not include a swarm address, to locate the constructor parameters"); } else { return(deploymentObject); } } }
public T DecodeConstructorParameters <T>(T deploymentObject, string deploymentByteCode, string data) { if (!deploymentByteCode.StartsWith("0x")) { deploymentByteCode = "0x" + deploymentByteCode; } if (!data.StartsWith("0x")) { data = "0x" + data; } if ((data == "0x") || (data == deploymentByteCode)) { return(deploymentObject); } if (data.StartsWith(deploymentByteCode)) { data = data.Substring(deploymentByteCode.Length); } var type = typeof(T); var properties = PropertiesExtractor.GetPropertiesWithParameterAttribute(type); return(DecodeAttributes <T>(data, deploymentObject, properties.ToArray())); }
public void Verify_Compare() => PropertiesExtractor.Extract(new CompareAttribute("TheOther")) .Should() .HaveCount(1) .And .Contain("otherProperty", "TheOther");
public void Verify_MinLength() => PropertiesExtractor.Extract(new MinLengthAttribute(4)) .Should() .HaveCount(1) .And .Contain("length", 4);
public List <ParameterOutputProperty> GetParameterOutputsFromAttributes(Type type) { var properties = PropertiesExtractor.GetPropertiesWithParameterAttribute(type); return(GetParameterOutputsFromAttributes(properties.ToArray())); }
private static SearchField[] MapIndexedTopicsToFields() { var indexedTopics = PropertiesExtractor.GetIndexedTopics <T>(); return(indexedTopics.Select(t => MapTopicToField(t.PropertyInfo)).ToArray()); }
public void Verify_RegularExpressions() => PropertiesExtractor.Extract(new RegularExpressionAttribute("pattern")) .Should() .HaveCount(1) .And .Contain("pattern", "pattern");
public void Verify_CreditCard() => PropertiesExtractor.Extract(new CreditCardAttribute()) .Should() .HaveCount(0);
public void Verify_Email() => PropertiesExtractor.Extract(new EmailAddressAttribute()) .Should() .HaveCount(0);
public void Verify_EnumData() => PropertiesExtractor.Extract(new EnumDataTypeAttribute(typeof(DataType))) .Should() .HaveCount(0);
public void Verify_Url() => PropertiesExtractor.Extract(new UrlAttribute()) .Should() .HaveCount(0);
public void GetProperties_FromFile() { var properties = PropertiesExtractor.GetProperties(_ecConfigs); Assert.AreEqual("dev", properties.First(x => x.Key == "key").Value); }