Ejemplo n.º 1
0
        public async Task <AddRecordItemResponse> AddRecordItemAsync(Dictionary <ParameterTypeEnum, object> parameters)
        {
            telemetryClient.TrackTrace("Starting helper");

            AddRecordItemResponse result = new AddRecordItemResponse
            {
                IsSucceded = true,
                ResultId   = (int)AddRecordItemResultEnum.Success
            };

            try
            {
                telemetryClient.TrackTrace("Getting parameters");

                parameters.TryGetValue(ParameterTypeEnum.RecordItem, out global::System.Object orecordItemRequest);
                Domain.Models.Request.RecordItem recordItemRequest = orecordItemRequest as Domain.Models.Request.RecordItem;

                parameters.TryGetValue(ParameterTypeEnum.MasterAddress, out global::System.Object omasterAddress);
                string masterAddress = omasterAddress.ToString();

                parameters.TryGetValue(ParameterTypeEnum.MasterPrivateKey, out global::System.Object omasterPrivateKey);
                string masterPrivateKey = omasterPrivateKey.ToString();

                parameters.TryGetValue(ParameterTypeEnum.ContractAddress, out global::System.Object ocontractAddress);
                string contractAddress = ocontractAddress.ToString();

                parameters.TryGetValue(ParameterTypeEnum.ContractABI, out global::System.Object ocontractABI);
                string contractABI = ocontractABI.ToString();

                parameters.TryGetValue(ParameterTypeEnum.RecordItemImageContainer, out global::System.Object orecordItemImageContainer);
                string recordItemImageContainer = orecordItemImageContainer.ToString();

                //database helpers
                DBRecordItemHelper  dbRecordItemHelper  = new DBRecordItemHelper(DBCONNECTION_INFO);
                DBUserAccountHelper dbUserAccountHelper = new DBUserAccountHelper(DBCONNECTION_INFO);

                //blockchain helper
                BlockchainHelper bh = new BlockchainHelper(telemetryClient, STORAGE_ACCOUNT, RPC_CLIENT, masterAddress, masterPrivateKey);

                telemetryClient.TrackTrace("Validating username length");

                //validate username length
                if (!RegexValidation.IsValidUsername(recordItemRequest.username))
                {
                    result.IsSucceded = false;
                    result.ResultId   = (int)AddRecordItemResultEnum.InvalidUsernameLength;
                    return(result);
                }

                telemetryClient.TrackTrace("Validating username existance");

                //validate if account exists
                UserAccount userAccount = dbUserAccountHelper.GetUser(recordItemRequest.username);

                if (userAccount == null)
                {
                    result.IsSucceded = false;
                    result.ResultId   = (int)AddRecordItemResultEnum.UsernameNotExists;
                    return(result);
                }

                telemetryClient.TrackTrace("Validating record item existance");

                //validate if record item exists for voting
                RecordItem recordItemExists = dbRecordItemHelper.GetRecordItem(recordItemRequest.hash);

                if (recordItemExists != null)
                {
                    //there is no record item linked with this vote
                    result.IsSucceded = false;
                    result.ResultId   = (int)AddRecordItemResultEnum.AlreadyExists;
                }
                else
                {
                    telemetryClient.TrackTrace("Adding record item to blockchain");

                    var res_AddRecordAsync = await bh.AddRecordAsync(recordItemRequest.hash, recordItemRequest.username, contractAddress, contractABI);

                    telemetryClient.TrackTrace($"Add record item result: {res_AddRecordAsync}");

                    if (string.IsNullOrEmpty(res_AddRecordAsync))
                    {
                        //there was an error adding the record to the blockchain
                        result.IsSucceded = false;
                        result.ResultId   = (int)AddRecordItemResultEnum.BlockchainIssue;
                        return(result);
                    }

                    telemetryClient.TrackTrace("Adding record item to database");

                    RecordItem recordItem = RecordItemParser.TransformRecordItem(recordItemRequest);

                    recordItem.hash          = recordItem.hash.ToLower();
                    recordItem.transactionId = res_AddRecordAsync;
                    recordItem.createdDate   = Timezone.GetCustomTimeZone();
                    recordItem.image         = recordItem.image + ".png";

                    //perform insert in mongodb
                    await dbRecordItemHelper.CreateRecordItem(recordItem);

                    telemetryClient.TrackTrace("Adding record item image to storage");

                    //upload image to blobstorage
                    byte[] buffer = Convert.FromBase64String(recordItemRequest.ImageBytes);
                    await UploadRecordItemImageAsync(recordItemImageContainer, recordItem.image, buffer);
                }
            }
            catch (AggregateException ex)
            {
                foreach (var innerException in ex.Flatten().InnerExceptions)
                {
                    telemetryClient.TrackException(innerException);
                }
                result.IsSucceded = false;
                result.ResultId   = (int)AddRecordItemResultEnum.Failed;
            }
            catch (Exception ex)
            {
                telemetryClient.TrackException(ex);
                result.IsSucceded = false;
                result.ResultId   = (int)AddRecordItemResultEnum.Failed;
            }

            telemetryClient.TrackTrace("Finishing helper");
            return(result);
        }
Ejemplo n.º 2
0
        public SignInAccountResponse SignInAccount(Dictionary <ParameterTypeEnum, object> parameters)
        {
            telemetryClient.TrackTrace("Starting helper");

            SignInAccountResponse result = new SignInAccountResponse
            {
                IsSucceded = true,
                ResultId   = (int)SignUpAccountResultEnum.Success
            };

            try
            {
                telemetryClient.TrackTrace("Getting parameters");

                parameters.TryGetValue(ParameterTypeEnum.Username, out global::System.Object ousername);
                string username = ousername.ToString().ToLower();

                parameters.TryGetValue(ParameterTypeEnum.Password, out global::System.Object opassword);
                string password = opassword.ToString();

                //database helpers
                DBUserAccountHelper dbUserAccountHelper = new DBUserAccountHelper(DBCONNECTION_INFO);

                telemetryClient.TrackTrace("Validating username length");

                //validate username length
                if (!RegexValidation.IsValidUsername(username))
                {
                    result.IsSucceded = false;
                    result.ResultId   = (int)SignInAccountResultEnum.InvalidUsernameLength;
                    return(result);
                }

                telemetryClient.TrackTrace("Validating username existance");

                //validate if account exists
                UserAccount userAccount = dbUserAccountHelper.GetUser(username);

                if (userAccount == null)
                {
                    result.IsSucceded = false;
                    result.ResultId   = (int)SignInAccountResultEnum.NotExists;
                    return(result);
                }
                else
                {
                    if (userAccount.password == MD5Hash.CalculateMD5Hash(password))
                    {
                        result.IsSucceded = true;
                        result.ResultId   = (int)SignInAccountResultEnum.Success;
                        result.Username   = userAccount.username;
                        result.Image      = $"{userAccount.username}.png";
                    }
                    else
                    {
                        result.IsSucceded = false;
                        result.ResultId   = (int)SignInAccountResultEnum.IncorrectPassword;
                        return(result);
                    }
                }
            }
            catch (AggregateException ex)
            {
                foreach (var innerException in ex.Flatten().InnerExceptions)
                {
                    telemetryClient.TrackException(innerException);
                }
                result.IsSucceded = false;
                result.ResultId   = (int)SignUpAccountResultEnum.Failed;
            }
            catch (Exception ex)
            {
                telemetryClient.TrackException(ex);
                result.IsSucceded = false;
                result.ResultId   = (int)SignUpAccountResultEnum.Failed;
            }

            telemetryClient.TrackTrace("Finishing helper");
            return(result);
        }
Ejemplo n.º 3
0
        public async Task <AddRecordVoteResponse> AddRecordVoteAsync(Dictionary <ParameterTypeEnum, object> parameters)
        {
            telemetryClient.TrackTrace("Starting helper");

            AddRecordVoteResponse result = new AddRecordVoteResponse
            {
                IsSucceded = true,
                ResultId   = (int)AddRecordVoteResultEnum.Success
            };

            try
            {
                telemetryClient.TrackTrace("Getting parameters");

                parameters.TryGetValue(ParameterTypeEnum.Username, out global::System.Object ousername);
                string username = ousername.ToString().ToLower();

                parameters.TryGetValue(ParameterTypeEnum.Hash, out global::System.Object ohash);
                string hash = ohash.ToString().ToLower();

                parameters.TryGetValue(ParameterTypeEnum.IsApproval, out global::System.Object oisApproval);
                bool isApproval = Convert.ToBoolean(oisApproval.ToString());

                parameters.TryGetValue(ParameterTypeEnum.MasterAddress, out global::System.Object omasterAddress);
                string masterAddress = omasterAddress.ToString();

                parameters.TryGetValue(ParameterTypeEnum.MasterPrivateKey, out global::System.Object omasterPrivateKey);
                string masterPrivateKey = omasterPrivateKey.ToString();

                parameters.TryGetValue(ParameterTypeEnum.ContractAddress, out global::System.Object ocontractAddress);
                string contractAddress = ocontractAddress.ToString();

                parameters.TryGetValue(ParameterTypeEnum.ContractABI, out global::System.Object ocontractABI);
                string contractABI = ocontractABI.ToString();

                //database helpers
                DBUserAccountHelper dbUserAccountHelper = new DBUserAccountHelper(DBCONNECTION_INFO);
                DBRecordItemHelper  dbRecordItemHelper  = new DBRecordItemHelper(DBCONNECTION_INFO);
                DBRecordVoteHelper  dbRecordVoteHelper  = new DBRecordVoteHelper(DBCONNECTION_INFO);

                //blockchain helper
                BlockchainHelper bh = new BlockchainHelper(telemetryClient, STORAGE_ACCOUNT, RPC_CLIENT, masterAddress, masterPrivateKey);

                telemetryClient.TrackTrace("Validating username length");

                //validate username length
                if (!RegexValidation.IsValidUsername(username))
                {
                    result.IsSucceded = false;
                    result.ResultId   = (int)AddRecordVoteResultEnum.InvalidUsernameLength;
                    return(result);
                }

                telemetryClient.TrackTrace("Validating username existance");

                //validate if account exists
                UserAccount userAccount = dbUserAccountHelper.GetUser(username);

                if (userAccount == null)
                {
                    result.IsSucceded = false;
                    result.ResultId   = (int)AddRecordVoteResultEnum.UsernameNotExists;
                    return(result);
                }

                telemetryClient.TrackTrace("Validating record item existance");

                //validate if record item exists for voting
                RecordItem recordItem = dbRecordItemHelper.GetRecordItem(hash);

                if (recordItem == null)
                {
                    //there is no record item linked with this vote
                    result.IsSucceded = false;
                    result.ResultId   = (int)AddRecordVoteResultEnum.NotExists;
                    return(result);
                }
                else
                {
                    telemetryClient.TrackTrace("Validating record vote existance");

                    //validate if user has voted submit a vote before
                    RecordVote vote = dbRecordVoteHelper.GetRecordVote(hash, username);

                    if (vote != null)
                    {
                        //the user already voted
                        result.IsSucceded = false;
                        result.ResultId   = (int)AddRecordVoteResultEnum.AlreadyVoted;
                        return(result);
                    }
                    else
                    {
                        telemetryClient.TrackTrace("Adding record vote to blockchain");

                        //sending to the blockchain
                        var res_IncreaseOperationAsync = string.Empty;
                        if ((bool)isApproval)
                        {
                            res_IncreaseOperationAsync = await bh.IncreaseApprovalsAsync(hash, contractAddress, contractABI);

                            System.Diagnostics.Trace.TraceInformation($"Add record for approval vote result: {res_IncreaseOperationAsync}");
                        }
                        else
                        {
                            res_IncreaseOperationAsync = await bh.IncreaseDisapprovalsAsync(hash, contractAddress, contractABI);

                            System.Diagnostics.Trace.TraceInformation($"Add record for disapproval vote result: {res_IncreaseOperationAsync }");
                        }

                        if (string.IsNullOrEmpty(res_IncreaseOperationAsync))
                        {
                            //there was an error voting the record in the blockchain
                            result.IsSucceded = false;
                            result.ResultId   = (int)AddRecordVoteResultEnum.BlockchainIssue;
                            return(result);
                        }

                        telemetryClient.TrackTrace("Adding record vote to database");

                        //save record in mongodb
                        vote = new RecordVote
                        {
                            username      = username,
                            hash          = hash,
                            isApproval    = isApproval,
                            transactionId = res_IncreaseOperationAsync,
                            createdDate   = Timezone.GetCustomTimeZone()
                        };

                        //perform insert in mongodb
                        await dbRecordVoteHelper.CreateRecordVote(vote);
                    }
                }
            }
            catch (AggregateException ex)
            {
                foreach (var innerException in ex.Flatten().InnerExceptions)
                {
                    telemetryClient.TrackException(innerException);
                }
                result.IsSucceded = false;
                result.ResultId   = (int)AddRecordVoteResultEnum.Failed;
            }
            catch (Exception ex)
            {
                telemetryClient.TrackException(ex);
                result.IsSucceded = false;
                result.ResultId   = (int)AddRecordVoteResultEnum.Failed;
            }

            telemetryClient.TrackTrace("Finishing helper");
            return(result);
        }
Ejemplo n.º 4
0
        public async Task <SignUpAccountResponse> SignUpAccountAsync(Dictionary <ParameterTypeEnum, object> parameters)
        {
            telemetryClient.TrackTrace("Starting helper");

            SignUpAccountResponse result = new SignUpAccountResponse
            {
                IsSucceded = true,
                ResultId   = (int)SignUpAccountResultEnum.Success
            };

            try
            {
                telemetryClient.TrackTrace("Getting parameters");

                parameters.TryGetValue(ParameterTypeEnum.Username, out global::System.Object ousername);
                string username = ousername.ToString().ToLower();

                parameters.TryGetValue(ParameterTypeEnum.Password, out global::System.Object opassword);
                string password = opassword.ToString();

                parameters.TryGetValue(ParameterTypeEnum.AccountImagesContainer, out global::System.Object oaccountImagesContainer);
                string accountImagesContainer = oaccountImagesContainer.ToString();

                parameters.TryGetValue(ParameterTypeEnum.FunctionDirectory, out global::System.Object ofunctionDirectory);
                string functionDirectory = ofunctionDirectory.ToString();

                //database helpers
                DBUserAccountHelper dbUserAccountHelper = new DBUserAccountHelper(DBCONNECTION_INFO);

                telemetryClient.TrackTrace("Validating username length");

                //validate username length
                if (!RegexValidation.IsValidUsername(username))
                {
                    result.IsSucceded = false;
                    result.ResultId   = (int)SignUpAccountResultEnum.InvalidUsernameLength;
                    return(result);
                }

                telemetryClient.TrackTrace("Validating username existance");

                //validate if account exists
                UserAccount userAccount = dbUserAccountHelper.GetUser(username);

                if (userAccount != null)
                {
                    result.IsSucceded = false;
                    result.ResultId   = (int)SignUpAccountResultEnum.AlreadyExists;
                    return(result);
                }

                telemetryClient.TrackTrace("Adding account to database");

                //save username and account in mongodb
                userAccount = new UserAccount()
                {
                    username    = username,
                    password    = MD5Hash.CalculateMD5Hash(password),
                    createdDate = Timezone.GetCustomTimeZone()
                };

                //perform insert in mongodb
                await dbUserAccountHelper.CreateUserAccount(userAccount);

                telemetryClient.TrackTrace("Adding account image to storage");

                //create unique icon, upload and delete it
                var imageName = $"{username}.png";
                try
                {
                    var directory = Path.Combine(functionDirectory, "images");
                    var filePath  = Path.Combine(directory, imageName);

                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }

                    Identicon.FromValue(username, size: 160).SaveAsPng(filePath);
                    await UploadAccountImageAsync(accountImagesContainer, imageName, filePath);

                    File.Delete(filePath);
                }
                catch
                {
                    //it doesn't matter if for some reason the icon generation fails, then ignore it and proceed as success
                }

                result.Image = imageName;
            }
            catch (AggregateException ex)
            {
                foreach (var innerException in ex.Flatten().InnerExceptions)
                {
                    telemetryClient.TrackException(innerException);
                }
                result.IsSucceded = false;
                result.ResultId   = (int)SignUpAccountResultEnum.Failed;
            }
            catch (Exception ex)
            {
                telemetryClient.TrackException(ex);
                result.IsSucceded = false;
                result.ResultId   = (int)SignUpAccountResultEnum.Failed;
            }

            telemetryClient.TrackTrace("Finishing helper");
            return(result);
        }